Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extensions: Add Ruff extension #14198

Merged
merged 4 commits into from
Jul 20, 2024
Merged

extensions: Add Ruff extension #14198

merged 4 commits into from
Jul 20, 2024

Conversation

osiewicz
Copy link
Contributor

@osiewicz osiewicz commented Jul 11, 2024

To install Ruff extension, open command palette (cmd-shift-p on mac), search for zed: extensions and then in the search bar of the extensions view type in Ruff.
Release Notes:

  • Added extension for Ruff, an extremely fast Python linter and code formatter, written in Rust.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Jul 11, 2024
@rgbkrk rgbkrk added the python Python programming language support label Jul 16, 2024
@rgbkrk
Copy link
Member

rgbkrk commented Jul 16, 2024

Extension is working like a champ for me, once I disabled pyright in my config, since they overlap each other for formatting.

Update: This is not required anymore. We'll be shipping a way to pick the formatter out as well so the two LSPs don't collide.

@osiewicz osiewicz marked this pull request as ready for review July 19, 2024 22:28
@osiewicz osiewicz merged commit 1218a84 into main Jul 20, 2024
9 checks passed
@osiewicz osiewicz deleted the ruff-extension branch July 20, 2024 13:18
osiewicz added a commit to zed-industries/extensions that referenced this pull request Jul 20, 2024
@code-yeongyu
Copy link
Contributor

code-yeongyu commented Jul 21, 2024

Hi. I don't think the formatting is working properly. Is there anything else I need to set up? The LSP loaded well, and linter fixes are available, but formatting isn't working at all.

Screenshot 2024-07-21 at 16 09 13

@osiewicz
Copy link
Contributor Author

osiewicz commented Jul 21, 2024

That's still in progress. Right now there's no way to use a non-primary language server for formatting. It should be possible by our next update on Wednesday.

This however did not block releasing the extension itself, as setting up Ruff is independent from how Zed chooses a language server to format your code.

@code-yeongyu
Copy link
Contributor

That's still in progress. Right now there's no way to use a non-primary language server for formatting. It should be possible by our next update on Wednesday.

This however did not block releasing the extension itself, as setting up Ruff is independent from how Zed chooses a language server to format your code.

I didn't know there were such limitations. I currently have my zed config set up as follows to use the ruff format.

{
  "languages": {
    ..., // other languages
    "Python": {
      "format_on_save": {
        "external": {
          "command": "python",
          "arguments": [
            "-m",
            "ruff",
            "format",
            "-"
          ]
        }
      }
    }
  }
}

I think it can be introduced in the Python guide documents along with the extension. I made a PR for it: #14896

@osiewicz
Copy link
Contributor Author

Thank you so much!

@failable
Copy link

Can I use ruff with the default LSP? I can't get completions if I enable the ruff extension.

@osiewicz
Copy link
Contributor Author

osiewicz commented Jul 21, 2024

Yes, you can; Ruff doesn't provide completions, so by disabling pyright and using just ruff you won't get them.
You shouldn't have to put anything in your Python config, or you can do:

{
  "languages": {
    "Python": {
      "language_servers": ["pyright", "ruff"]
    }
}

(Note the lack of ! in front of pyright)
But that's the default, so you may as well not specify the language servers.

@failable
Copy link

@osiewicz Ah, thanks. After some restarting it seems to work now.

osiewicz pushed a commit that referenced this pull request Jul 22, 2024
…using Ruff extension (#14896)

Added documentation for #14198

I also suggest replacing format guides from `black` to `ruff` to unify
the tooling in the document.

Ruff is now widely used in the Python community, including
[fastapi](https://github.com/tiangolo/fastapi/blob/cd6e9db0653eabbf0fb14908c73939a11a131058/pyproject.toml#L213).
It's compatible with black but a lot faster.

Release Notes:

- N/A
@rgbkrk
Copy link
Member

rgbkrk commented Jul 22, 2024

Updated my comment from earlier so that people don't disable pyright.

@Fingel
Copy link

Fingel commented Jul 24, 2024

I'm a little confused as to what this extension actually does. The docs still say we need to add the external formatter config to our settings. Does this just install ruff if it isn't present?

@rgbkrk
Copy link
Member

rgbkrk commented Jul 24, 2024

It was showing up with Code Actions before:

image

There are some fixes coming to ruff that we need for the extension, which should be part of their release next week. We'll post updates as we hear more.

CharlesChen0823 pushed a commit to CharlesChen0823/zed that referenced this pull request Jul 29, 2024
…using Ruff extension (zed-industries#14896)

Added documentation for zed-industries#14198

I also suggest replacing format guides from `black` to `ruff` to unify
the tooling in the document.

Ruff is now widely used in the Python community, including
[fastapi](https://github.com/tiangolo/fastapi/blob/cd6e9db0653eabbf0fb14908c73939a11a131058/pyproject.toml#L213).
It's compatible with black but a lot faster.

Release Notes:

- N/A
@rgbkrk
Copy link
Member

rgbkrk commented Jul 30, 2024

@Fingel -- I just realized, you probably need to upgrade your version of ruff if you have it installed globally pip install --upgrade ruff (or however you originally installed it). Previous versions of ruff required a --preview flag that we no longer supply with the newer versions.

@Fingel
Copy link

Fingel commented Jul 30, 2024

@Fingel -- I just realized, you probably need to upgrade your version of ruff if you have it installed globally pip install --upgrade ruff (or however you originally installed it). Previous versions of ruff required a --preview flag that we no longer supply with the newer versions.

That makes sense!
What is the recommended way to install ruff for use with Zed? Install system wide, in the project's virtualenv, or do we let the zed extension install it? Thanks!

For what it's worth, I installed via Pipx. I am now seeing linting errors (nice!) and can format with the external command.

@jansol
Copy link
Contributor

jansol commented Jul 30, 2024

What is the recommended way to install ruff for use with Zed? Install system wide, in the project's virtualenv, or do we let the zed extension install it?

Whichever suits you best. System-wide is often the second easiest and safest option, but if you want to keep your system environment clean a per-project virtualenv should be a great option as well. (Or a Nix flake, if you are familiar with them.) The version available in your system package manager may not be as up to date as the other options, though.

Letting the zed extension download it is fine if the upstream project publishes prebuilt binaries for your platform on github (and you trust both them and the extension enough to just let the extension blindly download it). Personally I'm not a fan of this approach, but many people greatly appreciate the effortlessness of it, so as long as they understand the risks involved more power to them.

@Fingel
Copy link

Fingel commented Jul 30, 2024

What is the recommended way to install ruff for use with Zed? Install system wide, in the project's virtualenv, or do we let the zed extension install it?

Whichever suits you best. System-wide is often the second easiest and safest option, but if you want to keep your system environment clean a per-project virtualenv should be a great option as well. (Or a Nix flake, if you are familiar with them.) The version available in your system package manager may not be as up to date as the other options, though.

Letting the zed extension download it is fine if the upstream project publishes prebuilt binaries for your platform on github (and you trust both them and the extension enough to just let the extension blindly download it). Personally I'm not a fan of this approach, but many people greatly appreciate the effortlessness of it, so as long as they understand the risks involved more power to them.

I'd be fine with the Zed extension handling it all. But to get formatting to work (at least for now) you need to specify an executable. I'm not sure how smart Zed's Python handling is, but ideally it could run binaries from the project's Virtualenv. That doesn't seem to be the case (yet) so it's looking like global installation is the way.

adorabilis pushed a commit to adorabilis/zed-extensions that referenced this pull request Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement python Python programming language support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants