From a4809f2e68cc1ae119134b081fcd5819b5d71ec9 Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Mon, 15 Aug 2022 00:35:37 +0200 Subject: [PATCH] Update reedline to improved undo-system (#6326) * Update after Reedline API update * Remove references to deleted `ReedlineEvent::ActionHandler` * Update `DescriptionMenu` implementation for the new `Menu` trait API changes that work on `Editor` rather than `LineBuffer` objects * Update reedline Includes nushell/reedline#460 Co-authored-by: Ben Parks --- Cargo.lock | 2 +- crates/nu-cli/src/menus/description_menu.rs | 57 +++++++++++---------- crates/nu-cli/src/reedline_config.rs | 1 - 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 493bf884b7..b80789ef37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3924,7 +3924,7 @@ dependencies = [ [[package]] name = "reedline" version = "0.9.0" -source = "git+https://github.com/nushell/reedline.git?branch=main#a406bfc6621f01805a0a80cd520ac59c128aec23" +source = "git+https://github.com/nushell/reedline.git?branch=main#174255535dccb0fea5e2d0e055fc5884830df898" dependencies = [ "chrono", "crossterm 0.24.0", diff --git a/crates/nu-cli/src/menus/description_menu.rs b/crates/nu-cli/src/menus/description_menu.rs index 03ef4b71d7..ceec536a69 100644 --- a/crates/nu-cli/src/menus/description_menu.rs +++ b/crates/nu-cli/src/menus/description_menu.rs @@ -1,8 +1,8 @@ use { nu_ansi_term::{ansi::RESET, Style}, reedline::{ - menu_functions::string_difference, Completer, LineBuffer, Menu, MenuEvent, MenuTextStyle, - Painter, Suggestion, + menu_functions::string_difference, Completer, Editor, Menu, MenuEvent, MenuTextStyle, + Painter, Suggestion, UndoBehavior, }, }; @@ -459,7 +459,7 @@ impl Menu for DescriptionMenu { fn can_partially_complete( &mut self, _values_updated: bool, - _line_buffer: &mut LineBuffer, + _editor: &mut Editor, _completer: &mut dyn Completer, ) -> bool { false @@ -481,19 +481,21 @@ impl Menu for DescriptionMenu { } /// Updates menu values - fn update_values(&mut self, line_buffer: &mut LineBuffer, completer: &mut dyn Completer) { + fn update_values(&mut self, editor: &mut Editor, completer: &mut dyn Completer) { if self.only_buffer_difference { if let Some(old_string) = &self.input { - let (start, input) = string_difference(line_buffer.get_buffer(), old_string); + let (start, input) = string_difference(editor.get_buffer(), old_string); if !input.is_empty() { self.reset_position(); self.values = completer.complete(input, start); } } } else { - let trimmed_buffer = line_buffer.get_buffer().replace('\n', " "); - self.values = - completer.complete(trimmed_buffer.as_str(), line_buffer.insertion_point()); + let trimmed_buffer = editor.get_buffer().replace('\n', " "); + self.values = completer.complete( + trimmed_buffer.as_str(), + editor.line_buffer().insertion_point(), + ); self.reset_position(); } } @@ -502,7 +504,7 @@ impl Menu for DescriptionMenu { /// collected from the completer fn update_working_details( &mut self, - line_buffer: &mut LineBuffer, + editor: &mut Editor, completer: &mut dyn Completer, painter: &Painter, ) { @@ -560,13 +562,13 @@ impl Menu for DescriptionMenu { match event { MenuEvent::Activate(_) => { self.reset_position(); - self.input = Some(line_buffer.get_buffer().to_string()); - self.update_values(line_buffer, completer); + self.input = Some(editor.get_buffer().to_string()); + self.update_values(editor, completer); } MenuEvent::Deactivate => self.active = false, MenuEvent::Edit(_) => { self.reset_position(); - self.update_values(line_buffer, completer); + self.update_values(editor, completer); self.update_examples() } MenuEvent::NextElement => { @@ -627,27 +629,28 @@ impl Menu for DescriptionMenu { } /// The buffer gets replaced in the Span location - fn replace_in_buffer(&self, line_buffer: &mut LineBuffer) { + fn replace_in_buffer(&self, editor: &mut Editor) { if let Some(Suggestion { value, span, .. }) = self.get_value() { - let start = span.start.min(line_buffer.len()); - let end = span.end.min(line_buffer.len()); + let start = span.start.min(editor.line_buffer().len()); + let end = span.end.min(editor.line_buffer().len()); - let string_len = if let Some(example_index) = self.example_index { - let example = self - .examples + let replacement = if let Some(example_index) = self.example_index { + self.examples .get(example_index) - .expect("the example index is always checked"); - - line_buffer.replace(start..end, example); - example.len() + .expect("the example index is always checked") } else { - line_buffer.replace(start..end, &value); - value.len() + &value }; - let mut offset = line_buffer.insertion_point(); - offset += string_len.saturating_sub(end.saturating_sub(start)); - line_buffer.set_insertion_point(offset); + editor.edit_buffer( + |lb| { + lb.replace_range(start..end, replacement); + let mut offset = lb.insertion_point(); + offset += lb.len().saturating_sub(end.saturating_sub(start)); + lb.set_insertion_point(offset); + }, + UndoBehavior::CreateUndoPoint, + ); } } diff --git a/crates/nu-cli/src/reedline_config.rs b/crates/nu-cli/src/reedline_config.rs index d9f96083d3..dfc13e4eb1 100644 --- a/crates/nu-cli/src/reedline_config.rs +++ b/crates/nu-cli/src/reedline_config.rs @@ -814,7 +814,6 @@ fn event_from_record( ) -> Result { let event = match name { "none" => ReedlineEvent::None, - "actionhandler" => ReedlineEvent::ActionHandler, "clearscreen" => ReedlineEvent::ClearScreen, "clearscrollback" => ReedlineEvent::ClearScrollback, "historyhintcomplete" => ReedlineEvent::HistoryHintComplete,