Lutra/ui/src/components/goto/GotoPopup.vue
danny 943ff889df M2.6: go-to popup for files, classes and symbols
One popup with Files / Classes / Symbols tabs (Tab switches), opened by
goto.fileOpen (Mod+P), goto.classOpen (Mod+Alt+C) and goto.symbolOpen
(Mod+T), each with an optional {query} for agents. Live debounced query
against goto.* with stale answers dropped, matched characters marked,
directory / namespace / container column, library rows labelled, an
indexing hint with progress and a re-query on indexReady. Enter or a
click opens through editor.reveal; Escape closes. An exclusive modal and
a launcher: any other command closes it.

goto.file items now carry `library` (PM decision). indexProgress and
indexReady join the ipc event union. Notes in plan/phpstorm/08-goto.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 17:35:50 +02:00

563 lines
13 KiB
Vue

<script setup lang="ts">
/**
* The go-to popup (M2.6): find a file, a class or a symbol by name and
* jump to it.
*
* - **Modes:** Files, Classes and Symbols, shown as tabs. `goto.fileOpen`
* (`Mod+P`), `goto.classOpen` (`Mod+Alt+C`) and `goto.symbolOpen`
* (`Mod+T`) open it in their mode; Tab and Shift+Tab, or a click on a
* tab, switch the mode and keep the text. Each command takes an optional
* `{query}`, so an agent can open the popup with a query.
* - **Rows:** the matched characters are marked, the directory, namespace
* or container is shown beside the name, and library items (vendor) are
* dimmed and labelled. A hint shows while the index is still building.
* - **Keys:** arrows move the selection, Enter opens it through
* `editor.reveal` (files keep their cursor, symbols put it on the name),
* Escape or a click outside closes.
* - **One modal at a time** (see `commands/modals.ts`): opening the popup
* closes the palette or the settings, and opening one of those closes
* the popup.
* - State lives in `stores/goto.ts`; the go-to commands need an open project.
*/
import {
Box,
Component,
FileCode,
Hash,
ListOrdered,
Puzzle,
SquareFunction,
Variable,
} from "@lucide/vue";
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch, type Component as VueComponent } from "vue";
import { useModalsStore } from "../../commands/modals";
import { useCommandsStore } from "../../commands/store";
import { t } from "../../i18n";
import type { JsonValue } from "../../ipc";
import { GOTO_MODES, useGotoStore, type GotoItem, type GotoMode } from "../../stores/goto";
import { useProjectStore } from "../../stores/project";
import { segments } from "./marks";
const goto = useGotoStore();
const modals = useModalsStore();
const commands = useCommandsStore();
const project = useProjectStore();
/** The search field. */
const input = ref<HTMLInputElement | null>(null);
/** The list. */
const list = ref<HTMLElement | null>(null);
/** The element that had the focus before the popup opened. */
let returnFocus: HTMLElement | null = null;
/** Withdraws the command handlers; set while mounted. */
let withdraw: (() => void)[] = [];
/** UI command that opens each mode. */
const OPEN_COMMAND: Record<GotoMode, string> = {
file: "goto.fileOpen",
class: "goto.classOpen",
symbol: "goto.symbolOpen",
};
/**
* The tab label of a mode.
*
* @param mode - the mode.
*/
function modeLabel(mode: GotoMode): string {
switch (mode) {
case "file":
return t("goto.mode.file");
case "class":
return t("goto.mode.class");
case "symbol":
return t("goto.mode.symbol");
}
}
/** The field's placeholder in the current mode. */
const placeholder = computed(() => {
switch (goto.mode) {
case "file":
return t("goto.placeholder.file");
case "class":
return t("goto.placeholder.class");
case "symbol":
return t("goto.placeholder.symbol");
}
});
/**
* The icon of a row.
*
* @param item - the row.
*/
function iconOf(item: GotoItem): VueComponent {
switch (item.kind) {
case null:
return FileCode;
case "interface":
return Component;
case "trait":
return Puzzle;
case "enum":
case "enumCase":
return ListOrdered;
case "function":
case "method":
return SquareFunction;
case "property":
return Variable;
case "constant":
return Hash;
default:
return Box;
}
}
/** The line under the field: the indexing hint, an error or nothing. */
const hint = computed<string | null>(() => {
if (goto.error !== null) {
return goto.error;
}
if (!goto.indexing) {
return null;
}
const progress = goto.progress;
return progress && progress.total > 0
? t("goto.indexingProgress", { done: progress.done, total: progress.total })
: t("goto.indexing");
});
/** The text shown when there are no rows, or null while rows show. */
const emptyText = computed<string | null>(() => {
if (goto.items.length > 0) {
return null;
}
if (goto.query.trim() === "") {
return t("goto.empty.prompt");
}
return goto.busy ? null : t("goto.empty.noMatches");
});
/** DOM id of the selected row, for `aria-activedescendant`. */
const activeId = computed(() => (goto.items.length > 0 ? `lt-goto-${String(goto.selected)}` : undefined));
/**
* Reads the optional `query` of a command's params.
*
* @param params - the params; untrusted when an agent sent them.
*/
function queryOf(params: JsonValue): string | undefined {
if (typeof params === "object" && params !== null && !Array.isArray(params)) {
const query = params.query;
return typeof query === "string" ? query : undefined;
}
return undefined;
}
/**
* Opens the popup in a mode.
*
* @param mode - the mode.
* @param params - the command's params, maybe with a `query`.
*/
async function show(mode: GotoMode, params: JsonValue): Promise<void> {
modals.opened("goto", close);
if (!goto.open) {
const active = document.activeElement;
returnFocus = active instanceof HTMLElement ? active : null;
}
const searching = goto.show(mode, queryOf(params));
await nextTick();
input.value?.focus();
input.value?.select();
await searching;
}
/** Closes the popup and gives the focus back. */
function close(): void {
if (!goto.open) {
return;
}
goto.hide();
modals.closed("goto");
returnFocus?.focus();
returnFocus = null;
}
/**
* Opens a row's file at its position; the popup closes first.
*
* @param item - the row.
*/
function choose(item: GotoItem | undefined): void {
if (!item) {
return;
}
close();
const params: JsonValue =
item.line === null ? { path: item.path } : { path: item.path, line: item.line, column: item.column ?? 0 };
void commands.execute("editor.reveal", params);
}
/** Keeps the selected row in view. */
function scrollSelected(): void {
void nextTick(() => {
const row = list.value?.querySelector<HTMLElement>(`#lt-goto-${String(goto.selected)}`);
row?.scrollIntoView({ block: "nearest" });
});
}
/**
* Keyboard handling of the search field.
*
* @param event - the key press.
*/
function onKeydown(event: KeyboardEvent): void {
switch (event.key) {
case "ArrowDown":
goto.move(1);
scrollSelected();
break;
case "ArrowUp":
goto.move(-1);
scrollSelected();
break;
case "PageDown":
goto.move(10);
scrollSelected();
break;
case "PageUp":
goto.move(-10);
scrollSelected();
break;
case "Tab":
goto.cycleMode(event.shiftKey ? -1 : 1);
break;
case "Enter":
choose(goto.current);
break;
case "Escape":
close();
break;
default:
return;
}
event.preventDefault();
event.stopPropagation();
}
/**
* Switches the mode from a tab click and keeps the focus in the field.
*
* @param mode - the tab's mode.
*/
function pickMode(mode: GotoMode): void {
goto.setMode(mode);
input.value?.focus();
}
watch(
() => goto.focusRequest,
() => {
void nextTick(() => input.value?.focus());
},
);
onMounted(() => {
goto.start();
const hasProject = (): boolean => project.root !== null;
withdraw = GOTO_MODES.map((mode) =>
commands.provide(OPEN_COMMAND[mode], { run: (params) => show(mode, params), enabled: hasProject }),
);
});
onBeforeUnmount(() => {
for (const fn of withdraw) {
fn();
}
withdraw = [];
goto.stop();
});
defineExpose({ show, close });
</script>
<template>
<div
v-if="goto.open"
class="goto"
data-test="goto"
@mousedown.self.prevent="close()"
>
<div
class="goto__dialog"
role="dialog"
aria-modal="true"
:aria-label="t('goto.label')"
>
<div
class="goto__modes"
role="tablist"
:aria-label="t('goto.modes')"
>
<button
v-for="mode in GOTO_MODES"
:key="mode"
type="button"
class="goto__mode"
:class="{ 'goto__mode--active': mode === goto.mode }"
role="tab"
:aria-selected="mode === goto.mode"
tabindex="-1"
:data-mode="mode"
data-test="goto-mode"
@mousedown.prevent
@click="pickMode(mode)"
>
{{ modeLabel(mode) }}
</button>
<span class="goto__switch">{{ t("goto.switchHint") }}</span>
</div>
<input
ref="input"
:value="goto.query"
class="goto__input"
type="text"
role="combobox"
aria-autocomplete="list"
aria-expanded="true"
aria-controls="lt-goto-list"
:aria-activedescendant="activeId"
:aria-label="placeholder"
:placeholder="placeholder"
spellcheck="false"
autocomplete="off"
data-test="goto-input"
@input="goto.setQuery(($event.target as HTMLInputElement).value)"
@keydown="onKeydown"
>
<p
v-if="hint !== null"
class="goto__hint"
:class="{ 'goto__hint--error': goto.error !== null }"
role="status"
data-test="goto-hint"
>
{{ hint }}
</p>
<ul
id="lt-goto-list"
ref="list"
class="goto__list"
role="listbox"
:aria-label="t('goto.label')"
>
<li
v-for="(item, index) in goto.items"
:id="`lt-goto-${String(index)}`"
:key="item.key"
class="goto__row"
:class="{
'goto__row--selected': index === goto.selected,
'goto__row--library': item.library,
}"
role="option"
:aria-selected="index === goto.selected"
:data-path="item.path"
data-test="goto-row"
@mousemove="goto.selected = index"
@mousedown.prevent
@click="choose(item)"
>
<component
:is="iconOf(item)"
class="goto__icon"
:size="14"
aria-hidden="true"
/>
<span
class="goto__label"
data-test="goto-label"
><template
v-for="(part, i) in segments(item.label, item.labelMatches)"
:key="i"
><mark
v-if="part.mark"
class="goto__mark"
>{{ part.text }}</mark><template v-else>{{ part.text }}</template></template></span>
<span
class="goto__detail"
data-test="goto-detail"
><template
v-for="(part, i) in segments(item.detail, item.detailMatches)"
:key="i"
><mark
v-if="part.mark"
class="goto__mark"
>{{ part.text }}</mark><template v-else>{{ part.text }}</template></template></span>
<span
v-if="item.library"
class="goto__library"
data-test="goto-library"
>{{ t("goto.library") }}</span>
</li>
</ul>
<p
v-if="emptyText !== null"
class="goto__empty"
role="status"
data-test="goto-empty"
>
{{ emptyText }}
</p>
</div>
</div>
</template>
<style scoped>
.goto {
position: fixed;
inset: 0;
display: flex;
align-items: flex-start;
justify-content: center;
padding-top: 12vh;
background: color-mix(in srgb, var(--lt-bg) 40%, transparent);
z-index: 30;
}
.goto__dialog {
width: min(680px, 90vw);
max-height: 60vh;
display: flex;
flex-direction: column;
background: var(--lt-bg);
color: var(--lt-fg);
border: 1px solid var(--lt-border);
border-radius: 6px;
overflow: hidden;
}
.goto__modes {
display: flex;
align-items: center;
gap: 2px;
padding: 4px 8px 0;
}
.goto__mode {
padding: 4px 10px;
font: inherit;
color: var(--lt-fg-muted);
background: none;
border: none;
border-bottom: 2px solid transparent;
cursor: default;
}
.goto__mode--active {
color: var(--lt-fg);
border-bottom-color: var(--lt-accent);
}
.goto__switch {
margin-left: auto;
font-size: 0.85em;
color: var(--lt-fg-muted);
}
.goto__input {
padding: 8px 12px;
font: inherit;
color: var(--lt-fg);
background: var(--lt-editor-bg);
border: none;
border-top: 1px solid var(--lt-border);
border-bottom: 1px solid var(--lt-border);
outline: none;
}
.goto__hint {
margin: 0;
padding: 4px 12px;
font-size: 0.85em;
color: var(--lt-fg-muted);
border-bottom: 1px solid var(--lt-border);
}
.goto__hint--error {
color: var(--lt-error);
}
.goto__list {
margin: 0;
padding: 4px 0;
list-style: none;
overflow-y: auto;
}
.goto__row {
display: flex;
align-items: center;
gap: 8px;
padding: 3px 12px;
cursor: default;
white-space: nowrap;
}
.goto__row--selected {
background: var(--lt-editor-selection-bg);
}
.goto__icon {
flex-shrink: 0;
color: var(--lt-fg-muted);
}
.goto__label {
flex-shrink: 0;
}
.goto__detail {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
color: var(--lt-fg-muted);
}
.goto__mark {
color: inherit;
background: var(--lt-editor-match-bg);
font-weight: 700;
text-decoration: underline;
text-underline-offset: 2px;
border-radius: 2px;
}
.goto__row--library .goto__label {
color: var(--lt-fg-muted);
}
.goto__library {
flex-shrink: 0;
padding: 0 6px;
font-size: 0.8em;
color: var(--lt-fg-muted);
border: 1px solid var(--lt-border);
border-radius: 3px;
}
.goto__row--selected .goto__detail,
.goto__row--selected .goto__library {
color: var(--lt-fg);
}
.goto__empty {
margin: 0;
padding: 8px 12px;
color: var(--lt-fg-muted);
}
</style>