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

Enable fix/formatting commands for Python and Jupyter Notebook files only #487

Closed
wants to merge 1 commit into from

Conversation

snowsignal
Copy link
Contributor

Summary

Fixes astral-sh/ruff#11744.

The extension would allow commands like ruff.applyAutofix to run on any file, even non-Python files. This PR adds a command palette filter so that these commands only appear in the palette when working with Python or Jupyter Notebook files.

Test Plan

Ensure that commands like Ruff: Fix all auto-fixable problems still appear in the command palette (Ctrl/Cmd + Shift + P) for files with the following extensions:

  • *.py
  • *.pyi
  • *.ipynb

Then, ensure that these commands do not appear for other kinds of files (for example: *.toml or *.json). Ruff: Restart server should still be available.

@snowsignal snowsignal added the bug Something isn't working label Jun 4, 2024
Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

It might be worth looking at how ESLint does it as it doesn't raise any error if we run the "ESLint: fix all" command in settings.json.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I don't think this works for untitled documents or files that use custom extensions but are associated with the python language.

Comment on lines +378 to +386
"when": "resourceExtname == .py || resourceExtname == .pyi || resourceExtname == .ipynb"
},
{
"command": "ruff.executeFormat",
"when": "resourceExtname == .py || resourceExtname == .pyi || resourceExtname == .ipynb"
},
{
"command": "ruff.executeOrganizeImports",
"when": "resourceExtname == .py || resourceExtname == .pyi || resourceExtname == .ipynb"
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I don't think we can do that. You can configure ruff to lint files with arbitrary extensions using extend-include. These files would no longer be linted. It also means that auto fix would no longer run on any new/untitled files because they don't have an extension yet (they only have an associated language that we should probably use in the LSP).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, this wouldn't work with arbitrary file extensions.

I might try using @dhruvmanila's suggestion and just handle the snapshot not available error more gracefully in ruff server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ruff: Fix all auto-fixable problems erroring out in VSCode
3 participants