sublimelsp / LSP-ruff

LSP helper for ruff - an extremely fast Python linter, written in Rust.

Home Page:https://packagecontrol.io/packages/LSP-ruff

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ruff formatting on save

sryza opened this issue · comments

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
  },

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.

@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.

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

Gotcha - I filed an issue over there: astral-sh/ruff-lsp#293

The formatter is only supported in the latest ruff-lsp v0.0.42 which was released a few minutes 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.

Got it, thanks @charliermarsh. I submitted a PR to upgrade the requirement to ruff-lsp 0.0.42: #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?

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.

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

Closed by #43