From 9e4a2c45165a6ba8160999513e256077173ef6d6 Mon Sep 17 00:00:00 2001 From: aaronsms Date: Thu, 29 Jul 2021 17:04:03 +0800 Subject: fix: change peekDefinition method location --- lua/lsp/keybinds.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/keybinds.lua b/lua/lsp/keybinds.lua index 5722dab7..820ebce9 100644 --- a/lua/lsp/keybinds.lua +++ b/lua/lsp/keybinds.lua @@ -13,7 +13,7 @@ function M.setup() { noremap = true, silent = true } ) - vim.cmd "nnoremap gp lua require'lsp.utils'.PeekDefinition()" + vim.cmd "nnoremap gp lua require'lsp.service'.PeekDefinition()" vim.cmd "nnoremap K :lua vim.lsp.buf.hover()" vim.cmd "nnoremap :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})" vim.cmd "nnoremap :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})" -- cgit v1.2.3 From 3eaf6d461c896ba9fadaf63af0ecbe3769653fa2 Mon Sep 17 00:00:00 2001 From: Pasi Bergman Date: Thu, 29 Jul 2021 16:03:03 +0300 Subject: fix: Use null-ls eslint diagnostics config with eslint_d exe (#1159) --- lua/lsp/null-ls.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index a231698a..6a31de26 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -13,15 +13,25 @@ local find_local_exe = function(exe) return local_exe end +-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint +local get_normalized_exe = function(exe, type) + if type == "diagnostics" and exe == "eslint_d" then + return "eslint" + end + return exe +end + local function setup_ls(exe, type) if utils.has_value(local_executables, exe) then - local smart_executable = null_ls.builtins[type][exe] + local normalized_exe = get_normalized_exe(exe, type) + local smart_executable = null_ls.builtins[type][normalized_exe] local local_executable = find_local_exe(exe) if vim.fn.executable(local_executable) == 1 then smart_executable._opts.command = local_executable table.insert(sources, smart_executable) else if vim.fn.executable(exe) == 1 then + smart_executable._opts.command = exe table.insert(sources, smart_executable) end end -- cgit v1.2.3 From 9d89929d9bb47d1f78c2d3945b761da2f24a5643 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sat, 31 Jul 2021 06:06:08 +0200 Subject: Enable querying lang-server formatting capabilities (#1078) --- lua/lsp/init.lua | 98 +++++++++++++++++++++++++---------------- lua/lsp/keybinds.lua | 67 +++++++++++++++++++++++++++- lua/lsp/null-ls.lua | 113 ++++++++++++++++++++++++----------------------- lua/lsp/service.lua | 122 --------------------------------------------------- 4 files changed, 185 insertions(+), 215 deletions(-) delete mode 100644 lua/lsp/service.lua (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 62c42fd8..67007e81 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -1,55 +1,79 @@ -local utils = require "utils" -local service = require "lsp.service" -local null_ls = require "lsp.null-ls" local M = {} - +local u = require "utils" function M.config() require("lsp.kind").setup() require("lsp.handlers").setup() require("lsp.signs").setup() - require("lsp.keybinds").setup() end -function M.setup(lang) - local lang_server = lvim.lang[lang].lsp - local provider = lang_server.provider - if utils.check_lsp_client_active(provider) then - return +local function lsp_highlight_document(client) + if lvim.lsp.document_highlight == false then + return -- we don't need further + end + -- Set autocommands conditional on server_capabilities + if client.resolved_capabilities.document_highlight then + vim.api.nvim_exec( + [[ + hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646 + hi LspReferenceText cterm=bold ctermbg=red guibg=#464646 + hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646 + augroup lsp_document_highlight + autocmd! * + autocmd CursorHold lua vim.lsp.buf.document_highlight() + autocmd CursorMoved lua vim.lsp.buf.clear_references() + augroup END + ]], + false + ) end +end - local overrides = lvim.lsp.override +local function formatter_handler(client) + local buffer_filetype = vim.bo.filetype + local ext_provider = lvim.lang[buffer_filetype].formatter.exe - if utils.is_table(overrides) then - if utils.has_value(overrides, lang) then - return - end + if ext_provider then + client.resolved_capabilities.document_formatting = false + u.lvim_log( + string.format("Overriding [%s] formatting if exists, Using provider [%s] instead", client.name, ext_provider) + ) end +end - if utils.is_string(overrides) then - if overrides == lang then - return - end +function M.common_capabilities() + local capabilities = vim.lsp.protocol.make_client_capabilities() + capabilities.textDocument.completion.completionItem.snippetSupport = true + capabilities.textDocument.completion.completionItem.resolveSupport = { + properties = { + "documentation", + "detail", + "additionalTextEdits", + }, + } + return capabilities +end + +function M.common_on_init(client, bufnr) + if lvim.lsp.on_init_callback then + lvim.lsp.on_init_callback(client, bufnr) + return end - local sources = null_ls.setup(lang) - - for _, source in pairs(sources) do - local method = source.method - local format_method = "NULL_LS_FORMATTING" - - if utils.is_table(method) then - if utils.has_value(method, format_method) then - lang_server.setup.on_attach = service.no_formatter_on_attach - end - end - - if utils.is_string(method) then - if method == format_method then - lang_server.setup.on_attach = service.no_formatter_on_attach - end - end + formatter_handler(client) +end + +function M.common_on_attach(client, bufnr) + if lvim.lsp.on_attach_callback then + lvim.lsp.on_attach_callback(client, bufnr) end + lsp_highlight_document(client) + require("lsp.keybinds").setup() + require("lsp.null-ls").setup(vim.bo.filetype) +end - if provider == "" or provider == nil then +function M.setup(lang) + local lang_server = lvim.lang[lang].lsp + local provider = lang_server.provider + if require("utils").check_lsp_client_active(provider) then return end diff --git a/lua/lsp/keybinds.lua b/lua/lsp/keybinds.lua index 820ebce9..cc0d4ec9 100644 --- a/lua/lsp/keybinds.lua +++ b/lua/lsp/keybinds.lua @@ -1,5 +1,70 @@ local M = {} +-- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/ +function M.preview_location(location, context, before_context) + -- location may be LocationLink or Location (more useful for the former) + context = context or 15 + before_context = before_context or 0 + local uri = location.targetUri or location.uri + if uri == nil then + return + end + local bufnr = vim.uri_to_bufnr(uri) + if not vim.api.nvim_buf_is_loaded(bufnr) then + vim.fn.bufload(bufnr) + end + + local range = location.targetRange or location.range + local contents = vim.api.nvim_buf_get_lines( + bufnr, + range.start.line - before_context, + range["end"].line + 1 + context, + false + ) + local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") + return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border }) +end + +function M.preview_location_callback(_, method, result) + local context = 15 + if result == nil or vim.tbl_isempty(result) then + print("No location found: " .. method) + return nil + end + if vim.tbl_islist(result) then + M.floating_buf, M.floating_win = M.preview_location(result[1], context) + else + M.floating_buf, M.floating_win = M.preview_location(result, context) + end +end + +function M.PeekDefinition() + if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then + vim.api.nvim_set_current_win(M.floating_win) + else + local params = vim.lsp.util.make_position_params() + return vim.lsp.buf_request(0, "textDocument/definition", params, M.preview_location_callback) + end +end + +function M.PeekTypeDefinition() + if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then + vim.api.nvim_set_current_win(M.floating_win) + else + local params = vim.lsp.util.make_position_params() + return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, M.preview_location_callback) + end +end + +function M.PeekImplementation() + if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then + vim.api.nvim_set_current_win(M.floating_win) + else + local params = vim.lsp.util.make_position_params() + return vim.lsp.buf_request(0, "textDocument/implementation", params, M.preview_location_callback) + end +end + function M.setup() if lvim.lsp.default_keybinds then vim.cmd "nnoremap gd lua vim.lsp.buf.definition()" @@ -13,7 +78,7 @@ function M.setup() { noremap = true, silent = true } ) - vim.cmd "nnoremap gp lua require'lsp.service'.PeekDefinition()" + vim.cmd "nnoremap gp lua require'lsp.keybinds'.PeekDefinition()" vim.cmd "nnoremap K :lua vim.lsp.buf.hover()" vim.cmd "nnoremap :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})" vim.cmd "nnoremap :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})" diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 6a31de26..d2222602 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -1,76 +1,79 @@ local M = {} +local u = require "utils" +local null_ls = require "null-ls" -local _, null_ls = pcall(require, "null-ls") -local utils = require "utils" -local sources = {} +local nodejs_local_providers = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" } -local local_executables = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" } +M.requested_providers = {} -local find_local_exe = function(exe) - vim.cmd "let root_dir = FindRootDirectory()" - local root_dir = vim.api.nvim_get_var "root_dir" - local local_exe = root_dir .. "/node_modules/.bin/" .. exe - return local_exe +local function is_nodejs_provider(provider) + for _, local_provider in ipairs(nodejs_local_providers) do + if local_provider == provider.exe then + return true + end + end + return false end --- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint -local get_normalized_exe = function(exe, type) - if type == "diagnostics" and exe == "eslint_d" then - return "eslint" +local function is_provider_found(provider) + -- special case: fallback to "eslint" + -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint + provider._opts.command = provider._opts.command == "eslint_d" and "eslint" or provider._opts.command + + local retval = { is_local = false, path = nil } + if vim.fn.executable(provider._opts.command) == 1 then + return false, provider._opts.command end - return exe + if is_nodejs_provider(provider) then + vim.cmd "let root_dir = FindRootDirectory()" + local root_dir = vim.api.nvim_get_var "root_dir" + local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command + if vim.fn.executable(local_provider_command) == 1 then + retval.is_local = true + retval.path = local_provider_command + end + end + return retval.is_local, retval.path end -local function setup_ls(exe, type) - if utils.has_value(local_executables, exe) then - local normalized_exe = get_normalized_exe(exe, type) - local smart_executable = null_ls.builtins[type][normalized_exe] - local local_executable = find_local_exe(exe) - if vim.fn.executable(local_executable) == 1 then - smart_executable._opts.command = local_executable - table.insert(sources, smart_executable) - else - if vim.fn.executable(exe) == 1 then - smart_executable._opts.command = exe - table.insert(sources, smart_executable) - end - end - else - if null_ls.builtins[type][exe] and vim.fn.executable(null_ls.builtins[type][exe]._opts.command) then - table.insert(sources, null_ls.builtins[type][exe]) - end +local function validate_provider(provider) + local is_local, provider_path = is_provider_found(provider) + if not provider_path then + u.lvim_log(string.format("Unable to find the path for: [%s]", provider)) + return false end - null_ls.register { sources = sources } + if is_local then + provider._opts.command = provider_path + end + return true end -- TODO: for linters and formatters with spaces and '-' replace with '_' -local function setup(filetype, type) - local executables = nil - if type == "diagnostics" then - executables = lvim.lang[filetype].linters +function M.setup(filetype) + for _, formatter in pairs(lvim.lang[filetype].formatters) do + local builtin_formatter = null_ls.builtins.formatting[formatter.exe] + -- FIXME: why doesn't this work? + -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args + -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin + table.insert(M.requested_providers, builtin_formatter) + u.lvim_log(string.format("Using format provider: [%s]", formatter.exe)) end - if type == "formatting" then - executables = lvim.lang[filetype].formatter.exe + + for _, linter in pairs(lvim.lang[filetype].linters) do + local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe] + -- FIXME: why doesn't this work? + -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args + -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin + table.insert(M.requested_providers, builtin_diagnoser) + u.lvim_log(string.format("Using linter provider: [%s]", linter.exe)) end - if utils.is_table(executables) then - for _, exe in pairs(executables) do - if exe ~= "" then - setup_ls(exe, type) - end + for idx, provider in pairs(M.requested_providers) do + if not validate_provider(provider) then + table.remove(M.requested_providers, idx) end end - if utils.is_string(executables) and executables ~= "" then - setup_ls(executables, type) - end -end - --- TODO: return the formatter if one was registered, then turn off the builtin formatter -function M.setup(filetype) - setup(filetype, "formatting") - setup(filetype, "diagnostics") - lvim.sources = sources - return sources + null_ls.register { sources = M.requested_providers } end return M diff --git a/lua/lsp/service.lua b/lua/lsp/service.lua deleted file mode 100644 index 0c49bacd..00000000 --- a/lua/lsp/service.lua +++ /dev/null @@ -1,122 +0,0 @@ -local M = {} - -local function lsp_highlight_document(client) - if lvim.lsp.document_highlight == false then - return -- we don't need further - end - -- Set autocommands conditional on server_capabilities - if client.resolved_capabilities.document_highlight then - vim.api.nvim_exec( - [[ - hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646 - hi LspReferenceText cterm=bold ctermbg=red guibg=#464646 - hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646 - augroup lsp_document_highlight - autocmd! * - autocmd CursorHold lua vim.lsp.buf.document_highlight() - autocmd CursorMoved lua vim.lsp.buf.clear_references() - augroup END - ]], - false - ) - end -end - -function M.lsp_highlight_document(client) - lsp_highlight_document(client) -end - --- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/ -function M.preview_location(location, context, before_context) - -- location may be LocationLink or Location (more useful for the former) - context = context or 15 - before_context = before_context or 0 - local uri = location.targetUri or location.uri - if uri == nil then - return - end - local bufnr = vim.uri_to_bufnr(uri) - if not vim.api.nvim_buf_is_loaded(bufnr) then - vim.fn.bufload(bufnr) - end - - local range = location.targetRange or location.range - local contents = vim.api.nvim_buf_get_lines( - bufnr, - range.start.line - before_context, - range["end"].line + 1 + context, - false - ) - local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") - return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border }) -end - -function M.preview_location_callback(_, method, result) - local context = 15 - if result == nil or vim.tbl_isempty(result) then - print("No location found: " .. method) - return nil - end - if vim.tbl_islist(result) then - M.floating_buf, M.floating_win = M.preview_location(result[1], context) - else - M.floating_buf, M.floating_win = M.preview_location(result, context) - end -end - -function M.PeekDefinition() - if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then - vim.api.nvim_set_current_win(M.floating_win) - else - local params = vim.lsp.util.make_position_params() - return vim.lsp.buf_request(0, "textDocument/definition", params, M.preview_location_callback) - end -end - -function M.PeekTypeDefinition() - if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then - vim.api.nvim_set_current_win(M.floating_win) - else - local params = vim.lsp.util.make_position_params() - return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, M.preview_location_callback) - end -end - -function M.PeekImplementation() - if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then - vim.api.nvim_set_current_win(M.floating_win) - else - local params = vim.lsp.util.make_position_params() - return vim.lsp.buf_request(0, "textDocument/implementation", params, M.preview_location_callback) - end -end - -function M.common_on_attach(client, bufnr) - if lvim.lsp.on_attach_callback then - lvim.lsp.on_attach_callback(client, bufnr) - end - lsp_highlight_document(client) -end - -function M.no_formatter_on_attach(client, bufnr) - if lvim.lsp.on_attach_callback then - lvim.lsp.on_attach_callback(client, bufnr) - end - lsp_highlight_document(client) - client.resolved_capabilities.document_formatting = false -end - -function M.common_capabilities() - local capabilities = vim.lsp.protocol.make_client_capabilities() - capabilities.textDocument.completion.completionItem.snippetSupport = true - capabilities.textDocument.completion.completionItem.resolveSupport = { - properties = { - "documentation", - "detail", - "additionalTextEdits", - }, - } - return capabilities -end - -return M -- cgit v1.2.3 From 5646462a50c9e537952fba6c2faca518c3f04139 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 31 Jul 2021 00:07:47 -0400 Subject: fix format_handler --- lua/lsp/init.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 67007e81..b24d62f2 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -29,13 +29,12 @@ local function lsp_highlight_document(client) end local function formatter_handler(client) - local buffer_filetype = vim.bo.filetype - local ext_provider = lvim.lang[buffer_filetype].formatter.exe - - if ext_provider then + local formatter_exe = lvim.lang[vim.bo.filetype].formatters[1].exe + if formatter_exe and formatter_exe ~= "" then client.resolved_capabilities.document_formatting = false + __FORMATTER_OVERRIDE = true u.lvim_log( - string.format("Overriding [%s] formatting if exists, Using provider [%s] instead", client.name, ext_provider) + string.format("Overriding [%s] formatting if exists, Using provider [%s] instead", client.name, formatter_exe) ) end end -- cgit v1.2.3 From 2db171eee417de8916237c053244d7a44deac5c1 Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Sat, 31 Jul 2021 12:15:01 +0430 Subject: fix luacheck issues (#1184) --- lua/lsp/init.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index b24d62f2..d7109ac3 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -32,7 +32,8 @@ local function formatter_handler(client) local formatter_exe = lvim.lang[vim.bo.filetype].formatters[1].exe if formatter_exe and formatter_exe ~= "" then client.resolved_capabilities.document_formatting = false - __FORMATTER_OVERRIDE = true + -- NOTE: do we still need __FORMATTER_OVERRIDE? + -- __FORMATTER_OVERRIDE = true u.lvim_log( string.format("Overriding [%s] formatting if exists, Using provider [%s] instead", client.name, formatter_exe) ) -- cgit v1.2.3 From d977e7384e8ad449e1ffc039618910a9c780e0cf Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sat, 31 Jul 2021 10:19:57 +0200 Subject: cleanup formatting handler (#1185) --- lua/lsp/init.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index d7109ac3..1488aec0 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -29,13 +29,11 @@ local function lsp_highlight_document(client) end local function formatter_handler(client) - local formatter_exe = lvim.lang[vim.bo.filetype].formatters[1].exe - if formatter_exe and formatter_exe ~= "" then + local formatters = lvim.lang[vim.bo.filetype].formatters + if not vim.tbl_isempty(formatters) then client.resolved_capabilities.document_formatting = false - -- NOTE: do we still need __FORMATTER_OVERRIDE? - -- __FORMATTER_OVERRIDE = true u.lvim_log( - string.format("Overriding [%s] formatting if exists, Using provider [%s] instead", client.name, formatter_exe) + string.format("Overriding [%s] formatting if exists, Using provider [%s] instead", client.name, formatters[1].exe) ) end end -- cgit v1.2.3 From f36082da0dccf4cfc50dd3fec271b7a5cd41aaea Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sat, 31 Jul 2021 14:52:25 +0200 Subject: feat: add diagnostics source name (#1147) --- lua/lsp/handlers.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lua/lsp') diff --git a/lua/lsp/handlers.lua b/lua/lsp/handlers.lua index d3a487ae..3a467d42 100644 --- a/lua/lsp/handlers.lua +++ b/lua/lsp/handlers.lua @@ -9,6 +9,40 @@ function M.setup() underline = lvim.lsp.document_highlight, }) + vim.lsp.handlers["textDocument/publishDiagnostics"] = function(_, _, params, client_id, _) + local config = { -- your config + virtual_text = lvim.lsp.diagnostics.virtual_text, + signs = lvim.lsp.diagnostics.signs, + underline = lvim.lsp.diagnostics.underline, + update_in_insert = lvim.lsp.diagnostics.update_in_insert, + severity_sort = lvim.lsp.diagnostics.severity_sort, + } + local uri = params.uri + local bufnr = vim.uri_to_bufnr(uri) + + if not bufnr then + return + end + + local diagnostics = params.diagnostics + + for i, v in ipairs(diagnostics) do + diagnostics[i].message = string.format("%s: %s", v.source, v.message) + + if vim.tbl_contains(vim.tbl_keys(v), "code") then + diagnostics[i].message = diagnostics[i].message .. string.format(" [%s]", v.code) + end + end + + vim.lsp.diagnostic.save(diagnostics, bufnr, client_id) + + if not vim.api.nvim_buf_is_loaded(bufnr) then + return + end + + vim.lsp.diagnostic.display(diagnostics, bufnr, client_id, config) + end + vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = lvim.lsp.popup_border, }) -- cgit v1.2.3 From 8157f50d1308f42f3db1c7f69c226eb2e5c0b796 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sat, 31 Jul 2021 15:04:22 +0200 Subject: feat: get null-ls registered providers by filetype (#1186) --- lua/lsp/null-ls.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index d2222602..468d12a6 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -6,6 +6,17 @@ local nodejs_local_providers = { "prettier", "prettierd", "prettier_d_slim", "es M.requested_providers = {} +function M.get_registered_providers_by_filetype(ft) + local matches = {} + for _, provider in pairs(M.requested_providers) do + if vim.tbl_contains(provider.filetypes, ft) then + table.insert(matches, provider.name) + end + end + + return matches +end + local function is_nodejs_provider(provider) for _, local_provider in ipairs(nodejs_local_providers) do if local_provider == provider.exe then -- cgit v1.2.3 From cf16a2e826774e89d1bfe5812b6f73c3dd049db2 Mon Sep 17 00:00:00 2001 From: Geet Sethi <65440103+sethigeet@users.noreply.github.com> Date: Sat, 31 Jul 2021 19:14:08 +0530 Subject: Add the better peek functions (#1172) --- lua/lsp/keybinds.lua | 67 +----------------------- lua/lsp/peek.lua | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 66 deletions(-) create mode 100644 lua/lsp/peek.lua (limited to 'lua/lsp') diff --git a/lua/lsp/keybinds.lua b/lua/lsp/keybinds.lua index cc0d4ec9..21f29994 100644 --- a/lua/lsp/keybinds.lua +++ b/lua/lsp/keybinds.lua @@ -1,70 +1,5 @@ local M = {} --- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/ -function M.preview_location(location, context, before_context) - -- location may be LocationLink or Location (more useful for the former) - context = context or 15 - before_context = before_context or 0 - local uri = location.targetUri or location.uri - if uri == nil then - return - end - local bufnr = vim.uri_to_bufnr(uri) - if not vim.api.nvim_buf_is_loaded(bufnr) then - vim.fn.bufload(bufnr) - end - - local range = location.targetRange or location.range - local contents = vim.api.nvim_buf_get_lines( - bufnr, - range.start.line - before_context, - range["end"].line + 1 + context, - false - ) - local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") - return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border }) -end - -function M.preview_location_callback(_, method, result) - local context = 15 - if result == nil or vim.tbl_isempty(result) then - print("No location found: " .. method) - return nil - end - if vim.tbl_islist(result) then - M.floating_buf, M.floating_win = M.preview_location(result[1], context) - else - M.floating_buf, M.floating_win = M.preview_location(result, context) - end -end - -function M.PeekDefinition() - if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then - vim.api.nvim_set_current_win(M.floating_win) - else - local params = vim.lsp.util.make_position_params() - return vim.lsp.buf_request(0, "textDocument/definition", params, M.preview_location_callback) - end -end - -function M.PeekTypeDefinition() - if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then - vim.api.nvim_set_current_win(M.floating_win) - else - local params = vim.lsp.util.make_position_params() - return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, M.preview_location_callback) - end -end - -function M.PeekImplementation() - if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then - vim.api.nvim_set_current_win(M.floating_win) - else - local params = vim.lsp.util.make_position_params() - return vim.lsp.buf_request(0, "textDocument/implementation", params, M.preview_location_callback) - end -end - function M.setup() if lvim.lsp.default_keybinds then vim.cmd "nnoremap gd lua vim.lsp.buf.definition()" @@ -78,7 +13,7 @@ function M.setup() { noremap = true, silent = true } ) - vim.cmd "nnoremap gp lua require'lsp.keybinds'.PeekDefinition()" + vim.cmd "nnoremap gp lua require'lsp.peek'.Peek('definition')" vim.cmd "nnoremap K :lua vim.lsp.buf.hover()" vim.cmd "nnoremap :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})" vim.cmd "nnoremap :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})" diff --git a/lua/lsp/peek.lua b/lua/lsp/peek.lua new file mode 100644 index 00000000..e512eee0 --- /dev/null +++ b/lua/lsp/peek.lua @@ -0,0 +1,140 @@ +local M = { + floating_buf = nil, + floating_win = nil, + prev_result = nil, +} + +local function create_floating_file(location, opts) + vim.validate { + location = { location, "t" }, + opts = { opts, "t", true }, + } + + -- Set some defaults + opts = opts or {} + local close_events = opts.close_events or { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" } + + -- location may be LocationLink or Location + local uri = location.targetUri or location.uri + if uri == nil then + return + end + local bufnr = vim.uri_to_bufnr(uri) + if not vim.api.nvim_buf_is_loaded(bufnr) then + vim.fn.bufload(bufnr) + end + + local range = location.targetRange or location.range + + local contents = vim.api.nvim_buf_get_lines( + bufnr, + range.start.line, + math.min(range["end"].line + 1 + (opts.context or 10), range.start.line + (opts.max_height or 15)), -- Don't let the window be more that 15 lines long(height) + false + ) + local width, height = vim.lsp.util._make_floating_popup_size(contents, opts) + opts = vim.lsp.util.make_floating_popup_options(width, height, opts) + -- Don't make it minimal as it is meant to be fully featured + opts["style"] = nil + + vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe") + + local winnr = vim.api.nvim_open_win(bufnr, false, opts) + vim.api.nvim_win_set_option(winnr, "winblend", 0) + + vim.api.nvim_buf_set_var(bufnr, "lsp_floating_window", winnr) + + -- Set some autocmds to close the window + vim.api.nvim_command( + "autocmd QuitPre ++nested ++once lua pcall(vim.api.nvim_win_close, " .. winnr .. ", true)" + ) + vim.lsp.util.close_preview_autocmd(close_events, winnr) + + return bufnr, winnr +end + +local function preview_location_callback(_, method, result) + if result == nil or vim.tbl_isempty(result) then + print("peek: No location found: " .. method) + return nil + end + + local opts = { + border = "rounded", + context = 10, + } + + if vim.tbl_islist(result) then + M.prev_result = result[1] + M.floating_buf, M.floating_win = create_floating_file(result[1], opts) + else + M.prev_result = result + M.floating_buf, M.floating_win = create_floating_file(result, opts) + end +end + +function M.open_file() + -- Get the file currently open in the floating window + local filepath = vim.fn.expand "%:." + + if not filepath then + print "peek: Unable to open the file!" + return + end + + -- Close the floating window + pcall(vim.api.nvim_win_close, M.floating_win, true) + + -- Edit the file + vim.cmd("edit " .. filepath) + + local winnr = vim.api.nvim_get_current_win() + + -- Set the cursor at the right position + M.set_cursor_to_prev_pos(winnr) +end + +function M.set_cursor_to_prev_pos(winnr) + -- Get position of the thing to peek at + local location = M.prev_result + local range = location.targetRange or location.range + local cursor_pos = { range.start.line + 1, range.start.character } + + -- Set the winnr to the floting window if none was passed in + winnr = winnr or M.floating_win + -- Set the cursor at the correct position in the floating window + vim.api.nvim_win_set_cursor(winnr, cursor_pos) +end + +function M.Peek(what) + -- If a window already exists, focus it at the right position! + if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then + local success_1, _ = pcall(vim.api.nvim_set_current_win, M.floating_win) + if not success_1 then + print "peek: You cannot edit the current file in a preview!" + return + end + + -- Set the cursor at the correct position in the floating window + M.set_cursor_to_prev_pos() + + vim.api.nvim_buf_set_keymap( + M.floating_buf, + "n", + "", + ":lua require('lsp.peek').open_file()", + { noremap = true, silent = true } + ) + else + -- Make a new request and then create the new window in the callback + local params = vim.lsp.util.make_position_params() + local success, _ = pcall(vim.lsp.buf_request, 0, "textDocument/" .. what, params, preview_location_callback) + if not success then + print( + 'peek: Error calling LSP method "textDocument/' .. what .. '". The current language lsp might not support it.' + ) + end + end +end + +return M -- cgit v1.2.3 From fe5daa722fb75ad85c24936cbb645018bb9d655b Mon Sep 17 00:00:00 2001 From: Luc Sinet Date: Sat, 31 Jul 2021 16:12:29 +0200 Subject: [Feature] Expose lsp config (#1156) --- lua/lsp/handlers.lua | 2 +- lua/lsp/init.lua | 35 +++++++++++++++++------------------ lua/lsp/keybinds.lua | 27 --------------------------- lua/lsp/kind.lua | 33 --------------------------------- lua/lsp/signs.lua | 20 -------------------- 5 files changed, 18 insertions(+), 99 deletions(-) delete mode 100644 lua/lsp/keybinds.lua delete mode 100644 lua/lsp/kind.lua delete mode 100644 lua/lsp/signs.lua (limited to 'lua/lsp') diff --git a/lua/lsp/handlers.lua b/lua/lsp/handlers.lua index 3a467d42..a25db3c1 100644 --- a/lua/lsp/handlers.lua +++ b/lua/lsp/handlers.lua @@ -5,7 +5,7 @@ local M = {} function M.setup() vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { virtual_text = lvim.lsp.diagnostics.virtual_text, - signs = lvim.lsp.diagnostics.signs, + signs = lvim.lsp.diagnostics.signs.active, underline = lvim.lsp.document_highlight, }) diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 1488aec0..13b64dac 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -1,9 +1,14 @@ local M = {} local u = require "utils" + function M.config() - require("lsp.kind").setup() + vim.lsp.protocol.CompletionItemKind = lvim.lsp.completion.item_kind + + for _, sign in ipairs(lvim.lsp.diagnostics.signs.values) do + vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = sign.name }) + end + require("lsp.handlers").setup() - require("lsp.signs").setup() end local function lsp_highlight_document(client) @@ -28,16 +33,6 @@ local function lsp_highlight_document(client) end end -local function formatter_handler(client) - local formatters = lvim.lang[vim.bo.filetype].formatters - if not vim.tbl_isempty(formatters) then - client.resolved_capabilities.document_formatting = false - u.lvim_log( - string.format("Overriding [%s] formatting if exists, Using provider [%s] instead", client.name, formatters[1].exe) - ) - end -end - function M.common_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true @@ -56,7 +51,12 @@ function M.common_on_init(client, bufnr) lvim.lsp.on_init_callback(client, bufnr) return end - formatter_handler(client) + + local formatters = lvim.lang[vim.bo.filetype].formatters + if not vim.tbl_isempty(formatters) then + client.resolved_capabilities.document_formatting = false + u.lvim_log(string.format("Overriding [%s] formatter with [%s]", client.name, formatters[1].exe)) + end end function M.common_on_attach(client, bufnr) @@ -64,18 +64,17 @@ function M.common_on_attach(client, bufnr) lvim.lsp.on_attach_callback(client, bufnr) end lsp_highlight_document(client) - require("lsp.keybinds").setup() require("lsp.null-ls").setup(vim.bo.filetype) end function M.setup(lang) - local lang_server = lvim.lang[lang].lsp - local provider = lang_server.provider - if require("utils").check_lsp_client_active(provider) then + local lsp = lvim.lang[lang].lsp + if require("utils").check_lsp_client_active(lsp.provider) then return end - require("lspconfig")[provider].setup(lang_server.setup) + local lspconfig = require "lspconfig" + lspconfig[lsp.provider].setup(lsp.setup) end return M diff --git a/lua/lsp/keybinds.lua b/lua/lsp/keybinds.lua deleted file mode 100644 index 21f29994..00000000 --- a/lua/lsp/keybinds.lua +++ /dev/null @@ -1,27 +0,0 @@ -local M = {} - -function M.setup() - if lvim.lsp.default_keybinds then - vim.cmd "nnoremap gd lua vim.lsp.buf.definition()" - vim.cmd "nnoremap gD lua vim.lsp.buf.declaration()" - vim.cmd "nnoremap gr lua vim.lsp.buf.references()" - vim.cmd "nnoremap gi lua vim.lsp.buf.implementation()" - vim.api.nvim_set_keymap( - "n", - "gl", - 'lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = "single" })', - { noremap = true, silent = true } - ) - - vim.cmd "nnoremap gp lua require'lsp.peek'.Peek('definition')" - vim.cmd "nnoremap K :lua vim.lsp.buf.hover()" - vim.cmd "nnoremap :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})" - vim.cmd "nnoremap :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})" - -- vim.cmd "nnoremap gs lua vim.lsp.buf.signature_help()" - -- scroll down hover doc or scroll in definition preview - -- scroll up hover doc - -- vim.cmd 'command! -nargs=0 LspVirtualTextToggle lua require("lsp/virtual_text").toggle()' - end -end - -return M diff --git a/lua/lsp/kind.lua b/lua/lsp/kind.lua deleted file mode 100644 index e3b95ecb..00000000 --- a/lua/lsp/kind.lua +++ /dev/null @@ -1,33 +0,0 @@ -local M = {} - -function M.setup() - vim.lsp.protocol.CompletionItemKind = { - -- symbols for autocomplete - "  (Text) ", - "  (Method)", - "  (Function)", - "  (Constructor)", - " ﴲ (Field)", - "[] (Variable)", - "  (Class)", - " ﰮ (Interface)", - "  (Module)", - " 襁 (Property)", - "  (Unit)", - "  (Value)", - " 練 (Enum)", - "  (Keyword)", - "  (Snippet)", - "  (Color)", - "  (File)", - "  (Reference)", - "  (Folder)", - "  (EnumMember)", - " ﲀ (Constant)", - " ﳤ (Struct)", - "  (Event)", - "  (Operator)", - "  (TypeParameter)", - } -end -return M diff --git a/lua/lsp/signs.lua b/lua/lsp/signs.lua deleted file mode 100644 index fab6d302..00000000 --- a/lua/lsp/signs.lua +++ /dev/null @@ -1,20 +0,0 @@ -local M = {} -function M.setup() - vim.fn.sign_define( - "LspDiagnosticsSignError", - { texthl = "LspDiagnosticsSignError", text = "", numhl = "LspDiagnosticsSignError" } - ) - vim.fn.sign_define( - "LspDiagnosticsSignWarning", - { texthl = "LspDiagnosticsSignWarning", text = "", numhl = "LspDiagnosticsSignWarning" } - ) - vim.fn.sign_define( - "LspDiagnosticsSignHint", - { texthl = "LspDiagnosticsSignHint", text = "", numhl = "LspDiagnosticsSignHint" } - ) - vim.fn.sign_define( - "LspDiagnosticsSignInformation", - { texthl = "LspDiagnosticsSignInformation", text = "", numhl = "LspDiagnosticsSignInformation" } - ) -end -return M -- cgit v1.2.3 From de200da87d22c0a6e6505c3618fa64a53f9bf4c1 Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Sat, 31 Jul 2021 19:59:52 +0430 Subject: fix eslint_d issue (#1189) --- lua/lsp/null-ls.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 468d12a6..2098ea9e 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -27,10 +27,6 @@ local function is_nodejs_provider(provider) end local function is_provider_found(provider) - -- special case: fallback to "eslint" - -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint - provider._opts.command = provider._opts.command == "eslint_d" and "eslint" or provider._opts.command - local retval = { is_local = false, path = nil } if vim.fn.executable(provider._opts.command) == 1 then return false, provider._opts.command @@ -76,6 +72,11 @@ function M.setup(filetype) -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin table.insert(M.requested_providers, builtin_diagnoser) + -- special case: fallback to "eslint" + -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint + if linter.exe == "eslint_d" then + table.insert(M.requested_providers, null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }) + end u.lvim_log(string.format("Using linter provider: [%s]", linter.exe)) end -- cgit v1.2.3 From c4bff33745eb765ba49b170db2776284f5d015ef Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Sat, 31 Jul 2021 20:12:12 +0430 Subject: show the actual command (#1191) --- lua/lsp/null-ls.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 2098ea9e..fde6c5bd 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -10,7 +10,7 @@ function M.get_registered_providers_by_filetype(ft) local matches = {} for _, provider in pairs(M.requested_providers) do if vim.tbl_contains(provider.filetypes, ft) then - table.insert(matches, provider.name) + table.insert(matches, provider._opts.command) end end -- cgit v1.2.3 From 2badb25f361ddeae32fcd482384cccdab96f8e68 Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Sat, 31 Jul 2021 20:26:25 +0430 Subject: why is eslint_d so bad (#1192) --- lua/lsp/null-ls.lua | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index fde6c5bd..1d9f1ec1 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -10,7 +10,13 @@ function M.get_registered_providers_by_filetype(ft) local matches = {} for _, provider in pairs(M.requested_providers) do if vim.tbl_contains(provider.filetypes, ft) then - table.insert(matches, provider._opts.command) + local provider_name = provider.name + -- special case: show "eslint_d" instead of eslint + -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint + if string.find(provider._opts.command, "eslint_d") then + provider_name = "eslint_d" + end + table.insert(matches, provider_name) end end @@ -19,7 +25,7 @@ end local function is_nodejs_provider(provider) for _, local_provider in ipairs(nodejs_local_providers) do - if local_provider == provider.exe then + if local_provider == provider._opts.command then return true end end @@ -46,7 +52,7 @@ end local function validate_provider(provider) local is_local, provider_path = is_provider_found(provider) if not provider_path then - u.lvim_log(string.format("Unable to find the path for: [%s]", provider)) + u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider))) return false end if is_local then -- cgit v1.2.3 From 8ee070833e17efede8b01c81ff9d8439ec52fdb8 Mon Sep 17 00:00:00 2001 From: christianchiarulli Date: Sat, 31 Jul 2021 19:45:42 -0400 Subject: fix linter present when executable not found --- lua/lsp/null-ls.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 1d9f1ec1..798c02ee 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -77,7 +77,10 @@ function M.setup(filetype) -- FIXME: why doesn't this work? -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin - table.insert(M.requested_providers, builtin_diagnoser) + -- NOTE: validate before inserting to table + if validate_provider(builtin_diagnoser) then + table.insert(M.requested_providers, builtin_diagnoser) + end -- special case: fallback to "eslint" -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint if linter.exe == "eslint_d" then @@ -86,6 +89,7 @@ function M.setup(filetype) u.lvim_log(string.format("Using linter provider: [%s]", linter.exe)) end + -- FIXME: why would we need to remove if we never add? for idx, provider in pairs(M.requested_providers) do if not validate_provider(provider) then table.remove(M.requested_providers, idx) -- cgit v1.2.3 From 30ad4b81f5dbccb6d49eb28ffdd33cefcb758ee5 Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Sun, 1 Aug 2021 14:27:15 +0430 Subject: Hotfix/eslint d (#1198) --- lua/lsp/handlers.lua | 6 +++++- lua/lsp/null-ls.lua | 23 ++++++++--------------- 2 files changed, 13 insertions(+), 16 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/handlers.lua b/lua/lsp/handlers.lua index a25db3c1..849d2a03 100644 --- a/lua/lsp/handlers.lua +++ b/lua/lsp/handlers.lua @@ -27,7 +27,11 @@ function M.setup() local diagnostics = params.diagnostics for i, v in ipairs(diagnostics) do - diagnostics[i].message = string.format("%s: %s", v.source, v.message) + local source = v.source + if string.find(v.source, "/") then + source = string.sub(v.source, string.find(v.source, "([%w-_]+)$")) + end + diagnostics[i].message = string.format("%s: %s", source, v.message) if vim.tbl_contains(vim.tbl_keys(v), "code") then diagnostics[i].message = diagnostics[i].message .. string.format(" [%s]", v.code) diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 798c02ee..b8f9bfe6 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -34,18 +34,17 @@ end local function is_provider_found(provider) local retval = { is_local = false, path = nil } - if vim.fn.executable(provider._opts.command) == 1 then - return false, provider._opts.command - end if is_nodejs_provider(provider) then vim.cmd "let root_dir = FindRootDirectory()" local root_dir = vim.api.nvim_get_var "root_dir" local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command if vim.fn.executable(local_provider_command) == 1 then - retval.is_local = true - retval.path = local_provider_command + return true, local_provider_command end end + if vim.fn.executable(provider._opts.command) == 1 then + return false, provider._opts.command + end return retval.is_local, retval.path end @@ -78,23 +77,17 @@ function M.setup(filetype) -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin -- NOTE: validate before inserting to table - if validate_provider(builtin_diagnoser) then - table.insert(M.requested_providers, builtin_diagnoser) - end -- special case: fallback to "eslint" -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint if linter.exe == "eslint_d" then - table.insert(M.requested_providers, null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }) + builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" } + end + if validate_provider(builtin_diagnoser) then + table.insert(M.requested_providers, builtin_diagnoser) end u.lvim_log(string.format("Using linter provider: [%s]", linter.exe)) end - -- FIXME: why would we need to remove if we never add? - for idx, provider in pairs(M.requested_providers) do - if not validate_provider(provider) then - table.remove(M.requested_providers, idx) - end - end null_ls.register { sources = M.requested_providers } end -- cgit v1.2.3 From e2dd993ce753f8d28e6c32c4697ba51cc8ace2bb Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sun, 1 Aug 2021 12:02:10 +0200 Subject: refactor null-ls (#1202) --- lua/lsp/null-ls.lua | 67 +++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 35 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index b8f9bfe6..51fab00b 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -23,40 +23,37 @@ function M.get_registered_providers_by_filetype(ft) return matches end -local function is_nodejs_provider(provider) - for _, local_provider in ipairs(nodejs_local_providers) do - if local_provider == provider._opts.command then - return true - end +local function validate_nodejs_provider(requests, provider) + vim.cmd "let root_dir = FindRootDirectory()" + local root_dir = vim.api.nvim_get_var "root_dir" + local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command + u.lvim_log(string.format("checking for local node module: [%s]", vim.inspect(provider))) + if vim.fn.executable(local_nodejs_command) == 1 then + provider._opts.command = local_nodejs_command + table.insert(requests, provider) + elseif vim.fn.executable(provider._opts.command) == 1 then + u.lvim_log(string.format("checking in global path instead for node module: [%s]", provider._opts.command)) + table.insert(requests, provider) + else + u.lvim_log(string.format("Unable to find node module: [%s]", provider._opts.command)) + return false end - return false + return true end -local function is_provider_found(provider) - local retval = { is_local = false, path = nil } - if is_nodejs_provider(provider) then - vim.cmd "let root_dir = FindRootDirectory()" - local root_dir = vim.api.nvim_get_var "root_dir" - local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command - if vim.fn.executable(local_provider_command) == 1 then - return true, local_provider_command - end +local function validate_provider_request(requests, provider) + if provider == "" or provider == nil then + return false end - if vim.fn.executable(provider._opts.command) == 1 then - return false, provider._opts.command + -- NOTE: we can't use provider.name because eslint_d uses eslint name + if vim.tbl_contains(nodejs_local_providers, provider._opts.command) then + return validate_nodejs_provider(requests, provider) end - return retval.is_local, retval.path -end - -local function validate_provider(provider) - local is_local, provider_path = is_provider_found(provider) - if not provider_path then + if vim.fn.executable(provider._opts.command) ~= 1 then u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider))) return false end - if is_local then - provider._opts.command = provider_path - end + table.insert(requests, provider) return true end @@ -67,25 +64,25 @@ function M.setup(filetype) -- FIXME: why doesn't this work? -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin - table.insert(M.requested_providers, builtin_formatter) - u.lvim_log(string.format("Using format provider: [%s]", formatter.exe)) + if validate_provider_request(M.requested_providers, builtin_formatter) then + u.lvim_log(string.format("Using format provider: [%s]", formatter.exe)) + end end for _, linter in pairs(lvim.lang[filetype].linters) do local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe] - -- FIXME: why doesn't this work? - -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args - -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin - -- NOTE: validate before inserting to table -- special case: fallback to "eslint" -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint + -- if provider.exe if linter.exe == "eslint_d" then builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" } end - if validate_provider(builtin_diagnoser) then - table.insert(M.requested_providers, builtin_diagnoser) + -- FIXME: why doesn't this work? + -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args + -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin + if validate_provider_request(M.requested_providers, builtin_diagnoser) then + u.lvim_log(string.format("Using linter provider: [%s]", linter.exe)) end - u.lvim_log(string.format("Using linter provider: [%s]", linter.exe)) end null_ls.register { sources = M.requested_providers } -- cgit v1.2.3 From 0e05f05e9d584eec3764593a53eeeff2272718a0 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 1 Aug 2021 15:13:56 -0400 Subject: respect override table --- lua/lsp/init.lua | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 13b64dac..03efbd00 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -73,6 +73,15 @@ function M.setup(lang) return end + local overrides = lvim.lsp.override + + if type(overrides) == "table" then + if u.has_value(overrides, lang) then + return + end + end + + local lspconfig = require "lspconfig" lspconfig[lsp.provider].setup(lsp.setup) end -- cgit v1.2.3 From fbbf1b22a4ab48e19a33a5b10322253ebbca7b8f Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Mon, 2 Aug 2021 01:21:53 +0430 Subject: fix the formatting :pepehands: (#1208) --- lua/lsp/init.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 03efbd00..3373ac46 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -81,7 +81,6 @@ function M.setup(lang) end end - local lspconfig = require "lspconfig" lspconfig[lsp.provider].setup(lsp.setup) end -- cgit v1.2.3 From 77e283bd9c33166937756250918b12e349caf050 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Mon, 2 Aug 2021 23:42:56 +0200 Subject: [Refactor] Allow editing default keymaps (#1213) --- lua/lsp/init.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 3373ac46..b85dfcd2 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -33,6 +33,24 @@ local function lsp_highlight_document(client) end end +local function add_lsp_buffer_keybindings(bufnr) + local wk = require "which-key" + local keys = { + ["K"] = { "lua vim.lsp.buf.hover()", "Show hover" }, + ["gd"] = { "lua vim.lsp.buf.definition()", "Goto Definition" }, + ["gD"] = { "lua vim.lsp.buf.declaration()", "Goto declaration" }, + ["gr"] = { "lua vim.lsp.buf.references()", "Goto references" }, + ["gi"] = { "lua vim.lsp.buf.implementation()", "Goto implementation" }, + ["gs"] = { "lua vim.lsp.buf.signature_help()", "show signature help" }, + ["gp"] = { "lua require'lsp.peek'.Peek('definition')", "Peek definition" }, + ["gl"] = { + "lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = 'single' })", + "Show line diagnostics", + }, + } + wk.register(keys, { mode = "n", buffer = bufnr }) +end + function M.common_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true @@ -64,6 +82,7 @@ function M.common_on_attach(client, bufnr) lvim.lsp.on_attach_callback(client, bufnr) end lsp_highlight_document(client) + add_lsp_buffer_keybindings(bufnr) require("lsp.null-ls").setup(vim.bo.filetype) end -- cgit v1.2.3 From fc018cdc4735e3ba3e456a153ab672c8eb4df043 Mon Sep 17 00:00:00 2001 From: Will Leinweber Date: Mon, 2 Aug 2021 21:51:32 -0700 Subject: Fix lsp reporting when v.source is nil (#1209) --- lua/lsp/handlers.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/handlers.lua b/lua/lsp/handlers.lua index 849d2a03..2322e76a 100644 --- a/lua/lsp/handlers.lua +++ b/lua/lsp/handlers.lua @@ -28,10 +28,14 @@ function M.setup() for i, v in ipairs(diagnostics) do local source = v.source - if string.find(v.source, "/") then - source = string.sub(v.source, string.find(v.source, "([%w-_]+)$")) + if source then + if string.find(source, "/") then + source = string.sub(v.source, string.find(v.source, "([%w-_]+)$")) + end + diagnostics[i].message = string.format("%s: %s", source, v.message) + else + diagnostics[i].message = string.format("%s", v.message) end - diagnostics[i].message = string.format("%s: %s", source, v.message) if vim.tbl_contains(vim.tbl_keys(v), "code") then diagnostics[i].message = diagnostics[i].message .. string.format(" [%s]", v.code) -- cgit v1.2.3 From 4c3c3f388557a182794bffdbf923129c66af885a Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Tue, 3 Aug 2021 18:10:54 +0200 Subject: feat: add lvim.lsp.smart_cwd (#1218) - Enable querying the language-server for the `root_dir` - Use `root_dir` to set the current working-directory (CWD) - Make vim-rooter configurable and add an option to disable it Inspired by "ahmedkhalf/lsp-rooter.nvim" --- lua/lsp/init.lua | 4 ++++ lua/lsp/null-ls.lua | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index b85dfcd2..66efdafc 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -83,6 +83,10 @@ function M.common_on_attach(client, bufnr) end lsp_highlight_document(client) add_lsp_buffer_keybindings(bufnr) + if lvim.lsp.smart_cwd then + vim.api.nvim_set_current_dir(client.config.root_dir) + require("core.nvimtree").change_tree_dir(client.config.root_dir) + end require("lsp.null-ls").setup(vim.bo.filetype) end diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 51fab00b..d3f7931b 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -24,10 +24,14 @@ function M.get_registered_providers_by_filetype(ft) end local function validate_nodejs_provider(requests, provider) - vim.cmd "let root_dir = FindRootDirectory()" - local root_dir = vim.api.nvim_get_var "root_dir" + local ts_client = require("utils").get_active_client_by_ft "typescript" + if ts_client == nil then + u.lvim_log "Unable to determine root directory since tsserver didn't start correctly" + return + end + local root_dir = ts_client.config.root_dir local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command - u.lvim_log(string.format("checking for local node module: [%s]", vim.inspect(provider))) + u.lvim_log(string.format("checking [%s] for local node module: [%s]", local_nodejs_command, vim.inspect(provider))) if vim.fn.executable(local_nodejs_command) == 1 then provider._opts.command = local_nodejs_command table.insert(requests, provider) -- cgit v1.2.3 From 6c6fb67a8835cdcf911ef48ec2c19d553fe7d1a1 Mon Sep 17 00:00:00 2001 From: christianchiarulli Date: Tue, 3 Aug 2021 23:02:09 -0400 Subject: use old vim rooter logic, fallback on new way if vimrooter is disabled --- lua/lsp/null-ls.lua | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index d3f7931b..8bdea227 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -24,12 +24,22 @@ function M.get_registered_providers_by_filetype(ft) end local function validate_nodejs_provider(requests, provider) - local ts_client = require("utils").get_active_client_by_ft "typescript" - if ts_client == nil then - u.lvim_log "Unable to determine root directory since tsserver didn't start correctly" - return + local root_dir = "" + if lvim.builtin.rooter.active then + --- old logic to set root_dir + vim.cmd "let root_dir = FindRootDirectory()" + root_dir = vim.api.nvim_get_var "root_dir" + else + --- new logic to set root_dir + local ts_client = require("utils").get_active_client_by_ft "typescript" + if ts_client then + local root_dir = ts_client.config.root_dir + end + if ts_client == nil then + u.lvim_log "Unable to determine root directory since tsserver didn't start correctly" + return + end end - local root_dir = ts_client.config.root_dir local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command u.lvim_log(string.format("checking [%s] for local node module: [%s]", local_nodejs_command, vim.inspect(provider))) if vim.fn.executable(local_nodejs_command) == 1 then -- cgit v1.2.3 From e504e1f08cde5893cf6a6c39fd1029fced21b996 Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Wed, 4 Aug 2021 09:58:24 +0430 Subject: fix formatting and linting (#1220) --- lua/lsp/null-ls.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 8bdea227..04e35976 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -33,7 +33,7 @@ local function validate_nodejs_provider(requests, provider) --- new logic to set root_dir local ts_client = require("utils").get_active_client_by_ft "typescript" if ts_client then - local root_dir = ts_client.config.root_dir + root_dir = ts_client.config.root_dir end if ts_client == nil then u.lvim_log "Unable to determine root directory since tsserver didn't start correctly" -- cgit v1.2.3 From 7cd03ff4e332e1427497b54d5ca8b037afbc0982 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Wed, 4 Aug 2021 08:35:51 +0200 Subject: chore: minor cleanup to root_dir comments (#1222) --- lua/lsp/null-ls.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 04e35976..27cd1f78 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -24,21 +24,19 @@ function M.get_registered_providers_by_filetype(ft) end local function validate_nodejs_provider(requests, provider) - local root_dir = "" + local root_dir if lvim.builtin.rooter.active then - --- old logic to set root_dir + --- use vim-rooter to set root_dir vim.cmd "let root_dir = FindRootDirectory()" root_dir = vim.api.nvim_get_var "root_dir" else - --- new logic to set root_dir + --- use LSP to set root_dir local ts_client = require("utils").get_active_client_by_ft "typescript" - if ts_client then - root_dir = ts_client.config.root_dir - end if ts_client == nil then u.lvim_log "Unable to determine root directory since tsserver didn't start correctly" return end + root_dir = ts_client.config.root_dir end local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command u.lvim_log(string.format("checking [%s] for local node module: [%s]", local_nodejs_command, vim.inspect(provider))) -- cgit v1.2.3 From db19d4c13c3b2cf15150e7c51c05cff07a316247 Mon Sep 17 00:00:00 2001 From: Pasi Bergman Date: Wed, 4 Aug 2021 16:12:01 +0300 Subject: [Bugfix]: Don't override formatter with empty exe (#1224) * fix(lsp): don't override formatter with empty exe * Check for nil value --- lua/lsp/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 66efdafc..a12fc648 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -71,7 +71,7 @@ function M.common_on_init(client, bufnr) end local formatters = lvim.lang[vim.bo.filetype].formatters - if not vim.tbl_isempty(formatters) then + if not vim.tbl_isempty(formatters) and formatters[1]["exe"] ~= nil and formatters[1].exe ~= "" then client.resolved_capabilities.document_formatting = false u.lvim_log(string.format("Overriding [%s] formatter with [%s]", client.name, formatters[1].exe)) end -- cgit v1.2.3 From 76a16b6676183af564359c53184de1ebf6028b37 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Thu, 5 Aug 2021 16:34:25 +0200 Subject: stop registering duplicate null-ls providers (#1240) --- lua/lsp/null-ls.lua | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 27cd1f78..7adfa218 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -23,7 +23,8 @@ function M.get_registered_providers_by_filetype(ft) return matches end -local function validate_nodejs_provider(requests, provider) +local function validate_nodejs_provider(provider) + local command_path local root_dir if lvim.builtin.rooter.active then --- use vim-rooter to set root_dir @@ -41,43 +42,45 @@ local function validate_nodejs_provider(requests, provider) local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command u.lvim_log(string.format("checking [%s] for local node module: [%s]", local_nodejs_command, vim.inspect(provider))) if vim.fn.executable(local_nodejs_command) == 1 then - provider._opts.command = local_nodejs_command - table.insert(requests, provider) + command_path = local_nodejs_command elseif vim.fn.executable(provider._opts.command) == 1 then u.lvim_log(string.format("checking in global path instead for node module: [%s]", provider._opts.command)) - table.insert(requests, provider) + command_path = provider._opts.command else u.lvim_log(string.format("Unable to find node module: [%s]", provider._opts.command)) - return false end - return true + return command_path end -local function validate_provider_request(requests, provider) +local function validate_provider_request(provider) if provider == "" or provider == nil then - return false + return end -- NOTE: we can't use provider.name because eslint_d uses eslint name if vim.tbl_contains(nodejs_local_providers, provider._opts.command) then - return validate_nodejs_provider(requests, provider) + return validate_nodejs_provider(provider) end if vim.fn.executable(provider._opts.command) ~= 1 then u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider))) - return false + return end - table.insert(requests, provider) - return true + return provider._opts.command end -- TODO: for linters and formatters with spaces and '-' replace with '_' function M.setup(filetype) for _, formatter in pairs(lvim.lang[filetype].formatters) do local builtin_formatter = null_ls.builtins.formatting[formatter.exe] - -- FIXME: why doesn't this work? - -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args - -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin - if validate_provider_request(M.requested_providers, builtin_formatter) then - u.lvim_log(string.format("Using format provider: [%s]", formatter.exe)) + if not vim.tbl_contains(M.requested_providers, builtin_formatter) then + -- FIXME: why doesn't this work? + -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args + -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin + local resolved_path = validate_provider_request(builtin_formatter) + if resolved_path then + builtin_formatter._opts.command = resolved_path + table.insert(M.requested_providers, builtin_formatter) + u.lvim_log(string.format("Using format provider: [%s]", builtin_formatter.name)) + end end end @@ -89,11 +92,16 @@ function M.setup(filetype) if linter.exe == "eslint_d" then builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" } end - -- FIXME: why doesn't this work? - -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args - -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin - if validate_provider_request(M.requested_providers, builtin_diagnoser) then - u.lvim_log(string.format("Using linter provider: [%s]", linter.exe)) + if not vim.tbl_contains(M.requested_providers, builtin_diagnoser) then + -- FIXME: why doesn't this work? + -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args + -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin + local resolved_path = validate_provider_request(builtin_diagnoser) + if resolved_path then + builtin_diagnoser._opts.command = resolved_path + table.insert(M.requested_providers, builtin_diagnoser) + u.lvim_log(string.format("Using linter provider: [%s]", builtin_diagnoser.name)) + end end end -- cgit v1.2.3 From 67de24227f7244dc0f8a7ba90ad9d594e9bf4717 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Thu, 5 Aug 2021 16:35:58 +0200 Subject: chore: remove unncessary logic from utils (#1238) --- lua/lsp/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index a12fc648..a219fb1b 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -99,7 +99,7 @@ function M.setup(lang) local overrides = lvim.lsp.override if type(overrides) == "table" then - if u.has_value(overrides, lang) then + if vim.tbl_contains(overrides, lang) then return end end -- cgit v1.2.3 From 9fc6a2e1cdac513c8ff09069263ff102852be86a Mon Sep 17 00:00:00 2001 From: grvxs Date: Fri, 6 Aug 2021 16:50:25 +0530 Subject: fix: typos in lua/ --- lua/lsp/peek.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/peek.lua b/lua/lsp/peek.lua index e512eee0..cc8e57a9 100644 --- a/lua/lsp/peek.lua +++ b/lua/lsp/peek.lua @@ -100,7 +100,7 @@ function M.set_cursor_to_prev_pos(winnr) local range = location.targetRange or location.range local cursor_pos = { range.start.line + 1, range.start.character } - -- Set the winnr to the floting window if none was passed in + -- Set the winnr to the floating window if none was passed in winnr = winnr or M.floating_win -- Set the cursor at the correct position in the floating window vim.api.nvim_win_set_cursor(winnr, cursor_pos) -- cgit v1.2.3 From 47ebd70817c99c657271e399c0b98b920f765f29 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Fri, 6 Aug 2021 16:27:19 +0200 Subject: Add LunarVim info panel (Experimental) (#1241) * feat: lunarvim info (Experimental) * Add missing providers info * Use nvim api directly to create the popup * width tweaks --- lua/lsp/init.lua | 30 ++++++++++++++++++++++++++++++ lua/lsp/null-ls.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index a219fb1b..29676214 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -64,6 +64,36 @@ function M.common_capabilities() return capabilities end +function M.get_ls_capabilities(client_id) + local client + if not client_id then + local buf_clients = vim.lsp.buf_get_clients() + for _, buf_client in ipairs(buf_clients) do + if buf_client.name ~= "null-ls" then + client_id = buf_client.id + break + end + end + end + if not client_id then + error "Unable to determine client_id" + end + + client = vim.lsp.get_client_by_id(tonumber(client_id)) + + local enabled_caps = {} + + for k, v in pairs(client.resolved_capabilities) do + if v == true then + -- print("got cap: ", vim.inspect(caps)) + table.insert(enabled_caps, k) + -- vim.list_extend(enabled_caps, cap) + end + end + + return enabled_caps +end + function M.common_on_init(client, bufnr) if lvim.lsp.on_init_callback then lvim.lsp.on_init_callback(client, bufnr) diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 7adfa218..19727e15 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -23,6 +23,26 @@ function M.get_registered_providers_by_filetype(ft) return matches end +function M.get_missing_providers_by_filetype(ft) + local matches = {} + for _, provider in pairs(M.requested_providers) do + if vim.tbl_contains(provider.filetypes, ft) then + local provider_name = provider.name + + table.insert(matches, provider_name) + end + end + + return matches +end + +local function register_failed_request(ft, provider, operation) + if not lvim.lang[ft][operation]._failed_requests then + lvim.lang[ft][operation]._failed_requests = {} + end + table.insert(lvim.lang[ft][operation]._failed_requests, provider) +end + local function validate_nodejs_provider(provider) local command_path local root_dir @@ -80,6 +100,9 @@ function M.setup(filetype) builtin_formatter._opts.command = resolved_path table.insert(M.requested_providers, builtin_formatter) u.lvim_log(string.format("Using format provider: [%s]", builtin_formatter.name)) + else + -- mark it here to avoid re-doing the lookup again + register_failed_request(filetype, formatter.exe, "formatters") end end end @@ -101,6 +124,9 @@ function M.setup(filetype) builtin_diagnoser._opts.command = resolved_path table.insert(M.requested_providers, builtin_diagnoser) u.lvim_log(string.format("Using linter provider: [%s]", builtin_diagnoser.name)) + else + -- mark it here to avoid re-doing the lookup again + register_failed_request(filetype, linter.exe, "linters") end end end -- cgit v1.2.3 From 6b98bc137894492ecbe5dfa03bc149e8760a6453 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Mon, 9 Aug 2021 07:58:22 +0200 Subject: avoid using smart_cwd if root_dir is not found --- lua/lsp/init.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 29676214..020c4313 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -51,6 +51,14 @@ local function add_lsp_buffer_keybindings(bufnr) wk.register(keys, { mode = "n", buffer = bufnr }) end +local function set_smart_cwd(client) + local proj_dir = client.config.root_dir + if lvim.lsp.smart_cwd and proj_dir ~= "/" then + vim.api.nvim_set_current_dir(proj_dir) + require("core.nvimtree").change_tree_dir(proj_dir) + end +end + function M.common_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true @@ -113,10 +121,7 @@ function M.common_on_attach(client, bufnr) end lsp_highlight_document(client) add_lsp_buffer_keybindings(bufnr) - if lvim.lsp.smart_cwd then - vim.api.nvim_set_current_dir(client.config.root_dir) - require("core.nvimtree").change_tree_dir(client.config.root_dir) - end + set_smart_cwd(client) require("lsp.null-ls").setup(vim.bo.filetype) end -- cgit v1.2.3 From 405423108fc31981c40116a827e845a1179c9053 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Mon, 9 Aug 2021 19:02:37 +0200 Subject: feat: Add an async logger using plenary (#1207) Co-authored-by: rebuilt --- lua/lsp/init.lua | 11 ++++++----- lua/lsp/null-ls.lua | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 020c4313..0d08ecee 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -1,6 +1,5 @@ local M = {} -local u = require "utils" - +local Log = require "core.log" function M.config() vim.lsp.protocol.CompletionItemKind = lvim.lsp.completion.item_kind @@ -93,9 +92,7 @@ function M.get_ls_capabilities(client_id) for k, v in pairs(client.resolved_capabilities) do if v == true then - -- print("got cap: ", vim.inspect(caps)) table.insert(enabled_caps, k) - -- vim.list_extend(enabled_caps, cap) end end @@ -105,19 +102,23 @@ end function M.common_on_init(client, bufnr) if lvim.lsp.on_init_callback then lvim.lsp.on_init_callback(client, bufnr) + Log:get_default().info "Called lsp.on_init_callback" return end local formatters = lvim.lang[vim.bo.filetype].formatters if not vim.tbl_isempty(formatters) and formatters[1]["exe"] ~= nil and formatters[1].exe ~= "" then client.resolved_capabilities.document_formatting = false - u.lvim_log(string.format("Overriding [%s] formatter with [%s]", client.name, formatters[1].exe)) + Log:get_default().info( + string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe) + ) end end function M.common_on_attach(client, bufnr) if lvim.lsp.on_attach_callback then lvim.lsp.on_attach_callback(client, bufnr) + Log:get_default().info "Called lsp.on_init_callback" end lsp_highlight_document(client) add_lsp_buffer_keybindings(bufnr) diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 19727e15..c5388109 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -1,5 +1,6 @@ local M = {} -local u = require "utils" +local Log = require "core.log" + local null_ls = require "null-ls" local nodejs_local_providers = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" } @@ -54,20 +55,21 @@ local function validate_nodejs_provider(provider) --- use LSP to set root_dir local ts_client = require("utils").get_active_client_by_ft "typescript" if ts_client == nil then - u.lvim_log "Unable to determine root directory since tsserver didn't start correctly" + Log:get_default().error "Unable to determine root directory since tsserver didn't start correctly" return end root_dir = ts_client.config.root_dir end local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command - u.lvim_log(string.format("checking [%s] for local node module: [%s]", local_nodejs_command, vim.inspect(provider))) + Log:get_default().debug("checking for local node module: ", vim.inspect(provider)) + if vim.fn.executable(local_nodejs_command) == 1 then command_path = local_nodejs_command elseif vim.fn.executable(provider._opts.command) == 1 then - u.lvim_log(string.format("checking in global path instead for node module: [%s]", provider._opts.command)) + Log:get_default().debug("checking in global path instead for node module", provider._opts.command) command_path = provider._opts.command else - u.lvim_log(string.format("Unable to find node module: [%s]", provider._opts.command)) + Log:get_default().debug("Unable to find node module", provider._opts.command) end return command_path end @@ -81,7 +83,7 @@ local function validate_provider_request(provider) return validate_nodejs_provider(provider) end if vim.fn.executable(provider._opts.command) ~= 1 then - u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider))) + Log:get_default().warn("Unable to find the path for", vim.inspect(provider)) return end return provider._opts.command @@ -90,6 +92,7 @@ end -- TODO: for linters and formatters with spaces and '-' replace with '_' function M.setup(filetype) for _, formatter in pairs(lvim.lang[filetype].formatters) do + Log:get_default().debug("validating format provider: ", formatter.exe) local builtin_formatter = null_ls.builtins.formatting[formatter.exe] if not vim.tbl_contains(M.requested_providers, builtin_formatter) then -- FIXME: why doesn't this work? @@ -99,7 +102,7 @@ function M.setup(filetype) if resolved_path then builtin_formatter._opts.command = resolved_path table.insert(M.requested_providers, builtin_formatter) - u.lvim_log(string.format("Using format provider: [%s]", builtin_formatter.name)) + Log:get_default().info("Using format provider", builtin_formatter.name) else -- mark it here to avoid re-doing the lookup again register_failed_request(filetype, formatter.exe, "formatters") @@ -109,6 +112,7 @@ function M.setup(filetype) for _, linter in pairs(lvim.lang[filetype].linters) do local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe] + Log:get_default().debug("validating lint provider: ", linter.exe) -- special case: fallback to "eslint" -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint -- if provider.exe @@ -123,7 +127,7 @@ function M.setup(filetype) if resolved_path then builtin_diagnoser._opts.command = resolved_path table.insert(M.requested_providers, builtin_diagnoser) - u.lvim_log(string.format("Using linter provider: [%s]", builtin_diagnoser.name)) + Log:get_default().info("Using linter provider", builtin_diagnoser.name) else -- mark it here to avoid re-doing the lookup again register_failed_request(filetype, linter.exe, "linters") -- cgit v1.2.3 From 44e43e225bee70cef004529d3a62ab82f2ca1a74 Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Tue, 10 Aug 2021 22:17:24 +0430 Subject: remove the violent message for provider not found (#1283) --- lua/lsp/null-ls.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lua/lsp') diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index c5388109..697eac39 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -83,7 +83,8 @@ local function validate_provider_request(provider) return validate_nodejs_provider(provider) end if vim.fn.executable(provider._opts.command) ~= 1 then - Log:get_default().warn("Unable to find the path for", vim.inspect(provider)) + Log:get_default().debug("Unable to find the path for", vim.inspect(provider)) + Log:get_default().warn("Unable to find the path for ", provider._opts.command) return end return provider._opts.command -- cgit v1.2.3 From b26b61e3041b860967640e7172eac87f16b258d9 Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Wed, 11 Aug 2021 14:50:01 +0430 Subject: fix nil exception for langs without providers (#1290) --- lua/lsp/init.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lua/lsp') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 0d08ecee..e4ea02db 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -140,8 +140,10 @@ function M.setup(lang) end end - local lspconfig = require "lspconfig" - lspconfig[lsp.provider].setup(lsp.setup) + if lsp.provider ~= nil and lsp.provider ~= "" then + local lspconfig = require "lspconfig" + lspconfig[lsp.provider].setup(lsp.setup) + end end return M -- cgit v1.2.3