languages: Fix Bash indentation issues with multi-cursors, newlines, and keyword outdenting (#35116)

Closes #34390

This PR fixes several Bash indentation issues:

- Adding indentation or comment using multi cursors no longer breaks
relative indentation
- Adding newline now places the cursor at the correct indent
- Typing a valid keyword triggers context-aware auto outdent

It also adds tests for all of them.

Release Notes:

- Fixed various issues with handling indentation in Bash.
This commit is contained in:
Smit Barmase 2025-07-26 04:58:10 +05:30 committed by GitHub
parent 07252c3309
commit 43d0aae617
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 459 additions and 25 deletions

View file

@ -18,17 +18,20 @@ brackets = [
{ start = "in", end = "esac", close = false, newline = true, not_in = ["comment", "string"] },
]
### WARN: the following is not working when you insert an `elif` just before an else
### example: (^ is cursor after hitting enter)
### ```
### if true; then
### foo
### elif
### ^
### else
### bar
### fi
### ```
increase_indent_pattern = "(^|\\s+|;)(do|then|in|else|elif)\\b.*$"
decrease_indent_pattern = "(^|\\s+|;)(fi|done|esac|else|elif)\\b.*$"
# make sure to test each line mode & block mode
auto_indent_using_last_non_empty_line = false
increase_indent_pattern = "^\\s*(\\b(else|elif)\\b|([^#]+\\b(do|then|in)\\b)|([\\w\\*]+\\)))\\s*$"
decrease_indent_patterns = [
{ pattern = "^\\s*elif\\b.*", valid_after = ["if", "elif"] },
{ pattern = "^\\s*else\\b.*", valid_after = ["if", "elif", "for", "while"] },
{ pattern = "^\\s*fi\\b.*", valid_after = ["if", "elif", "else"] },
{ pattern = "^\\s*done\\b.*", valid_after = ["for", "while"] },
{ pattern = "^\\s*esac\\b.*", valid_after = ["case"] },
{ pattern = "^\\s*[\\w\\*]+\\)\\s*$", valid_after = ["case_item"] },
]
# We can't use decrease_indent_patterns simply for elif, because
# there is bug in tree sitter which throws ERROR on if match.
#
# This is workaround. That means, elif will outdents with despite
# of wrong context. Like using elif after else.
decrease_indent_pattern = "(^|\\s+|;)(elif)\\b.*$"

View file

@ -1,12 +1,12 @@
(function_definition
"function"?
body: (
_
"{" @start
"}" @end
)) @indent
(_ "[" "]" @end) @indent
(_ "{" "}" @end) @indent
(_ "(" ")" @end) @indent
(array
"(" @start
")" @end
) @indent
(function_definition) @start.function
(if_statement) @start.if
(elif_clause) @start.elif
(else_clause) @start.else
(for_statement) @start.for
(while_statement) @start.while
(case_statement) @start.case
(case_item) @start.case_item