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

Add "Disable for this line" quickfix #76

Merged
merged 3 commits into from
Feb 27, 2023

Conversation

bluetech
Copy link
Contributor

Fix #50.

This likely has some unhandled edge cases, but hopefully better than nothing :)

@bluetech bluetech force-pushed the disable-rule-for-line-quickfix branch from 167d486 to b3cf81d Compare February 25, 2023 23:11
charliermarsh pushed a commit to astral-sh/ruff that referenced this pull request Feb 25, 2023
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.
@bluetech bluetech force-pushed the disable-rule-for-line-quickfix branch from b3cf81d to dad628a Compare February 25, 2023 23:16
@bluetech bluetech force-pushed the disable-rule-for-line-quickfix branch from dad628a to dc603a0 Compare February 25, 2023 23:16
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"]
Copy link
Member

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).

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 DiagnosticData is created by the LSP, when I create it I use noqa_row=check.get("noqa_row") so I think it's OK?

Copy link
Member

Choose a reason for hiding this comment

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

Ah you're right!

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}"
Copy link
Member

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.

Copy link
Contributor Author

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`
Copy link
Member

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?

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 managed it using the "useless noqa" lint :)

@charliermarsh charliermarsh merged commit 19ace4d into astral-sh:main Feb 27, 2023
@charliermarsh
Copy link
Member

Thx!

@charliermarsh
Copy link
Member

@bluetech - Out of curiosity, what editor do you use with the LSP?

@bluetech
Copy link
Contributor Author

bluetech commented Mar 3, 2023

I use vscode

@bluetech
Copy link
Contributor Author

bluetech commented Mar 3, 2023

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...

@charliermarsh
Copy link
Member

Yeah interesting. It's close to Ruff's --add-noqa feature.

@bluetech bluetech deleted the disable-rule-for-line-quickfix branch April 2, 2024 14:36
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.

[feature] quick fix to quickly ignore an error
2 participants