rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Make indentation (spaces vs tabs, tab width) configurable

Open 0x7CFE opened this issue 5 years ago • 16 comments

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

0x7CFE avatar Apr 22 '19 05:04 0x7CFE

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

matklad avatar Apr 22 '19 07:04 matklad

This also happens for "Fill match arms".

Keavon avatar May 25 '20 21:05 Keavon

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.

0x7CFE avatar Jun 22 '20 05:06 0x7CFE

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?

Timmmm avatar Oct 19 '20 14:10 Timmmm

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

matklad avatar Oct 19 '20 14:10 matklad

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.

flodiebold avatar Jun 07 '21 10:06 flodiebold

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

tehsunnliu avatar Nov 09 '21 11:11 tehsunnliu

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.

bjorn3 avatar Nov 09 '21 11:11 bjorn3

hard_tabs and tab_spaces could be read from rustfmt.toml

WilliamVenner avatar Jan 21 '22 16:01 WilliamVenner

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

bjorn3 avatar Jan 21 '22 17:01 bjorn3

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!

deanfra avatar Feb 02 '22 22:02 deanfra

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.

Barugon avatar Apr 14 '22 18:04 Barugon

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?

Dessix avatar Aug 06 '22 00:08 Dessix

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

Timmmm avatar Aug 06 '22 05:08 Timmmm

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 https://github.com/rust-lang/rust-analyzer/issues/1665)

Veykril avatar Aug 06 '22 06:08 Veykril

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

codemaster138 avatar Sep 05 '22 14:09 codemaster138

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

sslime336 avatar Dec 02 '22 02:12 sslime336

@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 avatar Dec 02 '22 10:12 lnicola

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.

lnicola avatar Dec 02 '22 12:12 lnicola

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 :)

sslime336 avatar Dec 02 '22 12:12 sslime336

Is anyone working on a solution right now?

Timoyoungster avatar Jul 01 '23 03:07 Timoyoungster

@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.

jacobaagaardbennike avatar Sep 26 '23 07:09 jacobaagaardbennike

@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.

Timoyoungster avatar Sep 26 '23 21:09 Timoyoungster