astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FR: Sort imports on format

lethefrost opened this issue · comments

Hi there, if I didn't miss something, for the current version of Ruff extension there's a way to auto sort the imports on save, by adding the following settings:

    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit"
        }
    },

However, I didn't find any information about how to sort on format. I hope to sort the imports when I trigger Format Document command (mostly by shortcut). I noticed there are commands ruff.executeOrganizeImports and editor.action.organizeImports that can be bound with shortcuts. However, when I tried to bind them with the same shortcut as Format Document, instead of triggering once and executing both of them, organize imports just replace the action of format document.

Can only auto sort on save can sometimes cause inconvenience. For example, in daily use with writing your own code, for convenience, you might want to keep this setting on. However, sometimes you would have to work on someone else's code who doesn't follow style guides at all. In this case, when you only want to modify a small part of the file, and don't want to or have to do the heavy job to refactor and clean the whole file, it will be very awkward when you save the file and find the extension just sorting the imports which have to be ordered in a specific way for the program to run properly. Then you have to turn the sort-on-save setting on and off all the time. That's why I think it would be nice to have a feature to sort-on-format, 'cause you are not likely to execute the formatter on the others' files which are not intended to follow specific style guides, but you will always need to save files after editing.

Thank you for your efforts on this great extension!

Hi! Thanks for your feedback. This is a duplicate of astral-sh/ruff-lsp#335 so we'll use that to track this instead.

Zanie beat me to it mid-comment but I'll post anyway to say thanks for the kind words :)

Hi! Thanks for your feedback. This is a duplicate of astral-sh/ruff-lsp#335 so we'll use that to track this instead.

Thank you for letting me know!

Zanie beat me to it mid-comment but I'll post anyway to say thanks for the kind words :)

and thanks for your kind words too😊