summaryrefslogtreecommitdiff
path: root/lua/lsp
diff options
context:
space:
mode:
Diffstat (limited to 'lua/lsp')
-rw-r--r--lua/lsp/handlers.lua44
-rw-r--r--lua/lsp/init.lua160
-rw-r--r--lua/lsp/keybinds.lua27
-rw-r--r--lua/lsp/kind.lua33
-rw-r--r--lua/lsp/null-ls.lua162
-rw-r--r--lua/lsp/peek.lua140
-rw-r--r--lua/lsp/service.lua122
-rw-r--r--lua/lsp/signs.lua20
8 files changed, 427 insertions, 281 deletions
diff --git a/lua/lsp/handlers.lua b/lua/lsp/handlers.lua
index d3a487ae..2322e76a 100644
--- a/lua/lsp/handlers.lua
+++ b/lua/lsp/handlers.lua
@@ -5,10 +5,52 @@ 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,
})
+ 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
+ local source = v.source
+ 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
+
+ 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,
})
diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua
index 62c42fd8..e4ea02db 100644
--- a/lua/lsp/init.lua
+++ b/lua/lsp/init.lua
@@ -1,59 +1,149 @@
-local utils = require "utils"
-local service = require "lsp.service"
-local null_ls = require "lsp.null-ls"
local M = {}
-
+local Log = require "core.log"
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()
- 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! * <buffer>
+ autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
+ autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
+ augroup END
+ ]],
+ false
+ )
end
+end
- local overrides = lvim.lsp.override
+local function add_lsp_buffer_keybindings(bufnr)
+ local wk = require "which-key"
+ local keys = {
+ ["K"] = { "<cmd>lua vim.lsp.buf.hover()<CR>", "Show hover" },
+ ["gd"] = { "<cmd>lua vim.lsp.buf.definition()<CR>", "Goto Definition" },
+ ["gD"] = { "<cmd>lua vim.lsp.buf.declaration()<CR>", "Goto declaration" },
+ ["gr"] = { "<cmd>lua vim.lsp.buf.references()<CR>", "Goto references" },
+ ["gi"] = { "<cmd>lua vim.lsp.buf.implementation()<CR>", "Goto implementation" },
+ ["gs"] = { "<cmd>lua vim.lsp.buf.signature_help()<CR>", "show signature help" },
+ ["gp"] = { "<cmd>lua require'lsp.peek'.Peek('definition')<CR>", "Peek definition" },
+ ["gl"] = {
+ "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = 'single' })<CR>",
+ "Show line diagnostics",
+ },
+ }
+ wk.register(keys, { mode = "n", buffer = bufnr })
+end
- if utils.is_table(overrides) then
- if utils.has_value(overrides, lang) then
- return
- 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
- if utils.is_string(overrides) then
- if overrides == lang then
- return
+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.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
- local sources = null_ls.setup(lang)
+ if not client_id then
+ error "Unable to determine client_id"
+ end
- for _, source in pairs(sources) do
- local method = source.method
- local format_method = "NULL_LS_FORMATTING"
+ client = vim.lsp.get_client_by_id(tonumber(client_id))
- 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
+ local enabled_caps = {}
- if utils.is_string(method) then
- if method == format_method then
- lang_server.setup.on_attach = service.no_formatter_on_attach
- end
+ for k, v in pairs(client.resolved_capabilities) do
+ if v == true then
+ table.insert(enabled_caps, k)
end
end
- if provider == "" or provider == nil then
+ 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)
+ 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
+ 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)
+ set_smart_cwd(client)
+ require("lsp.null-ls").setup(vim.bo.filetype)
+end
+
+function M.setup(lang)
+ 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 overrides = lvim.lsp.override
+
+ if type(overrides) == "table" then
+ if vim.tbl_contains(overrides, lang) then
+ return
+ end
+ end
+
+ if lsp.provider ~= nil and lsp.provider ~= "" then
+ local lspconfig = require "lspconfig"
+ lspconfig[lsp.provider].setup(lsp.setup)
+ end
end
return M
diff --git a/lua/lsp/keybinds.lua b/lua/lsp/keybinds.lua
deleted file mode 100644
index 5722dab7..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 <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>"
- vim.cmd "nnoremap <silent> gD <cmd>lua vim.lsp.buf.declaration()<CR>"
- vim.cmd "nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR>"
- vim.cmd "nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>"
- vim.api.nvim_set_keymap(
- "n",
- "gl",
- '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = "single" })<CR>',
- { noremap = true, silent = true }
- )
-
- vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp.utils'.PeekDefinition()<CR>"
- vim.cmd "nnoremap <silent> K :lua vim.lsp.buf.hover()<CR>"
- vim.cmd "nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})<CR>"
- vim.cmd "nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})<CR>"
- -- vim.cmd "nnoremap <silent> gs <cmd>lua vim.lsp.buf.signature_help()<CR>"
- -- 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/null-ls.lua b/lua/lsp/null-ls.lua
index a231698a..697eac39 100644
--- a/lua/lsp/null-ls.lua
+++ b/lua/lsp/null-ls.lua
@@ -1,66 +1,142 @@
local M = {}
+local Log = require "core.log"
-local _, null_ls = pcall(require, "null-ls")
-local utils = require "utils"
-local sources = {}
+local null_ls = require "null-ls"
-local local_executables = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" }
+local nodejs_local_providers = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" }
-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
-end
+M.requested_providers = {}
-local function setup_ls(exe, type)
- if utils.has_value(local_executables, exe) then
- local smart_executable = null_ls.builtins[type][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
- table.insert(sources, smart_executable)
+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
+ 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
+
+ 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
+ if lvim.builtin.rooter.active then
+ --- use vim-rooter to set root_dir
+ vim.cmd "let root_dir = FindRootDirectory()"
+ root_dir = vim.api.nvim_get_var "root_dir"
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])
+ --- use LSP to set root_dir
+ local ts_client = require("utils").get_active_client_by_ft "typescript"
+ if ts_client == nil then
+ 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
- null_ls.register { sources = sources }
+ local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command
+ 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
+ Log:get_default().debug("checking in global path instead for node module", provider._opts.command)
+ command_path = provider._opts.command
+ else
+ Log:get_default().debug("Unable to find node module", provider._opts.command)
+ end
+ return command_path
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
+local function validate_provider_request(provider)
+ if provider == "" or provider == nil then
+ return
end
- if type == "formatting" then
- executables = lvim.lang[filetype].formatter.exe
+ -- 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(provider)
end
+ if vim.fn.executable(provider._opts.command) ~= 1 then
+ 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
+end
- if utils.is_table(executables) then
- for _, exe in pairs(executables) do
- if exe ~= "" then
- setup_ls(exe, type)
+-- 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?
+ -- 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)
+ 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")
end
end
end
- if utils.is_string(executables) and executables ~= "" then
- setup_ls(executables, type)
+
+ 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
+ if linter.exe == "eslint_d" then
+ builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }
+ end
+ 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)
+ 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")
+ end
+ end
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/peek.lua b/lua/lsp/peek.lua
new file mode 100644
index 00000000..cc8e57a9
--- /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 <buffer> ++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 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)
+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",
+ "<CR>",
+ ":lua require('lsp.peek').open_file()<CR>",
+ { 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
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! * <buffer>
- autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
- autocmd CursorMoved <buffer> 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
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