1
//! Type-ahead for the focused tag cell of any rows list in the form: answers
2
//! are fetched once per source and kept for the form's lifetime.
3

            
4
use super::Form;
5
use crate::widgets::{Hint, RowsWidget, Widget, completion, matching};
6

            
7
const MAX_SHOWN: usize = 8;
8

            
9
impl Form {
10
1773
    fn focused_rows(&self) -> Option<&RowsWidget> {
11
1773
        match &self.fields.get(self.focus)?.widget {
12
75
            Widget::Rows(rows) => Some(rows),
13
357
            Widget::Splits(splits) => splits.tags(),
14
1298
            _ => None,
15
        }
16
1773
    }
17

            
18
39
    pub(super) fn focused_rows_mut(&mut self) -> Option<&mut RowsWidget> {
19
39
        match &mut self.fields.get_mut(self.focus)?.widget {
20
31
            Widget::Rows(rows) => Some(rows),
21
8
            Widget::Splits(splits) => splits.tags_mut(),
22
            _ => None,
23
        }
24
39
    }
25

            
26
    /// The lookup the focused cell needs, if not asked yet.
27
    #[must_use]
28
789
    pub fn wanted_hint(&self) -> Option<Hint> {
29
789
        let hint = Hint::of_focused(self.focused_rows()?)?;
30
36
        self.hints.wants(&hint).then_some(hint)
31
789
    }
32

            
33
21
    pub fn hint_asked(&mut self, hint: &Hint, seq: u64) {
34
21
        self.hints.asked(hint, seq);
35
21
    }
36

            
37
    /// Keeps the answer unless a newer request for its source superseded `seq`.
38
16
    pub fn hint_arrived(&mut self, hint: &Hint, seq: u64, strings: Vec<String>) {
39
16
        self.hints.arrived(hint, seq, strings);
40
16
    }
41

            
42
492
    fn focused_typed(&self) -> String {
43
492
        let text = self
44
492
            .focused_rows()
45
43
            .and_then(|rows| Some(rows.focused_text(rows.focused_key()?)));
46
492
        text.unwrap_or_default().to_string()
47
492
    }
48

            
49
492
    fn candidates_for(&self, typed: &str) -> Vec<&str> {
50
492
        let hint = self.focused_rows().and_then(Hint::of_focused);
51
492
        hint.map_or_else(Vec::new, |hint| matching(self.hints.answer(&hint), typed))
52
492
    }
53

            
54
    /// The suggestions that fit the focused cell as typed.
55
    #[must_use]
56
482
    pub fn candidates(&self) -> Vec<&str> {
57
482
        self.candidates_for(&self.focused_typed())
58
482
    }
59

            
60
    /// The suggestion line under the focused list, empty when nothing fits; `key` fills the cell.
61
    #[must_use]
62
466
    pub fn suggestion_line(&self, key: Option<String>) -> Option<String> {
63
466
        let shown: Vec<&str> = self.candidates().into_iter().take(MAX_SHOWN).collect();
64
466
        let label = key.map_or_else(|| "suggest".to_string(), |key| format!("suggest ({key})"));
65
466
        (!shown.is_empty()).then(|| format!("{label}: {}", shown.join(", ")))
66
466
    }
67

            
68
    /// Fills the focused cell with its next suggestion; repeated presses cycle
69
    /// through those matching what was typed before the first.
70
10
    pub fn complete_hint(&mut self) {
71
10
        let typed = self.focused_typed();
72
10
        let prefix = match &self.completing {
73
2
            Some((prefix, filled)) if *filled == typed => prefix.clone(),
74
8
            _ => typed.clone(),
75
        };
76
10
        let next = completion(&self.candidates_for(&prefix), &typed).map(str::to_string);
77
10
        if let Some(next) = next
78
10
            && let Some(rows) = self.focused_rows_mut()
79
        {
80
10
            rows.set_focused_text(&next);
81
10
            self.completing = Some((prefix, next));
82
        }
83
10
    }
84
}