Skip to content

Commit 21f7583

Browse files
authored
feat(ts_ls): add :LspTypescriptSourceAction command #3780
Source actions are code actions that apply to the whole file. They are not exposed via `vim.lsp.buf.code_action()` and must be requested explicitly.
1 parent 8bb2fac commit 21f7583

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lsp/ts_ls.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
--- }
3030
--- ```
3131
---
32+
--- Source actions such as organizing imports and removing unused code are available via `:LspTypescriptSourceAction`.
33+
---
3234
--- ### Vue support
3335
---
3436
--- As of 2.0.0, Volar no longer supports TypeScript itself. Instead, a plugin
@@ -93,4 +95,19 @@ return {
9395
return vim.NIL
9496
end,
9597
},
98+
on_attach = function(client)
99+
-- ts_ls provides code actions that have a prefix `source.` that apply to the whole file. These will only appear in
100+
-- `vim.lsp.buf.code_action()` if specified in `context.only`.
101+
vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function()
102+
local source_actions = vim.tbl_filter(function(action)
103+
return vim.startswith(action, 'source.')
104+
end, client.server_capabilities.codeActionProvider.codeActionKinds)
105+
106+
vim.lsp.buf.code_action({
107+
context = {
108+
only = source_actions,
109+
},
110+
})
111+
end, {})
112+
end,
96113
}

0 commit comments

Comments
 (0)