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

Conversation

thatsmydoing
Copy link
Contributor

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.

@thatsmydoing thatsmydoing requested a review from glepnir as a code owner April 26, 2025 08:30
@justinmk
Copy link
Member

They are not exposed via vim.lsp.buf.code_action()

Thanks for confirming that :)

lsp/ts_ls.lua Outdated
Comment on lines 85 to 88
-- ts_ls provides code actions that have a prefix `source.` that apply to the whole file. These are not discoverable
-- via the normal `vim.lsp.buf.code_action()` so add a command that allows access to them.
local function get_source_actions()
local codeActionKinds = client.server_capabilities.codeActionProvider.codeActionKinds
Copy link
Member

Choose a reason for hiding this comment

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

Why aren't these discoverable via code_action() ? Can we enhance code_action() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The typescript language server only returns these code actions if they're specified in context.only.

So there's nothing to fix on the nvim side. The command implementation actually does use code_action().

Copy link
Member

Choose a reason for hiding this comment

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

The typescript language server only returns these code actions if they're specified in context.only.

But why? Is it because code_action() is sending position info, or diagnostics info? Do we need a way to tell code_action() not to send that info?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is really just how they've implemented it. You can see the code checks it here where kinds is populated from context.only. The refactor and quickfix actions on the other hand have an extra !kinds check which means they're always returned if context.only is not provided.

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.
@thatsmydoing thatsmydoing force-pushed the typescript-source-action branch from 1e80dd9 to 9a33015 Compare April 26, 2025 12:26
Comment on lines +102 to +104
local source_actions = vim.tbl_filter(function(action)
return vim.startswith(action, 'source.')
end, client.server_capabilities.codeActionProvider.codeActionKinds)
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.

@justinmk justinmk merged commit 21f7583 into neovim:master Apr 26, 2025
11 checks passed
@thatsmydoing thatsmydoing deleted the typescript-source-action branch April 26, 2025 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants