From 0aa25db718518f99ef82bff7ce71a5e3baebaa14 Mon Sep 17 00:00:00 2001 From: Jules Sang Date: Tue, 19 Nov 2024 17:31:45 +0100 Subject: [PATCH] Work on LSP --- lua/plugins/better-ts-errors.lua | 10 ++ lua/plugins/lsp-config.lua | 181 +++++++++++++++++++++++++++++-- lua/plugins/mason.lua | 2 +- lua/plugins/mini.icons.lua | 16 +++ 4 files changed, 201 insertions(+), 8 deletions(-) create mode 100644 lua/plugins/better-ts-errors.lua create mode 100644 lua/plugins/mini.icons.lua diff --git a/lua/plugins/better-ts-errors.lua b/lua/plugins/better-ts-errors.lua new file mode 100644 index 0000000..3ed7df6 --- /dev/null +++ b/lua/plugins/better-ts-errors.lua @@ -0,0 +1,10 @@ +return { + "OlegGulevskyy/better-ts-errors.nvim", + dependencies = { "MunifTanjim/nui.nvim" }, + config = { + keymaps = { + toggle = "dd", -- default 'dd' + go_to_definition = "dx", -- default 'dx' + }, + }, +} diff --git a/lua/plugins/lsp-config.lua b/lua/plugins/lsp-config.lua index 9375c37..f05f19f 100644 --- a/lua/plugins/lsp-config.lua +++ b/lua/plugins/lsp-config.lua @@ -1,3 +1,6 @@ +local LazyVim = require("lazyvim.util") +local util = require("lspconfig.util") + return { "neovim/nvim-lspconfig", opts = { @@ -5,6 +8,105 @@ return { tailwindcss = { filetypes_exclude = { "markdown" }, }, + --- @deprecated -- tsserver renamed to ts_ls but not yet released, so keep this for now + --- the proper approach is to check the nvim-lspconfig release version when it's released to determine the server name dynamically + tsserver = { + enabled = false, + }, + ts_ls = { + enabled = false, + }, + vtsls = { + -- explicitly add default filetypes, so that we can extend + -- them in related extras + filetypes = { + "javascript", + "javascriptreact", + "javascript.jsx", + "typescript", + "typescriptreact", + "typescript.tsx", + }, + root_dir = util.root_pattern("tsconfig.json", "jsconfig.json", "package.json", ".git"), + settings = { + complete_function_calls = true, + vtsls = { + enableMoveToFileCodeAction = true, + autoUseWorkspaceTsdk = true, + experimental = { + completion = { + enableServerSideFuzzyMatch = true, + }, + }, + }, + typescript = { + updateImportsOnFileMove = { enabled = "always" }, + suggest = { + completeFunctionCalls = true, + }, + inlayHints = { + enumMemberValues = { enabled = true }, + functionLikeReturnTypes = { enabled = true }, + parameterNames = { enabled = "literals" }, + parameterTypes = { enabled = true }, + propertyDeclarationTypes = { enabled = true }, + variableTypes = { enabled = false }, + }, + }, + keys = { + { + "gD", + function() + local params = vim.lsp.util.make_position_params() + LazyVim.lsp.execute({ + command = "typescript.goToSourceDefinition", + arguments = { params.textDocument.uri, params.position }, + open = true, + }) + end, + desc = "Goto Source Definition", + }, + { + "gR", + function() + LazyVim.lsp.execute({ + command = "typescript.findAllFileReferences", + arguments = { vim.uri_from_bufnr(0) }, + open = true, + }) + end, + desc = "File References", + }, + { + "co", + LazyVim.lsp.action["source.organizeImports"], + desc = "Organize Imports", + }, + { + "cM", + LazyVim.lsp.action["source.addMissingImports.ts"], + desc = "Add missing imports", + }, + { + "cu", + LazyVim.lsp.action["source.removeUnused.ts"], + desc = "Remove unused imports", + }, + { + "cD", + LazyVim.lsp.action["source.fixAll.ts"], + desc = "Fix all diagnostics", + }, + { + "cV", + function() + LazyVim.lsp.execute({ command = "typescript.selectTypeScriptVersion" }) + end, + desc = "Select TS workspace version", + }, + }, + }, + }, marksman = {}, eslint = {}, emmet_language_server = { @@ -45,20 +147,85 @@ return { setup = { eslint = function() require("lazyvim.util").lsp.on_attach(function(client) + print(client.name) if client.name == "eslint" then client.server_capabilities.documentFormattingProvider = true - elseif client.name == "ts_ls" then - client.server_capabilities.documentFormattingProvider = false + -- elseif client.name == "ts_ls" then + -- client.server_capabilities.documentFormattingProvider = false end end) end, + tsserver = function() + -- disable tsserver + return true + end, + ts_ls = function() + -- disable tsserver + return true + end, + vtsls = function(_, opts) + LazyVim.lsp.on_attach(function(client, buffer) + client.commands["_typescript.moveToFileRefactoring"] = function(command, ctx) + ---@type string, string, lsp.Range + local action, uri, range = unpack(command.arguments) + + local function move(newf) + client.request("workspace/executeCommand", { + command = command.command, + arguments = { action, uri, range, newf }, + }) + end + + local fname = vim.uri_to_fname(uri) + client.request("workspace/executeCommand", { + command = "typescript.tsserverRequest", + arguments = { + "getMoveToRefactoringFileSuggestions", + { + file = fname, + startLine = range.start.line + 1, + startOffset = range.start.character + 1, + endLine = range["end"].line + 1, + endOffset = range["end"].character + 1, + }, + }, + }, function(_, result) + ---@type string[] + local files = result.body.files + table.insert(files, 1, "Enter new path...") + vim.ui.select(files, { + prompt = "Select move destination:", + format_item = function(f) + return vim.fn.fnamemodify(f, ":~:.") + end, + }, function(f) + if f and f:find("^Enter new path") then + vim.ui.input({ + prompt = "Enter move destination:", + default = vim.fn.fnamemodify(fname, ":h") .. "/", + completion = "file", + }, function(newf) + return newf and move(newf) + end) + elseif f then + move(f) + end + end) + end) + end + end, "vtsls") + -- copy typescript settings to javascript + opts.settings.javascript = + vim.tbl_deep_extend("force", {}, opts.settings.typescript, opts.settings.javascript or {}) + end, + tailwindcss = function(_, opts) - -- local tw = require("lspconfig.server_configurations.tailwindcss") - -- --- @param ft string - -- opts.filetypes = vim.tbl_filter(function(ft) - -- return not vim.tbl_contains(opts.filetypes_exclude or {}, ft) - -- end, tw.default_config.filetypes) + local tw = require("lspconfig.server_configurations.tailwindcss") + --- @param ft string + opts.filetypes = vim.tbl_filter(function(ft) + return not vim.tbl_contains(opts.filetypes_exclude or {}, ft) + end, tw.default_config.filetypes) end, }, }, diff --git a/lua/plugins/mason.lua b/lua/plugins/mason.lua index 9252447..6565d3f 100644 --- a/lua/plugins/mason.lua +++ b/lua/plugins/mason.lua @@ -9,6 +9,6 @@ return { "williamboman/mason.nvim", opts = function(_, opts) opts.ensure_installed = opts.ensure_installed or {} - vim.list_extend(opts.ensure_installed, { "markdownlint", "marksman" }) + vim.list_extend(opts.ensure_installed, { "markdownlint", "marksman", "js-debug-adapter" }) end, } diff --git a/lua/plugins/mini.icons.lua b/lua/plugins/mini.icons.lua new file mode 100644 index 0000000..883f7d4 --- /dev/null +++ b/lua/plugins/mini.icons.lua @@ -0,0 +1,16 @@ +return { + "echasnovski/mini.icons", + opts = { + file = { + [".eslintrc.js"] = { glyph = "󰱺", hl = "MiniIconsYellow" }, + [".node-version"] = { glyph = "", hl = "MiniIconsGreen" }, + [".prettierrc"] = { glyph = "", hl = "MiniIconsPurple" }, + [".yarnrc.yml"] = { glyph = "", hl = "MiniIconsBlue" }, + ["eslint.config.js"] = { glyph = "󰱺", hl = "MiniIconsYellow" }, + ["package.json"] = { glyph = "", hl = "MiniIconsGreen" }, + ["tsconfig.json"] = { glyph = "", hl = "MiniIconsAzure" }, + ["tsconfig.build.json"] = { glyph = "", hl = "MiniIconsAzure" }, + ["yarn.lock"] = { glyph = "", hl = "MiniIconsBlue" }, + }, + }, +}