feat(typescript)!: the typescript extra now uses vtsls instead of tsserver. You may want to update your lsp settings.

This commit is contained in:
Folke Lemaitre 2024-05-29 15:10:53 +02:00
parent cf46d265ff
commit fba06ce9f5
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 84 additions and 57 deletions

View file

@ -1,14 +1,3 @@
local inlay_hints_settings = {
includeInlayEnumMemberValueHints = true,
includeInlayFunctionLikeReturnTypeHints = true,
includeInlayFunctionParameterTypeHints = true,
includeInlayParameterNameHints = "literals",
includeInlayParameterNameHintsWhenArgumentMatchesName = false,
includeInlayPropertyDeclarationTypeHints = true,
includeInlayVariableTypeHints = false,
includeInlayVariableTypeHintsWhenTypeMatchesName = false,
}
return { return {
recommended = function() recommended = function()
return LazyVim.extras.wants({ return LazyVim.extras.wants({
@ -24,13 +13,12 @@ return {
}) })
end, end,
-- add typescript to treesitter
{ {
"nvim-treesitter/nvim-treesitter", "yioneko/nvim-vtsls",
opts = function(_, opts) lazy = true,
if type(opts.ensure_installed) == "table" then opts = {},
vim.list_extend(opts.ensure_installed, { "typescript", "tsx" }) config = function(_, opts)
end require("vtsls").config(opts)
end, end,
}, },
@ -40,52 +28,85 @@ return {
opts = { opts = {
-- make sure mason installs the server -- make sure mason installs the server
servers = { servers = {
---@type lspconfig.options.tsserver
tsserver = { tsserver = {
enabled = false,
},
vtsls = {
settings = {
complete_function_calls = true,
typescript = {
updateImportsOnFileMove = { enabled = "always" },
enableMoveToFileCodeAction = true,
experimental = {
completion = {
enableServerSideFuzzyMatch = true,
},
},
suggest = {
completeFunctionCalls = true,
},
inlayHints = {
enumMemberValues = { enabled = true },
functionLikeReturnTypes = { enabled = true },
parameterNames = { enabled = "literals" },
parameterTypes = { enabled = true },
propertyDeclarationTypes = { enabled = true },
variableTypes = { enabled = false },
},
},
},
keys = { keys = {
{
"gD",
function()
require("vtsls").commands.goto_source_definition(0)
end,
desc = "Goto Source Definition",
},
{ {
"<leader>co", "<leader>co",
function() function()
vim.lsp.buf.code_action({ require("vtsls").commands.organize_imports(0)
apply = true,
context = {
only = { "source.organizeImports.ts" },
diagnostics = {},
},
})
end, end,
desc = "Organize Imports", desc = "Organize Imports",
}, },
{ {
"<leader>cR", "<leader>cM",
function() function()
vim.lsp.buf.code_action({ require("vtsls").commands.add_missing_imports(0)
apply = true,
context = {
only = { "source.removeUnused.ts" },
diagnostics = {},
},
})
end, end,
desc = "Remove Unused Imports", desc = "Add missing imports",
}, },
}, {
settings = { "<leader>cD",
typescript = { function()
inlayHints = inlay_hints_settings, require("vtsls").commands.fix_all(0)
}, end,
javascript = { desc = "Fix all diagnostics",
inlayHints = inlay_hints_settings,
},
completions = {
completeFunctionCalls = true,
}, },
}, },
}, },
}, },
setup = {
tsserver = function()
-- disable tsserver
return true
end,
},
}, },
}, },
{
"neovim/nvim-lspconfig",
opts = function(_, opts)
-- copy typescript settings to javascript
opts.servers.vtsls.settings.javascript = vim.deepcopy(opts.servers.vtsls.settings.typescript)
-- add vtsls to lspconfig
require("lspconfig.configs").vtsls = require("vtsls").lspconfig
end,
},
{ {
"mfussenegger/nvim-dap", "mfussenegger/nvim-dap",
optional = true, optional = true,

View file

@ -26,14 +26,18 @@ return {
volar = {}, volar = {},
-- Volar 2.0 has discontinued their "take over mode" which in previous version provided support for typescript in vue files. -- Volar 2.0 has discontinued their "take over mode" which in previous version provided support for typescript in vue files.
-- The new approach to get typescript support involves using the typescript language server along side volar. -- The new approach to get typescript support involves using the typescript language server along side volar.
tsserver = { vtsls = {
init_options = { settings = {
plugins = { vtsls = {
-- Use typescript language server along with vue typescript plugin tsserver = {
{ globalPlugins = {
name = "@vue/typescript-plugin", -- Use typescript language server along with vue typescript plugin
location = vue_typescript_plugin, {
languages = { "javascript", "typescript", "vue" }, name = "@vue/typescript-plugin",
location = vue_typescript_plugin,
languages = { "vue" },
},
},
}, },
}, },
}, },

View file

@ -225,11 +225,13 @@ return {
for server, server_opts in pairs(servers) do for server, server_opts in pairs(servers) do
if server_opts then if server_opts then
server_opts = server_opts == true and {} or server_opts server_opts = server_opts == true and {} or server_opts
-- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig if server_opts.enabled ~= false then
if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then -- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig
setup(server) if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then
elseif server_opts.enabled ~= false then setup(server)
ensure_installed[#ensure_installed + 1] = server else
ensure_installed[#ensure_installed + 1] = server
end
end end
end end
end end