sublimelsp / LSP-ruff

LSP helper for ruff - an extremely fast Python linter, written in Rust.
https://packagecontrol.io/packages/LSP-ruff
MIT License
26 stars 4 forks source link

ruff formatting on save #41

Closed sryza closed 8 months ago

sryza commented 8 months ago

Ruff includes a new formatter: https://github.com/astral-sh/ruff/blob/main/crates/ruff_python_formatter/README.md

Is there a way to automatically invoke this formatter on save?

I took a stab in the dark and gave these a shot:

  "lsp_code_actions_on_save": {
    "source.lsp_format_on_save": true,
    "source.fixAll": true,
    "source.organizeImports": true,
    "source.applyFormat": true
  },
rchl commented 8 months ago

The documentation you've linked doesn't mention anything about supporting formatting through code actions. It only mentions support "Format on save" LSP functionality which can be enabled with the lsp_format_on_save option in global LSP settings.

sryza commented 8 months ago

@rchl thanks a ton for the quick response.

I've given this a shot in my global LSP settings (started with just the top-level lsp_format_on_save but then tried a couple others just in case):

{
  "log_debug": true,
  "lsp_format_on_save": true,
  "lsp_code_actions_on_save": {
    "lsp_format_on_save": true,
    "source.lsp_format_on_save": true,
    "source.fixAll": true,
    "source.organizeImports": true,
    "source.applyFormat": true
  },
  "clients": {
    "ruff-lsp": {
      "command": [
        "ruff-lsp"
      ],
      "enabled": false,
      "selector": "source.python",
      "initializationOptions": {
        "settings": {
          "args": [],
        },
      },
    },
  },
}

But not seeing my Python code get formatted on save. My JSON code does get formatted on save, so I think lsp_format_on_save is working, just not with ruff.

rchl commented 8 months ago

I think it might be an issue with ruff lsp as it doesn't report formatting capability.

sryza commented 8 months ago

Gotcha - I filed an issue over there: https://github.com/astral-sh/ruff-lsp/issues/293

LDAP commented 8 months ago

The formatter is only supported in the latest ruff-lsp v0.0.42 which was released a few minutes ago.

charliermarsh commented 8 months ago

Prior to v0.0.41, we gated format capabilities behind an environment variable (RUFF_EXPERIMENTAL_FORMATTER=1), since the formatter was in alpha. The most recent release removes that gating, so v0.0.42 and later should work as expected here.

sryza commented 8 months ago

Got it, thanks @charliermarsh. I submitted a PR to upgrade the requirement to ruff-lsp 0.0.42: https://github.com/sublimelsp/LSP-ruff/pull/42.

Maybe out of the scope of this GH issue, but I noticed that the Sublime plugin is invoking a ruff-lsp that's installed inside the package dir (which is still using the older ruff-lsp version).

LSP: starting ['/Users/sryza/Library/Caches/Sublime Text/Package Storage/LSP-ruff/bin/ruff-lsp'] in /Users/sryza/dagster

Ideally I'd like it to just run using the ruff-lsp command that's installed in my virtualenv. Is there a way to set that up?

rwols commented 8 months ago

Yes. If you run “Preferences: LSP-ruff Settings” from the command palette, you can override the ”command” that’s used to start the subprocess. I recommend not doing that, as server settings may be out of sync.

rwols commented 8 months ago

This method is also mentioned in the readme: https://github.com/sublimelsp/LSP-ruff#configuration

LDAP commented 8 months ago

Closed by #43