@@ -24,7 +24,7 @@ pub struct Prompt {
24 selection: Option<usize>,
24 selection: Option<usize>,
25 history_register: Option<char>,
25 history_register: Option<char>,
26 history_pos: Option<usize>,
26 history_pos: Option<usize>,
27− completion_fn: Box<dyn FnMut(&Context, &str) -> Vec<Completion>>,
27+ completion_fn: Box<dyn FnMut(&Editor, &str) -> Vec<Completion>>,
28 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
28 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
29 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
29 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
@@ -59,7 +59,7 @@ impl Prompt {
59 pub fn new(
59 pub fn new(
60 prompt: Cow<'static, str>,
60 prompt: Cow<'static, str>,
61 history_register: Option<char>,
61 history_register: Option<char>,
62− completion_fn: impl FnMut(&Context, &str) -> Vec<Completion> + 'static,
62+ completion_fn: impl FnMut(&Editor, &str) -> Vec<Completion> + 'static,
63 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
63 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
64 ) -> Self {
64 ) -> Self {
@@ -76,6 +76,10 @@ impl Prompt {
79+ pub fn recalculate_completion(&mut self, editor: &Editor) {
80+ self.completion = (self.completion_fn)(editor, &self.line);
79 /// Compute the cursor position after applying movement
83 /// Compute the cursor position after applying movement
80 /// Taken from: https://github.com/wez/wezterm/blob/e0b62d07ca9bf8ce69a61e30a3c20e7abc48ce7e/termwiz/src/lineedit/mod.rs#L516-L611
84 /// Taken from: https://github.com/wez/wezterm/blob/e0b62d07ca9bf8ce69a61e30a3c20e7abc48ce7e/termwiz/src/lineedit/mod.rs#L516-L611
81 fn eval_movement(&self, movement: Movement) -> usize {
85 fn eval_movement(&self, movement: Movement) -> usize {
@@ -183,7 +187,7 @@ impl Prompt {
183 if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
187 if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
184 self.cursor = pos;
188 self.cursor = pos;
186− self.completion = (self.completion_fn)(cx, &self.line);
190+ self.recalculate_completion(cx.editor);
187 self.exit_selection();
191 self.exit_selection();
@@ -211,7 +215,7 @@ impl Prompt {
211 self.cursor = pos;
215 self.cursor = pos;
213 self.exit_selection();
217 self.exit_selection();
214− self.completion = (self.completion_fn)(cx, &self.line);
218+ self.recalculate_completion(cx.editor);
217 pub fn delete_char_forwards(&mut self, cx: &Context) {
221 pub fn delete_char_forwards(&mut self, cx: &Context) {
@@ -219,7 +223,7 @@ impl Prompt {
219 self.line.replace_range(self.cursor..pos, "");
223 self.line.replace_range(self.cursor..pos, "");
221 self.exit_selection();
225 self.exit_selection();
222− self.completion = (self.completion_fn)(cx, &self.line);
226+ self.recalculate_completion(cx.editor);
225 pub fn delete_word_backwards(&mut self, cx: &Context) {
229 pub fn delete_word_backwards(&mut self, cx: &Context) {
@@ -228,7 +232,7 @@ impl Prompt {
228 self.cursor = pos;
232 self.cursor = pos;
230 self.exit_selection();
234 self.exit_selection();
231− self.completion = (self.completion_fn)(cx, &self.line);
235+ self.recalculate_completion(cx.editor);
234 pub fn delete_word_forwards(&mut self, cx: &Context) {
238 pub fn delete_word_forwards(&mut self, cx: &Context) {
@@ -236,7 +240,7 @@ impl Prompt {
236 self.line.replace_range(self.cursor..pos, "");
240 self.line.replace_range(self.cursor..pos, "");
238 self.exit_selection();
242 self.exit_selection();
239− self.completion = (self.completion_fn)(cx, &self.line);
243+ self.recalculate_completion(cx.editor);
242 pub fn kill_to_start_of_line(&mut self, cx: &Context) {
246 pub fn kill_to_start_of_line(&mut self, cx: &Context) {
@@ -245,7 +249,7 @@ impl Prompt {
245 self.cursor = pos;
249 self.cursor = pos;
247 self.exit_selection();
251 self.exit_selection();
248− self.completion = (self.completion_fn)(cx, &self.line);
252+ self.recalculate_completion(cx.editor);
251 pub fn kill_to_end_of_line(&mut self, cx: &Context) {
255 pub fn kill_to_end_of_line(&mut self, cx: &Context) {
@@ -253,13 +257,13 @@ impl Prompt {
253 self.line.replace_range(self.cursor..pos, "");
257 self.line.replace_range(self.cursor..pos, "");
255 self.exit_selection();
259 self.exit_selection();
256− self.completion = (self.completion_fn)(cx, &self.line);
260+ self.recalculate_completion(cx.editor);
259 pub fn clear(&mut self, cx: &Context) {
263 pub fn clear(&mut self, cx: &Context) {
260 self.line.clear();
264 self.line.clear();
261 self.cursor = 0;
265 self.cursor = 0;
262− self.completion = (self.completion_fn)(cx, &self.line);
266+ self.recalculate_completion(cx.editor);
263 self.exit_selection();
267 self.exit_selection();
@@ -474,7 +478,7 @@ impl Component for Prompt {
475 key!(Enter) => {
479 key!(Enter) => {
476 if self.selection.is_some() && self.line.ends_with(std::path::MAIN_SEPARATOR) {
480 if self.selection.is_some() && self.line.ends_with(std::path::MAIN_SEPARATOR) {
477− self.completion = (self.completion_fn)(cx, &self.line);
481+ self.recalculate_completion(cx.editor);
478 self.exit_selection();
482 self.exit_selection();
480 (self.callback_fn)(cx, &self.line, PromptEvent::Validate);
484 (self.callback_fn)(cx, &self.line, PromptEvent::Validate);
@@ -525,7 +529,6 @@ impl Component for Prompt {
527 fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
531 fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
528− self.completion = (self.completion_fn)(cx, &self.line);
529 self.render_prompt(area, surface, cx)
532 self.render_prompt(area, surface, cx)