Unify usage of indent_style - #5918
Conversation
There was a problem hiding this comment.
If I understand this change correctly that means that the number of spaces in the indent unit now always corresponds to the tabwidth.
In other words:
- If my document contains both spaces and tabs
- I set the indent width to 4
- Then all tabs are rendered with a width of 4
This does not correspond to the current config whichs allows setting tab-width separately from the unit field.
So for example this change would break using 4 spaces for indentation and a tab-width of two:
indent-style = { unit = " ", tab-width=2 }
Is unifying these two really necessary to fix the underling issue here?
From my understanding, the correct fix would be to have to separate parameters for indent_level_for_line
the tab-width which is used when \t is encountered and the indent-width winch is used for calculating the indent level
|
Thanks for the super-fast review! You're right, I hadn't really thought about the distinction between I also removed the |
pascalkuthe
left a comment
There was a problem hiding this comment.
Yeah it's definitely a bit subtle 👍 People usually stick to a single indent style they tend to o think about indent_width and tab_width as the same thing.
The changes to the autodetection now LGTM 👍
However this PR regresses some things like config reloading. I think a simpler fix is possible by simply adding a new indent_width function instead of actually changing IndentStyle. I added suggestion below.
| @@ -1122,13 +1130,6 @@ impl Document { | |||
| self.syntax.as_ref() | |||
| } | |||
|
|
|||
There was a problem hiding this comment.
With this config removed tab-width is not updated on :config-reload anymore. I actually think that we should keep this function around and not make IndentStyle a struct at all.
Rather we should add a second function to the document to compute the indent_width and then use that in the right places.
| /// Tab size in columns. | |
| pub fn tab_width(&self) -> usize { | |
| self.language_config() | |
| .and_then(|config| config.indent.as_ref()) | |
| .map_or(4, |config| config.tab_width) // fallback to 4 columns | |
| } | |
| /// Indentation size in columns | |
| pub fn indent_width(&self) -> usize { | |
| match self.indent_style{ | |
| IndentStyle::Tab => self.tab_width(), | |
| IndentStyle::Spaces(width) => width as usize | |
| } | |
| } |
e92990f to
594b893
Compare
|
Thanks for all the feedback! |
Previously, we used sometimes
doc.indent_styleand sometimesdoc.language_config.indent. This caused some issues if they differ (e.g. if a non-default indent style is auto-detected in a document or manually set). For example, in a markdown file, setting:indent-style 8and then continuously pressing enter (starting on an indented line) increases the indent each time. This happens because the indent_style used to detect the indentation level of an existing line is different from the indent_style used to convert an indentation level to a string. The same issue occurs in any language without indent queries.With these changes,
language_config.indentis only used to initializedoc.indent_styleand afterwards ignored. Resolves #2236 and #3406.