sublimelsp / LSP-ruff

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

How and where to configure #60

Closed a412h closed 5 months ago

a412h commented 5 months ago

I have followed the installation procedure. Where do I configure Ruff for Sublimetext ? Should I configure the file accessible from: Package Settings -> LSP -> Servers -> LSP-ruff ? More precisely I need to lint following Flake8 and Pycodestyle, for example automatic detection of warnings, like W291. How and where do I do that ?

LDAP commented 5 months ago

You configure ruff-lsp like you configure ruff, see https://docs.astral.sh/ruff/configuration/

a412h commented 5 months ago

A concrete example would be greatly appreciated. What should I write, and where, to make ruff signal W291 ?

LDAP commented 5 months ago

Ruff can be configured through a pyproject.toml, ruff.toml, or .ruff.toml file.

Contents for pyproject.toml:

[tool.ruff.lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`)  codes by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
# McCabe complexity (`C901`) by default.
select = ["E4", "E7", "E9", "F", "W"]
# -----add this-------------------^
a412h commented 5 months ago

In Ubuntu, where do I place such file so that Sublimetext uses it for ruff ?

LDAP commented 5 months ago

The file must be in the project root, i.e. the folder you open with sublime text.

a412h commented 5 months ago

Isn't there a way to have one file for all projects, as a default configuration for ruff under sublimetext ?

LDAP commented 5 months ago

This can be achieved with the lint.args here Package Settings -> LSP -> Servers -> LSP-ruff, have a look in the documentation here https://github.com/astral-sh/ruff-lsp. This is only a bridge to ruff-lsp so all settings from there apply here.

a412h commented 5 months ago

The .toml file you provided works perfectly when placed at the root of a project. However, I don't know how to translate it into a json file; this has no effect (~/.config/sublime-text/Packages/User/LSP-ruff.sublime-settings):

{
  "settings": {
    "LSP": {
      "LSP-ruff": {
        "initializationOptions": {
          "settings": {
            "args": ["E", "F", "W"],
            "preview": true
          }
        }
      }
    }
  }
}
LDAP commented 5 months ago

Description for lint.args from here https://github.com/astral-sh/ruff-lsp

Additional command-line arguments to pass to ruff check, e.g., "args": ["--config=/path/to/pyproject.toml"]. Supports a subset of Ruff's command-line arguments, ignoring those that are required to operate the LSP, like --force-exclude and --verbose.

This means, you should use something like ["--config=/path/to/pyproject.toml"]

a412h commented 5 months ago

Could you please write the minimal working file under Package Settings -> LSP -> Servers -> LSP-ruff where I have to put the ["--config=/path/to/pyproject.toml"] (it is not clear from the documentation what fields the file must have) ?

a412h commented 5 months ago

A minimal working example of a file that allows a global configuration of Ruff: 1) Open Package Settings -> LSP -> Servers -> LSP-ruff 2) Save there the following lines:

{
    "initializationOptions": {
        "globalSettings": {
            // Custom arguments passed to ruff.
            // See ruff documentation at https://github.com/charliermarsh/ruff/blob/main/README.md#configuration
            "lint.args": ["--config=/folder_of_your_toml_file/pyproject.toml"],
        },
    },
}