From dcc40278c596ee8b918a4f655d0edd54c1691c43 Mon Sep 17 00:00:00 2001 From: ray-x Date: Wed, 27 Jul 2022 18:17:07 +1000 Subject: [PATCH] use create_autocmd API; split clients.lua into 3 files --- lua/navigator/diagnostics.lua | 61 +++-- lua/navigator/foldlsp.lua | 21 +- lua/navigator/lspclient/attach.lua | 10 +- lua/navigator/lspclient/clients.lua | 281 +------------------- lua/navigator/lspclient/clients_default.lua | 191 +++++++++++++ lua/navigator/lspclient/sumneko_lua.lua | 4 + lua/navigator/util.lua | 14 + 7 files changed, 270 insertions(+), 312 deletions(-) create mode 100644 lua/navigator/lspclient/clients_default.lua diff --git a/lua/navigator/diagnostics.lua b/lua/navigator/diagnostics.lua index ae178a3..6cf39d4 100644 --- a/lua/navigator/diagnostics.lua +++ b/lua/navigator/diagnostics.lua @@ -2,7 +2,6 @@ local gui = require('navigator.gui') local diagnostic_list = {} local diagnostic = vim.diagnostic or vim.lsp.diagnostic -- local hide = diagnostic.hide or diagnostic.clear -_NG_VT_DIAG_NS = vim.api.nvim_create_namespace('navigator_lua_diag') local util = require('navigator.util') local log = util.log local trace = require('guihua.log').trace @@ -11,6 +10,8 @@ local error = util.error local path_sep = require('navigator.util').path_sep() local path_cur = require('navigator.util').path_cur() local empty = util.empty +local api = vim.api +_NG_VT_DIAG_NS = api.nvim_create_namespace('navigator_lua_diag') if not util.nvim_0_6_1() then util.warn('Navigator 0.4+ only support nvim-0.6+, please use Navigator 0.3.x or a newer version of neovim') @@ -57,14 +58,14 @@ local function error_marker(result, ctx, config) if bufnr == nil then bufnr = vim.uri_to_bufnr(result.uri) end - local fname = vim.api.nvim_buf_get_name(bufnr) + local fname = api.nvim_buf_get_name(bufnr) local uri = vim.uri_from_fname(fname) if uri ~= result.uri then log('not same buf', ctx, result.uri, bufnr, vim.fn.bufnr()) return end - if not vim.api.nvim_buf_is_loaded(bufnr) then + if not api.nvim_buf_is_loaded(bufnr) then log('buf not loaded', bufnr) return end @@ -76,7 +77,7 @@ local function error_marker(result, ctx, config) local diag_cnt = get_count(bufnr, [[Error]]) + get_count(bufnr, [[Warning]]) if diag_cnt == 0 and _NG_VT_DIAG_NS ~= nil then log('great no errors') - vim.api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) + api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) end return end @@ -84,12 +85,12 @@ local function error_marker(result, ctx, config) -- total line num of current buffer -- local winid = vim.fn.win_getid(vim.fn.winnr()) - -- local winid = vim.api.nvim_get_current_win() - local total_num = vim.api.nvim_buf_line_count(bufnr) + -- local winid = api.nvim_get_current_win() + local total_num = api.nvim_buf_line_count(bufnr) -- local total_num = vim.fn.getbufinfo(vim.fn.winbufnr(winid))[1].linecount -- window size of current buffer - local stats = vim.api.nvim_list_uis()[1] + local stats = api.nvim_list_uis()[1] -- local wwidth = stats.width; local wheight = stats.height @@ -97,7 +98,7 @@ local function error_marker(result, ctx, config) return end if _NG_VT_DIAG_NS == nil then - _NG_VT_DIAG_NS = vim.api.nvim_create_namespace('navigator_lua_diag') + _NG_VT_DIAG_NS = api.nvim_create_namespace('navigator_lua_diag') end local pos = {} @@ -139,7 +140,7 @@ local function error_marker(result, ctx, config) end if not vim.tbl_isempty(pos) then - vim.api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) + api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) end for _, s in pairs(pos) do local hl = 'ErrorMsg' @@ -160,7 +161,7 @@ local function error_marker(result, ctx, config) end trace('add pos', s, bufnr) - vim.api.nvim_buf_set_extmark( + api.nvim_buf_set_extmark( bufnr, _NG_VT_DIAG_NS, l, @@ -184,7 +185,7 @@ local diag_hdlr = function(err, result, ctx, config) return end - local mode = vim.api.nvim_get_mode().mode + local mode = api.nvim_get_mode().mode if mode ~= 'n' and config.update_in_insert == false then log('skip sign update in insert mode') end @@ -242,7 +243,7 @@ local diag_hdlr = function(err, result, ctx, config) end end local bufnr1 = vim.uri_to_bufnr(uri) - local loaded = vim.api.nvim_buf_is_loaded(bufnr1) + local loaded = api.nvim_buf_is_loaded(bufnr1) if _NgConfigValues.diagnostic_load_files then -- print('load buffers') if not loaded then @@ -250,7 +251,7 @@ local diag_hdlr = function(err, result, ctx, config) end local pos = v.range.start local row = pos.line - local line = (vim.api.nvim_buf_get_lines(bufnr1, row, row + 1, false) or { '' })[1] + local line = (api.nvim_buf_get_lines(bufnr1, row, row + 1, false) or { '' })[1] if line ~= nil then item.text = head .. line .. _NgConfigValues.icons.diagnostic_head_description .. v.message else @@ -284,7 +285,7 @@ local diag_hdlr = function(err, result, ctx, config) marker(result, ctx, config) else trace('great, no diag errors') - vim.api.nvim_buf_clear_namespace(0, _NG_VT_DIAG_NS, 0, -1) + api.nvim_buf_clear_namespace(0, _NG_VT_DIAG_NS, 0, -1) _NG_VT_DIAG_NS = nil end end @@ -327,18 +328,24 @@ function M.setup() vim.diagnostic.config(diagnostic_cfg) if _NgConfigValues.lsp.diagnostic_scrollbar_sign then - vim.cmd([[autocmd WinScrolled * lua require'navigator.diagnostics'.update_err_marker()]]) + api.nvim_create_autocmd({ 'WinScrolled' }, { + group = api.nvim_create_augroup('NGWinScrolledGroup', {}), + pattern = '*', + callback = function() + require('navigator.diagnostics').update_err_marker() + end, + }) end end local function clear_diag_VT(bufnr) -- important for clearing out when no more errors - bufnr = bufnr or vim.api.nvim_get_current_buf() + bufnr = bufnr or api.nvim_get_current_buf() log(bufnr, _NG_VT_DIAG_NS) if _NG_VT_DIAG_NS == nil then return end - vim.api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) + api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) _NG_VT_DIAG_NS = nil end @@ -380,7 +387,7 @@ M.show_buf_diagnostics = function() end trace('new buffer', listview.bufnr) if listview.bufnr then - vim.api.nvim_buf_add_highlight(listview.bufnr, -1, 'Title', 0, 0, -1) + api.nvim_buf_add_highlight(listview.bufnr, -1, 'Title', 0, 0, -1) end end end @@ -388,7 +395,7 @@ end -- set loc list win M.set_diag_loclist = function() - local bufnr = vim.api.nvim_get_current_buf() + local bufnr = api.nvim_get_current_buf() local diag_cnt = get_count(bufnr, [[Error]]) + get_count(bufnr, [[Warning]]) if diag_cnt == 0 then log('great, no errors!') @@ -424,7 +431,7 @@ function M.update_err_marker() -- nothing to update return end - local bufnr = vim.api.nvim_get_current_buf() + local bufnr = api.nvim_get_current_buf() local diag_cnt = get_count(bufnr, [[Error]]) + get_count(bufnr, [[Warning]]) @@ -433,12 +440,12 @@ function M.update_err_marker() -- redraw if diag_cnt == 0 and _NG_VT_DIAG_NS ~= nil then - vim.api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) + api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) trace('no errors') return end - vim.api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) + api.nvim_buf_clear_namespace(bufnr, _NG_VT_DIAG_NS, 0, -1) local errors = diagnostic.get(bufnr) if #errors == 0 then trace('no errors', errors) @@ -453,13 +460,13 @@ function M.update_err_marker() end function M.get_line_diagnostic() - local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1 - return diagnostic.get(vim.api.nvim_get_current_buf(), { lnum = lnum }) + local lnum = api.nvim_win_get_cursor(0)[1] - 1 + return diagnostic.get(api.nvim_get_current_buf(), { lnum = lnum }) end function M.show_diagnostics(pos) - local bufnr = vim.api.nvim_get_current_buf() - local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1 + local bufnr = api.nvim_get_current_buf() + local lnum = api.nvim_win_get_cursor(0)[1] - 1 local opt = { border = 'single' } if diagnostic.open_float and type(diagnostic.open_float) == 'function' then if pos == true then @@ -481,7 +488,7 @@ function M.treesitter_and_diag_panel() local results = diagnostic_list[ft] log(diagnostic_list, ft) - local bufnr = vim.api.nvim_get_current_buf() + local bufnr = api.nvim_get_current_buf() local p = Panel:new({ header = 'treesitter', render = function(b) diff --git a/lua/navigator/foldlsp.lua b/lua/navigator/foldlsp.lua index ce05ae7..c23da43 100644 --- a/lua/navigator/foldlsp.lua +++ b/lua/navigator/foldlsp.lua @@ -28,19 +28,14 @@ function M.on_attach() end function M.setup_plugin() - api.nvim_command('augroup FoldingCommand') - api.nvim_command('autocmd! * ') - api.nvim_command("autocmd BufEnter lua require'navigator.foldlsp'.update_folds()") - api.nvim_command("autocmd BufWritePost lua require'navigator.foldlsp'.update_folds()") - api.nvim_command('augroup end') - - -- vim.cmd([[ - -- - -- function! folding_nvim#foldexpr() - -- return luaeval(printf('require"navigator.foldlsp".get_fold_indic(%d)', v:lnum)) - -- endfunction - -- - -- ]]) + local cmd_group = api.nvim_create_augroup('NGFoldGroup', {}) + api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost' }, { + group = cmd_group, + pattern = '*', + callback = function() + require('navigator.foldlsp').update_folds() + end, + }) local clients = vim.lsp.buf_get_clients(0) diff --git a/lua/navigator/lspclient/attach.lua b/lua/navigator/lspclient/attach.lua index 75f3449..dcbad49 100644 --- a/lua/navigator/lspclient/attach.lua +++ b/lua/navigator/lspclient/attach.lua @@ -63,9 +63,13 @@ M.on_attach = function(client, bufnr) if _NgConfigValues.lsp.code_action.enable then if client.server_capabilities.codeActionProvider and client.name ~= 'null-ls' then log('code action enabled for client', client.server_capabilities.codeActionProvider) - api.nvim_command('augroup NCodeAction') - vim.cmd([[autocmd CursorHold,CursorHoldI lua require'navigator.codeAction'.code_action_prompt()]]) - api.nvim_command('augroup end') + api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { + group = api.nvim_create_augroup('NGCodeActGroup', {}), + buffer = bufnr, + callback = function() + require('navigator.codeAction').code_action_prompt() + end, + }) end end end diff --git a/lua/navigator/lspclient/clients.lua b/lua/navigator/lspclient/clients.lua index 6bed481..9ae9863 100644 --- a/lua/navigator/lspclient/clients.lua +++ b/lua/navigator/lspclient/clients.lua @@ -79,286 +79,29 @@ if _NgConfigValues.mason then require('navigator.lazyloader').load('mason-lspconfig.nvim', 'williamboman/mason-lspconfig.nvim') end -local ok, l = pcall(require, 'lua-dev') -if ok and l then - luadev = l.setup(luadevcfg) -end - -local function add(lib) - for _, p in pairs(vfn.expand(lib, false, true)) do - p = vim.loop.fs_realpath(p) - if p then - library[p] = true - end - end -end - --- add runtime -add('$VIMRUNTIME') - --- add your config --- local home = vfn.expand("$HOME") -add(vfn.stdpath('config')) - --- add plugins it may be very slow to add all in path --- if vfn.isdirectory(home .. "/.config/share/nvim/site/pack/packer") then --- add(home .. "/.local/share/nvim/site/pack/packer/opt/*") --- add(home .. "/.local/share/nvim/site/pack/packer/start/*") --- end - -library[vfn.expand('$VIMRUNTIME/lua')] = true -library[vfn.expand('$VIMRUNTIME/lua/vim')] = true -library[vfn.expand('$VIMRUNTIME/lua/vim/lsp')] = true --- [vfn.expand("~/repos/nvim/lua")] = true - --- TODO remove onece PR #944 merged to lspconfig -local path_sep = require('navigator.util').path_sep() -local strip_dir_pat = path_sep .. '([^' .. path_sep .. ']+)$' -local strip_sep_pat = path_sep .. '$' -local dirname = function(pathname) - if not pathname or #pathname == 0 then - return - end - local result = pathname:gsub(strip_sep_pat, ''):gsub(strip_dir_pat, '') - if #result == 0 then - return '/' - end - return result -end --- TODO end - -local setups = { - clojure_lsp = { - root_dir = function(fname) - return util.root_pattern('deps.edn', 'build.boot', 'project.clj', 'shadow-cljs.edn', 'bb.edn', '.git')(fname) - or util.path.dirname(fname) - end, - on_attach = on_attach, - filetypes = { 'clojure', 'edn' }, - message_level = vim.lsp.protocol.MessageType.error, - cmd = { 'clojure-lsp' }, - }, - - elixirls = { - on_attach = on_attach, - filetypes = { 'elixir', 'eelixir' }, - cmd = { 'elixir-ls' }, - message_level = vim.lsp.protocol.MessageType.error, - settings = { - elixirLS = { - dialyzerEnabled = true, - fetchDeps = false, - }, - }, - root_dir = function(fname) - return util.root_pattern('mix.exs', '.git')(fname) or util.path.dirname(fname) - end, - }, - - gopls = { - on_attach = on_attach, - -- capabilities = cap, - filetypes = { 'go', 'gomod', 'gohtmltmpl', 'gotexttmpl' }, - message_level = vim.lsp.protocol.MessageType.Error, - cmd = { - 'gopls', -- share the gopls instance if there is one already - '-remote=auto', --[[ debug options ]] -- - -- "-logfile=auto", - -- "-debug=:0", - '-remote.debug=:0', - -- "-rpc.trace", - }, - - flags = { allow_incremental_sync = true, debounce_text_changes = 1000 }, - settings = { - gopls = { - -- more settings: https://github.com/golang/tools/blob/master/gopls/doc/settings.md - -- flags = {allow_incremental_sync = true, debounce_text_changes = 500}, - -- not supported - 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. - test = true, - tidy = true, - }, - usePlaceholders = true, - completeUnimported = true, - staticcheck = true, - matcher = 'fuzzy', - diagnosticsDelay = '500ms', - experimentalWatchedFileDelay = '1000ms', - symbolMatcher = 'fuzzy', - gofumpt = false, -- true, -- turn on for new repos, gofmpt is good but also create code turmoils - buildFlags = { '-tags', 'integration' }, - -- buildFlags = {"-tags", "functional"} - }, - }, - root_dir = function(fname) - return util.root_pattern('go.mod', '.git')(fname) or dirname(fname) -- util.path.dirname(fname) - end, - }, - clangd = { - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, - cmd = { - 'clangd', - '--background-index', - '--suggest-missing-includes', - '--clang-tidy', - '--header-insertion=iwyu', - '--clang-tidy-checks=-*,llvm-*,clang-analyzer-*', - '--cross-file-rename', - }, - filetypes = { 'c', 'cpp', 'objc', 'objcpp' }, - on_attach = function(client, bufnr) - client.server_capabilities.documentFormattingProvider = client.server_capabilities.documentFormattingProvider - or true - on_attach(client, bufnr) - end, - }, - rust_analyzer = { - root_dir = function(fname) - return util.root_pattern('Cargo.toml', 'rust-project.json', '.git')(fname) or util.path.dirname(fname) - end, - filetypes = { 'rust' }, - message_level = vim.lsp.protocol.MessageType.error, - on_attach = on_attach, - settings = { - ['rust-analyzer'] = { - assist = { importMergeBehavior = 'last', importPrefix = 'by_self' }, - cargo = { loadOutDirsFromCheck = true }, - procMacro = { enable = true }, - }, - }, - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, - }, - sqls = { - filetypes = { 'sql' }, - on_attach = function(client, _) - client.server_capabilities.executeCommandProvider = client.server_capabilities.documentFormattingProvider or true - highlight.diagnositc_config_sign() - require('sqls').setup({ picker = 'telescope' }) -- or default - end, - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, - 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', - -- }, - -- }, - }, - }, - sumneko_lua = { - cmd = { 'lua-language-server' }, - filetypes = { 'lua' }, - on_attach = on_attach, - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, - settings = { - Lua = { - runtime = { - -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) - version = 'LuaJIT', - }, - diagnostics = { - enable = true, - -- Get the language server to recognize the `vim` global - globals = { 'vim', 'describe', 'it', 'before_each', 'after_each', 'teardown', 'pending' }, - }, - completion = { callSnippet = 'Both' }, - workspace = { - -- Make the server aware of Neovim runtime files - library = library, - maxPreload = 2000, - preloadFileSize = 40000, - }, - telemetry = { enable = false }, - }, - }, - on_new_config = function(cfg, root) - local libs = vim.tbl_deep_extend('force', {}, library) - libs[root] = nil - cfg.settings.Lua.workspace.library = libs - return cfg - end, - }, - pyright = { - on_attach = on_attach, - cmd = { 'pyright-langserver', '--stdio' }, - filetypes = { 'python' }, - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, - settings = { - python = { - formatting = { provider = 'black' }, - analysis = { - autoSearchPaths = true, - useLibraryCodeForTypes = true, - diagnosticMode = 'workspace', - }, - }, - }, - }, - ccls = { - on_attach = on_attach, - init_options = { - compilationDatabaseDirectory = 'build', - root_dir = [[ util.root_pattern("compile_commands.json", "compile_flags.txt", "CMakeLists.txt", "Makefile", ".git") or util.path.dirname ]], - index = { threads = 2 }, - clang = { excludeArgs = { '-frounding-math' } }, - }, - flags = { allow_incremental_sync = true }, - }, - jdtls = { - settings = { - java = { signatureHelp = { enabled = true }, contentProvider = { preferred = 'fernflower' } }, - }, - }, - omnisharp = { - cmd = { 'omnisharp', '--languageserver', '--hostPID', tostring(vfn.getpid()) }, - }, - terraformls = { - filetypes = { 'terraform', 'tf' }, - }, - - sourcekit = { - cmd = { 'sourcekit-lsp' }, - filetypes = { 'swift' }, -- This is recommended if you have separate settings for clangd. - }, -} - -setups.sumneko_lua = vim.tbl_deep_extend('force', luadev, setups.sumneko_lua) - +local setups = require('navigator.lspclient.clients_default').defaults() local servers = require('navigator.lspclient.servers') local lsp_installer_servers = {} local has_lspinst = false local has_mason = false -if config.lsp_installer == true then - has_lspinst, _ = pcall(require, 'nvim-lsp-installer') - if has_lspinst then - local srvs = require('nvim-lsp-installer.servers').get_installed_servers() - log('lsp_installered servers', srvs) - if #srvs > 0 then - lsp_installer_servers = srvs - end +has_lspinst, _ = pcall(require, 'nvim-lsp-installer') +if has_lspinst then + local srvs = require('nvim-lsp-installer.servers').get_installed_servers() + if #srvs > 0 then + lsp_installer_servers = srvs end - log(lsp_installer_servers) end -if config.mason == true then - has_mason, _ = pcall(require, 'mason-lspconfig') - if has_mason then - local srvs=require'mason-lspconfig'.get_installed_servers() - log('lsp_installered servers', srvs) - if #srvs > 0 then - lsp_installer_servers = srvs - end +has_mason, _ = pcall(require, 'mason-lspconfig') +if has_mason then + local srvs=require'mason-lspconfig'.get_installed_servers() + if #srvs > 0 then + lsp_installer_servers = srvs end - log(lsp_installer_servers) end +log("lsp_installer:", lsp_installer_servers) if config.lsp.disable_lsp == 'all' then config.lsp.disable_lsp = servers diff --git a/lua/navigator/lspclient/clients_default.lua b/lua/navigator/lspclient/clients_default.lua new file mode 100644 index 0000000..56ab64a --- /dev/null +++ b/lua/navigator/lspclient/clients_default.lua @@ -0,0 +1,191 @@ +local M = {} +local vfn = vim.fn +M.defaults = function() + local has_lsp, lspconfig = pcall(require, 'lspconfig') + local highlight = require('navigator.lspclient.highlight') + if not has_lsp then + return { + setup = function() + vim.notify('loading lsp config failed LSP may not working correctly', vim.lsp.log_levels.WARN) + end, + } + end + local util = lspconfig.util + local on_attach = require('navigator.lspclient.attach').on_attach + + + local setups = { + clojure_lsp = { + root_dir = function(fname) + return util.root_pattern('deps.edn', 'build.boot', 'project.clj', 'shadow-cljs.edn', 'bb.edn', '.git')(fname) + or util.path.dirname(fname) + end, + on_attach = on_attach, + filetypes = { 'clojure', 'edn' }, + message_level = vim.lsp.protocol.MessageType.error, + cmd = { 'clojure-lsp' }, + }, + + elixirls = { + on_attach = on_attach, + filetypes = { 'elixir', 'eelixir' }, + cmd = { 'elixir-ls' }, + message_level = vim.lsp.protocol.MessageType.error, + settings = { + elixirLS = { + dialyzerEnabled = true, + fetchDeps = false, + }, + }, + root_dir = function(fname) + return util.root_pattern('mix.exs', '.git')(fname) or util.path.dirname(fname) + end, + }, + + gopls = { + on_attach = on_attach, + -- capabilities = cap, + filetypes = { 'go', 'gomod', 'gohtmltmpl', 'gotexttmpl' }, + message_level = vim.lsp.protocol.MessageType.Error, + cmd = { + 'gopls', -- share the gopls instance if there is one already + '-remote=auto', --[[ debug options ]] -- + -- "-logfile=auto", + -- "-debug=:0", + '-remote.debug=:0', + -- "-rpc.trace", + }, + + flags = { allow_incremental_sync = true, debounce_text_changes = 1000 }, + settings = { + gopls = { + -- more settings: https://github.com/golang/tools/blob/master/gopls/doc/settings.md + -- flags = {allow_incremental_sync = true, debounce_text_changes = 500}, + -- not supported + 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. + test = true, + tidy = true, + }, + usePlaceholders = true, + completeUnimported = true, + staticcheck = true, + matcher = 'fuzzy', + diagnosticsDelay = '500ms', + experimentalWatchedFileDelay = '1000ms', + symbolMatcher = 'fuzzy', + gofumpt = false, -- true, -- turn on for new repos, gofmpt is good but also create code turmoils + buildFlags = { '-tags', 'integration' }, + -- buildFlags = {"-tags", "functional"} + }, + }, + root_dir = function(fname) + return util.root_pattern('go.mod', '.git')(fname) or dirname(fname) -- util.path.dirname(fname) + end, + }, + clangd = { + flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + cmd = { + 'clangd', + '--background-index', + '--suggest-missing-includes', + '--clang-tidy', + '--header-insertion=iwyu', + '--clang-tidy-checks=-*,llvm-*,clang-analyzer-*', + '--cross-file-rename', + }, + filetypes = { 'c', 'cpp', 'objc', 'objcpp' }, + on_attach = function(client, bufnr) + client.server_capabilities.documentFormattingProvider = client.server_capabilities.documentFormattingProvider + or true + on_attach(client, bufnr) + end, + }, + rust_analyzer = { + root_dir = function(fname) + return util.root_pattern('Cargo.toml', 'rust-project.json', '.git')(fname) or util.path.dirname(fname) + end, + filetypes = { 'rust' }, + message_level = vim.lsp.protocol.MessageType.error, + on_attach = on_attach, + settings = { + ['rust-analyzer'] = { + assist = { importMergeBehavior = 'last', importPrefix = 'by_self' }, + cargo = { loadOutDirsFromCheck = true }, + procMacro = { enable = true }, + }, + }, + flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + }, + sqls = { + filetypes = { 'sql' }, + on_attach = function(client, _) + client.server_capabilities.executeCommandProvider = client.server_capabilities.documentFormattingProvider + or true + highlight.diagnositc_config_sign() + require('sqls').setup({ picker = 'telescope' }) -- or default + end, + flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + 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', + -- }, + -- }, + }, + }, + + pyright = { + on_attach = on_attach, + cmd = { 'pyright-langserver', '--stdio' }, + filetypes = { 'python' }, + flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + settings = { + python = { + formatting = { provider = 'black' }, + analysis = { + autoSearchPaths = true, + useLibraryCodeForTypes = true, + diagnosticMode = 'workspace', + }, + }, + }, + }, + ccls = { + on_attach = on_attach, + init_options = { + compilationDatabaseDirectory = 'build', + root_dir = [[ util.root_pattern("compile_commands.json", "compile_flags.txt", "CMakeLists.txt", "Makefile", ".git") or util.path.dirname ]], + index = { threads = 2 }, + clang = { excludeArgs = { '-frounding-math' } }, + }, + flags = { allow_incremental_sync = true }, + }, + jdtls = { + settings = { + java = { signatureHelp = { enabled = true }, contentProvider = { preferred = 'fernflower' } }, + }, + }, + omnisharp = { + cmd = { 'omnisharp', '--languageserver', '--hostPID', tostring(vfn.getpid()) }, + }, + terraformls = { + filetypes = { 'terraform', 'tf' }, + }, + + sourcekit = { + cmd = { 'sourcekit-lsp' }, + filetypes = { 'swift' }, -- This is recommended if you have separate settings for clangd. + }, + } + + setups.sumneko_lua = require('navigator.lspclient.sumneko_lua').sumneko_lua() + return setups +end + +return M diff --git a/lua/navigator/lspclient/sumneko_lua.lua b/lua/navigator/lspclient/sumneko_lua.lua index 0c0364c..4971957 100644 --- a/lua/navigator/lspclient/sumneko_lua.lua +++ b/lua/navigator/lspclient/sumneko_lua.lua @@ -83,3 +83,7 @@ local function sumneko_lua() sumneko_cfg = vim.tbl_deep_extend('force', luadev, sumneko_cfg) return sumneko_cfg end + +return { + sumneko_lua = sumneko_lua, +} diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index 876d36d..cb861e1 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -540,4 +540,18 @@ function M.range_inside(outer, inner) return outer.start.line <= inner.start.line and outer['end'].line >= inner['end'].line end +function M.dirname(pathname) + local path_sep = require('navigator.util').path_sep() + local strip_dir_pat = path_sep .. '([^' .. path_sep .. ']+)$' + local strip_sep_pat = path_sep .. '$' + if not pathname or #pathname == 0 then + return + end + local result = pathname:gsub(strip_sep_pat, ''):gsub(strip_dir_pat, '') + if #result == 0 then + return '/' + end + return result +end + return M