From a88de9d3454eb20d2d9c6cf282949c2fd53f7a81 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Wed, 25 Dec 2024 20:20:03 +0100 Subject: [PATCH] Remove unused data from autosuggestion cache --- src/reader.rs | 59 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 32d0e2f3f..55728c7c8 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -4300,36 +4300,20 @@ impl<'a> Reader<'a> { } } -/// The result of an autosuggestion computation. #[derive(Default)] struct Autosuggestion { - // The text to use, as an extension of the command line. + // The text to use, as an extension/replacement of the command line. text: WString, - // The string which was searched for. - search_string: WString, - - // The list of completions which may need loading. - needs_load: Vec, - // Whether the autosuggestion should be case insensitive. // This is true for file-generated autosuggestions, but not for history. icase: bool, } impl Autosuggestion { - fn new(text: WString, search_string: WString, icase: bool) -> Self { - Self { - text, - search_string, - needs_load: vec![], - icase, - } - } // Clear our contents. fn clear(&mut self) { self.text.clear(); - self.search_string.clear(); } // Return whether we have empty text. @@ -4338,6 +4322,35 @@ impl Autosuggestion { } } +/// The result of an autosuggestion computation. +#[derive(Default)] +struct AutosuggestionResult { + autosuggestion: Autosuggestion, + + // The string which was searched for. + search_string: WString, + + // The list of completions which may need loading. + needs_load: Vec, +} + +impl std::ops::Deref for AutosuggestionResult { + type Target = Autosuggestion; + fn deref(&self) -> &Self::Target { + &self.autosuggestion + } +} + +impl AutosuggestionResult { + fn new(text: WString, search_string: WString, icase: bool) -> Self { + Self { + autosuggestion: Autosuggestion { text, icase }, + search_string, + needs_load: vec![], + } + } +} + // Returns a function that can be invoked (potentially // on a background thread) to determine the autosuggestion fn get_autosuggestion_performer( @@ -4345,13 +4358,13 @@ fn get_autosuggestion_performer( search_string: WString, cursor_pos: usize, history: Arc, -) -> impl FnOnce() -> Autosuggestion { +) -> impl FnOnce() -> AutosuggestionResult { let generation_count = read_generation_count(); let vars = parser.vars().snapshot(); let working_directory = parser.vars().get_pwd_slash(); move || { assert_is_background_thread(); - let nothing = Autosuggestion::default(); + let nothing = AutosuggestionResult::default(); let ctx = get_bg_context(&vars, generation_count); if ctx.check_cancel() { return nothing; @@ -4376,7 +4389,7 @@ fn get_autosuggestion_performer( if autosuggest_validate_from_history(item, &working_directory, &ctx) { // The command autosuggestion was handled specially, so we're done. // History items are case-sensitive, see #3978. - return Autosuggestion::new( + return AutosuggestionResult::new( searcher.current_string().to_owned(), search_string.to_owned(), /*icase=*/ false, @@ -4421,7 +4434,7 @@ fn get_autosuggestion_performer( /*append_only=*/ true, ) }; - let mut result = Autosuggestion::new( + let mut result = AutosuggestionResult::new( full_line, search_string.to_owned(), true, // normal completions are case-insensitive @@ -4452,7 +4465,7 @@ impl<'a> Reader<'a> { } // Called after an autosuggestion has been computed on a background thread. - fn autosuggest_completed(&mut self, result: Autosuggestion) { + fn autosuggest_completed(&mut self, result: AutosuggestionResult) { assert_is_main_thread(); if result.search_string == self.data.in_flight_autosuggest_request { self.data.in_flight_autosuggest_request.clear(); @@ -4482,7 +4495,7 @@ impl<'a> Reader<'a> { && string_prefixes_string_case_insensitive(&result.search_string, &result.text) { // Autosuggestion is active and the search term has not changed, so we're good to go. - self.autosuggestion = result; + self.autosuggestion = result.autosuggestion; if self.is_repaint_needed(None) { self.layout_and_repaint(L!("autosuggest")); }