astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
28.75k stars 933 forks source link

`ruff server` setting enabling code actions on save #11756

Open DanCardin opened 1 month ago

DanCardin commented 1 month ago

In the PR for adding the organizeImports code action, i saw a reference to:

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

which, as far as i can tell, is a VSCode specific thing. Per https://github.com/astral-sh/ruff-lsp/issues/409 or https://github.com/astral-sh/ruff-lsp/issues/335, I'd love for there to be a ruff server-specific setting that optionally enables either/or both of organizeImports or fixAll actions on lsp format.

Personally, I'd likely enable just the organizeImports on save (which i see no reason ever to not run, which is why i'd rather have it automatic than a code action i need to remember to perform), perhaps not fixAll.

snowsignal commented 1 month ago

Thanks for opening this feature request, @DanCardin!

I do think this would a setting worth exploring for editors that don't have a way to run code actions / commands on save.

I'd love for there to be a ruff server-specific setting that optionally enables either/or both of organizeImports or fixAll actions on lsp format.

Just to clarify - are you also proposing that we introduce a separate setting to enable running organizeImports / fixAll when we format?

DanCardin commented 1 month ago

Per my parenthesis block in the OP, i'd personally be happy with an organizeImportsOnFormat = true or onFormat = {organizeImports = true}} or whatever server setting (separate from the normal project-specific ruff config).

But with that said, I could imagine (by looking at https://github.com/astral-sh/ruff-lsp/issues/335) that someone might also want to enable fixAll on save too. They're essentially the same feature request. i'm just not personally invested in having both.

snowsignal commented 1 month ago

Okay, thanks for clarifying 👍

T-256 commented 4 weeks ago

Another usecase at here is you always trigger format action manually and you didn't set code actions on save. I usually organize imports, format, and fix violations after I complemented the source of target module. Currently I most use 3 actions each time I finished coding, it'd be nice to have three into one command.

Proposal

ruff server

Editor settings:

{
    "ruff.format.action": {
        "fix": true,
        "isort": true,
        "style": true,
    }
}

ruff cli

The Ruff CLI needs further discussion around new command (e.g. ruff action --fix --isort--no-style). Also, see this most-wanted feature request: https://github.com/astral-sh/ruff/issues/8232