Enable ruff-specific source actions#10916
Conversation
|
There was a problem hiding this comment.
It looks good. I would have found it useful if the PR summary contained a small description of how this PR fixes the issue.
What happens if you put the following in your configuration
{
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"source.organizeImports.ruff": "explicit",
},
}
}
Is this the same way we promote both actions today? Could the LSP know which code actions the client enables to avoid generating unnecessary actions?
I looked at ESLint, and it seems they only generate the source.fixAll.eslint action (and not source.fixAll). I tried to test this locally and it seems to work (There's the chance that I messed up my testing setup but I'm somewhat confident):
VS code configurations:
"editor.codeActionsOnSave": {
"source.fixAll": "always"
}
fix_all
dbg!("Source fix all ruff only");
Ok([SOURCE_FIX_ALL_RUFF]
.into_iter()
.map(move |kind| types::CodeAction {
title: format!("{DIAGNOSTIC_NAME}: Fix all auto-fixable problems"),
kind: Some(kind),
edit: edit.clone(),
data: data.clone(),
..Default::default()
})
.map(CodeActionOrCommand::CodeAction)
.collect())And VS code automatically applied the fixes. So maybe generating the fixes twice isn't necessary after all.
|
(I was in the middle of writing the review comment when I pressed the back button which erased everything, I'll try my best to write everything again.) I don’t think the server should return two code actions if the user has provided both Think of it more as a hierarchy where a more specific code action kind wins. Here’s a snippet from the LSP spec: /**
* The kind of a code action.
*
* Kinds are a hierarchical list of identifiers separated by `.`,
* e.g. `"refactor.extract.function"`.
*
* The set of kinds is open and client needs to announce the kinds it supports
* to the server during initialization.
*/
export type CodeActionKind = string;I’d also recommend testing this out with different values of I did a small test locally and saw a difference in the response between |
4d28e57 to
e5c43ec
Compare
|
@MichaReiser @dhruvmanila I've tried to address your feedback as best as I could. We now only return the ruff-specific code actions ( Here are the various configurations I've tested with the latest changes: Configuration: {
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
},
}
}Result: Manually saving automatically fixes all problems in the file. Configuration: {
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
},
}
}Result: Manually saving automatically fixes all problems in the file. Configuration: {
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll": "never",
"source.fixAll.ruff": "explicit"
},
}
}Result: Manually saving automatically fixes all problems in the file. Configuration: {
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.fixAll.ruff": "explicit"
},
}
}Result: Manually saving automatically fixes all problems in the file. Configuration: {
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.fixAll.ruff": "never"
},
}
}Result: Manual saving does nothing. It's the same for One strange thing I noticed is that the |
Yeah, I think the behavior in this PR is correct for the last example. I also confirmed with ESLint with the following VS Code settings: "[javascript]": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.fixAll.eslint": "never"
}
}And the following code: /* eslint curly: "error"*/
if (foo) foo++;I think |
dhruvmanila
left a comment
There was a problem hiding this comment.
Thank you for addressing all the feedback and providing all of the test scenarios.
|
Any reason why is this PR labeled as "bug"? I don't think there was support for it before this PR so I wouldn't classify it as a bug. |
The original issue that this PR solves was that we should have supported this but that support was broken. |
Summary
Fixes #10780.
The server now send code actions to the client with a Ruff-specific kind,
source.*.ruff. The kind filtering logic has also been reworked to support this.Test Plan
Add this to your
settings.jsonin VS Code:{ "[python]": { "editor.codeActionsOnSave": { "source.organizeImports.ruff": "explicit", }, } }Imports should be automatically organized when you manually save with
Ctrl/Cmd+S.