Skip to content

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

Closed
snowsignal wants to merge 1 commit intomainfrom
jane/command-file-filter
Closed

Enable fix/formatting commands for Python and Jupyter Notebook files only#487
snowsignal wants to merge 1 commit intomainfrom
jane/command-file-filter

Conversation

@snowsignal
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 thread package.json
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
Copy Markdown
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
Copy Markdown
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