-
Notifications
You must be signed in to change notification settings - Fork 46
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
Add "Disable for this line" quickfix #76
Add "Disable for this line" quickfix #76
Conversation
167d486
to
b3cf81d
Compare
In ruff-lsp (astral-sh/ruff-lsp#76) we want to add a "Disable \<rule\> for this line" quickfix. However, finding the correct line into which the `noqa` comment should be inserted is non-trivial (multi-line strings for example). Ruff already has this info, so expose it in the JSON output for use by ruff-lsp.
b3cf81d
to
dad628a
Compare
dad628a
to
dc603a0
Compare
if not params.context.only or CodeActionKind.QuickFix in params.context.only: | ||
for diagnostic in params.context.diagnostics: | ||
if diagnostic.source == "Ruff": | ||
noqa_row = cast(DiagnosticData, diagnostic.data)["noqa_row"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be more defensive, since this key could be absent for older versions of Ruff (as opposed to being None
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DiagnosticData is created by the LSP, when I create it I use noqa_row=check.get("noqa_row")
so I think it's OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you're right!
ruff_lsp/server.py
Outdated
match = NOQA_REGEX.search(line) | ||
# `foo # noqa: OLD` -> `foo # noqa: OLD,NEW` | ||
if match and match.group("codes") is not None: | ||
codes = match.group("codes") + f",{diagnostic.code}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: lets add a space after the comma, to be consistent with Ruff's own RUF100
behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a space after the comma
codes = match.group("codes") + f",{diagnostic.code}" | ||
start, end = match.start("codes"), match.end("codes") | ||
new_line = line[:start] + codes + line[end:] | ||
# `foo # noqa` -> `foo # noqa: NEW` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this case might be impossible... since if a line has foo # noqa
, the error will by definition be suppressed, right? And so this action wouldn't appear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I managed it using the "useless noqa" lint :)
Thx! |
@bluetech - Out of curiosity, what editor do you use with the LSP? |
I use vscode |
BTW, since this was merged I was thinking that it might have been better if the entire autofix was generated in ruff, and not in the LSP, since ruff has a much better idea how to do it best. Of course it can't be a regular rule/autofix, but maybe the LSP could ask ruff to generate them using some undocumented side-channel flag. OTOH, if the LSP can do it maybe it's better... |
Yeah interesting. It's close to Ruff's |
Fix #50.
This likely has some unhandled edge cases, but hopefully better than nothing :)