(952a9f8) Complete the verb, the statuses, the tags and the campaigns in the prompt, and bump to 0.33.4
With the ticket suggested, the rest of the line was still typed blind. The prompt now completes everything fish does: the verb and the verbs of a group, the five statuses, the tags the board carries, the campaigns a codename resolves against, and relate --clear's three kinds. `ticket_argument` becomes `argument_at`, which answers what a position names rather than whether it is a ticket. The tables say Ticket, Campaign or Tag; clap says the rest, so the verbs, the statuses, `set --clear`'s fields and the `completions` shells are never written down twice. Only `relate --clear` needed a table of its own: the flag takes a kind and then a target, so possible values on it would refuse every target. The row draws the longest opening every candidate agrees on, which is exactly what the next tab takes. One rule for one candidate and for many: a ticket position has only the selected card, so the rest of its id is drawn as before; the five statuses agree on nothing, so nothing is drawn and tab steps through them one press at a time. The walk holds the line as it stood before the first press, not the list and not the candidate the last press chose, so every press re-derives the candidates and stepping cannot narrow them to what it just typed. Every edit ends it, beside `refused` and for the same reason. 193f9be is not inherited: `tags set` is offered only the tags the vocabulary declares, and `tags add` and both `--tags` that take the whole list are offered nothing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
c714c73a75
commit
a885f81da5
+253
-59
@@ -53,41 +53,6 @@ pub const TICKET_FLAGS: [(&str, &str); 4] = [
|
||||
("chown", "clear"),
|
||||
];
|
||||
|
||||
/// Whether the argument at `index` of a typed line names a ticket, `index` counting from
|
||||
/// the verb at 0.
|
||||
///
|
||||
/// The three tables above read as one question, which is what the board's command prompt
|
||||
/// asks to decide whether to suggest the selected ticket. Here rather than in `tui.rs`
|
||||
/// because the tables are here and the tests that keep them honest against clap are here:
|
||||
/// a second copy of the positions on the board would go wrong the next time a command grew
|
||||
/// a ticket argument, and `no_ticket_taking_subcommand_is_missing` would not notice.
|
||||
///
|
||||
/// Position is counted rather than inferred, exactly as the fish half counts it, so
|
||||
/// `mv --status done <ticket>` is not recognised: the ticket is `mv`'s first *positional*
|
||||
/// and the third argument on the line. That is the fish rules' limit too, and one table
|
||||
/// answering both is worth more than two tables disagreeing about the corner.
|
||||
pub fn ticket_argument(args: &[String], index: usize) -> bool {
|
||||
// The verb is not a ticket, and index 0 read against the tables below would make
|
||||
// `show` its own first argument.
|
||||
if index == 0 {
|
||||
return false;
|
||||
}
|
||||
let Some(verb) = args.first().map(String::as_str) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
// A flag's value is the argument after it, which is where the flag sits whether the
|
||||
// value is half typed or has not been started.
|
||||
if let Some(flag) = args.get(index - 1).and_then(|arg| arg.strip_prefix("--"))
|
||||
&& TICKET_FLAGS.contains(&(verb, flag))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
(index == 1 && TICKET_COMMANDS.contains(&verb))
|
||||
|| (index == 2 && TICKET_SECOND_COMMANDS.contains(&verb))
|
||||
}
|
||||
|
||||
/// Subcommands whose first positional is a campaign.
|
||||
///
|
||||
/// `assign` alone, which takes the campaign first and the ticket second. Kept honest by
|
||||
@@ -136,6 +101,154 @@ pub const TAG_FLAGS: [(&str, &str); 2] = [("ls", "tag"), ("append", "tags")];
|
||||
/// `no_tag_taking_option_is_missing` makes somebody decide.
|
||||
pub const TAG_NESTED_COMMANDS: [(&str, &str); 1] = [("tags", "set")];
|
||||
|
||||
/// Options whose first value is a relation kind.
|
||||
///
|
||||
/// `relate --clear` alone, and it is a list rather than a line in [`argument_at`] for the
|
||||
/// reason every other position is: what an argument names belongs beside the tables that
|
||||
/// answer the same question about the rest of them. It cannot come off clap the way a
|
||||
/// status does, which is why it is here at all — the flag takes a kind and then a target,
|
||||
/// two values of different kinds under one name, so possible values on it would refuse
|
||||
/// every target.
|
||||
pub const RELATION_KIND_FLAGS: [(&str, &str); 1] = [("relate", "clear")];
|
||||
|
||||
/// What an argument of a typed line names, where worklane can answer what goes there.
|
||||
///
|
||||
/// The board's command prompt completes against this: the tables above say *what* the
|
||||
/// position takes and the prompt says what the board currently holds of it. A ticket and a
|
||||
/// campaign and a tag are all "a word off the board" to clap and three different questions
|
||||
/// to the reader, which is why this is an enum rather than a candidate list — the values
|
||||
/// live where the board is and the positions live here.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Argument {
|
||||
/// A ticket on the board. One candidate, the card the reader has selected.
|
||||
Ticket,
|
||||
/// A campaign, which is named by its codename and drawn by it on every card it holds.
|
||||
Campaign,
|
||||
/// A tag. `declared` is `tags set`'s: it changes what a declared tag means and refuses
|
||||
/// one the vocabulary file does not declare, so offering a carried-but-undeclared tag
|
||||
/// there would offer exactly the values the command rejects. That is 193f9be in the
|
||||
/// fish half, and this is the prompt not inheriting it.
|
||||
Tag { declared: bool },
|
||||
/// Values the tool itself holds: the subcommand names at a verb position, the possible
|
||||
/// values of an argument clap built from a `ValueEnum`, and the relation kinds.
|
||||
///
|
||||
/// Read off the clap definition rather than written out, so a command, a status or a
|
||||
/// clearable field added anywhere is typeable on the board the day it is added.
|
||||
Fixed(Vec<String>),
|
||||
}
|
||||
|
||||
/// What the argument at `index` of a typed line names, `index` counting from the verb at 0,
|
||||
/// or nothing where worklane has nothing to offer.
|
||||
///
|
||||
/// The tables above are what answer it, reached rather than restated: a second copy of the
|
||||
/// positions on the board would go wrong the next time a command grew a ticket argument, and
|
||||
/// it would go wrong silently, since the tests that keep these honest against clap would
|
||||
/// not read the copy.
|
||||
///
|
||||
/// Position is counted rather than inferred, exactly as the fish half counts it, so
|
||||
/// `mv --status done <ticket>` is not recognised: the ticket is `mv`'s first *positional*
|
||||
/// and the third argument on the line. That is the fish rules' limit too, and one answer
|
||||
/// for both is worth more than two that disagree about the corner.
|
||||
///
|
||||
/// The clap definition is rebuilt on every call, at 32µs in release, and the caller is a
|
||||
/// board that redraws four times a second: a definition cached beside the board would be a
|
||||
/// second copy of what `Cli::command()` already answers, for a cost nothing can measure.
|
||||
/// The fish half spends 2.5ms per keypress running worklane itself for the same answers.
|
||||
pub fn argument_at(args: &[String], index: usize) -> Option<Argument> {
|
||||
let cli = crate::command::Cli::command();
|
||||
|
||||
// The verb itself, which is every subcommand the tool has. Hidden ones are left out:
|
||||
// they are not in the help either, and a candidate nobody can read about is one a
|
||||
// reader has to go and look up.
|
||||
if index == 0 {
|
||||
return Some(Argument::Fixed(subcommand_names(&cli)));
|
||||
}
|
||||
|
||||
let verb = args.first().map(String::as_str)?;
|
||||
let group = cli.find_subcommand(verb);
|
||||
|
||||
// A flag's value is the argument after it, which is where the flag sits whether the
|
||||
// value is half typed or has not been started.
|
||||
if let Some(flag) = args.get(index - 1).and_then(|arg| arg.strip_prefix("--")) {
|
||||
if TICKET_FLAGS.contains(&(verb, flag)) {
|
||||
return Some(Argument::Ticket);
|
||||
}
|
||||
if CAMPAIGN_FLAGS.contains(&flag) {
|
||||
return Some(Argument::Campaign);
|
||||
}
|
||||
if TAG_FLAGS.contains(&(verb, flag)) {
|
||||
return Some(Argument::Tag { declared: false });
|
||||
}
|
||||
if RELATION_KIND_FLAGS.contains(&(verb, flag)) {
|
||||
let kinds = RelationKind::ALL.iter().map(|kind| kind.flag().to_string());
|
||||
return Some(Argument::Fixed(kinds.collect()));
|
||||
}
|
||||
|
||||
// And everything else the flag could take, which is whatever clap knows: `--status`
|
||||
// and `set --clear` are both `ValueEnum`s, so their values are already written down
|
||||
// once, in the enum.
|
||||
let arg = group?.get_arguments().find(|arg| arg.get_long() == Some(flag))?;
|
||||
return fixed(arg);
|
||||
}
|
||||
|
||||
// The positionals, at the index each table records.
|
||||
match index {
|
||||
1 if TICKET_COMMANDS.contains(&verb) => return Some(Argument::Ticket),
|
||||
1 if CAMPAIGN_COMMANDS.contains(&verb) => return Some(Argument::Campaign),
|
||||
2 if TICKET_SECOND_COMMANDS.contains(&verb) => return Some(Argument::Ticket),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let group = group?;
|
||||
|
||||
// The verb of a group, `campaign show` and `tags set`, which is a subcommand name
|
||||
// exactly as the top-level verb is.
|
||||
if index == 1 && group.has_subcommands() {
|
||||
return Some(Argument::Fixed(subcommand_names(group)));
|
||||
}
|
||||
|
||||
// And the first argument of one, which is where a campaign and a declared tag are named.
|
||||
if index == 2
|
||||
&& let Some(child) = args.get(1).map(String::as_str)
|
||||
{
|
||||
if CAMPAIGN_NESTED_COMMANDS.contains(&(verb, child)) {
|
||||
return Some(Argument::Campaign);
|
||||
}
|
||||
if TAG_NESTED_COMMANDS.contains(&(verb, child)) {
|
||||
return Some(Argument::Tag { declared: true });
|
||||
}
|
||||
}
|
||||
|
||||
// And whatever else the positional itself knows, which is the `ls` status and the
|
||||
// `completions` shell: both are `ValueEnum`s that the fish generator has to list by
|
||||
// hand, because it emits possible values for an option and none for a positional.
|
||||
fixed(group.get_positionals().nth(index - 1)?)
|
||||
}
|
||||
|
||||
/// The names a command answers to, hidden ones left out.
|
||||
fn subcommand_names(command: &clap::Command) -> Vec<String> {
|
||||
command
|
||||
.get_subcommands()
|
||||
.filter(|sub| !sub.is_hide_set())
|
||||
.map(|sub| sub.get_name().to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The possible values of one argument, or nothing for an argument that takes free text.
|
||||
///
|
||||
/// `get_positionals` and `get_long` both read what the derive wrote, so nothing here needs
|
||||
/// the command built the way `get_index` would.
|
||||
fn fixed(arg: &clap::Arg) -> Option<Argument> {
|
||||
let values: Vec<String> = arg
|
||||
.get_possible_values()
|
||||
.iter()
|
||||
.filter(|value| !value.is_hide_set())
|
||||
.map(|value| value.get_name().to_string())
|
||||
.collect();
|
||||
|
||||
(!values.is_empty()).then_some(Argument::Fixed(values))
|
||||
}
|
||||
|
||||
pub fn generate(shell: Shell, out: &mut dyn io::Write) -> io::Result<()> {
|
||||
let mut command = crate::command::Cli::command();
|
||||
clap_complete::generate(shell, &mut command, "worklane", out);
|
||||
@@ -541,13 +654,22 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// One line, split the way the prompt splits it, and the argument the cursor is in.
|
||||
/// One line, split the way the prompt splits it, and what the cursor's argument names.
|
||||
///
|
||||
/// The splitter is the prompt's own, so a row here states a line as a reader types it
|
||||
/// The splitter is the prompt's own, so a row below states a line as a reader types it
|
||||
/// rather than as a hand-written argument vector, and the two cannot come apart.
|
||||
fn at_cursor(line: &str) -> bool {
|
||||
fn at_cursor(line: &str) -> Option<Argument> {
|
||||
let split = crate::split::split_line(line, line.len());
|
||||
ticket_argument(&split.args, split.cursor_arg)
|
||||
argument_at(&split.args, split.cursor_arg)
|
||||
}
|
||||
|
||||
/// The fixed values offered at the cursor, or nothing where what goes there is not a
|
||||
/// fixed list.
|
||||
fn values(line: &str) -> Option<Vec<String>> {
|
||||
match at_cursor(line) {
|
||||
Some(Argument::Fixed(values)) => Some(values),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Every position a ticket is taken in, as the line that reaches it.
|
||||
@@ -569,39 +691,111 @@ mod tests {
|
||||
"relate 1b78a24 --relates-to ",
|
||||
"chown --clear ",
|
||||
] {
|
||||
assert!(
|
||||
assert_eq!(
|
||||
at_cursor(line),
|
||||
Some(Argument::Ticket),
|
||||
"`{line}` takes a ticket at the cursor and was not offered one"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// And every position that takes something else, which is the half a naive version
|
||||
/// gets wrong.
|
||||
/// The three places a campaign is named, and the two a tag is, which are why the lists
|
||||
/// are not one list.
|
||||
#[test]
|
||||
fn nothing_is_recognised_where_the_argument_is_not_a_ticket() {
|
||||
fn a_campaign_and_a_tag_are_recognised_where_each_is_named() {
|
||||
for line in [
|
||||
// The verb, on an empty line and on a half typed one.
|
||||
"",
|
||||
"m",
|
||||
// `chown`'s first argument is a user and `assign`'s is a campaign.
|
||||
"chown ",
|
||||
"assign ",
|
||||
// `ls` takes a status and `campaign show` a campaign.
|
||||
"ls ",
|
||||
"campaign show ",
|
||||
// The two `--clear` flags that are spelled like `chown`'s and take something
|
||||
// else: a relation kind, and a field name.
|
||||
"relate 1b78a24 --clear ",
|
||||
"set 1b78a24 --clear ",
|
||||
// `relate --clear`'s second value is a ticket the board no longer holds, which
|
||||
// is what the flag exists for, so nothing on the board is offered there.
|
||||
"relate 1b78a24 --clear blocked-by ",
|
||||
// One past the ticket, which is `mv`'s status and `comment`'s body.
|
||||
"campaign set v1",
|
||||
"ls --campaign ",
|
||||
] {
|
||||
assert_eq!(at_cursor(line), Some(Argument::Campaign), "`{line}`");
|
||||
}
|
||||
|
||||
for line in ["ls --tag ", "append 1b78a24 --tags bu"] {
|
||||
assert_eq!(at_cursor(line), Some(Argument::Tag { declared: false }), "`{line}`");
|
||||
}
|
||||
|
||||
// `tags set` changes what a declared tag means and refuses one the file does not
|
||||
// declare, so only the declared ones may be offered there. That is the fish half's
|
||||
// 193f9be, which this deliberately does not inherit.
|
||||
assert_eq!(at_cursor("tags set "), Some(Argument::Tag { declared: true }));
|
||||
}
|
||||
|
||||
/// The verbs, which are clap's own subcommands at the top level and inside a group.
|
||||
#[test]
|
||||
fn the_verb_position_is_offered_the_subcommands() {
|
||||
let top = values("").expect("the empty line names a verb");
|
||||
for command in ["add", "ls", "mv", "relate", "campaign", "tags"] {
|
||||
assert!(top.contains(&command.to_string()), "`{command}` is not offered: {top:?}");
|
||||
}
|
||||
|
||||
// A group's own verb sits where a top-level command's first argument does, which is
|
||||
// why `campaign show` needs a rule of its own in the fish half as well. In the
|
||||
// order the definition spells them, since that is the order tab steps through.
|
||||
let nested = values("campaign ").expect("a group names a verb");
|
||||
assert_eq!(nested, ["add", "set", "append", "ls", "show"], "{nested:?}");
|
||||
|
||||
// `help` is clap's own and is added when the command is built, which nothing here
|
||||
// does. That suits the board: what `help` prints is a page, and the prompt has a
|
||||
// row.
|
||||
assert!(!top.contains(&"help".to_string()), "{top:?}");
|
||||
}
|
||||
|
||||
/// The values clap itself holds, which is every `ValueEnum` in the tool: nothing here
|
||||
/// writes a status or a clearable field down a second time.
|
||||
#[test]
|
||||
fn an_enum_valued_argument_is_offered_the_enums_own_values() {
|
||||
let statuses = values("mv 1b78a24 --status ").expect("--status takes a status");
|
||||
let listed: Vec<String> = Status::ALL.iter().map(|s| s.as_str().to_string()).collect();
|
||||
assert_eq!(statuses, listed, "the five statuses are not what `--status` offers");
|
||||
|
||||
// The one place a status is positional, which clap emits nothing for and the fish
|
||||
// half lists by hand.
|
||||
assert_eq!(values("ls "), Some(listed));
|
||||
|
||||
// And the other two enums a reader types: the fields `set --clear` empties and the
|
||||
// shells `completions` writes for.
|
||||
let fields = values("set 1b78a24 --clear ").expect("--clear takes a field");
|
||||
assert!(fields.contains(&"acceptance".to_string()), "{fields:?}");
|
||||
let shells = values("completions ").expect("completions takes a shell");
|
||||
assert!(shells.contains(&"fish".to_string()), "{shells:?}");
|
||||
}
|
||||
|
||||
/// `relate --clear` takes the kind and then the target, which is the one argument clap
|
||||
/// cannot answer for: possible values on the flag would refuse every target.
|
||||
#[test]
|
||||
fn relate_clear_is_offered_the_relation_kinds_and_no_ticket() {
|
||||
let kinds = values("relate 1b78a24 --clear ").expect("--clear takes a kind");
|
||||
let listed: Vec<String> = RelationKind::ALL
|
||||
.iter()
|
||||
.map(|kind| kind.flag().to_string())
|
||||
.collect();
|
||||
assert_eq!(kinds, listed, "{kinds:?}");
|
||||
|
||||
// And the second value is a target the board may no longer hold, which is what the
|
||||
// flag exists for, so nothing is offered there at all.
|
||||
assert_eq!(at_cursor("relate 1b78a24 --clear blocked-by "), None);
|
||||
}
|
||||
|
||||
/// The positions that take free text, where offering anything would be offering a guess.
|
||||
#[test]
|
||||
fn nothing_is_recognised_where_the_argument_is_free_text() {
|
||||
for line in [
|
||||
// `chown`'s first argument is a user, checked against nothing.
|
||||
"chown ",
|
||||
// `mv` has no second positional, and a comment body is prose.
|
||||
"mv 1b78a24 ",
|
||||
"comment 1b78a24 the ",
|
||||
// `tags add` declares a word the board does not have yet, so the tags it does
|
||||
// have are exactly the values it refuses.
|
||||
"tags add ",
|
||||
// And both `--tags` that take the whole list, where a single tag offered into
|
||||
// `bug,tui` would be one the field never asked for.
|
||||
"add --tags ",
|
||||
"set 1b78a24 --tags bug,",
|
||||
] {
|
||||
assert!(!at_cursor(line), "`{line}` was offered a ticket where none goes");
|
||||
assert_eq!(at_cursor(line), None, "`{line}` was offered a candidate where none goes");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+426
-47
@@ -28,7 +28,7 @@ use ratatui::widgets::{Block, Borders, Clear, Padding, Paragraph, Tabs, Wrap};
|
||||
|
||||
use crate::campaign::{CAMPAIGNS_DIR, CampaignIndex, Standing};
|
||||
use crate::command::{Cli, Command};
|
||||
use crate::completions::ticket_argument;
|
||||
use crate::completions::{Argument, argument_at};
|
||||
use crate::history::{HistoryPart, history_parts, history_root, read_history};
|
||||
use crate::split::{Unclosed, split_line};
|
||||
use crate::tag::{NO_ANNOTATION, TAGS_FILE, TagIndex};
|
||||
@@ -742,6 +742,26 @@ pub struct Prompt {
|
||||
/// The half typed line `up` walked away from, put back by walking down past the newest
|
||||
/// entry. `Some` exactly while `position` is.
|
||||
pending: Option<String>,
|
||||
/// Where a `tab` walk has got to, or `None` while nothing on the row came from one.
|
||||
///
|
||||
/// Cleared by every edit of the line, beside `refused` and for the same reason: the walk
|
||||
/// is about an argument, and a line that has changed is a different argument.
|
||||
completing: Option<Completing>,
|
||||
}
|
||||
|
||||
/// A `tab` walk in progress: the line as it stood before the first press, and how many
|
||||
/// presses have landed on it since.
|
||||
///
|
||||
/// The line rather than the candidate it chose, so each press rebuilds the row from what was
|
||||
/// typed and reads the candidates fresh. Holding the list instead would be a copy going
|
||||
/// stale between two presses, and holding the last candidate would narrow the list to it —
|
||||
/// a walk that stepped once and then stood still.
|
||||
#[derive(Debug)]
|
||||
struct Completing {
|
||||
line: String,
|
||||
cursor: usize,
|
||||
/// How many times `tab` has been pressed with nothing else in between, counting from 1.
|
||||
presses: usize,
|
||||
}
|
||||
|
||||
impl Prompt {
|
||||
@@ -778,6 +798,7 @@ impl Prompt {
|
||||
self.line.insert_str(self.cursor, text);
|
||||
self.cursor += text.len();
|
||||
self.refused = None;
|
||||
self.completing = None;
|
||||
}
|
||||
|
||||
/// The offset one character to the left of the cursor, or the cursor itself at the
|
||||
@@ -838,6 +859,7 @@ impl Prompt {
|
||||
self.line.replace_range(from..self.cursor, "");
|
||||
self.cursor = from;
|
||||
self.refused = None;
|
||||
self.completing = None;
|
||||
}
|
||||
|
||||
/// Put a recalled line on the row with the cursor at its end, the way a shell hands
|
||||
@@ -850,6 +872,7 @@ impl Prompt {
|
||||
self.cursor = line.len();
|
||||
self.line = line;
|
||||
self.refused = None;
|
||||
self.completing = None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2365,7 +2388,7 @@ impl Board {
|
||||
KeyCode::Down if !control => self.recall_forward(),
|
||||
// The mode's too, and never the tabs': every key belongs to the prompt while it
|
||||
// is open, and what this one takes is made of the selected card.
|
||||
KeyCode::Tab if !control => self.accept_suggestion(),
|
||||
KeyCode::Tab if !control => self.complete(),
|
||||
_ => {
|
||||
if let Some(prompt) = &mut self.prompt {
|
||||
prompt.key(key, control);
|
||||
@@ -2376,62 +2399,177 @@ impl Board {
|
||||
true
|
||||
}
|
||||
|
||||
/// The rest of the selected card's short id where the line is naming a ticket, or
|
||||
/// nothing anywhere else.
|
||||
/// Every candidate for the argument the cursor is in, and what has been typed into it.
|
||||
///
|
||||
/// Derived on every call rather than held on the [`Prompt`], so it cannot come to
|
||||
/// describe a line that has since been edited or a card the selection has left: what
|
||||
/// the row draws and what `tab` takes are one answer, asked twice.
|
||||
/// The one question the row's suggestion and the `tab` walk both ask, so what is drawn
|
||||
/// and what a press takes cannot come apart. Derived on every call and held nowhere:
|
||||
/// the line is edited and the board reloads under it, and a list kept between two
|
||||
/// presses would describe neither.
|
||||
///
|
||||
/// Where a ticket goes is [`ticket_argument`]'s, off the tables the fish completions
|
||||
/// are generated from. A copy of those positions here would go wrong the next time a
|
||||
/// command grew a ticket argument, and the test that keeps them honest against clap
|
||||
/// would not notice.
|
||||
/// **What goes where is [`argument_at`]'s**, off the tables the fish completions are
|
||||
/// generated from, and **what the board holds of it is this file's**. Nothing here reads
|
||||
/// the tree: a ticket is the selected card, a campaign and a tag are the tab's own rows,
|
||||
/// rebuilt at every reload beside the columns, and everything else is written down once
|
||||
/// in the clap definition.
|
||||
///
|
||||
/// Three things have to hold, and each is its own answer to the reader:
|
||||
/// Two things have to hold, each its own answer to the reader:
|
||||
///
|
||||
/// - **The cursor is at the end of the line.** The suggestion is drawn after it and
|
||||
/// `tab` inserts it there, so with the line continuing past the cursor both would be
|
||||
/// in the middle of something already typed. That is the rule a shell's own
|
||||
/// suggestion keeps.
|
||||
/// - **A card is selected.** On the frame there is none, and the frame's own id is a
|
||||
/// campaign or a tag rather than a ticket.
|
||||
/// - **What is already typed is still a prefix of the id.** A suggestion that no longer
|
||||
/// matches the argument under it describes nothing, and what is left is empty once
|
||||
/// the whole id is typed.
|
||||
pub fn suggestion(&self) -> Option<&str> {
|
||||
/// - **The cursor is at the end of the line.** The suggestion is drawn after it and a
|
||||
/// press replaces the argument it is in, so with the line continuing past the cursor
|
||||
/// both would land in the middle of something already typed. That is the rule a
|
||||
/// shell's own suggestion keeps.
|
||||
/// - **A candidate still starts with what is typed.** An offer that no longer matches
|
||||
/// the argument under it describes nothing.
|
||||
///
|
||||
/// The line it reads is the one that was on the row before `tab` was first pressed,
|
||||
/// where a walk is under way: what is on the row then is a candidate, and reading the
|
||||
/// stem off it would narrow the list to the candidate the last press chose.
|
||||
fn completion(&self) -> Option<(String, Vec<String>)> {
|
||||
let prompt = self.prompt.as_ref()?;
|
||||
if prompt.cursor != prompt.line.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let id = self.current().selected()?.ticket.id.as_str();
|
||||
let split = split_line(&prompt.line, prompt.cursor);
|
||||
if !ticket_argument(&split.args, split.cursor_arg) {
|
||||
let line = match &prompt.completing {
|
||||
Some(walk) => walk.line.as_str(),
|
||||
None => prompt.line.as_str(),
|
||||
};
|
||||
let split = split_line(line, line.len());
|
||||
|
||||
// Empty where the argument has been opened and nothing of it lies behind the
|
||||
// cursor, which is every candidate offered.
|
||||
let stem = match split.at_new_arg {
|
||||
true => String::new(),
|
||||
false => split.args[split.cursor_arg].clone(),
|
||||
};
|
||||
|
||||
let offers: Vec<String> = self
|
||||
.candidates(&split.args, split.cursor_arg)
|
||||
.into_iter()
|
||||
.filter(|candidate| candidate.starts_with(&stem))
|
||||
.collect();
|
||||
|
||||
(!offers.is_empty()).then_some((stem, offers))
|
||||
}
|
||||
|
||||
/// What the board currently holds of whatever the argument names.
|
||||
///
|
||||
/// The tab's own rows for a campaign and a tag, which `reload` rebuilds beside the
|
||||
/// columns and the `c` and `t` pickers draw: the answer is already in memory, and a
|
||||
/// read of the tree on a keypress would be a second source for it.
|
||||
///
|
||||
/// A campaign is offered by codename, which is what the board draws on every card in it
|
||||
/// and the shortest thing that addresses it. One that has none falls back to its short
|
||||
/// id, which is what the board labels it with and what `assign` resolves — leaving it
|
||||
/// out would hide a campaign from the only place the reader can reach it by typing.
|
||||
fn candidates(&self, args: &[String], index: usize) -> Vec<String> {
|
||||
let tab = self.current();
|
||||
|
||||
let mut found: Vec<String> = match argument_at(args, index) {
|
||||
None => return Vec::new(),
|
||||
Some(Argument::Fixed(values)) => values,
|
||||
Some(Argument::Ticket) => tab
|
||||
.selected()
|
||||
.map(|card| card.ticket.id.clone())
|
||||
.into_iter()
|
||||
.collect(),
|
||||
Some(Argument::Campaign) => tab
|
||||
.campaigns
|
||||
.iter()
|
||||
.map(|row| match &row.codename {
|
||||
Some(codename) => codename.clone(),
|
||||
None => short_id(&row.uuid).to_string(),
|
||||
})
|
||||
.collect(),
|
||||
Some(Argument::Tag { declared }) => tab
|
||||
.tags
|
||||
.iter()
|
||||
.filter(|row| !declared || row.declared())
|
||||
.map(|row| row.name.clone())
|
||||
.collect(),
|
||||
};
|
||||
|
||||
// The merge tab holds several boards' campaigns, so one codename can arrive twice.
|
||||
// A repeat is a press that appears to do nothing, which is worse than the tab
|
||||
// costing a moment here.
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
found.retain(|candidate| seen.insert(candidate.clone()));
|
||||
|
||||
found
|
||||
}
|
||||
|
||||
/// What the row draws after the cursor: the part of the common prefix of the candidates
|
||||
/// that is not typed yet, which is exactly what the next `tab` takes.
|
||||
///
|
||||
/// One rule for one candidate and for many. With a ticket there is only ever the
|
||||
/// selected card, so what is drawn is the rest of its id; with the five statuses there
|
||||
/// is nothing every candidate agrees on, so nothing is drawn and `tab` steps through
|
||||
/// them instead. What the row shows is never something a press would not do.
|
||||
///
|
||||
/// Nothing while a walk is under way: the row is holding a whole candidate then, and a
|
||||
/// second one drawn against it would read as part of the same word.
|
||||
pub fn suggestion(&self) -> Option<String> {
|
||||
if self.prompt.as_ref()?.completing.is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Empty where the argument has been opened and nothing of it is behind the cursor,
|
||||
// which is the whole id offered.
|
||||
let typed = match split.at_new_arg {
|
||||
true => "",
|
||||
false => split.args[split.cursor_arg].as_str(),
|
||||
};
|
||||
|
||||
id.strip_prefix(typed).filter(|rest| !rest.is_empty())
|
||||
let (stem, offers) = self.completion()?;
|
||||
common_prefix(&offers)
|
||||
.strip_prefix(&stem)
|
||||
.filter(|rest| !rest.is_empty())
|
||||
.map(str::to_string)
|
||||
}
|
||||
|
||||
/// `tab`: take the suggestion into the line, or do nothing where there is none.
|
||||
/// `tab`: take what every candidate agrees on, and then step through them one press at
|
||||
/// a time.
|
||||
///
|
||||
/// The first press takes the common prefix where there is more of it than has been
|
||||
/// typed, which is the whole candidate when there is only one. From there each press
|
||||
/// puts one candidate on the row in place of the last, wrapping at the end. A shell
|
||||
/// does the same, and it needs no list drawn over the columns — which is the trade this
|
||||
/// makes, since the board behind the prompt is what the reader is reading.
|
||||
///
|
||||
/// **Each press rebuilds the row from the line as it stood before the first**, so the
|
||||
/// candidates are read fresh every time and nothing is stored but where the walk has
|
||||
/// got to. Stepping off a candidate the last press typed would narrow the list to it.
|
||||
///
|
||||
/// The whole board rather than the prompt alone, for the reason the recall walk is:
|
||||
/// the suggestion is made of the selected card and the line is only where it lands.
|
||||
fn accept_suggestion(&mut self) {
|
||||
let Some(rest) = self.suggestion().map(str::to_string) else {
|
||||
/// what is on offer is made of the board and the line is only where it lands.
|
||||
fn complete(&mut self) {
|
||||
let Some((stem, offers)) = self.completion() else {
|
||||
return;
|
||||
};
|
||||
if let Some(prompt) = &mut self.prompt {
|
||||
prompt.insert(&rest);
|
||||
}
|
||||
|
||||
let presses = self
|
||||
.prompt
|
||||
.as_ref()
|
||||
.and_then(|prompt| prompt.completing.as_ref())
|
||||
.map_or(0, |walk| walk.presses)
|
||||
+ 1;
|
||||
|
||||
// The prefix step is only a step where it adds something. With the statuses, which
|
||||
// agree on nothing, the first press is already the first candidate.
|
||||
let common = common_prefix(&offers);
|
||||
let taken = match (common.len() > stem.len(), presses) {
|
||||
(true, 1) => common.as_str(),
|
||||
(true, step) => offers[(step - 2) % offers.len()].as_str(),
|
||||
(false, step) => offers[(step - 1) % offers.len()].as_str(),
|
||||
};
|
||||
let rest = taken[stem.len()..].to_string();
|
||||
|
||||
let Some(prompt) = &mut self.prompt else {
|
||||
return;
|
||||
};
|
||||
let (line, cursor) = match prompt.completing.take() {
|
||||
Some(walk) => (walk.line, walk.cursor),
|
||||
None => (prompt.line.clone(), prompt.cursor),
|
||||
};
|
||||
|
||||
prompt.line = line.clone();
|
||||
prompt.cursor = cursor;
|
||||
// After the insert, which clears the walk the way every other edit of the line does.
|
||||
prompt.insert(&rest);
|
||||
prompt.completing = Some(Completing { line, cursor, presses });
|
||||
}
|
||||
|
||||
/// `up`: one line further back, or the newest line when the reader is still on their
|
||||
@@ -4623,7 +4761,7 @@ fn draw_footer(frame: &mut Frame, board: &Board, area: Rect) {
|
||||
// and is asked first: an input cannot be covered, and it is the newest of the three
|
||||
// things that can hold the row. Only one of the two prompts can be open at a time.
|
||||
if let Some(prompt) = &board.prompt {
|
||||
draw_prompt(frame, prompt, board.suggestion(), area);
|
||||
draw_prompt(frame, prompt, board.suggestion().as_deref(), area);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -4882,6 +5020,28 @@ fn draw_prompt(frame: &mut Frame, prompt: &Prompt, suggestion: Option<&str>, are
|
||||
frame.set_cursor_position((area.x + column.min(area.width.saturating_sub(1)), area.y));
|
||||
}
|
||||
|
||||
/// The longest opening every one of `values` shares, which is what one `tab` can take
|
||||
/// without choosing between them.
|
||||
///
|
||||
/// Counted in characters rather than bytes, so a tag spelled in more than ascii is cut on
|
||||
/// one of its own letters. An empty list has no prefix and neither has a list whose members
|
||||
/// start differently, which is the answer that puts the first press on the first candidate
|
||||
/// instead.
|
||||
fn common_prefix(values: &[String]) -> String {
|
||||
let Some(first) = values.first() else {
|
||||
return String::new();
|
||||
};
|
||||
|
||||
values[1..].iter().fold(first.clone(), |prefix, value| {
|
||||
prefix
|
||||
.chars()
|
||||
.zip(value.chars())
|
||||
.take_while(|(held, c)| held == c)
|
||||
.map(|(held, _)| held)
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// The part of `line` a field `room` cells wide shows with the cursor at `cursor`, and the
|
||||
/// column the cursor lands on inside it.
|
||||
///
|
||||
@@ -9147,11 +9307,33 @@ approach";
|
||||
fn suggested(board: &mut Board, line: &str) -> Option<String> {
|
||||
board.open_prompt();
|
||||
type_line(board, line);
|
||||
let rest = board.suggestion().map(str::to_string);
|
||||
let rest = board.suggestion();
|
||||
board.prompt = None;
|
||||
rest
|
||||
}
|
||||
|
||||
/// The same, for every candidate the argument at the end of `line` has rather than the
|
||||
/// part of them the row draws.
|
||||
fn offered(board: &mut Board, line: &str) -> Vec<String> {
|
||||
board.open_prompt();
|
||||
type_line(board, line);
|
||||
let found = board.completion().map(|(_, offers)| offers).unwrap_or_default();
|
||||
board.prompt = None;
|
||||
found
|
||||
}
|
||||
|
||||
/// Type `line` and then press tab `presses` times, and answer what is on the row.
|
||||
fn tabbed(board: &mut Board, line: &str, presses: usize) -> String {
|
||||
board.open_prompt();
|
||||
type_line(board, line);
|
||||
for _ in 0..presses {
|
||||
press_prompt(board, KeyCode::Tab);
|
||||
}
|
||||
let row = prompt_line(board).expect("the prompt closed").to_string();
|
||||
board.prompt = None;
|
||||
row
|
||||
}
|
||||
|
||||
/// Every position a command takes a ticket in, driven as a line typed at the prompt.
|
||||
///
|
||||
/// The whole id where the argument has been opened and nothing typed into it, which is
|
||||
@@ -9186,9 +9368,14 @@ approach";
|
||||
|
||||
/// And every position that takes something else, which is the half a version counting
|
||||
/// the first argument alone gets wrong.
|
||||
///
|
||||
/// Asked of the candidates rather than of the row, because several of these positions
|
||||
/// now have candidates of their own: what has to be true is that none of them is the
|
||||
/// card under the selection.
|
||||
#[test]
|
||||
fn nothing_is_suggested_where_the_argument_is_not_a_ticket() {
|
||||
fn the_selected_id_is_not_offered_where_the_argument_is_not_a_ticket() {
|
||||
let (_root, mut board) = runnable();
|
||||
let id = selected_id(&board);
|
||||
|
||||
for line in [
|
||||
// The verb itself, empty and half typed.
|
||||
@@ -9209,10 +9396,9 @@ approach";
|
||||
// One past the ticket, where `mv` wants a status.
|
||||
"mv 2222222 ",
|
||||
] {
|
||||
assert_eq!(
|
||||
suggested(&mut board, line),
|
||||
None,
|
||||
"`{line}` was offered a ticket where none goes"
|
||||
assert!(
|
||||
!offered(&mut board, line).contains(&id),
|
||||
"`{line}` was offered the selected ticket where none goes"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9320,6 +9506,199 @@ approach";
|
||||
}
|
||||
}
|
||||
|
||||
/// A board carrying everything the prompt completes off the tab's own rows: two
|
||||
/// campaigns, one without a codename, and a vocabulary with a tag nothing declares.
|
||||
fn completable() -> (Fixture, Board) {
|
||||
let dir = board_dir();
|
||||
write_tag_ticket(
|
||||
&dir,
|
||||
"backlog",
|
||||
"11111111-0000-4000-8000-000000000000",
|
||||
"One",
|
||||
"bug,sync",
|
||||
);
|
||||
write_tag_ticket(&dir, "committed", "22222222-0000-4000-8000-000000000000", "Two", "bug");
|
||||
write_campaign(&dir, "aaaaaaaa-0000-4000-8000-000000000000", "First release", "v1", "One.");
|
||||
write_campaign(&dir, "bbbbbbbb-0000-4000-8000-000000000000", "Nameless", "", "Two.");
|
||||
write_vocabulary(
|
||||
&dir,
|
||||
&[
|
||||
("bug", "Behaviour that contradicts what the tool says."),
|
||||
("chore", "Work."),
|
||||
],
|
||||
);
|
||||
|
||||
let board = one_board(&dir);
|
||||
(dir, board)
|
||||
}
|
||||
|
||||
/// The verb position, which is clap's own subcommands and the campaign group's own.
|
||||
#[test]
|
||||
fn the_verb_is_completed_from_the_subcommands() {
|
||||
let (_dir, mut board) = completable();
|
||||
|
||||
assert!(offered(&mut board, "").contains(&"relate".to_string()));
|
||||
// Narrowed to what has been typed, and the common opening taken on the first press.
|
||||
assert_eq!(offered(&mut board, "comm"), ["comment"]);
|
||||
assert_eq!(tabbed(&mut board, "comm", 1), "comment");
|
||||
|
||||
// A group's verbs sit where a top-level command's first argument does.
|
||||
assert_eq!(tabbed(&mut board, "campaign sh", 1), "campaign show");
|
||||
}
|
||||
|
||||
/// The five statuses, in both places one is named.
|
||||
#[test]
|
||||
fn a_status_argument_is_completed_from_the_status_enum() {
|
||||
let (_dir, mut board) = completable();
|
||||
let listed: Vec<String> = Status::ALL.iter().map(|s| s.as_str().to_string()).collect();
|
||||
|
||||
assert_eq!(offered(&mut board, "mv 1111111 --status "), listed);
|
||||
assert_eq!(offered(&mut board, "ls "), listed);
|
||||
// And narrowed by what is typed, where `d` reaches two of them.
|
||||
assert_eq!(offered(&mut board, "ls d"), ["done", "discarded"]);
|
||||
}
|
||||
|
||||
/// Tags and campaigns, which come off the rows the tab already holds.
|
||||
#[test]
|
||||
fn a_tag_and_a_campaign_are_completed_from_the_tabs_own_rows() {
|
||||
let (_dir, mut board) = completable();
|
||||
|
||||
// Every tag the board declares or carries, which is what `--tag` matches.
|
||||
assert_eq!(offered(&mut board, "ls --tag "), ["bug", "chore", "sync"]);
|
||||
assert_eq!(offered(&mut board, "append 1111111 --tags s"), ["sync"]);
|
||||
|
||||
// A campaign by its codename, and by its short id where it has none: that is what
|
||||
// the board labels it with and what `assign` resolves.
|
||||
assert_eq!(offered(&mut board, "assign "), ["v1", "bbbbbbb"]);
|
||||
assert_eq!(offered(&mut board, "campaign show v"), ["v1"]);
|
||||
assert_eq!(offered(&mut board, "ls --campaign v"), ["v1"]);
|
||||
}
|
||||
|
||||
/// `tags set` refuses a tag the vocabulary does not declare, so the prompt does not
|
||||
/// offer one. That is the fish half's 193f9be, which this deliberately does not inherit.
|
||||
#[test]
|
||||
fn the_prompt_offers_no_candidate_the_command_would_refuse() {
|
||||
let (_dir, mut board) = completable();
|
||||
|
||||
assert_eq!(offered(&mut board, "tags set "), ["bug", "chore"]);
|
||||
assert!(
|
||||
!offered(&mut board, "tags set ").contains(&"sync".to_string()),
|
||||
"a carried but undeclared tag was offered where only a declared one is taken"
|
||||
);
|
||||
|
||||
// `tags add` takes a word the board does not have yet, so every tag it has is a
|
||||
// value it refuses, and both `--tags` that take the whole list would have one tag
|
||||
// replace what is already in it.
|
||||
for line in ["tags add ", "add --tags ", "set 1111111 --tags bug,"] {
|
||||
assert!(offered(&mut board, line).is_empty(), "`{line}` offered a candidate");
|
||||
}
|
||||
}
|
||||
|
||||
/// The candidates are what the tab already holds, so a keypress never goes to disk.
|
||||
///
|
||||
/// Pinned by taking the board away from under it: a completion that read the tree would
|
||||
/// answer nothing here, and one reading the rows `reload` built answers exactly what it
|
||||
/// answered before. The rows outlive the directory because they are the same rows the
|
||||
/// `c` and `t` pickers draw.
|
||||
#[test]
|
||||
fn the_candidates_come_off_the_loaded_rows_and_never_off_the_tree() {
|
||||
let (dir, mut board) = completable();
|
||||
|
||||
let tags = offered(&mut board, "ls --tag ");
|
||||
let campaigns = offered(&mut board, "assign ");
|
||||
let id = selected_id(&board);
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
|
||||
assert_eq!(offered(&mut board, "ls --tag "), tags, "the tags were read off the tree");
|
||||
assert_eq!(offered(&mut board, "assign "), campaigns, "the campaigns were");
|
||||
assert_eq!(offered(&mut board, "show "), [id], "the ticket was");
|
||||
// And the fixed lists were never anywhere but the clap definition.
|
||||
assert!(offered(&mut board, "ls ").contains(&"backlog".to_string()));
|
||||
}
|
||||
|
||||
/// `relate --clear` takes the kind first, and the three kinds are what is offered there
|
||||
/// rather than the board listing the other three flags get.
|
||||
#[test]
|
||||
fn relate_clear_is_completed_with_the_relation_kinds() {
|
||||
let (_dir, mut board) = completable();
|
||||
let kinds: Vec<String> = RelationKind::ALL
|
||||
.iter()
|
||||
.map(|kind| kind.flag().to_string())
|
||||
.collect();
|
||||
|
||||
assert_eq!(offered(&mut board, "relate 1111111 --clear "), kinds);
|
||||
assert_eq!(
|
||||
tabbed(&mut board, "relate 1111111 --clear b", 1),
|
||||
"relate 1111111 --clear blocked-by"
|
||||
);
|
||||
// And the target after it is one the board may no longer hold.
|
||||
assert!(offered(&mut board, "relate 1111111 --clear blocked-by ").is_empty());
|
||||
}
|
||||
|
||||
/// The walk itself: the common opening on the first press, then one candidate per press
|
||||
/// in place of the last, round to the start again.
|
||||
#[test]
|
||||
fn tab_takes_the_common_prefix_and_then_steps_through_the_candidates() {
|
||||
let (_dir, mut board) = completable();
|
||||
|
||||
// `comment` and `completions` are what `co` narrows to, and `com` is as far as one
|
||||
// press can go without choosing between them.
|
||||
assert_eq!(tabbed(&mut board, "co", 0), "co");
|
||||
assert_eq!(tabbed(&mut board, "co", 1), "com");
|
||||
assert_eq!(tabbed(&mut board, "co", 2), "comment");
|
||||
assert_eq!(tabbed(&mut board, "co", 3), "completions");
|
||||
assert_eq!(tabbed(&mut board, "co", 4), "comment", "the walk did not come round");
|
||||
|
||||
// With nothing in common the first press is already the first candidate, and each
|
||||
// one replaces the last rather than being typed after it.
|
||||
assert_eq!(tabbed(&mut board, "ls ", 0), "ls ");
|
||||
assert_eq!(tabbed(&mut board, "ls ", 1), "ls backlog");
|
||||
assert_eq!(tabbed(&mut board, "ls ", 2), "ls committed");
|
||||
assert_eq!(tabbed(&mut board, "ls ", 6), "ls backlog");
|
||||
}
|
||||
|
||||
/// What the row draws is what the next press takes, which is why it is nothing where
|
||||
/// the candidates agree on nothing.
|
||||
#[test]
|
||||
fn the_row_draws_the_common_prefix_and_nothing_where_there_is_none() {
|
||||
let (_dir, mut board) = completable();
|
||||
|
||||
assert_eq!(suggested(&mut board, "comm").as_deref(), Some("ent"));
|
||||
assert_eq!(
|
||||
suggested(&mut board, "co").as_deref(),
|
||||
Some("m"),
|
||||
"`comment` and `completions` agree on one more letter"
|
||||
);
|
||||
assert_eq!(suggested(&mut board, "ls "), None, "the five statuses agree on nothing");
|
||||
|
||||
// And nothing at all while a walk is on the row: what is there is a whole candidate.
|
||||
board.open_prompt();
|
||||
type_line(&mut board, "co");
|
||||
press_prompt(&mut board, KeyCode::Tab);
|
||||
press_prompt(&mut board, KeyCode::Tab);
|
||||
assert_eq!(prompt_line(&board), Some("comment"));
|
||||
assert_eq!(board.suggestion(), None, "a second offer was drawn against a candidate");
|
||||
}
|
||||
|
||||
/// The walk is over by the next keypress, so a candidate stepped onto is kept and the
|
||||
/// next press starts a new one.
|
||||
#[test]
|
||||
fn an_edit_ends_the_walk_and_leaves_what_it_chose_on_the_row() {
|
||||
let (_dir, mut board) = completable();
|
||||
|
||||
board.open_prompt();
|
||||
type_line(&mut board, "ls d");
|
||||
press_prompt(&mut board, KeyCode::Tab);
|
||||
assert_eq!(prompt_line(&board), Some("ls done"));
|
||||
|
||||
// A backspace is an edit, so the next press walks the line as it now stands rather
|
||||
// than stepping on from where the last one had got to.
|
||||
press_prompt(&mut board, KeyCode::Backspace);
|
||||
assert_eq!(prompt_line(&board), Some("ls don"));
|
||||
press_prompt(&mut board, KeyCode::Tab);
|
||||
assert_eq!(prompt_line(&board), Some("ls done"));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ the copy key --
|
||||
|
||||
/// A sink that records what it was handed, standing in for the clipboard.
|
||||
|
||||
Reference in New Issue
Block a user