Skip to content

Commit cc07b70

Browse files
Fix bracket coloring for flow control keywords (#675)
Exclude flow control keywords like `if`, `while`, `until`, `for`, `case` and their closing counterparts `fi`, `done`, `esac` from being assigned a bracket depth, which means they won't get rainbow coloring that is intended for real brackets. --- *PR created automatically by Jules for task [1692456607942179532](https://jules.google.com/task/1692456607942179532) started by @HalFrgrd* Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 8073518 commit cc07b70

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

src/app/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,14 +1218,10 @@ impl<'a> App<'a> {
12181218
match handle.receiver.try_recv() {
12191219
Ok(Some((builder, elapsed))) => {
12201220
// Take ownership of wuc_substring from the waiting state.
1221-
let wuc =
1222-
match std::mem::replace(&mut self.content_mode, ContentMode::Normal) {
1223-
ContentMode::TabCompletionWaiting {
1224-
wuc_substring,
1225-
..
1226-
} => wuc_substring,
1227-
_ => unreachable!(),
1228-
};
1221+
let wuc = match std::mem::replace(&mut self.content_mode, ContentMode::Normal) {
1222+
ContentMode::TabCompletionWaiting { wuc_substring, .. } => wuc_substring,
1223+
_ => unreachable!(),
1224+
};
12291225
self.finish_tab_complete(builder, wuc, elapsed);
12301226
self.on_possible_buffer_change();
12311227
return true;

src/app/tab_completion.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,8 @@ impl App<'_> {
10061006

10071007
let wuc_substring = completion_context.word_under_cursor.clone();
10081008

1009-
let (tx, rx) = std::sync::mpsc::channel::<Option<(ActiveSuggestionsBuilder, std::time::Duration)>>();
1009+
let (tx, rx) =
1010+
std::sync::mpsc::channel::<Option<(ActiveSuggestionsBuilder, std::time::Duration)>>();
10101011

10111012
let completion_context_owned = completion_context.into_owned();
10121013

src/dparser.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,19 @@ impl DParser {
480480
{
481481
let depth = nestings.len();
482482
self.tokens[idx].annotations.opening = Some(OpeningState::Unmatched);
483-
self.tokens[idx].annotations.bracket_depth = Some(depth);
483+
484+
if matches!(
485+
token.kind,
486+
TokenKind::If
487+
| TokenKind::Case
488+
| TokenKind::For
489+
| TokenKind::While
490+
| TokenKind::Until
491+
) {
492+
// Do not color keywords like bracket tokens
493+
} else {
494+
self.tokens[idx].annotations.bracket_depth = Some(depth);
495+
}
484496

485497
if self.current_command_range.is_none() {
486498
self.current_command_range = Some(idx..=idx);
@@ -515,7 +527,15 @@ impl DParser {
515527
opening_idx,
516528
is_auto_inserted: false,
517529
});
518-
self.tokens[idx].annotations.bracket_depth = Some(depth);
530+
531+
if matches!(
532+
token.kind,
533+
TokenKind::Fi | TokenKind::Done | TokenKind::Esac
534+
) {
535+
// Do not color keywords like bracket tokens
536+
} else {
537+
self.tokens[idx].annotations.bracket_depth = Some(depth);
538+
}
519539

520540
let current_command_range_contains_cursor =
521541
cursor_byte_pos.is_some_and(|pos| {
@@ -2426,3 +2446,22 @@ mod tests {
24262446
}
24272447
}
24282448
}
2449+
2450+
#[cfg(test)]
2451+
mod test_keyword_bracket_color {
2452+
use super::*;
2453+
2454+
#[test]
2455+
fn test_keywords_have_no_bracket_depth() {
2456+
let mut parser = DParser::from("if true; then echo hi; while true; do echo; done; fi");
2457+
parser.walk_to_end();
2458+
for token in parser.tokens() {
2459+
if matches!(
2460+
token.token.kind,
2461+
TokenKind::If | TokenKind::Fi | TokenKind::While | TokenKind::Done
2462+
) {
2463+
assert_eq!(token.annotations.bracket_depth, None);
2464+
}
2465+
}
2466+
}
2467+
}

0 commit comments

Comments
 (0)