Skip to content
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

ruff server QOL improvement: Make code action ordering more consistent #12279

Closed
ravibrock opened this issue Jul 10, 2024 · 2 comments
Closed
Labels
question Asking for support or clarification server Related to the LSP server

Comments

@ravibrock
Copy link

ravibrock commented Jul 10, 2024

Hello,

I wanted to suggest that code action ordering be kept consistent. For example, in Neovim I have <leader>ca bound to toggling code actions, so it would be helpful if <leader>ca 2 always selected "Organize imports":

On line with no diagnostics:
Screenshot 2024-07-10 at 11 22 44
On line with diagnostics:
Screenshot 2024-07-10 at 11 23 12

Ideally, 1 and 2 would select the same thing in both cases since those options are always available.

Thank you!

@dhruvmanila
Copy link
Member

Thank you for the suggestion! For discussion around the current ordering, refer to astral-sh/ruff-lsp#210.

The code actions that you've mentioned are the source level code actions i.e., they apply to an entire file. Currently, Ruff always provide the said code actions even if they're no-op but that could change in the future (astral-sh/ruff-lsp#91). For example, the Ruff: Organize imports action won't be provided if there are no imports to organize.

I would instead suggest to use a keybinding if you always need to execute a specific source code action. For example, the following will apply the Ruff: Organize imports code actions:

local function ruff_organize_imports()
	vim.lsp.buf.code_action {
	  context = {
	    only = { 'source.organizeImports.ruff' },
	  },
	  apply = true,
	}
end

vim.keymap.set('n', '<leader>oi', ruff_organize_imports, {
	desc = 'ruff: [o]rganize [i]mports'
})

Does this help?

@dhruvmanila dhruvmanila added the question Asking for support or clarification label Jul 11, 2024
@ravibrock
Copy link
Author

Ah, that looks good thank you! I might even modify that for use as a BufWritePre autocommand so I don't have to worry about organizing them by hand. And yeah, I see the benefits of the other approach too given the example in astral-sh/ruff-lsp#210.

@dhruvmanila dhruvmanila added the server Related to the LSP server label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification server Related to the LSP server
Projects
None yet
Development

No branches or pull requests

3 participants