Fix columnar menu completion corrupting the editor insertion point (#534)

* fix columnar menu issue and add test

* use saturating_add/sub
This commit is contained in:
Kornél Csernai 2023-02-01 07:46:18 -08:00 committed by GitHub
parent c658bea04f
commit 829a1a9f49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -667,7 +667,8 @@ impl Menu for ColumnarMenu {
line_buffer.replace_range(start..end, &value);
let mut offset = line_buffer.insertion_point();
offset += value.len().saturating_sub(end.saturating_sub(start));
offset = offset.saturating_add(value.len());
offset = offset.saturating_sub(end.saturating_sub(start));
line_buffer.set_insertion_point(offset);
editor.set_line_buffer(line_buffer, UndoBehavior::CreateUndoPoint);
}
@ -810,4 +811,25 @@ mod tests {
append_whitespace: false,
}
}
#[test]
fn test_menu_replace_backtick() {
// https://github.com/nushell/nushell/issues/7885
let mut completer = FakeCompleter::new(&["file1.txt", "file2.txt"]);
let mut menu = ColumnarMenu::default().with_name("testmenu");
let mut editor = Editor::default();
// backtick at the end of the line
editor.set_buffer("file1.txt`".to_string(), UndoBehavior::CreateUndoPoint);
menu.update_values(&mut editor, &mut completer);
menu.replace_in_buffer(&mut editor);
// After replacing the editor, make sure insertion_point is at the right spot
assert!(
editor.is_cursor_at_buffer_end(),
"cursor should be at the end after completion"
);
}
}