Skip to content

Commit

Permalink
feat: use vim.lsp.config & vim.lsp.enable
Browse files Browse the repository at this point in the history
  • Loading branch information
konradmalik committed Dec 12, 2024
1 parent 696400e commit fe3a7e4
Show file tree
Hide file tree
Showing 54 changed files with 515 additions and 731 deletions.
4 changes: 0 additions & 4 deletions config/native/after/ftplugin/c.lua

This file was deleted.

10 changes: 0 additions & 10 deletions config/native/after/ftplugin/go.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
local bufnr = vim.api.nvim_get_current_buf()

vim.o.expandtab = false

local lsp = require("pde.lsp")

lsp.start(require("pde.lsp.configs.gopls").config(bufnr), { bufnr = bufnr })
lsp.start(
require("pde.lsp.configs.efm").config_from_single("golangci_lint", bufnr),
{ bufnr = bufnr }
)
5 changes: 0 additions & 5 deletions config/native/after/ftplugin/gomod.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
local bufnr = vim.api.nvim_get_current_buf()

vim.o.expandtab = false

local lsp = require("pde.lsp")
lsp.start(require("pde.lsp.configs.gopls").config(bufnr), { bufnr = bufnr })
5 changes: 0 additions & 5 deletions config/native/after/ftplugin/gotmpl.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
local bufnr = vim.api.nvim_get_current_buf()

vim.o.expandtab = false

local lsp = require("pde.lsp")
lsp.start(require("pde.lsp.configs.gopls").config(bufnr), { bufnr = bufnr })
5 changes: 0 additions & 5 deletions config/native/after/ftplugin/gowork.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
local bufnr = vim.api.nvim_get_current_buf()

vim.o.expandtab = false

local lsp = require("pde.lsp")
lsp.start(require("pde.lsp.configs.gopls").config(bufnr), { bufnr = bufnr })
9 changes: 0 additions & 9 deletions config/native/after/ftplugin/json.lua

This file was deleted.

6 changes: 0 additions & 6 deletions config/native/after/ftplugin/lua.lua

This file was deleted.

6 changes: 0 additions & 6 deletions config/native/after/ftplugin/markdown.lua

This file was deleted.

5 changes: 0 additions & 5 deletions config/native/after/ftplugin/nix.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
local bufnr = vim.api.nvim_get_current_buf()

vim.o.shiftwidth = 2

local lsp = require("pde.lsp")
lsp.start(require("pde.lsp.configs.nixd").config(bufnr), { bufnr = bufnr })
9 changes: 0 additions & 9 deletions config/native/after/ftplugin/python.lua

This file was deleted.

5 changes: 0 additions & 5 deletions config/native/after/ftplugin/rust.lua

This file was deleted.

8 changes: 0 additions & 8 deletions config/native/after/ftplugin/sh.lua

This file was deleted.

5 changes: 0 additions & 5 deletions config/native/after/ftplugin/terraform-vars.lua

This file was deleted.

6 changes: 0 additions & 6 deletions config/native/after/ftplugin/terraform.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
-- default is empty
vim.o.commentstring = "// %s"

local bufnr = vim.api.nvim_get_current_buf()

local lsp = require("pde.lsp")

lsp.start(require("pde.lsp.configs.terraformls").config(bufnr), { bufnr = bufnr })
5 changes: 0 additions & 5 deletions config/native/after/ftplugin/toml.lua

This file was deleted.

7 changes: 0 additions & 7 deletions config/native/after/ftplugin/yaml.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
local bufnr = vim.api.nvim_get_current_buf()

vim.o.shiftwidth = 2

local lsp = require("pde.lsp")

lsp.start(require("pde.lsp.configs.efm").config_from_single("prettier", bufnr), { bufnr = bufnr })
lsp.start(require("pde.lsp.configs.yamlls").config(bufnr), { bufnr = bufnr })
5 changes: 0 additions & 5 deletions config/native/after/ftplugin/zig.lua

This file was deleted.

5 changes: 0 additions & 5 deletions config/native/after/ftplugin/zir.lua

This file was deleted.

14 changes: 14 additions & 0 deletions config/native/after/lsp/clangd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local binaries = require("pde.binaries")

vim.lsp.config("clangd", {
cmd = { binaries.clangd() },
root_markers = {
".clangd",
".clang-tidy",
".clang-format",
"compile_commands.json",
"compile_flags.txt",
"configure.ac",
},
filetypes = { "c", "cpp" },
})
3 changes: 3 additions & 0 deletions config/native/after/lsp/efmson.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local config = require("pde.lsp.configs.efm").config_from_multi("efmson", { "prettier", "jq" })
config.filetypes = { "json", "jsonc" }
vim.lsp.config("efmson", config)
3 changes: 3 additions & 0 deletions config/native/after/lsp/golangci_lint.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local config = require("pde.lsp.configs.efm").config_from_single("golangci_lint")
config.filetypes = { "go" }
vim.lsp.config("golangci_lint", config)
14 changes: 14 additions & 0 deletions config/native/after/lsp/gopls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- https://github.com/golang/tools/tree/master/gopls
local binaries = require("pde.binaries")

vim.lsp.config("gopls", {
cmd = { binaries.gopls() },
-- https://github.com/golang/tools/blob/master/gopls/doc/settings.md
settings = {
gopls = {
allExperiments = true,
},
},
root_markers = { "go.work", "go.mod" },
filetypes = { "go", "gomod", "gotmpl", "gowork" },
})
19 changes: 19 additions & 0 deletions config/native/after/lsp/jsonls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- https://github.com/hrsh7th/vscode-langservers-extracted

local binaries = require("pde.binaries")
local schemastore = require("schemastore")

vim.lsp.config("jsonls", {
cmd = { binaries.jsonls(), "--stdio" },
init_options = {
provideFormatter = false, -- use prettier instead
},
settings = {
json = {
format = false,
validate = true,
schemas = schemastore.json.schemas(),
},
},
filetypes = { "json", "jsonc" },
})
146 changes: 146 additions & 0 deletions config/native/after/lsp/ltex.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
-- https://github.com/valentjn/ltex-ls

local binaries = require("pde.binaries")
local paths = require("pde.paths")

local dictionary = {}
local disabledRules = {}
local hiddenFalsePositives = {}

local name = "ltex"
local current_language = "en-US"

---@param path string
---@param lines string[]
local function append_to_file(path, lines)
local file = io.open(path, "a")
if file then
for _, line in ipairs(lines) do
file:write(line .. "\n")
end
file:close()
end
end

---@param command lsp.Command
---@return boolean
local function validate_command(command)
if #command.arguments ~= 1 then
vim.notify("unexpected arguments: " .. vim.inspect(command.arguments), vim.log.levels.ERROR)
return false
end

return true
end

---@param path string file to read
---@param target string[] target to append the lines to
local function read_lines_into(path, target)
local file = io.open(path, "r")
if file then
for line in file:lines() do
table.insert(target, line)
end

file:close()
end
end

---@param command table
---@param key string
---@param tbl table<string,string[]>
---@return table<string,string[]>
local function handle_action(command, key, tbl)
if not validate_command(command) then return {} end

local entries = command.arguments[1][key]
for lang, entry in pairs(entries) do
if not tbl[lang] then tbl[lang] = {} end
vim.list_extend(tbl[lang], entry)
end
return entries
end

local update_client_with = function(changed_settings)
local client = vim.lsp.get_clients({ name = name })[1]
if not client then return end
client:notify("workspace/didChangeConfiguration", { settings = changed_settings })
end

local function get_dictionary_file(language) return paths.get_spellfile(vim.split(language, "-")[1]) end

local function get_false_positives_file(language)
return paths.get_spellfile(nil) .. "/ltex_false-positives_" .. language .. ".txt"
end

local function get_disabled_rules_file(language)
return paths.get_spellfile(nil) .. "/ltex_disabled-rules_" .. language .. ".txt"
end

vim.lsp.config(name, {
cmd = { binaries.ltex_ls() },
filetypes = { "markdown" },
before_init = function()
if not dictionary[current_language] then
dictionary[current_language] = {}
read_lines_into(get_dictionary_file(current_language), dictionary[current_language])
end

if not hiddenFalsePositives[current_language] then
hiddenFalsePositives[current_language] = {}
read_lines_into(
get_false_positives_file(current_language),
hiddenFalsePositives[current_language]
)
end

if not disabledRules[current_language] then
disabledRules[current_language] = {}
read_lines_into(
get_disabled_rules_file(current_language),
disabledRules[current_language]
)
end
end,
settings = {
ltex = {
enabled = { "html", "markdown" },
language = current_language,
checkFrequency = "save",
dictionary = dictionary,
disabledRules = disabledRules,
hiddenFalsePositives = hiddenFalsePositives,
},
},
on_attach = function(_, buf)
vim.api.nvim_buf_call(buf, function() vim.o.spell = false end)
-- TODO: buf_command for changing the language
end,
commands = {
["_ltex.addToDictionary"] = function(command)
local new = handle_action(command, "words", dictionary)
update_client_with({ ltex = { dictionary = dictionary } })
for lang, entries in pairs(new) do
local path = get_dictionary_file(lang)
append_to_file(path, entries)
end
vim.cmd("MkSpell")
end,
["_ltex.hideFalsePositives"] = function(command)
local new = handle_action(command, "falsePositives", hiddenFalsePositives)
update_client_with({ ltex = { hiddenFalsePositives = hiddenFalsePositives } })
for lang, entries in pairs(new) do
local path = get_false_positives_file(lang)
append_to_file(path, entries)
end
end,
["_ltex.disableRules"] = function(command)
local new = handle_action(command, "ruleIds", disabledRules)
update_client_with({ ltex = { disabledRules = disabledRules } })
for lang, entries in pairs(new) do
local path = get_disabled_rules_file(lang)
append_to_file(path, entries)
end
end,
},
})
26 changes: 26 additions & 0 deletions config/native/after/lsp/lua_ls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- https://github.com/LuaLS/lua-language-server
local binaries = require("pde.binaries")

vim.lsp.config("lua_ls", {
cmd = { binaries.lua_ls() },
filetypes = { "lua" },
on_init = function(client)
-- use stylua via efm, this formatter is not great and it clears diagnostic text on save
client.server_capabilities.documentFormattingProvider = nil
client.server_capabilities.documentRangeFormattingProvider = nil
end,
settings = {
Lua = {
addonManager = { enable = false },
-- use stylua via efm, this formatter is not great and it clears diagnostic text on save
format = { enable = false },
hint = { enable = true },
runtime = { version = "LuaJIT" },
telemetry = { enable = false },
workspace = {
checkThirdParty = false,
},
},
},
root_markers = { ".luarc.json" },
})
16 changes: 16 additions & 0 deletions config/native/after/lsp/nixd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- https://github.com/nix-community/nixd

local binaries = require("pde.binaries")

vim.lsp.config("nixd", {
cmd = { binaries.nixd() },
filetypes = { "nix" },
settings = {
nixd = {
formatting = {
command = { binaries.nixfmt() },
},
},
},
root_markers = { "flake.nix" },
})
3 changes: 3 additions & 0 deletions config/native/after/lsp/prettier.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local config = require("pde.lsp.configs.efm").config_from_single("prettier")
config.filetypes = { "markdown", "yaml" }
vim.lsp.config("prettier", config)
Loading

0 comments on commit fe3a7e4

Please sign in to comment.