Skip to content

Commit

Permalink
feat: in-progress implementation of lsp without lspconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
konradmalik committed Oct 26, 2023
1 parent 4c2440e commit 4d2cf5f
Show file tree
Hide file tree
Showing 20 changed files with 350 additions and 2 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions config/native/after/plugin/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("konrad.lsp.borders")
require("konrad.lsp.commands")

require("konrad.lsp").init_efm()

Expand Down
4 changes: 4 additions & 0 deletions config/native/ftplugin/cs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local lsp = require("konrad.lsp")

local config = require("konrad.lsp.configs.omnisharp").config()
lsp.start_and_attach(config)
4 changes: 4 additions & 0 deletions config/native/ftplugin/go.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local lsp = require("konrad.lsp")

local config = require("konrad.lsp.configs.gopls").config()
lsp.start_and_attach(config)
4 changes: 4 additions & 0 deletions config/native/ftplugin/gomod.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local lsp = require("konrad.lsp")

local config = require("konrad.lsp.configs.gopls").config()
lsp.start_and_attach(config)
4 changes: 4 additions & 0 deletions config/native/ftplugin/gotmpl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local lsp = require("konrad.lsp")

local config = require("konrad.lsp.configs.gopls").config()
lsp.start_and_attach(config)
4 changes: 4 additions & 0 deletions config/native/ftplugin/gowork.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local lsp = require("konrad.lsp")

local config = require("konrad.lsp.configs.gopls").config()
lsp.start_and_attach(config)
5 changes: 5 additions & 0 deletions config/native/ftplugin/lua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local lsp = require("konrad.lsp")
local efm = require("konrad.lsp.efm")

lsp.start_and_attach(efm.build_config("stylua", { "stylua" }))
lsp.start_and_attach(require("konrad.lsp.configs.lua_ls").config())
2 changes: 2 additions & 0 deletions config/native/ftplugin/nix.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local lsp = require("konrad.lsp")
lsp.start_and_attach(require("konrad.lsp.configs.nil_ls").config())
4 changes: 4 additions & 0 deletions config/native/ftplugin/rust.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local lsp = require("konrad.lsp")

local config = require("konrad.lsp.configs.rust_analyzer").config()
lsp.start_and_attach(config)
35 changes: 35 additions & 0 deletions config/native/lua/konrad/lsp/commands.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
vim.api.nvim_create_user_command("LspClients", function()
local data = {}
for _, client in ipairs(vim.lsp.get_clients()) do
data[tostring(client.id)] = {
name = client.name,
cmd = client.config.cmd,
buffers = vim.tbl_keys(client.attached_buffers),
root_dir = client.config.root_dir,
}
end

vim.print(vim.inspect(data))
end, {
desc = "List LSP clients with their details",
})

vim.api.nvim_create_user_command("LspStopClient", function(info)
local server_id
local arguments = vim.split(info.args, "%s")
for _, v in pairs(arguments) do
if v:find("^[0-9]+$") then
server_id = tonumber(v)
end
end
if server_id then
vim.notify("stoping server with id: " .. server_id)
vim.lsp.stop_client(server_id)
else
vim.notify("stoping all lsp servers")
vim.lsp.stop_client(vim.lsp.get_clients())
end
end, {
desc = "Stops specified LSP by id",
nargs = 1,
})
18 changes: 18 additions & 0 deletions config/native/lua/konrad/lsp/configs/gopls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- https://github.com/golang/tools/tree/master/gopls

local binaries = require("konrad.binaries")
local configs = require("konrad.lsp.configs")
return {
config = function()
return {
cmd = { binaries.gopls() },
-- https://github.com/golang/tools/blob/master/gopls/doc/settings.md
settings = {
gopls = {
allExperiments = true,
},
},
root_dir = configs.root_dir({ "go.work", "go.mod", ".git" }),
}
end,
}
24 changes: 24 additions & 0 deletions config/native/lua/konrad/lsp/configs/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local M = {}

---@param config table
---@return table
M.make_config = function(config)
local base = {
capabilities = require("konrad.lsp.capabilities"),
}

return vim.tbl_deep_extend("force", base, config)
end

---@param names string[]|string
---@return string|nil
M.root_dir = function(names)
local found = vim.fs.find(names, {
upward = true,
stop = vim.uv.os_homedir(),
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
})
return vim.fs.dirname(found[1])
end

return M
37 changes: 37 additions & 0 deletions config/native/lua/konrad/lsp/configs/lua_ls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- https://github.com/sumneko/lua-language-server
vim.cmd("packadd neodev.nvim")
require("neodev").setup({
lspconfig = false,
})
local before_init = require("neodev.lsp").before_init
local binaries = require("konrad.binaries")
local configs = require("konrad.lsp.configs")

return {
config = function()
return {
name = "lua_ls",
cmd = { binaries.lua_ls() },
before_init = before_init,
init_options = {
documentFormatting = false,
documentRangeFormatting = false,
},
on_init = function(client)
-- use stylua via efm, this formatter is not great and it cleares diagnostic text on save
client.server_capabilities.documentFormattingProvider = nil
client.server_capabilities.documentRangeFormattingProvider = nil
end,
settings = {
Lua = {
addonManager = { enable = false },
telemetry = { enable = false },
hint = { enable = true },
-- use stylua via efm, this formatter is not great and it cleares diagnostic text on save
format = { enable = false },
},
},
root_dir = configs.root_dir({ ".luarc.json", "lua", ".git" }),
}
end,
}
18 changes: 18 additions & 0 deletions config/native/lua/konrad/lsp/configs/nil_ls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- https://github.com/oxalica/nil
local binaries = require("konrad.binaries")
local configs = require("konrad.lsp.configs")
return {
config = function()
return {
cmd = { binaries.nil_ls() },
settings = {
["nil"] = {
formatting = {
command = { binaries.nixpkgs_fmt() },
},
},
},
root_dir = configs.root_dir({ "flake.nix", ".git" }),
}
end,
}
94 changes: 94 additions & 0 deletions config/native/lua/konrad/lsp/configs/omnisharp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#omnisharp

-- https://github.com/Hoffs/omnisharp-extended-lsp.nvim
vim.cmd("packadd omnisharp-extended-lsp.nvim")

local binaries = require("konrad.binaries")
local configs = require("konrad.lsp.configs")
local function register_cap()
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.workspace.workspaceFolders = false -- https://github.com/OmniSharp/omnisharp-roslyn/issues/909
return capabilities
end

local M = {}

M.options = {
-- Only run analyzers against open files when 'enableRoslynAnalyzers' is
-- true
analyze_open_documents_only = true,
-- decompilation support via omnisharp-extended-lsp
enableDecompilationSupport = true,
-- Enables support for reading code style, naming convention and analyzer
-- settings from .editorconfig.
enable_editorconfig_support = true,
-- Enables support for roslyn analyzers, code fixes and rulesets.
enable_roslyn_analyzers = true,
-- Specifies whether 'using' directives should be grouped and sorted during
-- document formatting.
organize_imports_on_format = true,
-- Enables support for showing unimported types and unimported extension
-- methods in completion lists. When committed, the appropriate using
-- directive will be added at the top of the current file. This option can
-- have a negative impact on initial completion responsiveness,
-- particularly for the first few completion sessions after opening a
-- solution.
enable_import_completion = true,
}

M.config = function()
local cmd = { binaries.omnisharp() }
-- Append hard-coded command arguments
table.insert(cmd, "-z") -- https://github.com/OmniSharp/omnisharp-vscode/pull/4300
vim.list_extend(cmd, { "--hostPID", tostring(vim.fn.getpid()) })
table.insert(cmd, "DotNet:enablePackageRestore=false")
vim.list_extend(cmd, { "--encoding", "utf-8" })
table.insert(cmd, "--languageserver")

-- Append configuration-dependent command arguments
if M.options.enable_editorconfig_support then
table.insert(cmd, "FormattingOptions:EnableEditorConfigSupport=true")
end

if M.options.organize_imports_on_format then
table.insert(cmd, "FormattingOptions:OrganizeImports=true")
end

if M.options.enable_ms_build_load_projects_on_demand then
table.insert(cmd, "MsBuild:LoadProjectsOnDemand=true")
end

if M.options.enable_roslyn_analyzers then
table.insert(cmd, "RoslynExtensionsOptions:EnableAnalyzersSupport=true")
end

if M.options.enable_import_completion then
table.insert(cmd, "RoslynExtensionsOptions:EnableImportCompletion=true")
end

if M.options.sdk_include_prereleases then
table.insert(cmd, "Sdk:IncludePrereleases=true")
end

if M.options.analyze_open_documents_only then
table.insert(cmd, "RoslynExtensionsOptions:AnalyzeOpenDocumentsOnly=true")
end

return {
cmd = cmd,
on_init = function(client, initialize_result)
-- disable codelens for omnisharp because it makes it extremely slow
client.server_capabilities.codeLensProvider = nil
-- inlayHints are broken as well as of 1.39.10
client.server_capabilities.inlayHintProvider = nil
client.notify("workspace/didChangeConfiguration", { settings = client.config.settings })
end,
capabilities = register_cap(),
handlers = {
["textDocument/definition"] = require("omnisharp_extended").handler,
},
root_dir = configs.root_dir(".sln") or configs.root_dir(".csproj"),
}
end

return M
47 changes: 47 additions & 0 deletions config/native/lua/konrad/lsp/configs/rust_analyzer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local binaries = require("konrad.binaries")
local configs = require("konrad.lsp.configs")

local function register_cap()
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.experimental = {
serverStatusNotification = true,
}
return capabilities
end

return {
config = function()
return {
cmd = { binaries.rust_analyzer() },
capabilities = register_cap(),
settings = {
["rust-analyzer"] = {
rustfmt = {
overrideCommand = { binaries.rustfmt(), "--" },
},
files = {
excludeDirs = {
"./.direnv/",
"./.git/",
"./.github/",
"./.gitlab/",
"./node_modules/",
"./ci/",
"./docs/",
},
},
checkOnSave = {
enable = true,
},
diagnostics = {
enable = true,
experimental = {
enable = true,
},
},
},
},
root_dir = configs.root_dir({ "Cargo.toml", "rust-project.json" }),
}
end,
}
31 changes: 31 additions & 0 deletions config/native/lua/konrad/lsp/efm/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,35 @@ M.build_lspconfig = function(plugins)
}
end

---@param name string unique name of this efm instance
---@param plugins string[] names of plugins to add, ex. 'prettier'
---@return table config to be put into lspconfig['efm'].setup(config)
M.build_config = function(name, plugins)
local languages = {}
for _, v in ipairs(plugins) do
local plugin = require("konrad.lsp.efm." .. v)
local langages_entry = make_languages_entry_for_plugin(plugin)
for key, value in pairs(langages_entry) do
if languages[key] then
languages[key] = vim.list_extend(languages[key], value)
else
languages[key] = value
end
end
end

return {
name = name,
cmd = { binaries.efm() },
init_options = {
documentFormatting = true,
documentRangeFormatting = true,
},
settings = {
rootMarkers = { ".git/" },
languages = languages,
},
}
end

return M
13 changes: 13 additions & 0 deletions config/native/lua/konrad/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ M.init_efm = function()
initialized_efm = true
end

---@param config table
---@return integer
M.start_and_attach = function(config)
local made_config = require("konrad.lsp.configs").make_config(config)
local client_id = vim.lsp.start(made_config)
if not client_id then
vim.notify("cannot start lsp: " .. config.cmd, vim.log.levels.ERROR)
return 0
end
vim.lsp.buf_attach_client(vim.api.nvim_get_current_buf(), client_id)
return client_id
end

return M
3 changes: 1 addition & 2 deletions config/native/plugin/comment.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local comment = require("Comment")
comment.setup({
require("Comment").setup({
mappings = {
basic = true,
extra = false,
Expand Down

0 comments on commit 4d2cf5f

Please sign in to comment.