M2.7: Structure tool window and syntax.outline
Engine: every open PHP buffer keeps a tree-sitter tree, reparsed
incrementally on a worker thread off the engine lock (200 ms debounce,
edit batches recorded by buffer.edit, full parse when the chain breaks).
syntax.outline {id} returns {items, truncated, version}; outlineChanged
{id} at most every 200 ms per buffer. Replaced trees and outlines are
freed on the worker.
UI: left stripe with Project | Structure in MainLayout, the Structure
tool window (WAI-ARIA tree, Lucide kind icons, signatures, jumps via
editor.reveal), view.toggleStructure on Mod+2.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
73819df926
commit
c68932c39c
27 changed files with 2596 additions and 28 deletions
434
ui/src/components/structure/StructureToolWindow.vue
Normal file
434
ui/src/components/structure/StructureToolWindow.vue
Normal file
|
|
@ -0,0 +1,434 @@
|
|||
<script setup lang="ts">
|
||||
/**
|
||||
* The Structure tool window (M2.7): the declarations of the active editor
|
||||
* tab as a keyboard-navigable tree (WAI-ARIA tree view), with a kind icon,
|
||||
* the name and the short signature.
|
||||
*
|
||||
* Keys: Up/Down move the selection, Home/End jump to the first/last row,
|
||||
* Right expands a node or moves into it, Left collapses a node or moves to
|
||||
* its parent, Enter jumps to the declaration. A click selects a row and
|
||||
* jumps too; the chevron only expands or collapses. Jumps run the UI
|
||||
* command `editor.reveal {path, line, column}` at the start of the name.
|
||||
* Without a tab, for a file without declarations and for a file too large
|
||||
* to parse the window says so. State lives in {@link useStructureStore},
|
||||
* which follows the active tab and `outlineChanged` while the window is
|
||||
* mounted.
|
||||
*/
|
||||
import {
|
||||
Box,
|
||||
ChevronDown,
|
||||
ChevronRight,
|
||||
CircleDot,
|
||||
Component,
|
||||
Layers,
|
||||
ListOrdered,
|
||||
Package,
|
||||
Parentheses,
|
||||
SquareFunction,
|
||||
SquarePi,
|
||||
Variable,
|
||||
} from "@lucide/vue";
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, ref, type Component as VueComponent } from "vue";
|
||||
import { useCommandsStore } from "../../commands/store";
|
||||
import { t } from "../../i18n";
|
||||
import { useEditorStore } from "../../stores/editor";
|
||||
import { parentKey, useStructureStore, type OutlineKind, type StructureRow } from "./structure";
|
||||
|
||||
const structure = useStructureStore();
|
||||
const editor = useEditorStore();
|
||||
const commands = useCommandsStore();
|
||||
|
||||
/** The tree element, for keeping the selected row in view. */
|
||||
const treeEl = ref<HTMLElement | null>(null);
|
||||
|
||||
/** Lucide icon per kind. */
|
||||
const ICONS: Record<OutlineKind, VueComponent> = {
|
||||
namespace: Package,
|
||||
class: Box,
|
||||
interface: Component,
|
||||
trait: Layers,
|
||||
enum: ListOrdered,
|
||||
function: SquareFunction,
|
||||
method: Parentheses,
|
||||
property: Variable,
|
||||
constant: SquarePi,
|
||||
enumCase: CircleDot,
|
||||
};
|
||||
|
||||
/** Accessible name per kind. */
|
||||
const KIND_LABELS: Record<OutlineKind, () => string> = {
|
||||
namespace: () => t("structure.kind.namespace"),
|
||||
class: () => t("structure.kind.class"),
|
||||
interface: () => t("structure.kind.interface"),
|
||||
trait: () => t("structure.kind.trait"),
|
||||
enum: () => t("structure.kind.enum"),
|
||||
function: () => t("structure.kind.function"),
|
||||
method: () => t("structure.kind.method"),
|
||||
property: () => t("structure.kind.property"),
|
||||
constant: () => t("structure.kind.constant"),
|
||||
enumCase: () => t("structure.kind.enumCase"),
|
||||
};
|
||||
|
||||
/** Index of the selected row, -1 if none. */
|
||||
const selectedIndex = computed(() => structure.rows.findIndex((row) => row.key === structure.selected));
|
||||
|
||||
/**
|
||||
* DOM id of a row, for `aria-activedescendant`.
|
||||
*
|
||||
* @param key - the row key.
|
||||
*/
|
||||
function rowId(key: string): string {
|
||||
return `lt-structure-${encodeURIComponent(key)}`;
|
||||
}
|
||||
|
||||
/** Id of the selected row, or undefined. */
|
||||
const activeId = computed(() =>
|
||||
structure.selected !== null && selectedIndex.value >= 0 ? rowId(structure.selected) : undefined,
|
||||
);
|
||||
|
||||
/** Text shown instead of the tree, or null when the tree shows. */
|
||||
const emptyText = computed(() => {
|
||||
if (editor.activeId === null) {
|
||||
return t("structure.empty.noFile");
|
||||
}
|
||||
if (structure.error) {
|
||||
return null;
|
||||
}
|
||||
if (structure.truncated) {
|
||||
return t("structure.empty.tooLarge");
|
||||
}
|
||||
if (structure.items.length === 0) {
|
||||
return structure.pending ? t("structure.empty.parsing") : t("structure.empty.none");
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
/** Scrolls the selected row into view after the DOM updated. */
|
||||
async function revealSelected(): Promise<void> {
|
||||
await nextTick();
|
||||
const id = activeId.value;
|
||||
if (id === undefined || !treeEl.value) {
|
||||
return;
|
||||
}
|
||||
const el = treeEl.value.querySelector(`#${CSS.escape(id)}`);
|
||||
if (el instanceof HTMLElement && typeof el.scrollIntoView === "function") {
|
||||
el.scrollIntoView({ block: "nearest" });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the row at an index, clamped to the list.
|
||||
*
|
||||
* @param index - the wanted index.
|
||||
*/
|
||||
function selectIndex(index: number): void {
|
||||
const rows = structure.rows;
|
||||
const row = rows[Math.max(0, Math.min(index, rows.length - 1))];
|
||||
if (row) {
|
||||
structure.select(row.key);
|
||||
void revealSelected();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Jumps to a row's declaration in the editor.
|
||||
*
|
||||
* @param row - the row.
|
||||
*/
|
||||
function jump(row: StructureRow): void {
|
||||
const path = structure.path;
|
||||
if (path === null) {
|
||||
return;
|
||||
}
|
||||
void commands.execute("editor.reveal", { path, line: row.node.start.line, column: row.node.start.column });
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a click on a row: selects it and jumps there.
|
||||
*
|
||||
* @param row - the clicked row.
|
||||
*/
|
||||
function onRowClick(row: StructureRow): void {
|
||||
structure.select(row.key);
|
||||
jump(row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a click on a chevron: selects the row and toggles it.
|
||||
*
|
||||
* @param row - the row.
|
||||
*/
|
||||
function onChevronClick(row: StructureRow): void {
|
||||
structure.select(row.key);
|
||||
if (row.parent) {
|
||||
structure.setExpanded(row.key, !row.expanded);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves right: expands a collapsed node, or enters an expanded one.
|
||||
*
|
||||
* @param row - the selected row.
|
||||
*/
|
||||
function moveRight(row: StructureRow): void {
|
||||
if (!row.parent) {
|
||||
return;
|
||||
}
|
||||
if (!row.expanded) {
|
||||
structure.setExpanded(row.key, true);
|
||||
return;
|
||||
}
|
||||
selectIndex(selectedIndex.value + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves left: collapses an expanded node, or selects the parent.
|
||||
*
|
||||
* @param row - the selected row.
|
||||
*/
|
||||
function moveLeft(row: StructureRow): void {
|
||||
if (row.expanded) {
|
||||
structure.setExpanded(row.key, false);
|
||||
return;
|
||||
}
|
||||
const parent = parentKey(row.key);
|
||||
if (parent !== null) {
|
||||
structure.select(parent);
|
||||
void revealSelected();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keyboard navigation of the tree.
|
||||
*
|
||||
* @param event - the key event on the tree element.
|
||||
*/
|
||||
function onKeydown(event: KeyboardEvent): void {
|
||||
const index = selectedIndex.value;
|
||||
const row = index >= 0 ? structure.rows[index] : undefined;
|
||||
switch (event.key) {
|
||||
case "ArrowDown":
|
||||
selectIndex(row ? index + 1 : 0);
|
||||
break;
|
||||
case "ArrowUp":
|
||||
selectIndex(row ? index - 1 : 0);
|
||||
break;
|
||||
case "Home":
|
||||
selectIndex(0);
|
||||
break;
|
||||
case "End":
|
||||
selectIndex(structure.rows.length - 1);
|
||||
break;
|
||||
case "ArrowRight":
|
||||
if (row) {
|
||||
moveRight(row);
|
||||
}
|
||||
break;
|
||||
case "ArrowLeft":
|
||||
if (row) {
|
||||
moveLeft(row);
|
||||
}
|
||||
break;
|
||||
case "Enter":
|
||||
if (row) {
|
||||
jump(row);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
/** Selects the first row when the tree gains focus without a selection. */
|
||||
function onFocus(): void {
|
||||
if (selectedIndex.value < 0) {
|
||||
selectIndex(0);
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
structure.start();
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
structure.stop();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="structure-view">
|
||||
<p
|
||||
v-if="structure.error"
|
||||
class="structure-view__note structure-view__note--error"
|
||||
role="alert"
|
||||
data-test="structure-error"
|
||||
>
|
||||
{{ structure.error.message }}
|
||||
</p>
|
||||
<p
|
||||
v-if="emptyText !== null"
|
||||
class="structure-view__note"
|
||||
data-test="structure-empty"
|
||||
>
|
||||
{{ emptyText }}
|
||||
</p>
|
||||
<div
|
||||
v-else-if="structure.rows.length > 0"
|
||||
ref="treeEl"
|
||||
class="structure-tree"
|
||||
role="tree"
|
||||
tabindex="0"
|
||||
:aria-label="t('structure.tree.label')"
|
||||
:aria-activedescendant="activeId"
|
||||
data-test="structure-tree"
|
||||
@keydown="onKeydown"
|
||||
@focus="onFocus"
|
||||
>
|
||||
<div
|
||||
v-for="row in structure.rows"
|
||||
:id="rowId(row.key)"
|
||||
:key="row.key"
|
||||
class="structure-tree__row"
|
||||
:class="{ 'structure-tree__row--selected': row.key === structure.selected }"
|
||||
role="treeitem"
|
||||
:aria-level="row.depth + 1"
|
||||
:aria-selected="row.key === structure.selected"
|
||||
:aria-expanded="row.parent ? row.expanded : undefined"
|
||||
:style="{ '--tree-depth': row.depth }"
|
||||
data-test="structure-row"
|
||||
:data-kind="row.node.kind"
|
||||
@click="onRowClick(row)"
|
||||
>
|
||||
<span
|
||||
class="structure-tree__chevron"
|
||||
data-test="structure-chevron"
|
||||
@click.stop="onChevronClick(row)"
|
||||
>
|
||||
<template v-if="row.parent">
|
||||
<ChevronDown
|
||||
v-if="row.expanded"
|
||||
:size="14"
|
||||
/>
|
||||
<ChevronRight
|
||||
v-else
|
||||
:size="14"
|
||||
/>
|
||||
</template>
|
||||
</span>
|
||||
<span
|
||||
class="structure-tree__icon"
|
||||
:class="`structure-tree__icon--${row.node.kind}`"
|
||||
:title="KIND_LABELS[row.node.kind]()"
|
||||
>
|
||||
<component
|
||||
:is="ICONS[row.node.kind]"
|
||||
:size="16"
|
||||
:aria-label="KIND_LABELS[row.node.kind]()"
|
||||
/>
|
||||
</span>
|
||||
<span class="structure-tree__name">{{ row.node.name }}</span>
|
||||
<span
|
||||
v-if="row.node.detail !== null"
|
||||
class="structure-tree__detail"
|
||||
>{{ row.node.detail }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.structure-view {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.structure-view__note {
|
||||
margin: 0;
|
||||
padding: var(--lt-space-m, 8px);
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.structure-view__note--error {
|
||||
color: var(--lt-error);
|
||||
}
|
||||
|
||||
.structure-tree {
|
||||
flex: 1;
|
||||
overflow: auto;
|
||||
padding: var(--lt-space-xs, 2px) 0;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.structure-tree__row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--lt-space-xs, 2px);
|
||||
height: var(--lt-row-height, 22px);
|
||||
padding-left: calc(var(--lt-space-s, 4px) + var(--tree-depth) * var(--lt-tree-indent, 16px));
|
||||
padding-right: var(--lt-space-m, 8px);
|
||||
white-space: nowrap;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.structure-tree__row:hover {
|
||||
background: color-mix(in srgb, var(--lt-fg) 6%, transparent);
|
||||
}
|
||||
|
||||
.structure-tree__row--selected {
|
||||
background: color-mix(in srgb, var(--lt-accent) 18%, transparent);
|
||||
}
|
||||
|
||||
.structure-tree:focus-visible .structure-tree__row--selected {
|
||||
background: color-mix(in srgb, var(--lt-accent) 32%, transparent);
|
||||
}
|
||||
|
||||
.structure-tree__chevron {
|
||||
display: inline-flex;
|
||||
width: 14px;
|
||||
flex: none;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.structure-tree__icon {
|
||||
display: inline-flex;
|
||||
flex: none;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.structure-tree__icon--namespace,
|
||||
.structure-tree__icon--class,
|
||||
.structure-tree__icon--interface,
|
||||
.structure-tree__icon--trait,
|
||||
.structure-tree__icon--enum {
|
||||
color: var(--lt-syntax-type);
|
||||
}
|
||||
|
||||
.structure-tree__icon--function,
|
||||
.structure-tree__icon--method {
|
||||
color: var(--lt-syntax-function);
|
||||
}
|
||||
|
||||
.structure-tree__icon--property {
|
||||
color: var(--lt-syntax-variable);
|
||||
}
|
||||
|
||||
.structure-tree__icon--constant,
|
||||
.structure-tree__icon--enumCase {
|
||||
color: var(--lt-syntax-constant);
|
||||
}
|
||||
|
||||
.structure-tree__name {
|
||||
flex: none;
|
||||
}
|
||||
|
||||
.structure-tree__detail {
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
margin-left: var(--lt-space-s, 4px);
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
</style>
|
||||
Loading…
Add table
Add a link
Reference in a new issue