From 53df8a59580f1f866ea377e75fe14880d8db8fbb Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Sat, 2 Jul 2022 17:58:32 -0500 Subject: nvim: eslint and prettier for typescript --- nvim/.config/nvim/lua/lsp_config.lua | 45 ++++++++++++++++++++++++++++-------- nvim/.config/nvim/lua/plugins.lua | 4 ++++ 2 files changed, 39 insertions(+), 10 deletions(-) (limited to 'nvim') diff --git a/nvim/.config/nvim/lua/lsp_config.lua b/nvim/.config/nvim/lua/lsp_config.lua index 63a51d8..4bb64bf 100644 --- a/nvim/.config/nvim/lua/lsp_config.lua +++ b/nvim/.config/nvim/lua/lsp_config.lua @@ -1,11 +1,5 @@ --- Mappings. --- See `:help vim.diagnostic.*` for documentation on any of the below functions - -local map = function(mode, lhs, rhs, opts) - vim.api.nvim_set_keymap(mode, lhs, rhs, opts or { - noremap = true, silent = true - }) -end +local lspconfig = require('lspconfig') +local null_ls = require("null-ls") local buf_map = function(bufnr, mode, lhs, rhs, opts) vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, opts or { @@ -50,17 +44,48 @@ local on_attach = function(client, bufnr) buf_map(bufnr, 'n', 'rn', ':LspRename') buf_map(bufnr, 'n', 'ca', ':LspCodeAction') buf_map(bufnr, 'n', 'f', ':LspFormatting') + + if client.resolved_capabilities.document_formatting then + vim.cmd("autocmd BufWritePre lua vim.lsp.buf.formatting_sync()") + end end -- Use a loop to conveniently call 'setup' on multiple servers and -- map buffer local keybindings when the language server attaches local servers = { 'tsserver', 'zls' } for _, lsp in pairs(servers) do - require('lspconfig')[lsp].setup { + lspconfig[lsp].setup({ on_attach = on_attach, flags = { -- This will be the default in neovim 0.7+ debounce_text_changes = 150, } - } + }) end + +-- overrides for typescript +lspconfig.tsserver.setup { + on_attach = function(client, bufnr) + -- disable tsserver's formatting; we'll use prettier instead + client.resolved_capabilities.document_formatting = false + client.resolved_capabilities.document_range_formatting = false + + local ts_utils = require("nvim-lsp-ts-utils") + ts_utils.setup({}) + ts_utils.setup_client(client) + buf_map(bufnr, "n", "gs", ":TSLspOrganize") + --buf_map(bufnr, "n", "gi", ":TSLspRenameFile") + buf_map(bufnr, "n", "go", ":TSLspImportAll") + + on_attach(client, bufnr) + end +} + +null_ls.setup({ + sources = { + null_ls.builtins.diagnostics.eslint, -- eslint or eslint_d + null_ls.builtins.code_actions.eslint, -- eslint or eslint_d + null_ls.builtins.formatting.prettierd -- prettier, eslint, eslint_d, or prettierd + }, + on_attach = on_attach, +}) diff --git a/nvim/.config/nvim/lua/plugins.lua b/nvim/.config/nvim/lua/plugins.lua index b54d4ee..ae53f90 100644 --- a/nvim/.config/nvim/lua/plugins.lua +++ b/nvim/.config/nvim/lua/plugins.lua @@ -36,6 +36,10 @@ return require('packer').startup(function() use { 'neovim/nvim-lspconfig', + requires = { + "jose-elias-alvarez/null-ls.nvim", + "jose-elias-alvarez/nvim-lsp-ts-utils", + }, config = function() require('lsp_config') end } -- cgit v1.2.3