ts_ls: add comments for vue integration and adhere to code style

This commit is contained in:
Mia 2025-12-26 01:19:59 +01:00
commit a792378b46

View file

@ -65,32 +65,30 @@
end end
''; '';
}; };
on_attach = on_attach = mkLuaInline ''
mkLuaInline function(client, bufnr)
# lua -- 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`.
function(client, bufnr) vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function()
-- ts_ls provides `source.*` code actions that apply to the whole file. These only appear in local source_actions = vim.tbl_filter(function(action)
-- `vim.lsp.buf.code_action()` if specified in `context.only`. return vim.startswith(action, 'source.')
vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function() end, client.server_capabilities.codeActionProvider.codeActionKinds)
local source_actions = vim.tbl_filter(function(action)
return vim.startswith(action, 'source.')
end, client.server_capabilities.codeActionProvider.codeActionKinds)
vim.lsp.buf.code_action({ vim.lsp.buf.code_action({
context = { context = {
only = source_actions, only = source_actions,
}, },
}) })
end, {}) end, {})
if vim.bo.filetype == 'vue' then -- make sure that vue-language-server handles semantic tokens when we are in a vue file
client.server_capabilities.semanticTokensProvider.full = false if vim.bo.filetype == 'vue' then
else client.server_capabilities.semanticTokensProvider.full = false
client.server_capabilities.semanticTokensProvider.full = true else
end client.server_capabilities.semanticTokensProvider.full = true
end end
''; end
'';
}; };
in { in {
inherit ts_ls; inherit ts_ls;
@ -113,44 +111,42 @@
"package.json" "package.json"
".git" ".git"
]; ];
on_init = on_init = mkLuaInline ''
mkLuaInline -- forward typescript blocks to ts_ls or vtsls depending on which is available first
# lua function(client)
'' client.handlers['tsserver/request'] = function(_, result, context)
function(client) local ts_clients = vim.lsp.get_clients({ bufnr = context.bufnr, name = 'ts_ls' })
client.handlers['tsserver/request'] = function(_, result, context) local clients = {}
local ts_clients = vim.lsp.get_clients({ bufnr = context.bufnr, name = 'ts_ls' })
local clients = {}
vim.list_extend(clients, ts_clients) vim.list_extend(clients, ts_clients)
if #clients == 0 then 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) vim.notify('Could not find `vtsls` or `ts_ls` lsp client, `vue_ls` would not work without it.', vim.log.levels.ERROR)
return 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
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
''; end
'';
}; };
denols = { denols = {