103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
import type { ChatCommandSpec, TaskRef } from "./state";
|
|
|
|
/**
|
|
* The composer palette's arithmetic, kept out of the composer so it is decided
|
|
* in one place rather than reconstructed from DOM positions twice. Two
|
|
* triggers share it: `/` completes a command, `#` picks a task.
|
|
*/
|
|
|
|
export type Trigger = "/" | "#";
|
|
|
|
/** One row of the palette: what is shown, and the key that identifies it. */
|
|
export interface Completion {
|
|
key: string;
|
|
label: string;
|
|
detail: string;
|
|
}
|
|
|
|
/** The trigger word the caret sits in: at the start or after whitespace. */
|
|
const OPEN_TOKEN = /(?:^|\s)([/#])([^\s]*)$/;
|
|
|
|
/** How many matches the palette will show at once. */
|
|
const MAX_MATCHES = 8;
|
|
|
|
/** detectTrigger returns the trigger and the text typed after it, if any. */
|
|
export function detectTrigger(
|
|
value: string,
|
|
caret: number,
|
|
): { trigger: Trigger; query: string } | null {
|
|
const match = value.slice(0, caret).match(OPEN_TOKEN);
|
|
if (!match) return null;
|
|
return { trigger: match[1] as Trigger, query: match[2].toLowerCase() };
|
|
}
|
|
|
|
/** Commands matched on the command and its description together. */
|
|
export function commandCompletions(
|
|
specs: ChatCommandSpec[],
|
|
query: string,
|
|
): Completion[] {
|
|
return specs
|
|
.filter((spec) =>
|
|
`${spec.command} ${spec.description ?? ""}`.toLowerCase().includes(query),
|
|
)
|
|
.slice(0, MAX_MATCHES)
|
|
.map((spec) => ({
|
|
key: spec.command,
|
|
label: spec.command,
|
|
detail: spec.description || spec.usage || "",
|
|
}));
|
|
}
|
|
|
|
/** Tasks matched on id and title, so `#12` and a title word both find one. */
|
|
export function taskCompletions(tasks: TaskRef[], query: string): Completion[] {
|
|
return tasks
|
|
.filter((task) =>
|
|
`${task.id} ${task.title ?? ""}`.toLowerCase().includes(query),
|
|
)
|
|
.slice(0, MAX_MATCHES)
|
|
.map((task) => ({
|
|
key: String(task.id),
|
|
label: `#${task.id}`,
|
|
detail: task.title ?? "",
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* replaceToken swaps the trigger word the caret sits in for `insert` and
|
|
* returns the caret just after it. A space follows the insert unless one
|
|
* already does, so completing mid-sentence never doubles it. An empty insert
|
|
* removes the token.
|
|
*/
|
|
export function replaceToken(
|
|
value: string,
|
|
caret: number,
|
|
insert: string,
|
|
): { text: string; cursor: number } {
|
|
const before = value.slice(0, caret);
|
|
const after = value.slice(caret);
|
|
const start = before.search(/(?:^|\s)[/#][^\s]*$/);
|
|
const tokenStart =
|
|
start < 0 ? before.length : start + (/\s/.test(before[start]) ? 1 : 0);
|
|
const head = before.slice(0, tokenStart);
|
|
const gap = insert === "" || /^\s/.test(after) ? "" : " ";
|
|
// Removing the token must not leave the words either side of it two spaces apart.
|
|
const tail = insert === "" && /\s$/.test(head) ? after.replace(/^[ \t]/, "") : after;
|
|
return {
|
|
text: `${head}${insert}${gap}${tail}`,
|
|
cursor: tokenStart + insert.length + gap.length,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* isExactCommand reports whether the composer already holds a command and
|
|
* nothing else. Enter then sends it rather than completing it, so a command
|
|
* typed in full is never re-entered by the palette that was offering it.
|
|
*/
|
|
export function isExactCommand(
|
|
specs: ChatCommandSpec[],
|
|
value: string,
|
|
): boolean {
|
|
const trimmed = value.trim();
|
|
return specs.some((spec) => spec.command === trimmed);
|
|
}
|