From 47bf3fbb78af41b7fbf46cc3c7266f5d563ec3cd Mon Sep 17 00:00:00 2001 From: Thuan Pham Date: Mon, 25 Oct 2021 23:32:27 +0700 Subject: [PATCH] feat(nvim): lsp config --- nvim/after/plugin/lsp.lua | 141 +++++++++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 1 deletion(-) diff --git a/nvim/after/plugin/lsp.lua b/nvim/after/plugin/lsp.lua index 6a4f59d6..d399675e 100644 --- a/nvim/after/plugin/lsp.lua +++ b/nvim/after/plugin/lsp.lua @@ -1,8 +1,147 @@ local nvim_lsp = require('lspconfig') -local protocol = require('vim.lsp.protocol') +local protocol = require'vim.lsp.protocol' local on_attach = function(client, bufnr) local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end + buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') + + local opts = { noremap=true, silent=true } + + buf_set_keymap('n', 'gD', 'lua vim.lsp.buf.declaration()', opts) + buf_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', opts) + buf_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()', opts) + buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', opts) + buf_set_keymap('i', '', 'lua vim.lsp.buf.signature_help()', opts) + buf_set_keymap('n', 'lwa', 'lua vim.lsp.buf.add_workspace_folder()', opts) + buf_set_keymap('n', 'lwr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) + buf_set_keymap('n', 'lwl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) + buf_set_keymap('n', 'lD', 'lua vim.lsp.buf.type_definition()', opts) + buf_set_keymap('n', 'lrn', 'lua vim.lsp.buf.rename()', opts) + buf_set_keymap('n', 'lca', 'lua vim.lsp.buf.code_action()', opts) + buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', opts) + buf_set_keymap('n', 'le', 'lua vim.lsp.diagnostic.show_line_diagnostics()', opts) + --buf_set_keymap('n', '', 'lua vim.lsp.diagnostic.goto_prev()', opts) + buf_set_keymap('n', ']d', 'lua vim.lsp.diagnostic.goto_next()', opts) + buf_set_keymap('n', 'lq', 'lua vim.lsp.diagnostic.set_loclist()', opts) + buf_set_keymap("n", "lf", "lua vim.lsp.buf.formatting()", opts) + + -- formatting + if client.resolved_capabilities.document_formatting then + vim.api.nvim_command [[augroup Format]] + vim.api.nvim_command [[autocmd! * ]] + vim.api.nvim_command [[autocmd BufWritePre lua vim.lsp.buf.formatting_seq_sync()]] + vim.api.nvim_command [[augroup END]] + end + + require'completion'.on_attach(client, bufnr) + + --protocol.SymbolKind = { } + protocol.CompletionItemKind = { + '', -- 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 + +nvim_lsp.flow.setup { + on_attach = on_attach +} + +nvim_lsp.tsserver.setup { + on_attach = on_attach, + filetypes = { "typescript", "typescriptreact", "typescript.tsx" } +} + +nvim_lsp.diagnosticls.setup { + on_attach = on_attach, + filetypes = { 'javascript', 'javascriptreact', 'json', 'typescript', 'typescriptreact', 'css', 'less', 'scss', 'markdown', 'pandoc' }, + init_options = { + linters = { + eslint = { + command = 'eslint_d', + rootPatterns = { '.git' }, + debounce = 100, + args = { '--stdin', '--stdin-filename', '%filepath', '--format', 'json' }, + sourceName = 'eslint_d', + parseJson = { + errorsRoot = '[0].messages', + line = 'line', + column = 'column', + endLine = 'endLine', + endColumn = 'endColumn', + message = '[eslint] ${message} [${ruleId}]', + security = 'severity' + }, + securities = { + [2] = 'error', + [1] = 'warning' + } + }, + }, + filetypes = { + javascript = 'eslint', + javascriptreact = 'eslint', + typescript = 'eslint', + typescriptreact = 'eslint', + }, + formatters = { + eslint_d = { + command = 'eslint_d', + args = { '--stdin', '--stdin-filename', '%filename', '--fix-to-stdout' }, + rootPatterns = { '.git' }, + }, + prettier = { + command = 'prettier', + args = { '--stdin-filepath', '%filename' } + } + }, + formatFiletypes = { + css = 'prettier', + javascript = 'eslint_d', + javascriptreact = 'eslint_d', + json = 'prettier', + scss = 'prettier', + less = 'prettier', + json = 'prettier', + typescript = 'eslint_d', + typescriptreact = 'eslint_d', + markdown = 'prettier', + } + } +} + +-- icon +vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( + vim.lsp.diagnostic.on_publish_diagnostics, { + underline = true, + -- This sets the spacing and the prefix, obviously. + virtual_text = { + spacing = 4, + prefix = '' + } + } +)