helix-editor / helix

A post-modern modal text editor.
https://helix-editor.com
Mozilla Public License 2.0
33.78k stars 2.51k forks source link

`indent-heuristic` that copies the leading whitespace from the previous line verbatim (instead of matching with tabs) #11951

Open Spiffyk opened 2 weeks ago

Spiffyk commented 2 weeks ago

There is a little gripe I have with indent-heuristic = simple while using the "tabs to indent, spaces to align" styling. For C functions, when parameters are long, I tend to do it like this:

static int my_func(int parameter1,
                   bool parameter2,
                   struct sth *parameter3)
/* ^^^^^^^^^^^^^^^
 * These are all spaces, to align the parameters with
 * the characters of the function header. */
{
        /* And the content is then indented with tabs. */
        /* snip */
}

With indent = { tab-width = 8, unit = "\t" }, when I add a new line to the parameters, Helix matches the previous indent by putting as many tabs as can fit, then padding the rest with spaces, like so (with . being spaces, <------> being tabs):

static int my_func(int parameter1,
...................bool parameter2,
<------><------>...bool new_parameter, /* added here with o */
...................struct mystruct *parameter3)
{
        /* snip */
}

It would be nice to have an indent-heuristic that simply copies the same exact whitespace onto the new line instead - where there are all tabs, copy tabs; where there are all spaces, copy spaces; if there is some weird mixture, just copy the mixture - i.e. to be even less clever about it.

If there is interest, I am willing to author a PR for this, be it by adding a new special heuristic, or changing the existing simple one.

daedroza commented 2 weeks ago

I am unsure if such a weird of way formatting the indent widths would actually work but if you like to disable, there is one PR here https://github.com/helix-editor/helix/pull/11920.

I think improving the heuristics like copying the algo from the NMAC427/guess-indent.nvim is much better but for your usecase, it doesn't seem like it would work.

Spiffyk commented 2 weeks ago

@daedroza To be clear, this issue is not so much about auto-detecting the indentation style.

Instead, it is about what happens when I create a new line. The indent-heuristic = simple is almost what I want - keeping the indent of the previous line - but instead of it trying to figure out the width, then padding that with its own mix of tabs and spaces, I want it to be basically even dumber and just copy verbatim whatever leading whitespace was on the previous line.

And as for the indent option in language.toml (or the auto-detected indent), I want that to only apply to me pressing Tab, <, or >.