Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retroactively update indentation #6044

Open
Triton171 opened this issue Feb 18, 2023 · 3 comments
Open

Retroactively update indentation #6044

Triton171 opened this issue Feb 18, 2023 · 3 comments
Labels
A-indent Area: Indentation C-enhancement Category: Improvements

Comments

@Triton171
Copy link
Contributor

Currently, we run indentation queries only when a new line is being inserted. However, other editors can in some cases update the indentation while typing, for example in C++ switch statements:

switch (var) {
    case 0: // As soon as this can be recognized as a case label, it should be outdented
}

in python if statements:

if condition:
    do_smth()
    else: # As soon as this can be recognized as an else statement, it should be outdented

or when inserting a closing curly brace } which should be outdented. CLion (basically IntelliJ for C++ development) even correctly indents multiple lines after surrounding them with curly braces (or adding something like an if statement around it):

{ // Adding these curly braces causes the entire block to be correctly indented
do_smth();
do_smth_on_multiple_lines();
}

Implementing features like this in helix would require us to run some kind of indent query on every edit (or at least every time the syntax tree changes significantly). Because of that (and because it has the potential to be very annoying if the updated indentation is incorrect), this kind of auto-indent should probably off by default but I still think it would be very nice to support it in helix. As far as I see, there are a few different ways it could be implemented:

  1. We could introduce a new kind of indent query "update-indent.scm" which just specifies situations in which the indent should be updated. This could either include the required update to the indentation (e.g. decrease the indentation by 1) or simply rerun the normal indent queries for the specified parts of the syntax trees. This is probably the easiest solution but it would require additional queries for all languages, for which we want to support this feature.

  2. A different option would be to simply use the existing indent queries. The advantage to this is that we don't need additional queries. However, there are obviously some cases where the indent shouldn't be updated and we would need to exclude them: The indent computed by our heuristic may not match the indent that the user wants (either because there is a bug in the heuristic or because the file uses a non-conventional indentation style). Incorrectly updating the indentation once may be fine but if the user changes it back to what it was before, the indentation shouldn't be updated again.

    One possible heuristic for this could be to only update the indentation when a new node is inserted in the syntax tree (we may also want to require that this node doesn't contain any errors). This way, the indentation is only updated once and the user is able to correct it. Additionally, it might be a good idea to check that the indentation heuristic matches the actual indentation for surrounding code. If that's not the case, it's not very likely that the heuristic can correctly predict the indentation for the part of the code that changed.

These are only some ideas and I haven't started any implementation, so let me know your thoughts on this. Also, please tell me if this is not something that helix should include out-of-the-box.

@Triton171 Triton171 added the C-enhancement Category: Improvements label Feb 18, 2023
@goyalyashpal

This comment was marked as off-topic.

@kirawi

This comment was marked as resolved.

@kirawi kirawi added the A-tree-sitter Area: Tree-sitter label Feb 19, 2023
@goyalyashpal

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-indent Area: Indentation C-enhancement Category: Improvements
Projects
None yet
Development

No branches or pull requests

4 participants