ts: add vue support for ts_ls

This commit is contained in:
Mia 2025-12-26 01:05:14 +01:00
commit 52d9434377

View file

@ -17,11 +17,27 @@
cfg = config.vim.languages.ts;
defaultServers = ["ts_ls"];
defaultServers = [
"ts_ls"
"vue_ls"
];
servers = let
ts_ls = {
cmd = [(getExe pkgs.typescript-language-server) "--stdio"];
init_options = {hostInfo = "neovim";};
cmd = [
(getExe pkgs.typescript-language-server)
"--stdio"
];
init_options = {
hostInfo = "neovim";
plugins = [
{
name = "@vue/typescript-plugin";
location = "${pkgs.vue-language-server}/lib/language-tools/packages/language-server";
languages = ["vue"];
configNamespace = "typescript";
}
];
};
filetypes = [
"javascript"
"javascriptreact"
@ -29,6 +45,7 @@
"typescript"
"typescriptreact"
"typescript.tsx"
"vue"
];
root_markers = ["tsconfig.json" "jsconfig.json" "package.json" ".git"];
handlers = {
@ -48,23 +65,32 @@
end
'';
};
on_attach = mkLuaInline ''
function(client, bufnr)
-- ts_ls provides `source.*` code actions that apply to the whole file. These only appear in
-- `vim.lsp.buf.code_action()` if specified in `context.only`.
vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function()
local source_actions = vim.tbl_filter(function(action)
return vim.startswith(action, 'source.')
end, client.server_capabilities.codeActionProvider.codeActionKinds)
on_attach =
mkLuaInline
# lua
''
function(client, bufnr)
-- ts_ls provides `source.*` code actions that apply to the whole file. These only appear in
-- `vim.lsp.buf.code_action()` if specified in `context.only`.
vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function()
local source_actions = vim.tbl_filter(function(action)
return vim.startswith(action, 'source.')
end, client.server_capabilities.codeActionProvider.codeActionKinds)
vim.lsp.buf.code_action({
context = {
only = source_actions,
},
})
end, {})
end
'';
vim.lsp.buf.code_action({
context = {
only = source_actions,
},
})
end, {})
if vim.bo.filetype == 'vue' then
client.server_capabilities.semanticTokensProvider.full = false
else
client.server_capabilities.semanticTokensProvider.full = true
end
end
'';
};
in {
inherit ts_ls;
@ -73,6 +99,60 @@
# redirect the user to the correct server.
tsserver = ts_ls;
vue_ls = {
cmd = [
(getExe pkgs.vue-language-server)
"--stdio"
];
filetypes = [
"vue"
];
root_markers = [
"tsconfig.json"
"jsconfig.json"
"package.json"
".git"
];
on_init =
mkLuaInline
# lua
''
function(client)
client.handlers['tsserver/request'] = function(_, result, context)
local ts_clients = vim.lsp.get_clients({ bufnr = context.bufnr, name = 'ts_ls' })
local clients = {}
vim.list_extend(clients, ts_clients)
if #clients == 0 then
vim.notify('Could not find `vtsls` or `ts_ls` lsp client, `vue_ls` would not work without it.', vim.log.levels.ERROR)
return
end
local ts_client = clients[1]
local param = unpack(result)
local id, command, payload = unpack(param)
ts_client:exec_cmd({
title = 'vue_request_forward', -- You can give title anything as it's used to represent a command in the UI, `:h Client:exec_cmd`
command = 'typescript.tsserverRequest',
arguments = {
command,
payload,
},
}, { bufnr = context.bufnr }, function(_, r)
local response = r and r.body
-- TODO: handle error or response nil here, e.g. logging
-- NOTE: Do NOT return if there's an error or no response, just return nil back to the vue_ls to prevent memory leak
local response_data = { { id, response } }
---@diagnostic disable-next-line: param-type-mismatch
client:notify('tsserver/response', response_data)
end)
end
end
'';
};
denols = {
cmd = [(getExe pkgs.deno) "lsp"];
cmd_env = {NO_COLOR = true;};