From 73ff6cbe392b9c725db20c962fdd1a4f2abb536d Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Thu, 17 Feb 2022 10:57:16 -0700 Subject: [PATCH] =?UTF-8?q?feat:=20case=20insensitive=20completion=20?= =?UTF-8?q?=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.rs | 49 +++++++++++++++++++++++++++-------------------- src/completion.rs | 42 ++++++++++++++++++++++++---------------- 2 files changed, 54 insertions(+), 37 deletions(-) diff --git a/src/app.rs b/src/app.rs index d3bc834..e1a1d90 100644 --- a/src/app.rs +++ b/src/app.rs @@ -890,11 +890,8 @@ impl TaskwarriorTui { .iter() .map(|p| { let lines = vec![Spans::from(vec![ - Span::styled( - p.display.replace(p.replacement.clone().as_str(), ""), - Style::default().add_modifier(Modifier::BOLD), - ), - Span::from(p.replacement.clone()), + Span::styled(p.3.clone(), Style::default().add_modifier(Modifier::BOLD)), + Span::from(p.4.clone()), ])]; ListItem::new(lines) }) @@ -2837,9 +2834,10 @@ impl TaskwarriorTui { Key::Char('\n') => { if self.show_completion_pane { self.show_completion_pane = false; - if let Some(s) = self.completion_list.selected() { - let s = format!("{}{}", self.modify.as_str(), &s); - self.modify.update(&s, s.len()); + if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() { + let (before, after) = self.modify.as_str().split_at(self.modify.pos()); + let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after); + self.modify.update(&fs, self.modify.pos() + r.len() - o.len()); } self.completion_list.unselect(); } else if self.error.is_some() { @@ -2958,10 +2956,10 @@ impl TaskwarriorTui { Key::Char('\n') => { if self.show_completion_pane { self.show_completion_pane = false; - if let Some(s) = self.completion_list.selected() { + if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() { let (before, after) = self.command.as_str().split_at(self.command.pos()); - let fs = format!("{}{}{}", before, s, after); - self.command.update(&fs, self.command.pos() + s.len()); + let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after); + self.command.update(&fs, self.command.pos() + r.len() - o.len()); } self.completion_list.unselect(); } else if self.error.is_some() { @@ -3057,10 +3055,10 @@ impl TaskwarriorTui { Key::Char('\n') => { if self.show_completion_pane { self.show_completion_pane = false; - if let Some(s) = self.completion_list.selected() { + if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() { let (before, after) = self.command.as_str().split_at(self.command.pos()); - let fs = format!("{}{}{}", before, s, after); - self.command.update(&fs, self.command.pos() + s.len()); + let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after); + self.command.update(&fs, self.command.pos() + r.len() - o.len()); } self.completion_list.unselect(); } else if self.error.is_some() { @@ -3181,10 +3179,10 @@ impl TaskwarriorTui { Key::Char('\n') => { if self.show_completion_pane { self.show_completion_pane = false; - if let Some(s) = self.completion_list.selected() { + if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() { let (before, after) = self.command.as_str().split_at(self.command.pos()); - let fs = format!("{}{}{}", before, s, after); - self.command.update(&fs, self.command.pos() + s.len()); + let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after); + self.command.update(&fs, self.command.pos() + r.len() - o.len()); } self.completion_list.unselect(); } else if self.error.is_some() { @@ -3289,10 +3287,10 @@ impl TaskwarriorTui { Key::Char('\n') => { if self.show_completion_pane { self.show_completion_pane = false; - if let Some(s) = self.completion_list.selected() { + if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() { let (before, after) = self.filter.as_str().split_at(self.filter.pos()); - let fs = format!("{}{}{}", before, s, after); - self.filter.update(&fs, self.filter.pos() + s.len()); + let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after); + self.filter.update(&fs, self.filter.pos() + r.len() - o.len()); } self.completion_list.unselect(); self.dirty = true; @@ -4824,15 +4822,24 @@ mod tests { // #[test] fn test_taskwarrior_tui_completion() { let mut app = TaskwarriorTui::new("next").unwrap(); + app.handle_input(Key::Char('z')).unwrap(); app.mode = Mode::Tasks(Action::Add); app.update_completion_list(); - let input = "Wash car project:packing project:p"; + let input = "Wash car"; for c in input.chars() { app.handle_input(Key::Char(c)).unwrap(); } + app.handle_input(Key::Ctrl('e')).unwrap(); + + let input = " project:CO"; + for c in input.chars() { + app.handle_input(Key::Char(c)).unwrap(); + } + app.mode = Mode::Tasks(Action::Add); app.update_completion_list(); app.handle_input(Key::Tab).unwrap(); + app.handle_input(Key::Char('\n')).unwrap(); let backend = TestBackend::new(80, 50); let mut terminal = Terminal::new(backend).unwrap(); terminal diff --git a/src/completion.rs b/src/completion.rs index 2642a2c..b1f7ced 100644 --- a/src/completion.rs +++ b/src/completion.rs @@ -8,7 +8,6 @@ use tui::{ Terminal, }; -use rustyline::completion::{Completer, FilenameCompleter, Pair}; use rustyline::error::ReadlineError; use rustyline::highlight::{Highlighter, MatchingBracketHighlighter}; use rustyline::hint::Hinter; @@ -38,19 +37,27 @@ pub struct TaskwarriorTuiCompletionHelper { pub input: String, } -impl Completer for TaskwarriorTuiCompletionHelper { - type Candidate = Pair; +type Completion = (String, String, String, String, String); - fn complete(&self, word: &str, pos: usize, _ctx: &Context) -> rustyline::Result<(usize, Vec)> { - let candidates: Vec = self +impl TaskwarriorTuiCompletionHelper { + fn complete(&self, word: &str, pos: usize, _ctx: &Context) -> rustyline::Result<(usize, Vec)> { + let candidates: Vec = self .candidates .iter() .filter_map(|(context, candidate)| { - if context == &self.context && candidate.starts_with(&word[..pos]) && !self.input.contains(candidate) { - Some(Pair { - display: candidate.clone(), - replacement: candidate[pos..].to_string(), - }) + if context == &self.context + && (candidate.starts_with(&word[..pos]) + || candidate.to_lowercase().starts_with(&word[..pos].to_lowercase())) + && (!self.input.contains(candidate) + || !self.input.to_lowercase().contains(&candidate.to_lowercase())) + { + Some(( + candidate.clone(), // display + candidate.to_string(), // replacement + word[..pos].to_string(), // original + candidate[..pos].to_string(), + candidate[pos..].to_string(), + )) } else { None } @@ -150,27 +157,30 @@ impl CompletionList { } pub fn max_width(&self) -> Option { - self.candidates().iter().map(|p| p.display.width() + 4).max() + self.candidates().iter().map(|p| p.1.width() + 4).max() } - pub fn get(&self, i: usize) -> Option { + pub fn get(&self, i: usize) -> Option { let candidates = self.candidates(); if i < candidates.len() { - Some(candidates[i].replacement.clone()) + Some(candidates[i].clone()) } else { None } } - pub fn selected(&self) -> Option { - self.state.selected().and_then(|i| self.get(i)) + pub fn selected(&self) -> Option<(usize, Completion)> { + self.state + .selected() + .and_then(|i| self.get(i)) + .and_then(|s| Some((self.pos, s))) } pub fn is_empty(&self) -> bool { self.candidates().is_empty() } - pub fn candidates(&self) -> Vec { + pub fn candidates(&self) -> Vec { let hist = rustyline::history::History::new(); let ctx = rustyline::Context::new(&hist); let (pos, candidates) = self.helper.complete(&self.current, self.pos, &ctx).unwrap();