Skip to content

feat(ts_ls): provide command to run source actions #3780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lsp/ts_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
--- }
--- ```
---
--- Source actions such as organizing imports and removing unused code are available via `:LspTypescriptSourceAction`.
---
--- ### Vue support
---
--- As of 2.0.0, Volar no longer supports TypeScript itself. Instead, a plugin
Expand Down Expand Up @@ -93,4 +95,19 @@ return {
return vim.NIL
end,
},
on_attach = function(client)
-- ts_ls provides code actions that have a prefix `source.` that apply to the whole file. These will 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)
Comment on lines +102 to +104
Copy link
Member

@justinmk justinmk Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if code_action() should have a parameter that does something like this (except without the source. constraint) by default? That would Nvim core to provide a default mapping (gra or something else) so that these are always discoverable for all servers.

Meanwhile, this is a good change, thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know how common this pattern is. I think in other language servers these would be workspace commands instead. That said, if other language servers do it, it might be nice to have.


vim.lsp.buf.code_action({
context = {
only = source_actions,
},
})
end, {})
end,
}
Loading