Skip to content

Commit c298fec

Browse files
committed
fix(vue_ls): make tsserver handler more resilient
Problem: The current `on_init` of `vue_ls` requiers `vtsls` or `ts_ls` to already be attached, which is not always guaranteed. Solution: Add a retry mechanism that reruns the logic for some time to give the other ls a chance to catch up.
1 parent a48eb0a commit c298fec

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

lsp/vue_ls.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,25 @@ return {
2424
filetypes = { 'vue' },
2525
root_markers = { 'package.json' },
2626
on_init = function(client)
27-
client.handlers['tsserver/request'] = function(_, result, context)
27+
local retries = 0
28+
29+
---@param _ lsp.ResponseError
30+
---@param result any
31+
---@param context lsp.HandlerContext
32+
local function typescriptHandler(_, result, context)
2833
local ts_client = vim.lsp.get_clients({ bufnr = context.bufnr, name = 'ts_ls' })[1]
2934
or vim.lsp.get_clients({ bufnr = context.bufnr, name = 'vtsls' })[1]
3035

3136
if not ts_client then
32-
vim.notify('Could not find `ts_ls` or `vtsls` lsp client, required by `vue_ls`.', vim.log.levels.ERROR)
37+
-- there can sometimes be a short delay until `ts_ls`/`vtsls` are attached so we retry for a few times until it is ready
38+
if retries <= 10 then
39+
retries = retries + 1
40+
vim.defer_fn(function()
41+
typescriptHandler(_, result, context)
42+
end, 100)
43+
else
44+
vim.notify('Could not find `ts_ls` or `vtsls` lsp client, required by `vue_ls`.', vim.log.levels.ERROR)
45+
end
3346
return
3447
end
3548

@@ -48,5 +61,7 @@ return {
4861
client:notify('tsserver/response', response_data)
4962
end)
5063
end
64+
65+
client.handlers['tsserver/request'] = typescriptHandler
5166
end,
5267
}

0 commit comments

Comments
 (0)