(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:
Ansho Rei
2026-09-15 15:18:59 +02:00
co-authored by Claude Opus 5
parent c714c73a75
commit a885f81da5
6 changed files with 697 additions and 113 deletions
+253 -59
View File
@@ -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");
}
}