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

"Format selections" with external formatter formats entire file #25343

Open
nt8r opened this issue Feb 21, 2025 · 2 comments
Open

"Format selections" with external formatter formats entire file #25343

nt8r opened this issue Feb 21, 2025 · 2 comments

Comments

@nt8r
Copy link

nt8r commented Feb 21, 2025

Summary

When an external formatter is configured, attempting to format only selected lines instead formats the entire file.

To reproduce:
Create a new project and place these contents in the project settings.json:

{
    "languages": {
        "Rust": {
            "formatter": [
                {
                    "external": {
                        "command": "rustfmt",
                        "arguments": []
                    }
                }
            ]
        }
    }
}

Then edit a Rust file and place this content in it:

fn main() {
    println!("Hello, world!");

}


fn main2() {
    println!("Hello, world!");

}

Select the latter function only and run "Format selections". The entire file is formatted, instead of just the latter function.

If the Rust language server (rust-analyzer) is used instead, Zed cancels the operation with an error message saying the LSP does not support formatting selections. This is a reasonable behavior that I would also expect for external formatters, if Zed doesn't intend to implement its own logic to limit the scope of formatting performed by an exernal formatter.

Zed Version and System Specs

Zed: v0.173.10 (Zed)
OS: Linux Wayland arch unknown
Memory: 15.3 GiB
Architecture: x86_64
GPU: Intel(R) Iris(R) Xe Graphics (TGL GT2) || Intel open-source Mesa driver || Mesa 24.3.4-arch1.1

@SomeoneToIgnore
Copy link
Contributor

IIRC, rustfmt is not able to format partial formats, so the example is quite bad.
Does it work with something that's able to format partially?

@SomeoneToIgnore SomeoneToIgnore added the awaiting info Issue that needs more information from the user label Feb 21, 2025
@nt8r
Copy link
Author

nt8r commented Feb 21, 2025

IIRC, rustfmt is not able to format partial formats, so the example is quite bad. Does it work with something that's able to format partially?

There is no standardized interface for external formatters to format partially. This should either fail with an error message, or Zed would have to implement a way to limit the scope of formatting when using an external formatter that does not natively support partial formatting. An error message is better than doing the wrong thing.

@SomeoneToIgnore SomeoneToIgnore removed the awaiting info Issue that needs more information from the user label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants