Fix tab completion behavior

This commit is contained in:
Dheepak Krishnamurthy 2021-04-06 19:25:40 -06:00
parent 92f9be8038
commit d3ed969905
2 changed files with 108 additions and 34 deletions

View file

@ -1,5 +1,5 @@
use crate::calendar::Calendar;
use crate::completion::CompletionList;
use crate::completion::{get_start_word_under_cursor, CompletionList};
use crate::config;
use crate::config::Config;
use crate::context::Context;
@ -637,12 +637,17 @@ impl TaskwarriorTuiApp {
}
fn draw_completion_pop_up(&mut self, f: &mut Frame<impl Backend>, rect: Rect, cursor_position: usize) {
if self.completion_list.candidates().is_empty() {
self.show_completion_pane = false;
return;
}
// Iterate through all elements in the `items` app and append some debug text to it.
let items: Vec<ListItem> = self
.completion_list
.candidates()
.iter()
.map(|i| {
let lines = vec![Spans::from(i.clone())];
.map(|p| {
let lines = vec![Spans::from(p.display.clone())];
ListItem::new(lines).style(Style::default().fg(Color::Black))
})
.collect();
@ -2201,7 +2206,7 @@ impl TaskwarriorTuiApp {
_ => {
self.command_history_context.last();
handle_movement(&mut self.modify, input);
self.update_completion_list();
self.update_input_for_completion();
}
},
AppMode::TaskSubprocess => match input {
@ -2295,6 +2300,7 @@ impl TaskwarriorTuiApp {
_ => {
self.command_history_context.last();
handle_movement(&mut self.command, input);
self.update_input_for_completion();
}
},
AppMode::TaskAnnotate => match input {
@ -2407,6 +2413,7 @@ impl TaskwarriorTuiApp {
_ => {
self.command_history_context.last();
handle_movement(&mut self.command, input);
self.update_input_for_completion();
}
},
AppMode::TaskFilter => match input {
@ -2476,6 +2483,7 @@ impl TaskwarriorTuiApp {
}
_ => {
handle_movement(&mut self.filter, input);
self.update_input_for_completion();
self.dirty = true;
}
},
@ -2565,6 +2573,27 @@ impl TaskwarriorTuiApp {
_ => {}
}
}
pub fn update_input_for_completion(&mut self) {
match self.mode {
AppMode::TaskAdd | AppMode::TaskLog => {
let i = get_start_word_under_cursor(self.command.as_str(), self.command.pos());
let input = self.command.as_str()[i..self.command.pos()].to_string();
self.completion_list.input(input);
}
AppMode::TaskModify => {
let i = get_start_word_under_cursor(self.modify.as_str(), self.modify.pos());
let input = self.modify.as_str()[i..self.modify.pos()].to_string();
self.completion_list.input(input);
}
AppMode::TaskFilter => {
let i = get_start_word_under_cursor(self.filter.as_str(), self.filter.pos());
let input = self.filter.as_str()[i..self.filter.pos()].to_string();
self.completion_list.input(input);
}
_ => {}
}
}
}
pub fn handle_movement(linebuffer: &mut LineBuffer, input: Key) {

View file

@ -18,10 +18,7 @@ use rustyline_derive::Helper;
use unicode_segmentation::Graphemes;
use unicode_segmentation::UnicodeSegmentation;
// find the beginning of the word in line which is currently under the cursor,
// whose position is cursor_pos.
//
fn get_start_word_under_cursor(line: &str, cursor_pos: usize) -> usize {
pub fn get_start_word_under_cursor(line: &str, cursor_pos: usize) -> usize {
let mut chars = line[..cursor_pos].chars();
let mut res = cursor_pos;
while let Some(c) = chars.next_back() {
@ -35,65 +32,79 @@ fn get_start_word_under_cursor(line: &str, cursor_pos: usize) -> usize {
}
pub struct TaskwarriorTuiCompletionHelper {
hints: Vec<String>,
candidates: Vec<String>,
completer: rustyline::completion::FilenameCompleter,
}
impl Completer for TaskwarriorTuiCompletionHelper {
type Candidate = String;
type Candidate = Pair;
fn complete(&self, line: &str, pos: usize, _ctx: &Context) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
let mut candidates: Vec<String> = self
.hints
fn complete(&self, word: &str, pos: usize, _ctx: &Context) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
let candidates: Vec<Pair> = self
.candidates
.iter()
.filter_map(|hint| {
if pos > 0 && hint.starts_with(&line[..pos]) && !hint[pos..].contains(" ") {
Some(hint[pos..].to_owned())
.filter_map(|candidate| {
if candidate.starts_with(&word[..pos]) {
Some(Pair {
display: candidate.clone(),
replacement: candidate[pos..].to_string(),
})
} else {
None
}
})
.collect();
candidates.sort();
Ok((pos, candidates))
}
}
pub struct CompletionList {
pub state: ListState,
pub input: String,
pub pos: usize,
pub helper: TaskwarriorTuiCompletionHelper,
}
impl CompletionList {
pub fn new() -> CompletionList {
let completer = FilenameCompleter::new();
CompletionList {
state: ListState::default(),
helper: TaskwarriorTuiCompletionHelper { hints: vec![] },
input: String::new(),
pos: 0,
helper: TaskwarriorTuiCompletionHelper {
candidates: vec![],
completer,
},
}
}
pub fn with_items(items: Vec<String>) -> CompletionList {
let mut hints = vec![];
let completer = FilenameCompleter::new();
let mut candidates = vec![];
for i in items {
if !hints.contains(&i) {
hints.push(i);
if !candidates.contains(&i) {
candidates.push(i);
}
}
CompletionList {
state: ListState::default(),
helper: TaskwarriorTuiCompletionHelper { hints },
input: String::new(),
pos: 0,
helper: TaskwarriorTuiCompletionHelper { candidates, completer },
}
}
pub fn insert(&mut self, item: String) {
if !self.helper.hints.contains(&item) {
self.helper.hints.push(item);
if !self.helper.candidates.contains(&item) {
self.helper.candidates.push(item);
}
}
pub fn next(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i >= self.helper.hints.len() - 1 {
if i >= self.candidates().len() - 1 {
0
} else {
i + 1
@ -108,7 +119,7 @@ impl CompletionList {
let i = match self.state.selected() {
Some(i) => {
if i == 0 {
self.helper.hints.len() - 1
self.candidates().len() - 1
} else {
i - 1
}
@ -123,21 +134,25 @@ impl CompletionList {
}
pub fn clear(&mut self) {
self.helper.hints.clear();
self.helper.candidates.clear();
self.state.select(None);
}
pub fn len(&self) -> usize {
self.helper.hints.iter().count()
self.candidates().iter().count()
}
pub fn max_width(&self) -> Option<usize> {
self.helper.hints.iter().map(|s| s.graphemes(true).count() + 4).max()
self.candidates()
.iter()
.map(|p| p.display.graphemes(true).count() + 4)
.max()
}
pub fn get(&self, i: usize) -> Option<String> {
if i < self.helper.hints.len() {
Some(self.helper.hints[i].clone())
let candidates = self.candidates();
if i < candidates.len() {
Some(candidates[i].replacement.clone())
} else {
None
}
@ -152,10 +167,40 @@ impl CompletionList {
}
pub fn is_empty(&self) -> bool {
self.helper.hints.is_empty()
self.helper.candidates.is_empty()
}
pub fn iter(&self) -> std::slice::Iter<String> {
self.helper.hints.iter()
pub fn candidates(&self) -> Vec<Pair> {
let hist = rustyline::history::History::new();
let ctx = rustyline::Context::new(&hist);
let (pos, candidates) = self.helper.complete(&self.input, self.pos, &ctx).unwrap();
candidates
}
pub fn input(&mut self, input: String) {
self.input = input;
self.pos = self.input.len();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_completion() {
let mut completion_list = CompletionList::new();
completion_list.insert("+test".to_string());
completion_list.insert("+shortcut".to_string());
completion_list.insert("project:color".to_string());
completion_list.insert("due:'2021-04-07T00:00:00'".to_string());
completion_list.input("due:".to_string());
for p in completion_list.candidates().iter() {
dbg!(format!("{:?}", p.display));
dbg!(format!("{:?}", p.replacement));
}
}
}