Different indent width for line indent vs hanging indent #8827
Replies: 2 comments
-
Hy @abridgland Supporting different styles of indentation would be possible by extending the formatter IR ( ruff/crates/ruff_formatter/src/printer/mod.rs Lines 940 to 947 in 14e65af So it would certainly be feasible but may add some slowdown to the printer. Ruff's formatter shouldn't support this option in my view because its philosophy is to be an opinionated formatter and supporting the above feature would require adding a new option which we try to avoid. |
Beta Was this translation helpful? Give feedback.
-
Thanks for such a detailed reply, Micha :) that all makes sense to me and I completely understand the push towards fewer options. If this were ever added then you can be sure you'd have at least one user who would happily use it, but I also appreciate that this is a fairly niche requirement. That being said, I do think that most people who use 2 space indents would appreciate this mixed 2/4 style if they tried it! |
Beta Was this translation helpful? Give feedback.
-
I'd be interested in the option to set a separate indent for hanging indentation (i.e. indentation used in a multi-line statement, typically within parentheses) compared to regular indentation (i.e. following statements that introduce indentation with a colon).
For instance this example has two spaces for regular indentation but 4 for hanging indentation:
or for an example of code that uses this style see the following:
https://github.com/google-deepmind/alphafold/blob/f715f016d875e2fd8515457af2b12277a716b9b0/run_alphafold.py#L175-L185
One motivation for this style is that the majority of indentation is the "regular" form, e.g. a for loop inside an if block inside a method definition inside a class definition. This means that if someone wants to save space by reducing indentation from 4 to 2 then this type of indentation is the one to change. However since hanging indentation is usually not deeply nested (apart from e.g. nested dictionaries) this type of indentation can be left at 4 spaces with minimal loss in space.
This has the added advantage of making it easy to skim a block of code and visually group statements that have been split over lines since these are identified by 4 space indents, rather than the two spaces that signify a nested code block.
I'd be very interested to hear any thoughts about the feasibility of this, even if you might not think that it's a great idea :)
Beta Was this translation helpful? Give feedback.
All reactions