Skip to content

Commit dfdb167

Browse files
authored
fix: columnar_menu create_string with quoted suggestions (#886)
* fix: columnar_menu create_string with quoted suggestions * cleanup * fix: same for the ide_menu
1 parent f12c4f1 commit dfdb167

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

src/menu/columnar_menu.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,23 @@ impl ColumnarMenu {
299299
use_ansi_coloring: bool,
300300
) -> String {
301301
if use_ansi_coloring {
302-
let match_len = self.working_details.shortest_base_string.len();
302+
// strip quotes
303+
let is_quote = |c: char| "`'\"".contains(c);
304+
let shortest_base = &self.working_details.shortest_base_string;
305+
let shortest_base = shortest_base
306+
.strip_prefix(is_quote)
307+
.unwrap_or(shortest_base);
308+
let match_len = shortest_base.len();
303309

304310
// Split string so the match text can be styled
305-
let (match_str, remaining_str) = suggestion.value.split_at(match_len);
311+
let skip_len = suggestion
312+
.value
313+
.chars()
314+
.take_while(|c| is_quote(*c))
315+
.count();
316+
let (match_str, remaining_str) = suggestion
317+
.value
318+
.split_at((match_len + skip_len).min(suggestion.value.len()));
306319

307320
let suggestion_style_prefix = suggestion
308321
.style
@@ -773,4 +786,16 @@ mod tests {
773786
"cursor should be at the end after completion"
774787
);
775788
}
789+
790+
#[test]
791+
fn test_menu_create_string() {
792+
// https://github.com/nushell/nushell/issues/13951
793+
let mut completer = FakeCompleter::new(&["おはよう", "`おはよう(`"]);
794+
let mut menu = ColumnarMenu::default().with_name("testmenu");
795+
let mut editor = Editor::default();
796+
797+
editor.set_buffer("おは".to_string(), UndoBehavior::CreateUndoPoint);
798+
menu.update_values(&mut editor, &mut completer);
799+
assert!(menu.menu_string(2, true).contains("`おは"));
800+
}
776801
}

src/menu/ide_menu.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,18 @@ impl IdeMenu {
512512
};
513513

514514
if use_ansi_coloring {
515-
let match_len = self
516-
.working_details
517-
.shortest_base_string
518-
.len()
519-
.min(string.len());
515+
// strip quotes
516+
let is_quote = |c: char| "`'\"".contains(c);
517+
let shortest_base = &self.working_details.shortest_base_string;
518+
let shortest_base = shortest_base
519+
.strip_prefix(is_quote)
520+
.unwrap_or(shortest_base);
521+
let match_len = shortest_base.len().min(string.len());
520522

521523
// Split string so the match text can be styled
522-
let (match_str, remaining_str) = string.split_at(match_len);
524+
let skip_len = string.chars().take_while(|c| is_quote(*c)).count();
525+
let (match_str, remaining_str) =
526+
string.split_at((match_len + skip_len).min(string.len()));
523527

524528
let suggestion_style_prefix = suggestion
525529
.style
@@ -1441,4 +1445,27 @@ mod tests {
14411445

14421446
menu.menu_string(500, true);
14431447
}
1448+
1449+
#[test]
1450+
fn test_menu_create_value_string() {
1451+
// https://github.com/nushell/nushell/issues/13951
1452+
let mut completer = FakeCompleter::new(&["おはよう", "`おはよう(`"]);
1453+
let mut menu = IdeMenu::default().with_name("testmenu");
1454+
menu.working_details = IdeMenuDetails {
1455+
cursor_col: 50,
1456+
menu_width: 50,
1457+
completion_width: 50,
1458+
description_width: 50,
1459+
description_is_right: true,
1460+
space_left: 50,
1461+
space_right: 50,
1462+
description_offset: 50,
1463+
shortest_base_string: String::new(),
1464+
};
1465+
let mut editor = Editor::default();
1466+
1467+
editor.set_buffer("おは".to_string(), UndoBehavior::CreateUndoPoint);
1468+
menu.update_values(&mut editor, &mut completer);
1469+
assert!(menu.menu_string(2, true).contains("`おは"));
1470+
}
14441471
}

0 commit comments

Comments
 (0)