rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Feature Request: Change and add new configurations to properly support tabs_spaces when using "hard tabs"

Open BenjaminBrienen opened this issue 2 years ago • 4 comments

Please refer to the linked project below

2 tabs of indentation are being interpreted as more than 2 characters/width, causing rustfmt to think that the line is overflowing. On the line in main(), 2 * tab_spaces + 26 = 110 > max_width, and error reporting is turned on, so it reports panicking in stdout. (26 is the width of the statement) Why would someone set such a ridiculous value for tabs_spaces? One reason might be to make it super obvious when the wrong indentation is being used if for some reason rustfmt fails to work properly in that respect.

rustfmt would panic on a more reasonable value in extremely nested scopes or longer statements as long as max_width is exceeded. This should not be an error because tabs_spaces does not inform how many spaces a tab is rendered as (its width), especially since "hard tabs" aren't a fixed width. It should only describe how many spaces to use for indentation when the indentation style is "soft". Tabs should be treated as one character - or a configurable number of characters - for the purposes of checking against max_width. tab_spaces should only be used in the process of overwriting the current line-leading indentation (including doc comments) The whole point of using "hard tabs" is allowing the developer to configure their visual indentation size preferences as an editor setting instead of enforcing a fixed width by using a set number of spaces in the source file. Hard vs soft tabs are fundementally different, and should be treated as such by a formatter.

The needed fixes are: Clarity: tab_spaces should be renamed to soft_indentation_size. Clarity: hard_tabs should be renamed to use_hard_indentation or intendation_style (with options "hard" or "soft"). Implementation: soft_indentation_size should only come into effect if use_hard_indentation/intendation_style is true/"hard". New: Create a config called hard_indentation_size to allow developers to enforce the current behavior.

Minimal example here: https://github.com/BenjaminBrienen/rustfmt-crash

BenjaminBrienen avatar Sep 21 '22 03:09 BenjaminBrienen

Is this not a duplicate of #5431?

ytmimi avatar Sep 21 '22 03:09 ytmimi

Do you see tabs_spaces or use_hard_indentation referred to anywhere in #4968? I don't know why you marked this as a duplicate the first time, and I thought this issue put the problem in more clear terms for you to understand. This could even be considered a feature request, so how can it be a duplicate of a bug in annotated_snippets? The line of code in the minimal example in that issue doesn't even use tabs for indentation! If you want to reopen #5431 I'd be glad to move my post there.

BenjaminBrienen avatar Sep 21 '22 03:09 BenjaminBrienen

I don't see tab_spaces or hard_tabs explicitly mentioned, but the output from both panics seems similar and the comment https://github.com/rust-lang/rustfmt/issues/4968#issuecomment-949461340 leads me to believe that the original open issue and this one are the same.

Please let me know if you disagree with my assessment, and if so could you help me understand how these issues are different?

ytmimi avatar Sep 21 '22 03:09 ytmimi

If this is a feature request could you update the title of the issue to reflect that

ytmimi avatar Sep 21 '22 03:09 ytmimi