feat: the / menu scrolls through every match and caches the global catalog

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 20:30:52 +01:00
1 parent 02f7f2465b
commit 56fa39dd18
1 file changed
+66 -9
@@ -24,7 +24,7 @@
// from the agent's status.
import { View, div, image } from "gpui-kit";
import { v_flex, h_flex, TextareaState, Textarea, InputState, Input, Popover, VirtualListScrollHandle } from "gpui-base";
import { v_flex, h_flex, TextareaState, Textarea, InputState, Input, Popover, ScrollHandle, VirtualListScrollHandle } from "gpui-base";
import {
agents as liveAgents,
panes as livePanes,
@@ -39,6 +39,9 @@ import {
setShared,
notify,
nextCommand,
home,
readTextFile,
checking,
} from "herdr";
import * as fs from "fs/promises";
@@ -145,10 +148,10 @@ const BLOCK_MAX_PX = 520;
/** The block is resized once the content has been quiet for this long, ms. */
const FIT_DELAY_MS = 120;
/** How many rows the menu of the suggestions shows at most. */
const MENU_LIMIT = 20;
/** How old the catalog of the global commands may be before it is read again, ms. */
const GLOBAL_TTL_MS = 60_000;
/** How much room the menu takes - the same with one row and with twenty. */
/** How much room the menu takes - the same with one row and with a hundred (it scrolls). */
const MENU_HEIGHT = 118;
/** The key context of the composer - the bindings of the mod sit on it. */
@@ -192,6 +195,13 @@ export default class Composer extends View {
// The menu of suggestions: what it shows, which row is picked.
this.menu = null;
// The person's and the plugins' commands: read once, in the background,
// and again when older than a minute (see refreshGlobal). The menu list
// scrolls, and the arrows keep the picked row in view through the handle.
this.globalCommands = null;
this.globalLoadedAt = 0;
this.globalLoading = false;
this.menuScroll = ScrollHandle.new();
this.debounce = null;
this.retry = null;
this.retryAttempt = 0;
@@ -1558,6 +1568,13 @@ export default class Composer extends View {
const menu = this.menu;
const count = (menu.items ?? []).length;
menu.index = (menu.index + step + count) % count;
// The row that is picked stays in view (the list scrolls). A line of
// error or of "nothing fits" is a child before the rows.
try {
this.menuScroll.scroll_to_item(menu.index + (menu.error ? 1 : 0));
} catch {
// The list keeps its place; the keys still work.
}
this.restoreCaret(menu.caret);
cx.notify();
}
@@ -2198,9 +2215,44 @@ export default class Composer extends View {
this.closeMenu();
return;
}
if (token.trigger === "/") this.refreshGlobal(cx);
this.scheduleLoad(agent, token, cx);
}
/**
* The global commands (`~/.claude`, the plugins) are read in the background:
* when there is no catalog yet or it is older than a minute, one read at a
* time. Typing never waits for it and never starts a read of its own.
*/
async refreshGlobal(cx) {
if (this.globalLoading || checking()) return;
if (this.globalCommands !== null && Date.now() - this.globalLoadedAt < GLOBAL_TTL_MS) return;
this.globalLoading = true;
let failed = false;
try {
const items = await catalog.globalCommands(home(), exec, readTextFile, {
onReadError: () => {
failed = true;
},
});
// A failed read keeps the catalog the menu already has.
if (!failed) this.globalCommands = items;
} catch {
failed = true;
}
// Failed or not, the next try is a minute away - not at every key.
this.globalLoadedAt = Date.now();
this.globalLoading = false;
// The menu of `/` is open: it shows the new catalog now.
const agent = this.target();
const menu = this.menu;
if (!failed && agent && menu?.trigger === "/" && this.value.length >= menu.start) {
this.loadSuggestions(agent, { trigger: menu.trigger, needle: menu.needle, start: menu.start }, cx);
}
}
/** The menu is closed and the pending load with it. */
closeMenu() {
if (this.debounce) {
@@ -2239,6 +2291,7 @@ export default class Composer extends View {
const raw = await catalog.commands(cwd, exec, {
agent: agentName(agent),
snippets: [],
global: this.globalCommands ?? [],
onReadError,
});
const usage = storage.loadUsage(this.store);
@@ -2260,6 +2313,12 @@ export default class Composer extends View {
if (this.value !== text) return;
this.menu = { ...token, caret: this.caret(), items, index: 0, error: failed };
// A new list starts at its top.
try {
this.menuScroll.scroll_to_item(0);
} catch {
// The list keeps its place.
}
if (!failed) this.retryAttempt = 0;
cx.notify();
}
@@ -2765,8 +2824,8 @@ export default class Composer extends View {
const menu = this.menu;
if (!menu) return null;
const all = menu.items ?? [];
const items = all.slice(0, MENU_LIMIT);
const items = menu.items ?? [];
const all = items;
const empty = menu.error
? catalog.READ_ERROR_MESSAGE
: menu.trigger === "@"
@@ -2788,6 +2847,7 @@ export default class Composer extends View {
.id("composer-menu-list")
.h(MENU_HEIGHT)
.overflow_y_scroll()
.track_scroll(this.menuScroll)
.gap(1)
.children(
[
@@ -2795,9 +2855,6 @@ export default class Composer extends View {
? div().px(6).py(5).text_size(11.5).text_color(menu.error ? t.blocked : t.dim).child(empty)
: null,
...items.map((item, index) => this.renderMenuItem(t, item, index, menu)),
all.length > items.length
? div().px(6).py(3).text_size(11).text_color(t.dim).child(`…i jeszcze ${all.length - items.length}`)
: null,
].filter(Boolean),
),
);