local nvim_lsp = require('lspconfig') local protocol = require('vim.lsp.protocol') local signature = require('lsp_signature') local ts_utils = require("nvim-lsp-ts-utils") local null_ls = require("null-ls") -- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/ function preview_location(location, context, before_context) -- location may be LocationLink or Location (more useful for the former) context = context or 10 before_context = before_context or 5 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) end function preview_location_callback(_, method, result) local context = 10 if result == nil or vim.tbl_isempty(result) then print("No location found: " .. method) return nil end if vim.tbl_islist(result) then floating_buf, floating_win = preview_location(result[1], context) else floating_buf, floating_win = preview_location(result, context) end end function peek_definition() if vim.tbl_contains(vim.api.nvim_list_wins(), floating_win) then vim.api.nvim_set_current_win(floating_win) else local params = vim.lsp.util.make_position_params() return vim.lsp.buf_request(0, "textDocument/definition", params, preview_location_callback) end end local on_attach = function(client, bufnr) signature.on_attach({ bind = true, hint_enable = true, hint_prefix = "đŸŧ ", hint_scheme = "String", handler_opts = { border = "single" }, decorator = {"`", "`"} }) vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') if client.config.flags then client.config.flags.allow_incremental_sync = true client.config.flags.debounce_text_changes = 100 end local opts = { noremap=true, silent=true } vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', 'lua vim.lsp.buf.definition()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', 'lua vim.lsp.buf.declaration()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', 'lua vim.lsp.buf.hover()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', 'lua vim.lsp.buf.references()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'ga', 'lua vim.lsp.buf.code_action()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', 'lua vim.lsp.buf.implementation()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', '', 'lua vim.lsp.buf.type_definition()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', '1gd', 'lua vim.lsp.buf.document_symbol()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', '1gD', 'lua vim.lsp.buf.workspace_symbol()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gR', 'lua vim.lsp.buf.rename()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'pd', 'lua peek_definition()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', '[d', 'lua vim.lsp.diagnostic.goto_prev { wrap = false }', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', ']d', 'lua vim.lsp.diagnostic.goto_next { wrap = false }', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', '[D', 'lua vim.lsp.diagnostic.goto_prev()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', ']D', 'lua vim.lsp.diagnostic.goto_next()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'do', 'lua vim.lsp.diagnostic.set_loclist()', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', ',d', 'lua vim.lsp.diagnostic.show_line_diagnostics()', opts) if client.resolved_capabilities.document_formatting then vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gq', 'lua vim.lsp.buf.formatting()', opts) elseif client.resolved_capabilities.document_range_formatting then vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gq', 'lua vim.lsp.buf.formatting()', opts) end if client.resolved_capabilities.code_lens then vim.api.nvim_buf_set_keymap(bufnr, "n", "gl", "lua vim.lsp.codelens.run()", opts) vim.api.nvim_command [[autocmd CursorHold,CursorHoldI,InsertLeave lua vim.lsp.codelens.refresh()]] end protocol.CompletionItemKind = { 'īžƒ '; -- text 'īž” '; -- method 'īž” '; -- function 'īŠ˛'; -- ctor 'ī‘„ '; -- field 'ī†’ '; -- variable 'īƒŠ '; -- class 'ī— '; -- interface 'īŖ– '; -- module 'ī‘„ '; -- property 'ī‡Ž '; -- unit 'ī›— '; -- value 'ī¤‘'; -- enum 'ī Š '; -- keyword 'īœ˜ '; -- snippet 'ī € '; -- color 'ī‡‰ '; -- file 'ī”Ŋ '; -- reference 'î—ŋ '; -- folder 'ī”Ģ '; -- enum member 'ī€Ŗ '; -- constant 'īƒŠ '; -- struct 'ī”Ŋ '; -- event 'īĻ”'; -- operator ' '; -- type parameter } if client.name == 'tsserver' then null_ls.setup {} ts_utils.setup { debug = false, disable_commands = false, enable_import_on_completion = false, import_on_completion_timeout = 5000, eslint_enable_code_actions = true, eslint_bin = "eslint", eslint_args = {"-f", "json", "--stdin", "--stdin-filename", "$FILENAME"}, eslint_enable_disable_comments = true, eslint_enable_diagnostics = true, eslint_diagnostics_debounce = 250, enable_formatting = true, formatter = "prettier", formatter_args = {"--stdin-filepath", "$FILENAME"}, format_on_save = false, no_save_after_format = false, complete_parens = true, signature_help_in_parens = false, update_imports_on_move = false, require_confirmation_on_move = false, } ts_utils.setup_client(client) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'go', ':TSLspOrganize', { silent = true }) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gf', ':TSLspFixCurrent', { silent = true }) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gI', ':TSLspImportAll', { silent = true }) end end function set_snippet_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 local servers = { 'hls', 'rust_analyzer', 'pylsp', 'tsserver' } for _, lsp in ipairs(servers) do nvim_lsp[lsp].setup { on_attach = on_attach, capabilities = set_snippet_capabilities(), } end