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

Respect rustfmt indentation settings #1191

Open
0x7CFE opened this issue Apr 22, 2019 · 25 comments
Open

Respect rustfmt indentation settings #1191

0x7CFE opened this issue Apr 22, 2019 · 25 comments
Labels
A-config configuration A-formatting formatting of r-a output/formatting on save C-enhancement Category: enhancement E-hard S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@0x7CFE
Copy link

0x7CFE commented Apr 22, 2019

The code in the if let block was indented by tabs, but "replace with match" suggestion mistakenly indents match arms with spaces.

@matklad matklad added the E-hard label Apr 22, 2019
@matklad
Copy link
Member

matklad commented Apr 22, 2019

Yeah, we unfortunately hard-code four space indentation for now.

@Keavon
Copy link

Keavon commented May 25, 2020

This also happens for "Fill match arms".

@matklad matklad changed the title "Replace with match" does not respect indentation style Make indentation (spaces vs tabs, tab width) configurable May 26, 2020
@0x7CFE
Copy link
Author

0x7CFE commented Jun 22, 2020

Out of curiosity, can https://github.com/udoprog/genco be used to generate the code on behalf of RA? It is reportedly whitespace aware and configurable.

@Timmmm
Copy link
Contributor

Timmmm commented Oct 19, 2020

This is kind of infuriating. What is involved in fixing it? Can I just make the 4 configurable here and here and add some kind of flag to tell it to use tabs instead?

@matklad
Copy link
Member

matklad commented Oct 19, 2020

Can I just make the 4 configurable here and here and add some kind of flag to tell it to use tabs instead?

Probably not, editing code should be agnostic about indentation configuration. I think the proper way to fix this is to remove manual indentation entirely in favor of generic indenter: #4182

@lnicola lnicola added the S-unactionable Issue requires feedback, design decisions or is blocked on other work label Dec 21, 2020
@flodiebold
Copy link
Member

Just as a note, the fact that this is marked unactionable doesn't mean we don't want to fix it, or that no-one is allowed to work on it. It just means we don't see a direct way of making progress on it, because it depends on another issue.

If you care about this and think there is a more simple fix for this, feel free to implement it and make a PR. It just needs to work and be maintainable; it should not make it harder to write assists.

@tehsunnliu
Copy link

Hi, any hint where this is hardcoded? and what is causing the problem?

@bjorn3
Copy link
Member

bjorn3 commented Nov 9, 2021

Every snippet literally contains the precise amount of spaces to insert. Changing the indentation currently requires updating all snippets AFAIK. Fixing this issue will require some way to either replace the fixed indentation of the snippets or be able to write the snippets without fixed indentation I think.

@WilliamVenner
Copy link

hard_tabs and tab_spaces could be read from rustfmt.toml

@bjorn3
Copy link
Member

bjorn3 commented Jan 21, 2022

It should probably be read from the editor configuration rather than rustfmt.toml.

@deanfra
Copy link

deanfra commented Feb 2, 2022

Taking a hint from the above comment, I added my preferred tab_spaces setting to rustfmt.toml and Rust Analyzer seems to abide by it in VS Code. Not sure if that's expected behaviour but I'm happy for now!

@Barugon
Copy link

Barugon commented Apr 14, 2022

This should be passed to rustfmt on the command line with --config tab_spaces=[tab-size value from vscode] as well as any other applicable options.

@Dessix
Copy link

Dessix commented Aug 6, 2022

While formatting via rustfmt works with tab_spaces=#, the remaining focuses of this issue appear to be that generated code and snippets are hardcoded to 4 spaces.

Items remaining to solve before this can be closed:

  • 1: as @bjorn3 mentioned, snippets require a means of whitespace placement that is not of a fixed width
    • Can this be addressed directly? Has this state of affairs changed since 2021?
    • Can this be worked around, perhaps by running rustfmt on the snippet after emplacement?
  • 2: Generated code assumes a hardcoded 4-spaced indentation and default rustfmt formatting
    • Can we simply invoke rustfmt on the selection within the current workspace context? Code generation lacks an interactive parameterization step, so doing this here may be easier than with the static snippets in (1).

Lastly, as tehsunnliu asked, do we know where all of these are hardcoded, such that we can build and evaluate a plan for fixing it?

@Timmmm
Copy link
Contributor

Timmmm commented Aug 6, 2022

I already linked where it is hardcoded. See my comment above and @matklad 's reply - which to be honest I didn't really understand.

@Veykril
Copy link
Member

Veykril commented Aug 6, 2022

Can this be addressed directly? Has this state of affairs changed since 2021?

It has not no

Can this be worked around, perhaps by running rustfmt on the snippet after emplacement?

In the general case, not really no. The text some assists create aren't really single entities that can be formatted by rustfmt (hence the need for our own syntax formatter #1665)

@codemaster138
Copy link

Taking a hint from the above comment, I added my preferred tab_spaces setting to rustfmt.toml and Rust Analyzer seems to abide by it in VS Code. Not sure if that's expected behaviour but I'm happy for now!

Ever since I updated to rustc 1.63.0 yesterday, this doesn't work anymore

@Veykril Veykril added the A-formatting formatting of r-a output/formatting on save label Nov 11, 2022
@sslime336
Copy link

sslime336 commented Dec 2, 2022

Here is my config in VSCode, edited in settings.json

"[rust]": {
  "editor.defaultFormatter": "rust-lang.rust-analyzer",
  "editor.tabSize": 2
},
"rust-analyzer.rustfmt.extraArgs": ["--config", "tab_spaces=2"],

My rust version is 1.67.0-nightly (73c9eaf21 2022-11-07)
But when I start a new proj using cargo new xxx, it is still 4 spaces by default...
Well, I'm going to force myself to adapt 4 spaces width in order to not change the default setting every time I create a new proj :P

@lnicola
Copy link
Member

lnicola commented Dec 2, 2022

@sslime336 you can configure it just once, but it might annoy people if you ever try to contribute to projects without a rustfmt.toml.

@lnicola
Copy link
Member

lnicola commented Dec 2, 2022

Don't have such fine-grained control, but Code has an editor.stickyTabStops option which snaps the cursor to tab stops, even when you're using spaces for indentation. I don't think you can configure it to show two instead of four spaces, though.

@sslime336
Copy link

sslime336 commented Dec 2, 2022

You are right. My muscle memory makes me didn't even notice that I've pressed the formatting buttons. Sorry. 😂

Btw, thanks for your reminding :)

@Veykril Veykril added the C-enhancement Category: enhancement label Feb 9, 2023
@Timoyoungster
Copy link

Is anyone working on a solution right now?

@randomaxess
Copy link

randomaxess commented Sep 26, 2023

@Timoyoungster there is a sollution. It's to add a rustfmt.toml file to your project with the following line:

tab_spaces=2

Then if you have the following in your VSC settings file:

 "[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer",
    "editor.formatOnSave": true
  },

It will work for not only you, but for anyone working on your project.

@Timoyoungster
Copy link

@Timoyoungster there is a sollution. It's to add a rustfmt.toml file to your project with the following line:

Thank you, I will try that for now.

@jxmked

This comment was marked as off-topic.

@vn971
Copy link

vn971 commented Feb 1, 2025

Can we please change the title from

make indentation ... configurable

to

obey indentation definitions from rustfmt.toml

? I don't think we want more ways to configure things. We just want compliance of Rust tools with... themselves.

@lnicola lnicola changed the title Make indentation (spaces vs tabs, tab width) configurable Respect rustfmt indentation settings Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-config configuration A-formatting formatting of r-a output/formatting on save C-enhancement Category: enhancement E-hard S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests