astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
1.1k stars 53 forks source link

Support Ruff configuration settings as VS Code extension options #6

Open charliermarsh opened 1 year ago

charliermarsh commented 1 year ago

E.g., you should be able to set your line-length in VS Code directly if desired.

AronDurkinSSS commented 1 year ago

@charliermarsh - how can you currently set line length / disable that error? I've tried editing pyproject.toml and failed. Thanks

charliermarsh commented 1 year ago

You can provide a command-line argument directly through the extension settings:

Screen Shot 2023-01-04 at 4 43 02 PM

Screen Shot 2023-01-04 at 4 42 55 PM

Although better would be to put a pyproject.toml in the same path as the files you're linting, and do:

[tool.ruff]
line-length = 10
XuehaiPan commented 1 year ago

Hi, have you ever consider allow passing more complex configurations to ruff from command line arguments (e.g., a JSON string or similar)? For example, like clang-format (docs):

When specifying configuration in the -style= option, the same configuration is applied for all input files. The format of the configuration is:

-style='{key1: value1, key2: value2, ...}'

This might need changes in ruff upstream. But I think we can discuss it here first.

For C/C++ projects, I'm using microsoft/vscode-cpptools and I have:

{
    //* C/C++ */
    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
    "C_Cpp.autocomplete": "Default",
    "C_Cpp.formatting": "clangFormat",
    "C_Cpp.clang_format_sortIncludes": false,
    "C_Cpp.clang_format_style": "file",
    "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, AccessModifierOffset: -3, ColumnLimit: 100, BinPackArguments: false, BinPackParameters: false }"
}

It provides a fallback style if the .clang-format file does not exist in the project. I think we can provide a similar thing like:

{
    //* Ruff */
    "ruff.configuration": "file",
    "ruff.fallbackConfiguration": "{ ling-length: 100, target-version: 'py38', show-source: true, isort.lines-after-imports: 2, flake8-quotes.inline-quotes: 'single' }"
}

when pyproject.toml or ruff.toml does not exist in the project. Currently, we cannot pass options for sub-checker from the command line. This would be a limitation for vscode configuration "ruff.args".

[tool.ruff.isort]
lines-after-imports = 2

[tool.ruff.flake8-quotes]
inline-quotes = "single"
udeepam commented 1 year ago

Would being able to customise ruff as described in the above way be worked on? If not how else can we do this without having to create a pyproject.toml or ruff.toml?

charliermarsh commented 1 year ago

@udeepam - Right now, it does require that you use a pyproject.toml or ruff.toml file. That being said, the pyproject.toml or ruff.toml file doesn't have to line in your project. You can either hardcode a path to your file (by setting --config=/path/to/file.toml in the VS Code extension's args field) or use a global, user-specific configuration file for Ruff. See https://docs.astral.sh/ruff/faq/#how-can-i-change-ruffs-default-configuration for more, but e.g. on macOS, you'd create the file at /Users/Alice/Library/Application Support/ruff/ruff.toml or similar.

Okroshiashvili commented 1 year ago

@charliermarsh

I think ruff format has a bug in vscode. I preferred to comment here instead of creating new issue. If this is the real bug and not my fault, let me know and I will create bug issue.

I've installed ruff vscode extension and then I've installed ruff via pip pip install ruff in my conda environment.

I've been using black formatter and as you know vscode offers command to format any document via command palette and I'm using that all the time.

Now, after installing ruff and ruff extension, I removed black formatter extension and everything related to black. I've set ruff as a default formatter. After that, I've altered two setting in GUI as well as in setting.json file

image

After this changes ruff formatter stoped working. It does not formats Python files anymore and also reducing line length to 88 did not have any effect. However, if I remove line length arguments then formatter works and cuts the line at 88 character. I guess this is the default.

Additionally, I've created ruff.toml file in /Users/my_user_here/Library/Application Support/ruff/ directory and adjusted vscode settings accordingly. This did not have effect as well.

zanieb commented 1 year ago

Hey @Okroshiashvili it looks like you're encountering this because the --line-length option is not supported in ruff format https://github.com/astral-sh/ruff/pull/8131

I'm not sure why it's not working in another location though, @charliermarsh may have insight there. Can you provide some logs from the Ruff extension when formatting?

Okroshiashvili commented 1 year ago

Hi @zanieb

Thanks for a quick replay ;)

I did not know about --line-length removal and it seems pretty new though. Then I understand why my effort was in vain :)

Let me retry the things with ruff.toml file and will update the thread

CarlaFernandez commented 9 months ago

Hello @charliermarsh , I'm encountering an error when trying to use VSCode settings to define an ignore for ruff. Specifically I have this in my settings.json:

    "ruff.lint.args": [
        "--ignore E501"
    ],

But I'm seeing this error, and of course it doesn't work. image

How should I specify my exception? Thank you!!

charliermarsh commented 9 months ago

@CarlaFernandez - Can you try "--ignore=E501"?

CarlaFernandez commented 9 months ago

That appears to be working, thank you!

pipme commented 8 months ago

For reference, the current solution with ruff v0.2.2 in VS Code is to set: "ruff.format.args": ["--config", "line-length=79"]

pnulty commented 8 months ago

For reference, the current solution with ruff v0.2.2 in VS Code is to set: "ruff.format.args": ["--config", "line-length=79"]

I'm having trouble getting this to work; on the VSCode Ruff extension page, what exactly do I need to put in the input box? Is it everything in the code formatted part of your comment? Or just "args":["--config", "line-length=79"]

pipme commented 8 months ago

@pnulty Please check the figure below. You can also directly specify the setting "ruff.format.args": ["--config", "line-length=79"] in settings.json.

image
charliermarsh commented 8 months ago

@pnulty - can you confirm that you’re using Ruff v0.2.2 (latest)? Very new feature.

pnulty commented 8 months ago

@pnulty - can you confirm that you’re using Ruff v0.2.2 (latest)? Very new feature.

Thanks, yes I just happened to install an hour ago, can confirm it is v0.2.2. New user so maybe I'm missing context, the --config part causes an error for me, seems like its looking for a toml file. For just the line length, what should be in user settings in settings.json? The below doesn't seem to be having an effect, but very possible I'm just missing something.

image

charliermarsh commented 8 months ago

Sorry, can you try --line-length=100, instead of line-length=100?

charliermarsh commented 8 months ago

Or, better yet: ["--config", "line-length=100"] (this is the newer syntax)

pnulty commented 8 months ago

["--line-length=100"] works! ["--config", "line-length=100"] gives the error below

image

charliermarsh commented 8 months ago

I think you might be using a version of Ruff other than v0.2.2. The extension will prioritize a Ruff installed in your environment / on your machine over the version that ships with the extension itself (which is viewed as a fallback).

charliermarsh commented 8 months ago

But I'd have to test it myself later to be certain. That's just the error I'd expect to see if you were accidentally using v0.2.1.

pnulty commented 8 months ago

Apologies, I was just reading "this extension ships with ruff==0.2.2" instead of actually checking what ruff I had installed on my system. I've upgraded ruff and can confirm that it works the second way now. Thanks for the very quick help!

tolni commented 8 months ago

how can i set quote-style to preserve in vscode?

charliermarsh commented 8 months ago

Try this in your settings.json:

{
    "ruff.format.args": ["--config", "format.quote-style='preserve'"]
}
T-256 commented 2 months ago

Fyi native server now supports use config file inside settings: https://docs.astral.sh/ruff/editors/settings/#configuration

dhruvmanila commented 2 months ago

We also support a subset of configuration directly in the editor: