diff --git a/lua/custom/lsp/mason.lua b/lua/custom/lsp/mason.lua index 3888ef67..4240ee20 100644 --- a/lua/custom/lsp/mason.lua +++ b/lua/custom/lsp/mason.lua @@ -12,7 +12,7 @@ local servers = { gopls = {}, -- pyright = {}, -- rust_analyzer = {}, - tsserver = {}, + -- tsserver = {}, -- html = { filetypes = { 'html', 'twig', 'hbs'} }, lua_ls = { @@ -48,3 +48,20 @@ mason_lspconfig.setup_handlers { } end } + +-- import mason-null-ls plugin safely +local mason_null_ls_status, mason_null_ls = pcall(require, "mason-null-ls") +if not mason_null_ls_status then + return +end + +mason_null_ls.setup({ + -- List of formatters and linters for Mason to install + ensure_installed = { + "prettier", -- ts/js formatter + "stylua", -- lua formatter + "eslint_d", -- ts/js linter + }, + -- auto-install configured formatters & linters (with null-ls) + automatic_installation = true, +}) diff --git a/lua/custom/lsp/null-ls.lua b/lua/custom/lsp/null-ls.lua new file mode 100644 index 00000000..acc43137 --- /dev/null +++ b/lua/custom/lsp/null-ls.lua @@ -0,0 +1,48 @@ +-- import null-ls plugin safely +local setup, null_ls = pcall(require, "null-ls") +if not setup then + return +end + +-- for conciseness +local formatting = null_ls.builtins.formatting -- to setup formatters +local diagnostics = null_ls.builtins.diagnostics -- to setup linters + +-- to setup format on save +local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) + +-- configure null_ls +null_ls.setup({ + -- setup formatters & linters + sources = { + -- to disable file types use + -- "formatting.prettier.with({disabled_filetypes = {}})" (see null-ls docs) + formatting.prettier, -- js/ts formatter + formatting.stylua, -- lua formatter + diagnostics.eslint_d.with({ -- js/ts linter + -- only enable eslint if root has .eslintrc.js or .eslintrc.cjs + condition = function(utils) + return utils.root_has_file(".eslintrc.js") or utils.root_has_file(".eslintrc.cjs") + end, + }), + }, + -- configure format on save + on_attach = function(current_client, bufnr) + if current_client.supports_method("textDocument/formatting") then + vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) + vim.api.nvim_create_autocmd("BufWritePre", { + group = augroup, + buffer = bufnr, + callback = function() + vim.lsp.buf.format({ + filter = function(client) + -- only use null-ls for formatting instead of lsp server + return client.name == "null-ls" + end, + bufnr = bufnr, + }) + end, + }) + end + end, +}) diff --git a/lua/custom/plugins/init.lua b/lua/custom/plugins/init.lua index 923fd858..9ccfaa66 100644 --- a/lua/custom/plugins/init.lua +++ b/lua/custom/plugins/init.lua @@ -6,4 +6,11 @@ return { 'tpope/vim-surround', 'szw/vim-maximizer', 'vim-scripts/ReplaceWithRegister', + { + "pmizio/typescript-tools.nvim", + dependencies = { "nvim-lua/plenary.nvim", "neovim/nvim-lspconfig" }, + opts = {}, + }, + 'jose-elias-alvarez/null-ls.nvim', + "jayp0521/mason-null-ls.nvim", }