The best way to control Claude Code, Codex and more from any computer or phone

main
ts 151 lines 5.23 KB
Raw
1 import type {
2 PullRequestActor,
3 PullRequestCapabilities,
4 PullRequestFilterOptions,
5 PullRequestReferenceCandidate,
6 PullRequestReferenceSigil,
7 } from "@t3tools/contracts";
8
9 import { replaceTextRange } from "./composerTrigger.ts";
10
11 export type PullRequestComposerTrigger = {
12 readonly query: string;
13 readonly rangeStart: number;
14 readonly rangeEnd: number;
15 } & (
16 | { readonly kind: "mention"; readonly sigil: "@" }
17 | { readonly kind: "reference"; readonly sigil: PullRequestReferenceSigil }
18 );
19
20 // Logins are ASCII, but the query also matches display names, which can be in any script.
21 // Combining marks (\p{M}) attach to the letter before them, so they cannot start a name.
22 const TOKEN = /^(?:@([\p{L}\p{N}_][\p{L}\p{M}\p{N}_.-]*)?|([#!])([\p{L}\p{N}_-]*))$/u;
23
24 /** What a pull request's host can complete; absent fields complete nothing. */
25 export type PullRequestCompletionSupport = Pick<
26 PullRequestCapabilities,
27 "mentions" | "referenceSigils"
28 >;
29
30 /**
31 * The `@login`, `#123` or `!123` being typed at the caret, when the caret sits at its end and the
32 * host completes that sigil.
33 */
34 export function detectPullRequestComposerTrigger(
35 text: string,
36 cursor: number,
37 support: PullRequestCompletionSupport,
38 ): PullRequestComposerTrigger | null {
39 const end = Math.max(0, Math.min(text.length, cursor));
40 const next = text[end];
41 if (next !== undefined && !/\s/.test(next)) return null;
42 let start = end;
43 while (start > 0 && !/\s/.test(text[start - 1] ?? "")) start -= 1;
44 const match = TOKEN.exec(text.slice(start, end));
45 if (!match) return null;
46 const sigil = match[2];
47 if (sigil === "#" || sigil === "!") {
48 return support.referenceSigils?.includes(sigil)
49 ? { kind: "reference", sigil, query: match[3] ?? "", rangeStart: start, rangeEnd: end }
50 : null;
51 }
52 return support.mentions === true
53 ? { kind: "mention", sigil: "@", query: match[1] ?? "", rangeStart: start, rangeEnd: end }
54 : null;
55 }
56
57 /** Replaces the trigger with `token` and leaves the caret past the space after it. */
58 export function insertPullRequestCompletion(
59 text: string,
60 trigger: PullRequestComposerTrigger,
61 token: string,
62 ): { text: string; cursor: number } {
63 const followedBySpace = /[ \t]/.test(text[trigger.rangeEnd] ?? "");
64 if (!followedBySpace) {
65 return replaceTextRange(text, trigger.rangeStart, trigger.rangeEnd, `${token} `);
66 }
67 const inserted = replaceTextRange(text, trigger.rangeStart, trigger.rangeEnd, token);
68 return { ...inserted, cursor: inserted.cursor + 1 };
69 }
70
71 /** The key a debounce delays: the trigger's sigil and its host search, or `null` with no trigger. */
72 export function pullRequestCompletionKey(
73 trigger: Pick<PullRequestComposerTrigger, "sigil" | "query"> | null,
74 ): string | null {
75 return trigger === null ? null : `${trigger.sigil}${trigger.query.slice(0, 100)}`;
76 }
77
78 /**
79 * The search to send for `trigger` given a debounced key. Until a key for this sigil settles, the
80 * host is asked for its unsearched list, which narrows locally, so a burst of typing sends one query.
81 */
82 export function debouncedPullRequestCompletionSearch(
83 trigger: Pick<PullRequestComposerTrigger, "sigil">,
84 debouncedKey: string | null,
85 ): string | undefined {
86 return debouncedKey?.startsWith(trigger.sigil)
87 ? debouncedKey.slice(trigger.sigil.length) || undefined
88 : undefined;
89 }
90
91 export function narrowPullRequestMentions<A extends { login: string; name: string | null }>(
92 users: ReadonlyArray<A>,
93 typed: string,
94 limit: number,
95 ): A[] {
96 const needle = typed.toLowerCase();
97 return users
98 .filter(
99 (user) =>
100 user.login.toLowerCase().includes(needle) ||
101 (user.name?.toLowerCase().includes(needle) ?? false),
102 )
103 .slice(0, limit);
104 }
105
106 /** Candidates the typed sigil links, by number prefix or title. */
107 export function narrowPullRequestReferences<
108 A extends { reference: string; number: number; title: string },
109 >(
110 candidates: ReadonlyArray<A>,
111 trigger: Pick<PullRequestComposerTrigger, "sigil" | "query">,
112 limit: number,
113 ): A[] {
114 const needle = trigger.query.toLowerCase();
115 return candidates
116 .filter(
117 (candidate) =>
118 candidate.reference.startsWith(trigger.sigil) &&
119 (String(candidate.number).startsWith(needle) ||
120 candidate.title.toLowerCase().includes(needle)),
121 )
122 .slice(0, limit);
123 }
124
125 export type PullRequestCompletion =
126 | { readonly kind: "mention"; readonly token: string; readonly actor: PullRequestActor }
127 | {
128 readonly kind: "reference";
129 readonly token: string;
130 readonly candidate: PullRequestReferenceCandidate;
131 };
132
133 /** The host's options for `trigger`, narrowed to what is typed now. */
134 export function pullRequestCompletions(
135 trigger: PullRequestComposerTrigger | null,
136 options: Pick<PullRequestFilterOptions, "authors" | "references"> | null,
137 limit: number,
138 ): ReadonlyArray<PullRequestCompletion> {
139 if (trigger === null) return [];
140 return trigger.kind === "mention"
141 ? narrowPullRequestMentions(options?.authors ?? [], trigger.query, limit).map((actor) => ({
142 kind: "mention",
143 token: `@${actor.login}`,
144 actor,
145 }))
146 : narrowPullRequestReferences(options?.references ?? [], trigger, limit).map((candidate) => ({
147 kind: "reference",
148 token: candidate.reference,
149 candidate,
150 }));
151 }