diff --git a/README.md b/README.md index 242b08d..e4e3b5a 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ use {'ray-x/navigator.lua', requires = {'ray-x/guihua.lua', run = 'cd lua/fzy && ## Setup +Easy setup lspconfig and navigator with one liner + ```lua lua require'navigator'.setup() ``` diff --git a/lua/navigator/lspclient/attach.lua b/lua/navigator/lspclient/attach.lua index 124da37..18e5ccf 100644 --- a/lua/navigator/lspclient/attach.lua +++ b/lua/navigator/lspclient/attach.lua @@ -54,7 +54,7 @@ M.on_attach = function(client, bufnr) require("navigator.lspclient.mapping").setup({client = client, bufnr = bufnr, cap = client.resolved_capabilities}) vim.cmd [[silent! packadd vim-illuminate]] - local hasilm, ilm = pcall(require, "illuminate") -- package.loaded["illuminate"] + local hasilm, ilm = pcall(require, "illuminate") -- package.loaded["illuminate"] if hasilm then ilm.on_attach(client) end @@ -62,7 +62,7 @@ M.on_attach = function(client, bufnr) local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true - local config = require'navigator'.config_value + local config = require "navigator".config_value if config ~= nil and config.on_attach ~= nil then config.on_attach(client, bufnr) end diff --git a/lua/navigator/lspclient/clients.lua b/lua/navigator/lspclient/clients.lua index 6285355..b4717b4 100644 --- a/lua/navigator/lspclient/clients.lua +++ b/lua/navigator/lspclient/clients.lua @@ -1,21 +1,26 @@ -- todo allow config passed in local lspconfig = nil +local log = require "navigator.util".log + if packer_plugins ~= nil then - if not packer_plugins["neovim/nvim-lspconfig"] or - not packer_plugins["neovim/nvim-lspconfig"].loaded then - vim.cmd [[packadd nvim-lspconfig]] - end - if not packer_plugins["ray-x/guihua.lua"] or - not packer_plugins["guihua.lua"].loaded then - vim.cmd [[packadd guihua.lua]] - -- if lazyloading - end + if not packer_plugins["neovim/nvim-lspconfig"] or not packer_plugins["neovim/nvim-lspconfig"].loaded then + vim.cmd [[packadd nvim-lspconfig]] + end + if not packer_plugins["ray-x/guihua.lua"] or not packer_plugins["guihua.lua"].loaded then + vim.cmd [[packadd guihua.lua]] + -- if lazyloading + end +end +if package.loaded["lspconfig"] then + lspconfig = require "lspconfig" end -if package.loaded["lspconfig"] then lspconfig = require "lspconfig" end local highlight = require "navigator.lspclient.highlight" -if lspconfig == nil then error("loading lsp config") end -local config = require"navigator".config_values() +if lspconfig == nil then + error("loading lsp config") +end +local util = lspconfig.util +local config = require "navigator".config_values() local cap = vim.lsp.protocol.make_client_capabilities() local on_attach = require("navigator.lspclient.attach").on_attach @@ -23,164 +28,218 @@ local on_attach = require("navigator.lspclient.attach").on_attach -- gopls["ui.completion.usePlaceholders"] = true local golang_setup = { - on_attach = on_attach, - capabilities = cap, - -- init_options = { - -- useplaceholders = true, - -- completeunimported = true - -- }, - message_level = vim.lsp.protocol.MessageType.Error, - cmd = { - "gopls" + on_attach = on_attach, + capabilities = cap, + -- init_options = { + -- useplaceholders = true, + -- completeunimported = true + -- }, + message_level = vim.lsp.protocol.MessageType.Error, + cmd = { + "gopls" - -- share the gopls instance if there is one already - -- "-remote=auto", + -- share the gopls instance if there is one already + -- "-remote=auto", - --[[ debug options ]] - -- - -- "-logfile=auto", - -- "-debug=:0", - -- "-remote.debug=:0", - -- "-rpc.trace", - }, - settings = { - gopls = { - analyses = {unusedparams = true, unreachable = false}, - codelenses = { - generate = true, -- show the `go generate` lens. - gc_details = true -- // Show a code lens toggling the display of gc's choices. - }, - usePlaceholders = true, - completeUnimported = true, - staticcheck = true, - matcher = "fuzzy", - symbolMatcher = "fuzzy", - gofumpt = true, - buildFlags = {"-tags", "integration"} - -- buildFlags = {"-tags", "functional"} - } - }, - root_dir = function(fname) - local util = require("lspconfig").util - return util.root_pattern("go.mod", ".git")(fname) or - util.path.dirname(fname) - end + --[[ debug options ]] + -- + -- "-logfile=auto", + -- "-debug=:0", + -- "-remote.debug=:0", + -- "-rpc.trace", + }, + settings = { + gopls = { + analyses = {unusedparams = true, unreachable = false}, + codelenses = { + generate = true, -- show the `go generate` lens. + gc_details = true -- // Show a code lens toggling the display of gc's choices. + }, + usePlaceholders = true, + completeUnimported = true, + staticcheck = true, + matcher = "fuzzy", + symbolMatcher = "fuzzy", + gofumpt = true, + buildFlags = {"-tags", "integration"} + -- buildFlags = {"-tags", "functional"} + } + }, + root_dir = function(fname) + local util = require("lspconfig").util + return util.root_pattern("go.mod", ".git")(fname) or util.path.dirname(fname) + end } local clang_cfg = { - cmd = { - "clangd", "--background-index", "--suggest-missing-includes", - "--clang-tidy", "--header-insertion=iwyu" - }, - on_attach = function(client) - client.resolved_capabilities.document_formatting = true - on_attach(client) - end + cmd = { + "clangd", + "--background-index", + "--suggest-missing-includes", + "--clang-tidy", + "--header-insertion=iwyu" + }, + on_attach = function(client) + client.resolved_capabilities.document_formatting = true + on_attach(client) + end } local rust_cfg = { - settings = { - ["rust-analyzer"] = { - assist = {importMergeBehavior = "last", importPrefix = "by_self"}, - cargo = {loadOutDirsFromCheck = true}, - procMacro = {enable = true} - } + settings = { + filetypes = {"rust"}, + root_dir = util.root_pattern("Cargo.toml", "rust-project.json", ".git"), + ["rust-analyzer"] = { + assist = {importMergeBehavior = "last", importPrefix = "by_self"}, + cargo = {loadOutDirsFromCheck = true}, + procMacro = {enable = true} } + } } local sqls_cfg = { - on_attach = function(client, bufnr) - client.resolved_capabilities.execute_command = true - highlight.diagnositc_config_sign() - require"sqls".setup {picker = "telescope"} -- or default - end, - settings = { - cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"} - -- alterantively: - -- connections = { - -- { - -- driver = 'postgresql', - -- datasourcename = 'host=127.0.0.1 port=5432 user=postgres password=password dbname=user_db sslmode=disable', - -- }, - -- }, - } + on_attach = function(client, bufnr) + client.resolved_capabilities.execute_command = true + highlight.diagnositc_config_sign() + require "sqls".setup {picker = "telescope"} -- or default + end, + settings = { + cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"} + -- alterantively: + -- connections = { + -- { + -- driver = 'postgresql', + -- datasourcename = 'host=127.0.0.1 port=5432 user=postgres password=password dbname=user_db sslmode=disable', + -- }, + -- }, + } } -- lua setup local sumneko_root_path = config.sumneko_root_path local sumneko_binary = config.sumneko_binary local lua_cfg = { - cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"}, - on_attach = on_attach, - settings = { - Lua = { - runtime = { - -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) - version = "LuaJIT", - -- Setup your lua path - path = vim.split(package.path, ";") - }, - diagnostics = { - enable = true, - -- Get the language server to recognize the `vim` global - globals = { - "vim", "describe", "it", "before_each", "after_each", - "teardown", "pending" - } - }, - workspace = { - -- Make the server aware of Neovim runtime files - library = { - [vim.fn.expand("$VIMRUNTIME/lua")] = true, - [vim.fn.expand("$VIMRUNTIME/lua/vim")] = true, - [vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true - -- [vim.fn.expand("~/repos/nvim/lua")] = true - } - } + cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"}, + on_attach = on_attach, + settings = { + Lua = { + runtime = { + -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) + version = "LuaJIT", + -- Setup your lua path + path = vim.split(package.path, ";") + }, + diagnostics = { + enable = true, + -- Get the language server to recognize the `vim` global + globals = { + "vim", + "describe", + "it", + "before_each", + "after_each", + "teardown", + "pending" + } + }, + workspace = { + -- Make the server aware of Neovim runtime files + library = { + [vim.fn.expand("$VIMRUNTIME/lua")] = true, + [vim.fn.expand("$VIMRUNTIME/lua/vim")] = true, + [vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true + -- [vim.fn.expand("~/repos/nvim/lua")] = true } + } } + } } + +local pyright_cfg = { + cmd = {"pyright-langserver", "--stdio"}, + filetypes = {"python"}, + settings = { + python = { + analysis = { + autoSearchPaths = true, + useLibraryCodeForTypes = true + } + } + } +} + +local rust_cfg = { + filetypes = {"rust"}, + root_dir = util.root_pattern("Cargo.toml", "rust-project.json", ".git"), + message_level = vim.lsp.protocol.MessageType.error, + log_level = vim.lsp.protocol.MessageType.error, + on_attach = on_attach +} + local servers = { - "gopls", "tsserver", "flow", "bashls", "dockerls", "pyls", - "jedi_language_server", "jdtls", "sumneko_lua", "vimls", "html", "jsonls", - "cssls", "yamlls", "clangd", "sqls", "denols", "dartls", "dotls", - "kotlin_language_server", "nimls", "phpactor", "r_language_server", - "rust_analyzer", "terraformls" + "gopls", + "tsserver", + "flow", + "bashls", + "dockerls", + "pyls", + "pyright", + "jedi_language_server", + "jdtls", + "sumneko_lua", + "vimls", + "html", + "jsonls", + "cssls", + "yamlls", + "clangd", + "sqls", + "denols", + "dartls", + "dotls", + "kotlin_language_server", + "nimls", + "phpactor", + "r_language_server", + "rust_analyzer", + "terraformls" } local function setup(user_opts) - if lspconfig == nil then - error("lsp-config need installed and enabled") - return - end + if lspconfig == nil then + error("lsp-config need installed and enabled") + return + end - highlight.diagnositc_config_sign() - highlight.add_highlight() - for _, lspclient in ipairs(servers) do - if lspconfig[lspclient] == nil then - print("not supported", lspclient) - goto continue - end - local lspft = lspconfig[lspclient].filetypes - if lspft ~= nil and #lspft > 0 then - local ft = vim.bo.filetype - local should_load = false - for _, value in ipairs(lspft) do - if ft == value then should_load = true end - end - if not should_load then goto continue end + highlight.diagnositc_config_sign() + highlight.add_highlight() + for _, lspclient in ipairs(servers) do + if lspconfig[lspclient] == nil then + print("not supported", lspclient) + goto continue + end + local lspft = lspconfig[lspclient].filetypes + if lspft ~= nil and #lspft > 0 then + local ft = vim.bo.filetype + local should_load = false + for _, value in ipairs(lspft) do + if ft == value then + should_load = true end - - lspconfig[lspclient].setup { - message_level = vim.lsp.protocol.MessageType.error, - log_level = vim.lsp.protocol.MessageType.error, - on_attach = on_attach - } - ::continue:: + end + if not should_load then + goto continue + end end - lspconfig.gopls.setup(golang_setup) - lspconfig.sqls.setup(sqls_cfg) - lspconfig.sumneko_lua.setup(lua_cfg) - lspconfig.clangd.setup(clang_cfg) - lspconfig.rust_analyzer.setup(rust_cfg) + ::continue:: + end + lspconfig.rust_analyzer.setup(rust_cfg) + lspconfig.gopls.setup(golang_setup) + lspconfig.sqls.setup(sqls_cfg) + lspconfig.sumneko_lua.setup(lua_cfg) + lspconfig.clangd.setup(clang_cfg) + lspconfig.rust_analyzer.setup(rust_cfg) + lspconfig.pyright.setup(pyright_cfg) + + log("setup all clients finished") end return {setup = setup, cap = cap} diff --git a/lua/navigator/lspclient/config.lua b/lua/navigator/lspclient/config.lua index ba89bf3..46879c9 100644 --- a/lua/navigator/lspclient/config.lua +++ b/lua/navigator/lspclient/config.lua @@ -1,6 +1,6 @@ local lsp = require("vim.lsp") -M={} +M = {} local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true @@ -18,6 +18,6 @@ vim.cmd("command! -nargs=0 LspLog call v:lua.open_lsp_log()") vim.cmd("command! -nargs=0 LspRestart call v:lua.reload_lsp()") local cfg = {} -require('lsp.clients').setup(cfg) -require('lsp.mappings').setup(cfg) + +require("lsp.clients").setup(cfg) return M diff --git a/lua/navigator/lspclient/init.lua b/lua/navigator/lspclient/init.lua index f965627..76fa89e 100644 --- a/lua/navigator/lspclient/init.lua +++ b/lua/navigator/lspclient/init.lua @@ -2,6 +2,7 @@ local M = {} M.setup = function(cfg) cfg = cfg or {} require('navigator.lspclient.clients').setup(cfg) + require("navigator.lspclient.mapping").setup(cfg) end return M diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index bb3d67c..62c8b5d 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -47,9 +47,7 @@ local function set_mapping(user_opts) local user_key = user_opts.keymaps or {} local bufnr = user_opts.bufnr or 0 - local function buf_set_keymap(...) - vim.api.nvim_buf_set_keymap(bufnr, ...) - end + local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end -- local function buf_set_option(...) -- vim.api.nvim_buf_set_option(bufnr, ...) @@ -80,13 +78,25 @@ local function set_mapping(user_opts) -- format setup + local range_fmt = false + local doc_fmt = false + for _, value in pairs(vim.lsp.buf_get_clients(0)) do + if value == nil or value.resolved_capabilities == nil then return end + if value.resolved_capabilities.document_formatting then doc_fmt = true end + if value.resolved_capabilities.document_range_formatting then range = true end + end + -- if user_opts.cap.document_formatting then - buf_set_keymap("n", "ff", "lua vim.lsp.buf.formatting()", + if doc_fmt then + buf_set_keymap("n", "ff", "lua vim.lsp.buf.formatting()", opts) - vim.cmd([[autocmd BufWritePre lua vim.lsp.buf.formatting()]]) + vim.cmd([[autocmd BufWritePre lua vim.lsp.buf.formatting()]]) + end -- if user_opts.cap.document_range_formatting then - buf_set_keymap("v", "ff", + if range_fmt then + buf_set_keymap("v", "ff", "lua vim.lsp.buf.range_formatting()", opts) + end end local function set_event_handler(user_opts) diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index d24d531..cf70ac6 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -98,7 +98,7 @@ local default_config = { level = "info" } -M._log = require("guihua.log").new({level = "error"}, true) +M._log = require("guihua.log").new({level = "trace"}, true) -- add log to you lsp.log M.log = M._log.info