From 6a1732982287ef5aff2a6de171192b9e2bb90758 Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Mon, 6 Sep 2021 21:53:56 +0200 Subject: Updates to nvim from lvim --- .config/nvim/lua/lsp/handlers.lua | 135 ++++++++++++++++++---------- .config/nvim/lua/lsp/init.lua | 18 ++-- .config/nvim/lua/lsp/null-ls/formatters.lua | 8 +- .config/nvim/lua/lsp/null-ls/init.lua | 2 +- .config/nvim/lua/lsp/null-ls/linters.lua | 8 +- .config/nvim/lua/lsp/peek.lua | 25 ++---- 6 files changed, 111 insertions(+), 85 deletions(-) (limited to '.config/nvim/lua/lsp') diff --git a/.config/nvim/lua/lsp/handlers.lua b/.config/nvim/lua/lsp/handlers.lua index e273261..d19cb33 100644 --- a/.config/nvim/lua/lsp/handlers.lua +++ b/.config/nvim/lua/lsp/handlers.lua @@ -3,69 +3,108 @@ local M = {} function M.setup() - vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( - vim.lsp.diagnostic.on_publish_diagnostics, - { + vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { + virtual_text = options.lsp.diagnostics.virtual_text, + signs = options.lsp.diagnostics.signs.active, + underline = options.lsp.document_highlight, + }) + + vim.lsp.handlers["textDocument/publishDiagnostics"] = function(_, _, params, client_id, _) + local config = { -- your config virtual_text = options.lsp.diagnostics.virtual_text, - signs = options.lsp.diagnostics.signs.active, - underline = options.lsp.document_highlight, + signs = options.lsp.diagnostics.signs, + underline = options.lsp.diagnostics.underline, + update_in_insert = options.lsp.diagnostics.update_in_insert, + severity_sort = options.lsp.diagnostics.severity_sort, } - ) + local uri = params.uri + local bufnr = vim.uri_to_bufnr(uri) - vim.lsp.handlers["textDocument/publishDiagnostics"] = - function(_, _, params, client_id, _) - local config = { -- your config - virtual_text = options.lsp.diagnostics.virtual_text, - signs = options.lsp.diagnostics.signs, - underline = options.lsp.diagnostics.underline, - update_in_insert = options.lsp.diagnostics.update_in_insert, - severity_sort = options.lsp.diagnostics.severity_sort, - } - local uri = params.uri - local bufnr = vim.uri_to_bufnr(uri) + if not bufnr then + return + end - if not bufnr then - return - end + local diagnostics = params.diagnostics - local diagnostics = params.diagnostics + vim.lsp.diagnostic.save(diagnostics, bufnr, client_id) - 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 not vim.api.nvim_buf_is_loaded(bufnr) then + return + end + vim.lsp.diagnostic.display(diagnostics, bufnr, client_id, config) + 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.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { + border = options.lsp.popup_border, + }) - vim.lsp.diagnostic.save(diagnostics, bufnr, client_id) + vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { + border = options.lsp.popup_border, + }) +end + +function M.show_line_diagnostics() + local diagnostics = vim.lsp.diagnostic.get_line_diagnostics() + local diags = vim.deepcopy(diagnostics) + local height = #diagnostics + local width = 0 + local opts = {} + local close_events = { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" } + local diagnostic_severities = { + "Error", + "Warning", + "Information", + "Hint", + } + if height == 0 then + return + end + local bufnr = vim.api.nvim_create_buf(false, true) - if not vim.api.nvim_buf_is_loaded(bufnr) then - return + for i, diagnostic in ipairs(diagnostics) do + local source = diagnostic.source + if source then + if string.find(source, "/") then + source = string.sub(diagnostic.source, string.find(diagnostic.source, "([%w-_]+)$")) end + diags[i].message = string.format("%s: %s", source, diagnostic.message) + else + diags[i].message = string.format("%s", diagnostic.message) + end - vim.lsp.diagnostic.display(diagnostics, bufnr, client_id, config) + if diagnostic.code then + diags[i].message = string.format("%s [%s]", diags[i].message, diagnostic.code) + end + if diags[i].message:len() > width then + width = string.len(diags[i].message) end + end - vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { - border = options.lsp.popup_border, - }) + opts = vim.lsp.util.make_floating_popup_options(width, height, opts) + opts["style"] = "minimal" + opts["border"] = "rounded" - vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with( - vim.lsp.handlers.signature_help, - { - border = options.lsp.popup_border, - } + 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) + for i, diag in ipairs(diags) do + local message = diag.message:gsub("[\n\r]", " ") + vim.api.nvim_buf_set_lines(bufnr, i - 1, i - 1, 0, { message }) + vim.api.nvim_buf_add_highlight( + bufnr, + -1, + "LspDiagnosticsFloating" .. diagnostic_severities[diag.severity], + i - 1, + 0, + diag.message:len() + ) + end + + 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) end return M diff --git a/.config/nvim/lua/lsp/init.lua b/.config/nvim/lua/lsp/init.lua index b693dec..f0f7b45 100644 --- a/.config/nvim/lua/lsp/init.lua +++ b/.config/nvim/lua/lsp/init.lua @@ -19,9 +19,9 @@ local function lsp_highlight_document(client) 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 + hi LspReferenceRead cterm=bold ctermbg=red guibg=#353d46 + hi LspReferenceText cterm=bold ctermbg=red guibg=#353d46 + hi LspReferenceWrite cterm=bold ctermbg=red guibg=#353d46 augroup lsp_document_highlight autocmd! * autocmd CursorHold lua vim.lsp.buf.document_highlight() @@ -44,11 +44,11 @@ local function add_lsp_buffer_keybindings(bufnr) ["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" }, + ["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' })", + "lua require'lsp.handlers'.show_line_diagnostics()", "Show line diagnostics", }, } @@ -99,14 +99,14 @@ end function M.common_on_init(client, bufnr) if options.lsp.on_init_callback then options.lsp.on_init_callback(client, bufnr) - Log:get_default().info "Called lsp.on_init_callback" + Log:debug "Called lsp.on_init_callback" return end local formatters = options.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( + Log:debug( string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe) ) end @@ -115,7 +115,7 @@ end function M.common_on_attach(client, bufnr) if options.lsp.on_attach_callback then options.lsp.on_attach_callback(client, bufnr) - Log:get_default().info "Called lsp.on_init_callback" + Log:debug "Called lsp.on_init_callback" end lsp_highlight_document(client) add_lsp_buffer_keybindings(bufnr) @@ -125,7 +125,7 @@ end function M.setup(lang) local lsp_utils = require "lsp.utils" local lsp = options.lang[lang].lsp - if lsp_utils.is_client_active(lsp.provider) then + if (lsp.active ~= nil and not lsp.active) or lsp_utils.is_client_active(lsp.provider) then return end diff --git a/.config/nvim/lua/lsp/null-ls/formatters.lua b/.config/nvim/lua/lsp/null-ls/formatters.lua index cf62615..651d6f1 100644 --- a/.config/nvim/lua/lsp/null-ls/formatters.lua +++ b/.config/nvim/lua/lsp/null-ls/formatters.lua @@ -3,7 +3,7 @@ local formatters_by_ft = {} local null_ls = require "null-ls" local services = require "lsp.null-ls.services" -local logger = require("core.log"):get_default() +local Log = require("core.log") local function list_names(formatters, option) option = option or {} @@ -45,15 +45,15 @@ function M.list_configured(formatter_configs) local formatter = null_ls.builtins.formatting[fmt_config.exe] if not formatter then - logger.error("Not a valid formatter:", fmt_config.exe) + Log:error("Not a valid formatter:" .. fmt_config.exe) errors[fmt_config.exe] = {} -- Add data here when necessary else local formatter_cmd = services.find_command(formatter._opts.command) if not formatter_cmd then - logger.warn("Not found:", formatter._opts.command) + Log:warn("Not found:" .. formatter._opts.command) errors[fmt_config.exe] = {} -- Add data here when necessary else - logger.info("Using formatter:", formatter_cmd) + Log:debug("Using formatter:" .. formatter_cmd) formatters[fmt_config.exe] = formatter.with { command = formatter_cmd, extra_args = fmt_config.args, diff --git a/.config/nvim/lua/lsp/null-ls/init.lua b/.config/nvim/lua/lsp/null-ls/init.lua index f2b1042..d12b40a 100644 --- a/.config/nvim/lua/lsp/null-ls/init.lua +++ b/.config/nvim/lua/lsp/null-ls/init.lua @@ -30,7 +30,7 @@ function M.setup(filetype, option) local ok, _ = pcall(require, "null-ls") if not ok then - require("core.log"):get_default().error "Missing null-ls dependency" + require("core.log"):error "Missing null-ls dependency" return end diff --git a/.config/nvim/lua/lsp/null-ls/linters.lua b/.config/nvim/lua/lsp/null-ls/linters.lua index a4eaa89..0f6cbc1 100644 --- a/.config/nvim/lua/lsp/null-ls/linters.lua +++ b/.config/nvim/lua/lsp/null-ls/linters.lua @@ -3,7 +3,7 @@ local linters_by_ft = {} local null_ls = require "null-ls" local services = require "lsp.null-ls.services" -local logger = require("core.log"):get_default() +local Log = require("core.log") local function list_names(linters, option) option = option or {} @@ -45,15 +45,15 @@ function M.list_configured(linter_configs) local linter = null_ls.builtins.diagnostics[lnt_config.exe] if not linter then - logger.error("Not a valid linter:", lnt_config.exe) + Log:error("Not a valid linter: " .. lnt_config.exe) errors[lnt_config.exe] = {} -- Add data here when necessary else local linter_cmd = services.find_command(linter._opts.command) if not linter_cmd then - logger.warn("Not found:", linter._opts.command) + Log:warn("Not found: " .. linter._opts.command) errors[lnt_config.exe] = {} -- Add data here when necessary else - logger.info("Using linter:", linter_cmd) + Log:debug("Using linter: " .. linter_cmd) linters[lnt_config.exe] = linter.with { command = linter_cmd, extra_args = lnt_config.args, diff --git a/.config/nvim/lua/lsp/peek.lua b/.config/nvim/lua/lsp/peek.lua index e4f4e1e..dbc6741 100644 --- a/.config/nvim/lua/lsp/peek.lua +++ b/.config/nvim/lua/lsp/peek.lua @@ -12,8 +12,7 @@ local function create_floating_file(location, opts) -- Set some defaults opts = opts or {} - local close_events = opts.close_events - or { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" } + local close_events = opts.close_events or { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" } -- location may be LocationLink or Location local uri = location.targetUri or location.uri @@ -30,10 +29,7 @@ local function create_floating_file(location, opts) 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) + 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) @@ -46,13 +42,12 @@ local function create_floating_file(location, opts) local winnr = vim.api.nvim_open_win(bufnr, false, opts) vim.api.nvim_win_set_option(winnr, "winblend", 0) + vim.api.nvim_win_set_cursor(winnr, { range.start.line + 1, range.start.character }) 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)" + "autocmd QuitPre ++nested ++once lua pcall(vim.api.nvim_win_close, " .. winnr .. ", true)" ) vim.lsp.util.close_preview_autocmd(close_events, winnr) @@ -134,18 +129,10 @@ function M.Peek(what) 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 - ) + 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.' + 'peek: Error calling LSP method "textDocument/' .. what .. '". The current language lsp might not support it.' ) end end -- cgit v1.2.3-70-g09d2