From 2ec16a9dfe88cff0b2246e2ab458fd5665d0983b Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:12:33 -0800 Subject: [PATCH 1/3] Add AST panel to Playground v2 Match the v1 AST viewer to help users understand parser output for the active file. --- packages/playground-v2/src/ast-types.ts | 8 + packages/playground-v2/src/index.html | 13 + packages/playground-v2/src/main.ts | 241 ++++++++++++++++++ .../playground-v2/src/native-compiler.test.ts | 13 + packages/playground-v2/src/native-compiler.ts | 148 ++++++++++- packages/playground-v2/src/native.ts | 13 + packages/playground-v2/src/native.worker.ts | 21 +- packages/playground-v2/src/strada.ts | 16 ++ packages/playground-v2/src/strada.worker.ts | 91 +++++++ packages/playground-v2/src/styles.css | 126 +++++++++ 10 files changed, 675 insertions(+), 15 deletions(-) create mode 100644 packages/playground-v2/src/ast-types.ts diff --git a/packages/playground-v2/src/ast-types.ts b/packages/playground-v2/src/ast-types.ts new file mode 100644 index 000000000000..c48cea7b1434 --- /dev/null +++ b/packages/playground-v2/src/ast-types.ts @@ -0,0 +1,8 @@ +export type AstNode = { + detail: string + hasChildren: boolean + id: string + kind: string + start: number + end: number +} diff --git a/packages/playground-v2/src/index.html b/packages/playground-v2/src/index.html index 3c93de346730..5d2b9db8e19c 100644 --- a/packages/playground-v2/src/index.html +++ b/packages/playground-v2/src/index.html @@ -124,6 +124,19 @@

Emit

+
+
+
+ +

AST

+
+ Active file +
+
+

Waiting for the compiler.

+ +
+
Problems diff --git a/packages/playground-v2/src/main.ts b/packages/playground-v2/src/main.ts index f7b31df12421..84ff69397083 100644 --- a/packages/playground-v2/src/main.ts +++ b/packages/playground-v2/src/main.ts @@ -35,6 +35,7 @@ import { configureExports, decodeCompressedHash } from "./exporter" import { resolveSymlinks, validateProjectSymlinks, type Symlinks } from "./symlinks" import { isSourceLanguage, languageForFile, sourceLanguages } from "./editor-languages" import { StradaBackend } from "./strada" +import type { AstNode } from "./ast-types" import { NativeBackend } from "./native" import { PackageTypeAcquirer, stradaDiscovery, type AcquisitionResult, type DiscoverDependencies } from "./ata" import { acquisitionOptions } from "./ata/config" @@ -94,6 +95,7 @@ type VersionedProjectState = ProjectState & { } type LayoutState = { + astVisible: boolean emitVisible: boolean filesVisible: boolean filesWidth: number @@ -192,6 +194,18 @@ const toggleOutputButton = getElement("toggle-output-button") const showFilesButton = getElement("show-files-button") const showOutputButton = getElement("show-output-button") const toggleEmitButton = getElement("toggle-emit-button") +const toggleAstButton = getElement("toggle-ast-button") +const astPanel = getElement("ast-panel") +const astMessage = getElement("ast-message") +const astTree = getElement("ast-tree") +let astRefreshTimer = 0 +let astCursorTimer = 0 +let astRenderVersion = 0 +let astCursorVersion = 0 +let astDecorations: string[] = [] +let astDecorationModel: monaco.editor.ITextModel | undefined +let astRootNode: AstNode | undefined +const astEntries = new Map() const toggleRunOutputButton = getElement("toggle-run-output-button") const navigateBackButton = getElement("navigate-back-button") const currentFile = getElement("current-file") @@ -441,10 +455,12 @@ inputEditor.onDidChangeModel(() => { trackedEditorLocation = nextLocation updateActiveFile() updateNavigationButtons() + scheduleAstRefresh() }) inputEditor.onDidChangeCursorSelection(() => { trackedEditorLocation = getEditorLocation() scheduleProjectPersistence() + scheduleAstCursorFollow() }) window.addEventListener("pagehide", () => { if (playgroundSettings.saveToUrl) persistProjectState() @@ -478,6 +494,7 @@ refreshCompilerOverrides() function loadLayoutState(): LayoutState { const defaults: LayoutState = { + astVisible: true, emitVisible: true, filesVisible: true, filesWidth: 208, @@ -488,6 +505,7 @@ function loadLayoutState(): LayoutState { try { const stored = JSON.parse(localStorage.getItem(layoutStorageKey) ?? "{}") return { + astVisible: typeof stored.astVisible === "boolean" ? stored.astVisible : defaults.astVisible, emitVisible: typeof stored.emitVisible === "boolean" ? stored.emitVisible : defaults.emitVisible, filesVisible: typeof stored.filesVisible === "boolean" ? stored.filesVisible : defaults.filesVisible, filesWidth: typeof stored.filesWidth === "number" ? stored.filesWidth : defaults.filesWidth, @@ -674,6 +692,7 @@ function applyLayoutState() { workspace.dataset.filesCollapsed = String(!layoutState.filesVisible) workspace.dataset.outputCollapsed = String(!layoutState.outputVisible) emitOutput.hidden = !layoutState.emitVisible + astPanel.dataset.collapsed = String(!layoutState.astVisible) runOutput.dataset.collapsed = String(!layoutState.runVisible) showFilesButton.hidden = layoutState.filesVisible @@ -686,6 +705,10 @@ function applyLayoutState() { toggleEmitButton.title = layoutState.emitVisible ? "Collapse Emit" : "Expand Emit" toggleEmitButton.setAttribute("aria-label", toggleEmitButton.title) toggleEmitButton.setAttribute("aria-expanded", String(layoutState.emitVisible)) + toggleAstButton.textContent = layoutState.astVisible ? "▾" : "▸" + toggleAstButton.title = layoutState.astVisible ? "Collapse AST" : "Expand AST" + toggleAstButton.setAttribute("aria-label", toggleAstButton.title) + toggleAstButton.setAttribute("aria-expanded", String(layoutState.astVisible)) toggleRunOutputButton.textContent = layoutState.runVisible ? "▾" : "▸" toggleRunOutputButton.title = layoutState.runVisible ? "Collapse Run output" : "Expand Run output" toggleRunOutputButton.setAttribute("aria-label", toggleRunOutputButton.title) @@ -864,6 +887,11 @@ toggleFilesButton.addEventListener("click", () => toggleWorkspacePanel("files")) showFilesButton.addEventListener("click", () => toggleWorkspacePanel("files")) toggleOutputButton.addEventListener("click", () => toggleWorkspacePanel("output")) showOutputButton.addEventListener("click", () => toggleWorkspacePanel("output")) +toggleAstButton.addEventListener("click", () => { + layoutState.astVisible = !layoutState.astVisible + applyLayoutState() + persistLayoutState() +}) toggleEmitButton.addEventListener("click", () => { layoutState.emitVisible = !layoutState.emitVisible applyLayoutState() @@ -956,6 +984,7 @@ async function initializeNativeCompiler() { discoverDependencies = file => backend.discover(file) compileActiveProject = compileNativeProject compilerReady = true + scheduleAstRefresh() startLanguageServer(module, libFiles) await compileNativeProject() void refreshTypeAcquisition() @@ -1035,6 +1064,7 @@ async function initializeStradaCompiler(requestedVersion: string) { compileActiveProject = compileStradaProject compilerReady = true lspReady = true + scheduleAstRefresh() await compileStradaProject() void refreshTypeAcquisition() renderStatus() @@ -2300,6 +2330,215 @@ function diagnosticSeverity(category: number) { } } +type AstTreeEntry = { + element: HTMLLIElement + node: AstNode + row: HTMLDivElement + toggle?: HTMLButtonElement +} + +function scheduleAstRefresh() { + window.clearTimeout(astRefreshTimer) + astRefreshTimer = window.setTimeout(() => void refreshAstTree(), 180) +} + +async function refreshAstTree() { + const version = ++astRenderVersion + astCursorVersion++ + clearAstDecoration() + astEntries.clear() + astRootNode = undefined + astTree.replaceChildren() + astTree.hidden = true + astMessage.hidden = false + const model = inputEditor.getModel() + if (!model || !isSourceLanguage(model.getLanguageId())) { + astMessage.textContent = "AST view is available for TypeScript and JavaScript files." + return + } + if (!compilerReady) { + astMessage.textContent = "Waiting for the compiler." + return + } + astMessage.textContent = "Loading AST…" + try { + const root = await requestAstRoot(model.uri.path) + if (version !== astRenderVersion || inputEditor.getModel() !== model) return + if (!root) { + astMessage.textContent = "AST is unavailable for this file with the selected compiler." + return + } + astRootNode = root + astTree.hidden = false + astMessage.hidden = true + renderAstEntry(astTree, root) + await followAstCursor(version) + } catch (error) { + if (version !== astRenderVersion) return + const reason = error instanceof Error ? error.message.split("\n", 1)[0] : String(error) + astMessage.textContent = `AST view unavailable: ${reason}` + } +} + +function nativeAstInput() { + return { + configFileName, + files: compilerFileContents(), + sourceFiles: [...projectModels] + .filter(([, model]) => isSourceLanguage(model.getLanguageId())) + .map(([fileName]) => fileName), + symlinks: projectSymlinks, + } +} + +function requestAstRoot(fileName: string) { + if (useNativeCompiler) return nativeBackend?.astRoot(nativeAstInput(), fileName) + return stradaBackend?.astRoot(fileName) +} + +function requestAstChildren(fileName: string, id: string) { + if (useNativeCompiler) return nativeBackend?.astChildren(fileName, id) + return stradaBackend?.astChildren(fileName, id) +} + +function requestAstPath(fileName: string, position: number) { + if (useNativeCompiler) return nativeBackend?.astPath(fileName, position) + return stradaBackend?.astPath(fileName, position) +} + +function renderAstEntry(parent: HTMLElement, node: AstNode) { + const element = document.createElement("li") + element.className = "ast-node" + element.setAttribute("role", "treeitem") + element.setAttribute("aria-level", String(node.id === "" ? 1 : node.id.split(".").length + 1)) + if (node.hasChildren) element.setAttribute("aria-expanded", "false") + const row = document.createElement("div") + row.className = "ast-node-row" + const toggle = node.hasChildren ? document.createElement("button") : undefined + if (toggle) { + toggle.className = "ast-node-toggle" + toggle.type = "button" + toggle.setAttribute("aria-label", `Expand ${node.kind}`) + toggle.appendChild(createText("span", "▸", "ast-node-arrow")) + toggle.addEventListener("click", () => void toggleAstEntry(node, toggle, element)) + toggle.addEventListener("pointerenter", () => highlightAstNode(node)) + toggle.addEventListener("focus", () => highlightAstNode(node)) + row.appendChild(toggle) + } else { + row.appendChild(createText("span", "", "ast-node-arrow")) + } + const select = document.createElement("button") + select.className = "ast-node-select" + select.type = "button" + select.title = `${node.kind}${node.detail ? ` · ${node.detail}` : ""}` + select.appendChild(createText("span", node.kind, "ast-node-kind")) + if (node.detail) select.appendChild(createText("span", node.detail, "ast-node-detail")) + select.addEventListener("click", () => selectAstNode(node)) + select.addEventListener("pointerenter", () => highlightAstNode(node)) + select.addEventListener("focus", () => highlightAstNode(node)) + row.addEventListener("pointerleave", clearAstDecoration) + row.addEventListener("focusout", event => { + if (!row.contains(event.relatedTarget as Node | null)) clearAstDecoration() + }) + row.appendChild(select) + element.appendChild(row) + parent.appendChild(element) + astEntries.set(node.id, { element, node, row, toggle }) +} + +async function toggleAstEntry(node: AstNode, toggle: HTMLButtonElement, element: HTMLLIElement) { + const expanded = element.getAttribute("aria-expanded") === "true" + const existingChildren = element.querySelector(".ast-node-children") + if (expanded) { + element.setAttribute("aria-expanded", "false") + toggle.setAttribute("aria-label", `Expand ${node.kind}`) + toggle.querySelector(".ast-node-arrow")!.textContent = "▸" + if (existingChildren) existingChildren.hidden = true + return + } + element.setAttribute("aria-expanded", "true") + toggle.setAttribute("aria-label", `Collapse ${node.kind}`) + toggle.querySelector(".ast-node-arrow")!.textContent = "▾" + if (existingChildren) { + existingChildren.hidden = false + return + } + const model = inputEditor.getModel() + if (!model) return + const version = astRenderVersion + const children = await requestAstChildren(model.uri.path, node.id) + if ( + !children || + version !== astRenderVersion || + !element.isConnected || + element.getAttribute("aria-expanded") !== "true" + ) + return + const list = document.createElement("ul") + list.className = "ast-node-children" + list.setAttribute("role", "group") + for (const child of children) renderAstEntry(list, child) + element.appendChild(list) + element.dataset.loaded = "true" +} + +function selectAstNode(node: AstNode) { + const model = inputEditor.getModel() + if (!model) return + const end = Math.min(model.getValueLength(), Math.max(node.start + 1, node.end)) + const range = spanRange(model, node.start, end) + inputEditor.setSelection(range) + inputEditor.revealRangeInCenter(range, monaco.editor.ScrollType.Immediate) + inputEditor.focus() +} + +function highlightAstNode(node: AstNode) { + const model = inputEditor.getModel() + if (!model || node.end <= node.start) return clearAstDecoration() + if (astDecorationModel && astDecorationModel !== model) clearAstDecoration() + const range = spanRange(model, node.start, node.end) + astDecorations = model.deltaDecorations(astDecorations, [ + { + range, + options: { inlineClassName: "ast-source-highlight" }, + }, + ]) + astDecorationModel = model +} + +function clearAstDecoration() { + if (!astDecorationModel || astDecorations.length === 0) return + astDecorations = astDecorationModel.deltaDecorations(astDecorations, []) + astDecorationModel = undefined +} + +function scheduleAstCursorFollow() { + window.clearTimeout(astCursorTimer) + astCursorTimer = window.setTimeout(() => void followAstCursor(astRenderVersion), 80) +} + +async function followAstCursor(version: number) { + const cursorVersion = ++astCursorVersion + const model = inputEditor.getModel() + const position = inputEditor.getPosition() + if (!model || !position || !astRootNode) return + const path = await requestAstPath(model.uri.path, model.getOffsetAt(position)) + if (!path || version !== astRenderVersion || cursorVersion !== astCursorVersion || inputEditor.getModel() !== model) + return + for (let index = 0; index < path.length - 1; index++) { + const current = astEntries.get(path[index].id) + if (!current) return + if (current.element.getAttribute("aria-expanded") !== "true" && current.toggle) + await toggleAstEntry(current.node, current.toggle, current.element) + } + for (const { row } of astEntries.values()) row.classList.remove("ast-node-active") + const active = path.length ? astEntries.get(path[path.length - 1].id) : undefined + if (active) { + active.row.classList.add("ast-node-active") + active.row.scrollIntoView({ block: "nearest" }) + } +} + async function renderEmittedFiles() { const renderVersion = ++emitRenderVersion emitOutput.replaceChildren() @@ -2577,6 +2816,7 @@ function updateActiveFile() { else button.removeAttribute("aria-current") } if (projectModel) persistProjectState() + scheduleAstRefresh() } function restoreProjectSelection(state: ProjectState) { @@ -2628,6 +2868,7 @@ function registerProjectModel(model: monaco.editor.ITextModel) { nativeCompileVersion++ stradaCompileVersion++ scheduleProjectPersistence() + scheduleAstRefresh() refreshCompilerOverrides() synchronizeLinkedProjectFiles() scheduleTypeAcquisition() diff --git a/packages/playground-v2/src/native-compiler.test.ts b/packages/playground-v2/src/native-compiler.test.ts index ad6ba2999363..1ea2a3d3fc7a 100644 --- a/packages/playground-v2/src/native-compiler.test.ts +++ b/packages/playground-v2/src/native-compiler.test.ts @@ -782,6 +782,19 @@ test("native compiler reuses snapshots across edits, types, and configuration ch assert.match(first.outputFiles["/workspace/src/index.js"], /42/) assert(first.typeQueries[source].some(query => query.label === "42")) + const astRoot = compiler.astRoot(input, source) + assert.equal(astRoot?.kind, "SourceFile") + assert.equal(astRoot?.hasChildren, true) + const declarationPath = compiler.astPath(source, input.files[source].indexOf("answer")) + assert.equal(declarationPath.at(-1)?.kind, "Identifier") + assert.equal(declarationPath.at(-1)?.detail, '"answer"') + const rootChildren = compiler.astChildren(source, astRoot!.id) + assert(rootChildren.length > 0, JSON.stringify(rootChildren)) + const outsideSource = "/workspace/outside.ts" + input.files[outsideSource] = "const outside = true;" + assert.equal(compiler.astRoot(input, outsideSource)?.kind, "SourceFile") + delete input.files[outsideSource] + input.files[source] = 'const answer: number = "wrong";\n// ^?\n' const changed = compiler.compile(input) assert(changed.diagnostics.some(diagnostic => diagnostic.code === 2322)) diff --git a/packages/playground-v2/src/native-compiler.ts b/packages/playground-v2/src/native-compiler.ts index 45db1f90548b..31997a58e90c 100644 --- a/packages/playground-v2/src/native-compiler.ts +++ b/packages/playground-v2/src/native-compiler.ts @@ -1,9 +1,10 @@ import type { API, Diagnostic, FileNotifications, Snapshot } from "@typescript/typescript/sync" import type * as Ast from "@typescript/typescript/ast" -import type { Node } from "@typescript/typescript/ast" +import type { Node, SourceFile as AstSourceFile } from "@typescript/typescript/ast" +import type { AstNode } from "./ast-types" import type { WasmTransport } from "@typescript/typescript/wasm" import { TextDocument } from "vscode-languageserver-textdocument" -import { corsaDiscovery, type Dependency, type SourceFile } from "./ata" +import { corsaDiscovery, type Dependency, type SourceFile as DependencySourceFile } from "./ata" import type { Symlinks } from "./symlinks" export type NativeCompileInput = { @@ -29,17 +30,19 @@ export class NativeCompiler { private snapshot: Snapshot | undefined private readonly files = new Map() private symlinkKey = "" + private lastInput: NativeCompileInput | undefined private readonly discoverDependencies: ReturnType - constructor(private readonly api: API, private readonly transport: WasmTransport, ast: typeof Ast) { + constructor(private readonly api: API, private readonly transport: WasmTransport, private readonly ast: typeof Ast) { this.discoverDependencies = corsaDiscovery(api, ast) } - discover(file: SourceFile): readonly Dependency[] { + discover(file: DependencySourceFile): readonly Dependency[] { return this.discoverDependencies(file) } - compile(input: NativeCompileInput): NativeCompileResult { + private prepareProgram(input: NativeCompileInput) { + this.lastInput = input const symlinkKey = JSON.stringify(input.symlinks ?? {}) if (this.snapshot && this.symlinkKey !== symlinkKey) { this.snapshot.dispose() @@ -64,16 +67,19 @@ export class NativeCompiler { deleted.push(fileName) } const notifications: FileNotifications = { changed, created, deleted } + const hasChanges = changed.length > 0 || created.length > 0 || deleted.length > 0 const next = this.snapshot - ? this.snapshot.update({ - ensurePrograms: true, - fileNotifications: notifications, - fileSystem: { - kind: "layer", - files: Object.fromEntries([...changed, ...created].map(fileName => [fileName, input.files[fileName]])), - removedPaths: deleted, - }, - }) + ? hasChanges + ? this.snapshot.update({ + ensurePrograms: true, + fileNotifications: notifications, + fileSystem: { + kind: "layer", + files: Object.fromEntries([...changed, ...created].map(fileName => [fileName, input.files[fileName]])), + removedPaths: deleted, + }, + }) + : this.snapshot : this.api.createSnapshot({ openProjects: [input.configFileName], fileSystem: { kind: "layer", files: input.files, symlinks }, @@ -96,6 +102,11 @@ export class NativeCompiler { projectReferences: parsed.projectReferences, configFileParsingDiagnostics: parsed.errors, }) + return { config, next, parsed, program, configuredProgram } + } + + compile(input: NativeCompileInput): NativeCompileResult { + const { config, next, parsed, program, configuredProgram } = this.prepareProgram(input) try { const emit = program.emitToString() const diagnostics = [ @@ -133,6 +144,68 @@ export class NativeCompiler { } } + astRoot(input: NativeCompileInput, fileName: string): AstNode | undefined { + return this.withAstSource(input, fileName, sourceFile => + describeAstNode(sourceFile, sourceFile, [], this.ast.SyntaxKind) + ) + } + + astChildren(fileName: string, id: string): AstNode[] { + if (!this.lastInput) return [] + const path = parseAstPath(id) + return ( + this.withAstSource(this.lastInput, fileName, sourceFile => { + const node = nodeAtPath(sourceFile, path) + return node + ? astChildren(node).map(({ child, index }) => + describeAstNode(child, sourceFile, [...path, index], this.ast.SyntaxKind) + ) + : [] + }) ?? [] + ) + } + + astPath(fileName: string, position: number): AstNode[] { + if (!this.lastInput) return [] + return ( + this.withAstSource(this.lastInput, fileName, sourceFile => { + const path: Node[] = [sourceFile] + let node: Node = sourceFile + let indices: number[] = [] + while (true) { + const child = astChildren(node).find( + ({ child }) => position >= child.getStart(sourceFile) && position <= child.getEnd() + ) + if (!child) break + node = child.child + indices = [...indices, child.index] + path.push(node) + } + return path.map((entry, index) => + describeAstNode(entry, sourceFile, indices.slice(0, index), this.ast.SyntaxKind) + ) + }) ?? [] + ) + } + + private withAstSource(input: NativeCompileInput, fileName: string, callback: (sourceFile: AstSourceFile) => T) { + const { next, parsed, program, configuredProgram } = this.prepareProgram(input) + let inferred: Snapshot | undefined + try { + let sourceFile = program.getSourceFile(fileName) + if (!sourceFile && input.files[fileName] !== undefined) { + inferred = next.update({ + createPrograms: [{ rootFiles: [fileName], compilerOptions: { ...parsed.options, allowJs: true } }], + }) + sourceFile = inferred.operation.createdPrograms?.[0]?.getSourceFile(fileName) + } + return sourceFile ? callback(sourceFile) : undefined + } finally { + inferred?.dispose() + if (!configuredProgram) program.dispose() + } + } + close() { this.snapshot?.dispose() this.snapshot = undefined @@ -173,6 +246,53 @@ export class NativeCompiler { } } +function parseAstPath(id: string) { + return id === "" ? [] : id.split(".").map(Number) +} + +function nodeAtPath(sourceFile: Node, path: number[]) { + let node = sourceFile + for (const index of path) { + const child = astChildren(node).find(candidate => candidate.index === index) + if (!child) return undefined + node = child.child + } + return node +} + +function astChildren(node: Node) { + const children: Array<{ child: Node; index: number }> = [] + node.forEachChild(child => { + children.push({ child, index: children.length }) + }) + return children +} + +function describeAstNode( + node: Node, + sourceFile: AstSourceFile, + path: number[], + syntaxKind: typeof Ast.SyntaxKind +): AstNode { + const properties = node as Node & { text?: string; escapedText?: string } + const text = + typeof properties.text === "string" + ? properties.text + : typeof properties.escapedText === "string" + ? properties.escapedText + : "" + const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : "" + const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : "" + return { + detail: [preview, flags].filter(Boolean).join(" · "), + end: node.getEnd(), + hasChildren: node.forEachChild(() => true) === true, + id: path.join("."), + kind: syntaxKind[node.kind] ?? String(node.kind), + start: node.getStart(sourceFile), + } +} + function hasTypeQuery(source: string | undefined): source is string { return source !== undefined && /^\s*\/\/\s*\^\?\s*$/m.test(source) } diff --git a/packages/playground-v2/src/native.ts b/packages/playground-v2/src/native.ts index 384b40ea3ab6..974587837d8c 100644 --- a/packages/playground-v2/src/native.ts +++ b/packages/playground-v2/src/native.ts @@ -1,5 +1,6 @@ import type { Dependency, SourceFile } from "./ata" import type { NativeCompileInput, NativeCompileResult } from "./native-compiler" +import type { AstNode } from "./ast-types" import type { CompilerModules } from "./compiler-distribution" type PendingRequest = { @@ -62,6 +63,18 @@ export class NativeBackend { return this.request("discover", file) } + astRoot(input: NativeCompileInput, fileName: string) { + return this.request("astRoot", { input, fileName }) + } + + astChildren(fileName: string, id: string) { + return this.request("astChildren", { fileName, id }) + } + + astPath(fileName: string, position: number) { + return this.request("astPath", { fileName, position }) + } + compile(input: NativeCompileInput): Promise { return new Promise((resolve, reject) => { if (this.queuedCompile) { diff --git a/packages/playground-v2/src/native.worker.ts b/packages/playground-v2/src/native.worker.ts index 38b8c0632ff5..a2665e6299ac 100644 --- a/packages/playground-v2/src/native.worker.ts +++ b/packages/playground-v2/src/native.worker.ts @@ -12,6 +12,9 @@ type Message = args: { module: WebAssembly.Module; libraries: Record; modules: CompilerModules } } | { id: number; method: "compile"; args: NativeCompileInput } + | { id: number; method: "astRoot"; args: { input: NativeCompileInput; fileName: string } } + | { id: number; method: "astChildren"; args: { fileName: string; id: string } } + | { id: number; method: "astPath"; args: { fileName: string; position: number } } | { id: number; method: "discover"; args: SourceFile } let compiler: NativeCompiler | undefined @@ -37,7 +40,23 @@ async function handle(message: Message) { result = { diagnosticCategories: sync.DiagnosticCategory } } else { if (!compiler) throw new Error("Native compiler worker is not initialized") - result = message.method === "compile" ? compiler.compile(message.args) : compiler.discover(message.args) + switch (message.method) { + case "compile": + result = compiler.compile(message.args) + break + case "astRoot": + result = compiler.astRoot(message.args.input, message.args.fileName) + break + case "astChildren": + result = compiler.astChildren(message.args.fileName, message.args.id) + break + case "astPath": + result = compiler.astPath(message.args.fileName, message.args.position) + break + case "discover": + result = compiler.discover(message.args) + break + } } self.postMessage({ id: message.id, result }) } catch (error) { diff --git a/packages/playground-v2/src/strada.ts b/packages/playground-v2/src/strada.ts index ee2fe0cd1649..9f26441bbc73 100644 --- a/packages/playground-v2/src/strada.ts +++ b/packages/playground-v2/src/strada.ts @@ -1,5 +1,6 @@ import { monaco } from "./tsgo-lsp" import type { StradaLibraryProgress } from "./strada-libraries" +import type { AstNode } from "./ast-types" import { isSourceLanguage, languageForFile, sourceLanguages } from "./editor-languages" import { resolveSymlinks, type Symlinks } from "./symlinks" @@ -152,6 +153,21 @@ export class StradaBackend { return this.#request("readFile", { fileName }) } + async astRoot(fileName: string) { + await this.updateFiles() + return this.#request("astRoot", { fileName }) + } + + async astChildren(fileName: string, id: string) { + await this.updateFiles() + return this.#request("astChildren", { fileName, id }) + } + + async astPath(fileName: string, position: number) { + await this.updateFiles() + return this.#request("astPath", { fileName, position }) + } + async #formatOptions(model: monaco.editor.ITextModel) { const options = model.getOptions() return { diff --git a/packages/playground-v2/src/strada.worker.ts b/packages/playground-v2/src/strada.worker.ts index 4ca72ded9527..a84b8f06f3b1 100644 --- a/packages/playground-v2/src/strada.worker.ts +++ b/packages/playground-v2/src/strada.worker.ts @@ -1,4 +1,5 @@ import type { StradaLoadingProgress } from "./strada" +import type { AstNode } from "./ast-types" import { loadStradaLibraries } from "./strada-libraries" import { createStradaFileSystem, normalizeStradaPath as normalizePath, parseStradaConfig } from "./strada-project" import { resolveSymlinks, type Symlinks } from "./symlinks" @@ -176,11 +177,101 @@ async function dispatch(method: string, args: any) { } case "readFile": return readFile(args.fileName) + case "astRoot": + return serializeAstRoot(args.fileName) + case "astChildren": + return serializeAstChildren(args.fileName, args.id) + case "astPath": + return serializeAstPath(args.fileName, args.position) default: throw new Error(`Unknown Strada request: ${method}`) } } +function serializeAstRoot(fileName: string): AstNode | undefined { + const { service, fileName: resolvedFileName } = languageServiceForFile(fileName) + const sourceFile = service.getProgram()?.getSourceFile(resolvedFileName) + return sourceFile ? describeAstNode(sourceFile, sourceFile, "") : undefined +} + +function serializeAstChildren(fileName: string, id: string): AstNode[] { + const sourceFile = sourceFileForAst(fileName) + if (!sourceFile) return [] + const path = parseAstPath(id) + const node = nodeAtPath(sourceFile, path) + return node ? astChildren(node).map(({ child, index }) => describeAstNode(child, sourceFile, [...path, index])) : [] +} + +function serializeAstPath(fileName: string, position: number): AstNode[] { + const sourceFile = sourceFileForAst(fileName) + if (!sourceFile) return [] + const path: import("typescript").Node[] = [sourceFile] + let node: import("typescript").Node = sourceFile + let indices: number[] = [] + while (true) { + const child = astChildren(node).find( + ({ child }) => position >= child.getStart(sourceFile) && position <= child.getEnd() + ) + if (!child) break + node = child.child + indices = [...indices, child.index] + path.push(node) + } + return path.map((entry, index) => describeAstNode(entry, sourceFile, indices.slice(0, index))) +} + +function sourceFileForAst(fileName: string): import("typescript").SourceFile | undefined { + const { service, fileName: resolvedFileName } = languageServiceForFile(fileName) + return service.getProgram()?.getSourceFile(resolvedFileName) as import("typescript").SourceFile | undefined +} + +function parseAstPath(id: string) { + return id === "" ? [] : id.split(".").map(Number) +} + +function nodeAtPath(sourceFile: import("typescript").SourceFile, path: number[]) { + let node: import("typescript").Node = sourceFile + for (const index of path) { + const child = astChildren(node).find(candidate => candidate.index === index) + if (!child) return undefined + node = child.child + } + return node +} + +function astChildren(node: import("typescript").Node) { + const children: Array<{ child: import("typescript").Node; index: number }> = [] + node.forEachChild(child => { + children.push({ child, index: children.length }) + }) + return children +} + +function describeAstNode( + node: import("typescript").Node, + sourceFile: import("typescript").SourceFile, + path: number[] | "" +): AstNode { + const kind = ts.SyntaxKind[node.kind] ?? String(node.kind) + const properties = node as import("typescript").Node & { text?: string; escapedText?: string } + const text = + typeof properties.text === "string" + ? properties.text + : typeof properties.escapedText === "string" + ? properties.escapedText + : "" + const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : "" + const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : "" + return { + detail: [preview, flags].filter(Boolean).join(" · "), + end: node.getEnd(), + hasChildren: node.forEachChild(() => true) === true, + id: Array.isArray(path) ? path.join(".") : path, + kind, + start: node.getStart(sourceFile), + } +} + function updateFiles( nextFiles: Record, deleted: readonly string[], diff --git a/packages/playground-v2/src/styles.css b/packages/playground-v2/src/styles.css index 0fdd72c9eed0..3bf6df85d846 100644 --- a/packages/playground-v2/src/styles.css +++ b/packages/playground-v2/src/styles.css @@ -1040,6 +1040,132 @@ code { font-size: 0.8rem; } +.ast-panel { + flex: 0 1 38%; + min-height: 0; + display: flex; + flex-direction: column; + border-top: 1px solid var(--border); + background: var(--panel); +} + +.ast-panel[data-collapsed="true"] .ast-tree-container { + display: none; +} + +.ast-panel-header { + position: sticky; + top: 0; + z-index: 1; + min-height: 2.75rem; + padding: 0.45rem 0.65rem; + display: flex; + align-items: center; + justify-content: space-between; + border-bottom: 1px solid var(--border); + background: var(--sidebar); +} + +.ast-panel-header h2 { + margin: 0; + font-size: 0.8rem; + font-weight: 600; +} + +.ast-panel-header > span { + color: var(--muted); + font-size: 0.72rem; +} + +.ast-tree-container { + min-height: 0; + max-height: 18rem; + padding: 0.35rem 0.35rem 0.5rem; + overflow: auto; +} + +.ast-tree, +.ast-node-children { + margin: 0; + padding: 0; + list-style: none; +} + +.ast-node-children { + margin-left: 0.85rem; + padding-left: 0.3rem; + border-left: 1px solid var(--border); +} + +.ast-node-row { + min-height: 1.65rem; + display: flex; + align-items: center; + gap: 0.2rem; +} + +.ast-node-row.ast-node-active, +.ast-node-select:hover { + background: var(--hover); +} + +.ast-node-toggle, +.ast-node-select { + min-height: 1.65rem; + border: 0; + color: inherit; + background: transparent; + cursor: pointer; + font: inherit; + text-align: left; +} + +.ast-node-toggle { + flex: 0 0 1.1rem; + padding: 0; +} + +.ast-node-select { + min-width: 0; + padding: 0.15rem 0.25rem; + display: flex; + align-items: center; + gap: 0.35rem; +} + +.ast-node-toggle:focus-visible, +.ast-node-select:focus-visible { + outline: 2px solid var(--accent); + outline-offset: -2px; +} + +.ast-node-arrow { + display: block; + color: var(--muted); + font-size: 0.7rem; + text-align: center; +} + +.ast-node-kind { + flex: 0 0 auto; + font-family: Hack, monospace; + font-size: 0.7rem; +} + +.ast-node-detail { + min-width: 0; + color: var(--muted); + font-family: Hack, monospace; + font-size: 0.65rem; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.ast-source-highlight { + background: color-mix(in srgb, var(--accent) 25%, transparent); +} + .run-output { flex: 0 0 auto; max-height: 12rem; From a0bbad5406e9a6479f0af4590541a5e74454c79c Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:28:33 -0800 Subject: [PATCH 2/3] Keep Playground v2 AST panel coherent and idle when hidden Child and cursor lookups could resolve against a newer AST than the rendered root once a compile or edit changed the worker's sources, and the hidden panel still queried the worker on every edit and cursor move. Snapshot the tree per root request so lookups stay tied to the rendered source, and query only while the panel is visible. Start the panel collapsed like the v1 AST tab, surface AST failures in the panel, keep a clicked node selected instead of jumping to its deepest descendant, and support arrow-key tree navigation. Enum reverse mappings also reported marker aliases such as FirstStatement, so node kinds now use their declared names. --- packages/playground-v2/src/ast-tree.ts | 111 +++++++ packages/playground-v2/src/ast-types.ts | 8 - packages/playground-v2/src/index.html | 4 +- packages/playground-v2/src/main.ts | 310 +++++++++++++----- .../playground-v2/src/native-compiler.test.ts | 26 +- packages/playground-v2/src/native-compiler.ts | 104 +----- packages/playground-v2/src/native.ts | 12 +- packages/playground-v2/src/native.worker.ts | 8 +- packages/playground-v2/src/strada.ts | 14 +- packages/playground-v2/src/strada.worker.ts | 95 +----- 10 files changed, 400 insertions(+), 292 deletions(-) create mode 100644 packages/playground-v2/src/ast-tree.ts delete mode 100644 packages/playground-v2/src/ast-types.ts diff --git a/packages/playground-v2/src/ast-tree.ts b/packages/playground-v2/src/ast-tree.ts new file mode 100644 index 000000000000..d3c818b28032 --- /dev/null +++ b/packages/playground-v2/src/ast-tree.ts @@ -0,0 +1,111 @@ +export type AstNode = { + detail: string + hasChildren: boolean + id: string + kind: string + start: number + end: number +} + +export type AstSnapshot = { + revision: number + root: AstNode +} + +export type AstSyntaxNode = { + kind: number + flags?: number + forEachChild(callback: (child: any) => T | undefined): T | undefined + getEnd(): number +} + +// Enum reverse mappings resolve to the marker aliases declared last +// (FirstStatement for VariableStatement), so keep the first declared name. +export function syntaxKindNames(syntaxKind: Record) { + const names = new Map() + for (const [name, value] of Object.entries(syntaxKind)) { + if (typeof value === "number" && !names.has(value)) names.set(value, name) + } + return (kind: number) => names.get(kind) +} + +type CachedAstNode = AstNode & { children: CachedAstNode[] } + +// Snapshots the tree so child and path requests keep resolving against the +// source text the root was built from, even after later compiles. +export class AstCache { + private revision = 0 + private readonly nodes = new Map() + + store( + root: T | undefined, + kindName: (kind: number) => string | undefined, + start: (node: T) => number + ): AstSnapshot | undefined { + this.revision++ + this.nodes.clear() + if (!root) return undefined + const cachedRoot = describe(root, "", kindName, start) + this.nodes.set("", cachedRoot) + const pending: Array<[T, CachedAstNode]> = [[root, cachedRoot]] + while (pending.length) { + const [node, cached] = pending.pop()! + node.forEachChild((child: T) => { + const id = cached.id === "" ? String(cached.children.length) : `${cached.id}.${cached.children.length}` + const entry = describe(child, id, kindName, start) + cached.children.push(entry) + this.nodes.set(id, entry) + pending.push([child, entry]) + return undefined + }) + cached.hasChildren = cached.children.length > 0 + } + return { revision: this.revision, root: summarize(cachedRoot) } + } + + children(revision: number, id: string): AstNode[] | undefined { + if (revision !== this.revision) return undefined + return this.nodes.get(id)?.children.map(summarize) + } + + path(revision: number, position: number): AstNode[] | undefined { + if (revision !== this.revision) return undefined + let node = this.nodes.get("") + const path: AstNode[] = [] + while (node) { + path.push(summarize(node)) + node = node.children.find(child => position >= child.start && position <= child.end) + } + return path + } +} + +function describe( + node: T, + id: string, + kindName: (kind: number) => string | undefined, + start: (node: T) => number +): CachedAstNode { + const properties = node as T & { text?: unknown; escapedText?: unknown } + const text = + typeof properties.text === "string" + ? properties.text + : typeof properties.escapedText === "string" + ? properties.escapedText + : "" + const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : "" + const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : "" + return { + children: [], + detail: [preview, flags].filter(Boolean).join(" · "), + end: node.getEnd(), + hasChildren: false, + id, + kind: kindName(node.kind) ?? String(node.kind), + start: start(node), + } +} + +function summarize({ children: _children, ...node }: CachedAstNode): AstNode { + return node +} diff --git a/packages/playground-v2/src/ast-types.ts b/packages/playground-v2/src/ast-types.ts deleted file mode 100644 index c48cea7b1434..000000000000 --- a/packages/playground-v2/src/ast-types.ts +++ /dev/null @@ -1,8 +0,0 @@ -export type AstNode = { - detail: string - hasChildren: boolean - id: string - kind: string - start: number - end: number -} diff --git a/packages/playground-v2/src/index.html b/packages/playground-v2/src/index.html index 5d2b9db8e19c..4c8840b52bee 100644 --- a/packages/playground-v2/src/index.html +++ b/packages/playground-v2/src/index.html @@ -124,10 +124,10 @@

Emit

-
+
- +

AST

Active file diff --git a/packages/playground-v2/src/main.ts b/packages/playground-v2/src/main.ts index 84ff69397083..4173810ed190 100644 --- a/packages/playground-v2/src/main.ts +++ b/packages/playground-v2/src/main.ts @@ -35,7 +35,7 @@ import { configureExports, decodeCompressedHash } from "./exporter" import { resolveSymlinks, validateProjectSymlinks, type Symlinks } from "./symlinks" import { isSourceLanguage, languageForFile, sourceLanguages } from "./editor-languages" import { StradaBackend } from "./strada" -import type { AstNode } from "./ast-types" +import type { AstNode } from "./ast-tree" import { NativeBackend } from "./native" import { PackageTypeAcquirer, stradaDiscovery, type AcquisitionResult, type DiscoverDependencies } from "./ata" import { acquisitionOptions } from "./ata/config" @@ -204,7 +204,9 @@ let astRenderVersion = 0 let astCursorVersion = 0 let astDecorations: string[] = [] let astDecorationModel: monaco.editor.ITextModel | undefined -let astRootNode: AstNode | undefined +let astTreeState: AstTreeState | undefined +let astSelection: { end: number; id: string; start: number } | undefined +let astWasActive = false const astEntries = new Map() const toggleRunOutputButton = getElement("toggle-run-output-button") const navigateBackButton = getElement("navigate-back-button") @@ -494,7 +496,7 @@ refreshCompilerOverrides() function loadLayoutState(): LayoutState { const defaults: LayoutState = { - astVisible: true, + astVisible: false, emitVisible: true, filesVisible: true, filesWidth: 208, @@ -694,6 +696,7 @@ function applyLayoutState() { emitOutput.hidden = !layoutState.emitVisible astPanel.dataset.collapsed = String(!layoutState.astVisible) runOutput.dataset.collapsed = String(!layoutState.runVisible) + updateAstActivity() showFilesButton.hidden = layoutState.filesVisible showOutputButton.hidden = layoutState.outputVisible @@ -887,6 +890,7 @@ toggleFilesButton.addEventListener("click", () => toggleWorkspacePanel("files")) showFilesButton.addEventListener("click", () => toggleWorkspacePanel("files")) toggleOutputButton.addEventListener("click", () => toggleWorkspacePanel("output")) showOutputButton.addEventListener("click", () => toggleWorkspacePanel("output")) +astTree.addEventListener("keydown", handleAstTreeKeydown) toggleAstButton.addEventListener("click", () => { layoutState.astVisible = !layoutState.astVisible applyLayoutState() @@ -2331,52 +2335,113 @@ function diagnosticSeverity(category: number) { } type AstTreeEntry = { + children?: HTMLUListElement element: HTMLLIElement + loading?: Promise node: AstNode row: HTMLDivElement + select: HTMLButtonElement toggle?: HTMLButtonElement } -function scheduleAstRefresh() { - window.clearTimeout(astRefreshTimer) - astRefreshTimer = window.setTimeout(() => void refreshAstTree(), 180) +type AstTreeState = { + model: monaco.editor.ITextModel + modelVersion: number + revision: number } -async function refreshAstTree() { - const version = ++astRenderVersion +function isAstActive() { + return layoutState.astVisible && layoutState.outputVisible +} + +function updateAstActivity() { + const active = isAstActive() + if (active === astWasActive) return + astWasActive = active + if (!active) { + astEntries.clear() + astTree.replaceChildren() + astTree.hidden = true + } + scheduleAstRefresh() +} + +function invalidateAstTree() { + astRenderVersion++ astCursorVersion++ + astTreeState = undefined + astSelection = undefined + window.clearTimeout(astCursorTimer) clearAstDecoration() + astTree.inert = true +} + +function scheduleAstRefresh() { + invalidateAstTree() + window.clearTimeout(astRefreshTimer) + if (isAstActive()) astRefreshTimer = window.setTimeout(() => void refreshAstTree(), 180) +} + +function isCurrentAst(state: AstTreeState) { + return ( + astTreeState === state && + inputEditor.getModel() === state.model && + state.model.getVersionId() === state.modelVersion + ) +} + +function showAstMessage(message: string) { astEntries.clear() - astRootNode = undefined astTree.replaceChildren() astTree.hidden = true astMessage.hidden = false + astMessage.textContent = message +} + +function reportAstError(version: number, error: unknown) { + if (version !== astRenderVersion) return + console.error(error) + invalidateAstTree() + const reason = error instanceof Error ? error.message.split("\n", 1)[0] : String(error) + showAstMessage(`AST view unavailable: ${reason}`) +} + +function runAstTask(task: () => Promise) { + const version = astRenderVersion + task().catch(error => reportAstError(version, error)) +} + +async function refreshAstTree() { + const version = astRenderVersion const model = inputEditor.getModel() if (!model || !isSourceLanguage(model.getLanguageId())) { - astMessage.textContent = "AST view is available for TypeScript and JavaScript files." + showAstMessage("AST view is available for TypeScript and JavaScript files.") return } if (!compilerReady) { - astMessage.textContent = "Waiting for the compiler." + showAstMessage("Waiting for the compiler.") return } - astMessage.textContent = "Loading AST…" + if (astTree.hidden) showAstMessage("Loading AST…") + const modelVersion = model.getVersionId() try { - const root = await requestAstRoot(model.uri.path) - if (version !== astRenderVersion || inputEditor.getModel() !== model) return - if (!root) { - astMessage.textContent = "AST is unavailable for this file with the selected compiler." + const snapshot = await requestAstRoot(model.uri.path) + if (version !== astRenderVersion || inputEditor.getModel() !== model || model.getVersionId() !== modelVersion) + return + if (!snapshot) { + showAstMessage("AST is unavailable for this file with the selected compiler.") return } - astRootNode = root + astTreeState = { model, modelVersion, revision: snapshot.revision } + astEntries.clear() + astTree.replaceChildren() astTree.hidden = false + astTree.inert = false astMessage.hidden = true - renderAstEntry(astTree, root) - await followAstCursor(version) + renderAstEntry(astTree, snapshot.root) + await followAstCursor() } catch (error) { - if (version !== astRenderVersion) return - const reason = error instanceof Error ? error.message.split("\n", 1)[0] : String(error) - astMessage.textContent = `AST view unavailable: ${reason}` + reportAstError(version, error) } } @@ -2396,14 +2461,14 @@ function requestAstRoot(fileName: string) { return stradaBackend?.astRoot(fileName) } -function requestAstChildren(fileName: string, id: string) { - if (useNativeCompiler) return nativeBackend?.astChildren(fileName, id) - return stradaBackend?.astChildren(fileName, id) +function requestAstChildren(revision: number, id: string) { + if (useNativeCompiler) return nativeBackend?.astChildren(revision, id) + return stradaBackend?.astChildren(revision, id) } -function requestAstPath(fileName: string, position: number) { - if (useNativeCompiler) return nativeBackend?.astPath(fileName, position) - return stradaBackend?.astPath(fileName, position) +function requestAstPath(revision: number, position: number) { + if (useNativeCompiler) return nativeBackend?.astPath(revision, position) + return stradaBackend?.astPath(revision, position) } function renderAstEntry(parent: HTMLElement, node: AstNode) { @@ -2418,11 +2483,10 @@ function renderAstEntry(parent: HTMLElement, node: AstNode) { if (toggle) { toggle.className = "ast-node-toggle" toggle.type = "button" + toggle.tabIndex = -1 toggle.setAttribute("aria-label", `Expand ${node.kind}`) toggle.appendChild(createText("span", "▸", "ast-node-arrow")) - toggle.addEventListener("click", () => void toggleAstEntry(node, toggle, element)) toggle.addEventListener("pointerenter", () => highlightAstNode(node)) - toggle.addEventListener("focus", () => highlightAstNode(node)) row.appendChild(toggle) } else { row.appendChild(createText("span", "", "ast-node-arrow")) @@ -2430,12 +2494,16 @@ function renderAstEntry(parent: HTMLElement, node: AstNode) { const select = document.createElement("button") select.className = "ast-node-select" select.type = "button" + select.tabIndex = astEntries.size === 0 ? 0 : -1 select.title = `${node.kind}${node.detail ? ` · ${node.detail}` : ""}` select.appendChild(createText("span", node.kind, "ast-node-kind")) if (node.detail) select.appendChild(createText("span", node.detail, "ast-node-detail")) select.addEventListener("click", () => selectAstNode(node)) select.addEventListener("pointerenter", () => highlightAstNode(node)) - select.addEventListener("focus", () => highlightAstNode(node)) + select.addEventListener("focus", () => { + for (const entry of astEntries.values()) entry.select.tabIndex = entry.select === select ? 0 : -1 + highlightAstNode(node) + }) row.addEventListener("pointerleave", clearAstDecoration) row.addEventListener("focusout", event => { if (!row.contains(event.relatedTarget as Node | null)) clearAstDecoration() @@ -2443,58 +2511,71 @@ function renderAstEntry(parent: HTMLElement, node: AstNode) { row.appendChild(select) element.appendChild(row) parent.appendChild(element) - astEntries.set(node.id, { element, node, row, toggle }) + const entry: AstTreeEntry = { element, node, row, select, toggle } + toggle?.addEventListener("click", () => runAstTask(() => toggleAstEntry(entry))) + astEntries.set(node.id, entry) } -async function toggleAstEntry(node: AstNode, toggle: HTMLButtonElement, element: HTMLLIElement) { - const expanded = element.getAttribute("aria-expanded") === "true" - const existingChildren = element.querySelector(".ast-node-children") - if (expanded) { - element.setAttribute("aria-expanded", "false") - toggle.setAttribute("aria-label", `Expand ${node.kind}`) - toggle.querySelector(".ast-node-arrow")!.textContent = "▸" - if (existingChildren) existingChildren.hidden = true - return - } - element.setAttribute("aria-expanded", "true") - toggle.setAttribute("aria-label", `Collapse ${node.kind}`) - toggle.querySelector(".ast-node-arrow")!.textContent = "▾" - if (existingChildren) { - existingChildren.hidden = false +function setAstEntryExpanded(entry: AstTreeEntry, expanded: boolean) { + entry.element.setAttribute("aria-expanded", String(expanded)) + entry.toggle?.setAttribute("aria-label", `${expanded ? "Collapse" : "Expand"} ${entry.node.kind}`) + const arrow = entry.toggle?.querySelector(".ast-node-arrow") + if (arrow) arrow.textContent = expanded ? "▾" : "▸" + if (entry.children) entry.children.hidden = !expanded +} + +function isAstEntryExpanded(entry: AstTreeEntry) { + return entry.element.getAttribute("aria-expanded") === "true" +} + +async function toggleAstEntry(entry: AstTreeEntry) { + if (isAstEntryExpanded(entry)) setAstEntryExpanded(entry, false) + else await expandAstEntry(entry) +} + +function expandAstEntry(entry: AstTreeEntry): Promise { + if (!entry.node.hasChildren) return Promise.resolve() + setAstEntryExpanded(entry, true) + if (entry.children) return Promise.resolve() + entry.loading ??= loadAstChildren(entry).finally(() => { + entry.loading = undefined + }) + return entry.loading +} + +async function loadAstChildren(entry: AstTreeEntry) { + const state = astTreeState + if (!state) return + const children = await requestAstChildren(state.revision, entry.node.id) + if (!isCurrentAst(state) || !entry.element.isConnected) return + if (!children) { + scheduleAstRefresh() return } - const model = inputEditor.getModel() - if (!model) return - const version = astRenderVersion - const children = await requestAstChildren(model.uri.path, node.id) - if ( - !children || - version !== astRenderVersion || - !element.isConnected || - element.getAttribute("aria-expanded") !== "true" - ) - return const list = document.createElement("ul") list.className = "ast-node-children" list.setAttribute("role", "group") for (const child of children) renderAstEntry(list, child) - element.appendChild(list) - element.dataset.loaded = "true" + list.hidden = !isAstEntryExpanded(entry) + entry.element.appendChild(list) + entry.children = list } function selectAstNode(node: AstNode) { - const model = inputEditor.getModel() - if (!model) return + const model = astTreeState?.model + if (!model || inputEditor.getModel() !== model) return + const start = Math.min(node.start, model.getValueLength()) const end = Math.min(model.getValueLength(), Math.max(node.start + 1, node.end)) - const range = spanRange(model, node.start, end) + astSelection = { end, id: node.id, start } + markActiveAstEntry(node.id) + const range = spanRange(model, start, end) inputEditor.setSelection(range) - inputEditor.revealRangeInCenter(range, monaco.editor.ScrollType.Immediate) - inputEditor.focus() + inputEditor.revealRangeInCenter(range, monaco.editor.ScrollType.Smooth) } function highlightAstNode(node: AstNode) { - const model = inputEditor.getModel() - if (!model || node.end <= node.start) return clearAstDecoration() + const model = astTreeState?.model + if (!model || inputEditor.getModel() !== model || node.end <= node.start) return clearAstDecoration() if (astDecorationModel && astDecorationModel !== model) clearAstDecoration() const range = spanRange(model, node.start, node.end) astDecorations = model.deltaDecorations(astDecorations, [ @@ -2514,29 +2595,90 @@ function clearAstDecoration() { function scheduleAstCursorFollow() { window.clearTimeout(astCursorTimer) - astCursorTimer = window.setTimeout(() => void followAstCursor(astRenderVersion), 80) + if (!astTreeState) return + astCursorTimer = window.setTimeout(() => runAstTask(followAstCursor), 80) } -async function followAstCursor(version: number) { +async function followAstCursor() { + const state = astTreeState + const selection = inputEditor.getSelection() + if (!state || !selection || !isCurrentAst(state)) return const cursorVersion = ++astCursorVersion - const model = inputEditor.getModel() - const position = inputEditor.getPosition() - if (!model || !position || !astRootNode) return - const path = await requestAstPath(model.uri.path, model.getOffsetAt(position)) - if (!path || version !== astRenderVersion || cursorVersion !== astCursorVersion || inputEditor.getModel() !== model) + const selectionStart = state.model.getOffsetAt(selection.getStartPosition()) + const selectionEnd = state.model.getOffsetAt(selection.getEndPosition()) + if (astSelection?.start === selectionStart && astSelection.end === selectionEnd) { + markActiveAstEntry(astSelection.id) + return + } + astSelection = undefined + const position = state.model.getOffsetAt(selection.getPosition()) + const path = await requestAstPath(state.revision, position) + const isCurrent = () => isCurrentAst(state) && cursorVersion === astCursorVersion + if (!isCurrent()) return + if (!path) { + scheduleAstRefresh() return - for (let index = 0; index < path.length - 1; index++) { - const current = astEntries.get(path[index].id) - if (!current) return - if (current.element.getAttribute("aria-expanded") !== "true" && current.toggle) - await toggleAstEntry(current.node, current.toggle, current.element) } + for (const node of path.slice(0, -1)) { + const entry = astEntries.get(node.id) + if (!entry) return + await expandAstEntry(entry) + if (!isCurrent()) return + } + markActiveAstEntry(path.at(-1)?.id) +} + +function markActiveAstEntry(id: string | undefined) { for (const { row } of astEntries.values()) row.classList.remove("ast-node-active") - const active = path.length ? astEntries.get(path[path.length - 1].id) : undefined - if (active) { - active.row.classList.add("ast-node-active") - active.row.scrollIntoView({ block: "nearest" }) + const active = id === undefined ? undefined : astEntries.get(id) + if (!active) return + active.row.classList.add("ast-node-active") + active.row.scrollIntoView({ block: "nearest" }) +} + +function visibleAstEntries() { + return [...astTree.querySelectorAll(".ast-node-select")].filter( + select => select.closest("[hidden]") === null + ) +} + +function handleAstTreeKeydown(event: KeyboardEvent) { + const entry = [...astEntries.values()].find(({ select }) => select === document.activeElement) + if (!entry) return + const visible = visibleAstEntries() + const index = visible.indexOf(entry.select) + let target: HTMLButtonElement | undefined + switch (event.key) { + case "ArrowDown": + target = visible[index + 1] + break + case "ArrowUp": + target = visible[index - 1] + break + case "Home": + target = visible[0] + break + case "End": + target = visible.at(-1) + break + case "ArrowRight": + if (!entry.node.hasChildren) return + if (isAstEntryExpanded(entry)) + target = entry.children?.querySelector(".ast-node-select") ?? undefined + else runAstTask(() => expandAstEntry(entry)) + break + case "ArrowLeft": + if (isAstEntryExpanded(entry)) setAstEntryExpanded(entry, false) + else + target = + entry.element.parentElement?.closest(".ast-node")?.querySelector(".ast-node-select") ?? + undefined + break + default: + return } + event.preventDefault() + target?.focus() } async function renderEmittedFiles() { @@ -2868,7 +3010,7 @@ function registerProjectModel(model: monaco.editor.ITextModel) { nativeCompileVersion++ stradaCompileVersion++ scheduleProjectPersistence() - scheduleAstRefresh() + if (model === inputEditor.getModel()) scheduleAstRefresh() refreshCompilerOverrides() synchronizeLinkedProjectFiles() scheduleTypeAcquisition() diff --git a/packages/playground-v2/src/native-compiler.test.ts b/packages/playground-v2/src/native-compiler.test.ts index 1ea2a3d3fc7a..45c8d45de29a 100644 --- a/packages/playground-v2/src/native-compiler.test.ts +++ b/packages/playground-v2/src/native-compiler.test.ts @@ -782,17 +782,25 @@ test("native compiler reuses snapshots across edits, types, and configuration ch assert.match(first.outputFiles["/workspace/src/index.js"], /42/) assert(first.typeQueries[source].some(query => query.label === "42")) - const astRoot = compiler.astRoot(input, source) - assert.equal(astRoot?.kind, "SourceFile") - assert.equal(astRoot?.hasChildren, true) - const declarationPath = compiler.astPath(source, input.files[source].indexOf("answer")) - assert.equal(declarationPath.at(-1)?.kind, "Identifier") - assert.equal(declarationPath.at(-1)?.detail, '"answer"') - const rootChildren = compiler.astChildren(source, astRoot!.id) - assert(rootChildren.length > 0, JSON.stringify(rootChildren)) + const ast = compiler.astRoot(input, source) + assert.equal(ast?.root.kind, "SourceFile") + assert.equal(ast?.root.hasChildren, true) + const answerPosition = input.files[source].indexOf("answer") + const declarationPath = compiler.astPath(ast!.revision, answerPosition) + assert.equal(declarationPath?.at(-1)?.kind, "Identifier") + assert.equal(declarationPath?.at(-1)?.detail, '"answer"') + const rootChildren = compiler.astChildren(ast!.revision, ast!.root.id) + assert.equal(rootChildren?.[0]?.kind, "VariableStatement", JSON.stringify(rootChildren)) + + compiler.compile({ ...input, files: { ...input.files, [source]: "\n\nlet renamed = { answer: 42 };\n" } }) + assert.deepEqual(compiler.astChildren(ast!.revision, ast!.root.id), rootChildren) + assert.deepEqual(compiler.astPath(ast!.revision, answerPosition), declarationPath) + const outsideSource = "/workspace/outside.ts" input.files[outsideSource] = "const outside = true;" - assert.equal(compiler.astRoot(input, outsideSource)?.kind, "SourceFile") + const outside = compiler.astRoot(input, outsideSource) + assert.equal(outside?.root.kind, "SourceFile") + assert.equal(compiler.astChildren(ast!.revision, ast!.root.id), undefined) delete input.files[outsideSource] input.files[source] = 'const answer: number = "wrong";\n// ^?\n' diff --git a/packages/playground-v2/src/native-compiler.ts b/packages/playground-v2/src/native-compiler.ts index 31997a58e90c..344599f6cefa 100644 --- a/packages/playground-v2/src/native-compiler.ts +++ b/packages/playground-v2/src/native-compiler.ts @@ -1,7 +1,7 @@ import type { API, Diagnostic, FileNotifications, Snapshot } from "@typescript/typescript/sync" import type * as Ast from "@typescript/typescript/ast" import type { Node, SourceFile as AstSourceFile } from "@typescript/typescript/ast" -import type { AstNode } from "./ast-types" +import { AstCache, syntaxKindNames, type AstNode, type AstSnapshot } from "./ast-tree" import type { WasmTransport } from "@typescript/typescript/wasm" import { TextDocument } from "vscode-languageserver-textdocument" import { corsaDiscovery, type Dependency, type SourceFile as DependencySourceFile } from "./ata" @@ -30,11 +30,13 @@ export class NativeCompiler { private snapshot: Snapshot | undefined private readonly files = new Map() private symlinkKey = "" - private lastInput: NativeCompileInput | undefined + private readonly astCache = new AstCache() + private readonly syntaxKindName: (kind: number) => string | undefined private readonly discoverDependencies: ReturnType constructor(private readonly api: API, private readonly transport: WasmTransport, private readonly ast: typeof Ast) { this.discoverDependencies = corsaDiscovery(api, ast) + this.syntaxKindName = syntaxKindNames(ast.SyntaxKind as unknown as Record) } discover(file: DependencySourceFile): readonly Dependency[] { @@ -42,7 +44,6 @@ export class NativeCompiler { } private prepareProgram(input: NativeCompileInput) { - this.lastInput = input const symlinkKey = JSON.stringify(input.symlinks ?? {}) if (this.snapshot && this.symlinkKey !== symlinkKey) { this.snapshot.dispose() @@ -144,51 +145,25 @@ export class NativeCompiler { } } - astRoot(input: NativeCompileInput, fileName: string): AstNode | undefined { + astRoot(input: NativeCompileInput, fileName: string): AstSnapshot | undefined { return this.withAstSource(input, fileName, sourceFile => - describeAstNode(sourceFile, sourceFile, [], this.ast.SyntaxKind) + this.astCache.store(sourceFile, this.syntaxKindName, node => node.getStart(sourceFile)) ) } - astChildren(fileName: string, id: string): AstNode[] { - if (!this.lastInput) return [] - const path = parseAstPath(id) - return ( - this.withAstSource(this.lastInput, fileName, sourceFile => { - const node = nodeAtPath(sourceFile, path) - return node - ? astChildren(node).map(({ child, index }) => - describeAstNode(child, sourceFile, [...path, index], this.ast.SyntaxKind) - ) - : [] - }) ?? [] - ) + astChildren(revision: number, id: string): AstNode[] | undefined { + return this.astCache.children(revision, id) } - astPath(fileName: string, position: number): AstNode[] { - if (!this.lastInput) return [] - return ( - this.withAstSource(this.lastInput, fileName, sourceFile => { - const path: Node[] = [sourceFile] - let node: Node = sourceFile - let indices: number[] = [] - while (true) { - const child = astChildren(node).find( - ({ child }) => position >= child.getStart(sourceFile) && position <= child.getEnd() - ) - if (!child) break - node = child.child - indices = [...indices, child.index] - path.push(node) - } - return path.map((entry, index) => - describeAstNode(entry, sourceFile, indices.slice(0, index), this.ast.SyntaxKind) - ) - }) ?? [] - ) + astPath(revision: number, position: number): AstNode[] | undefined { + return this.astCache.path(revision, position) } - private withAstSource(input: NativeCompileInput, fileName: string, callback: (sourceFile: AstSourceFile) => T) { + private withAstSource( + input: NativeCompileInput, + fileName: string, + callback: (sourceFile: AstSourceFile | undefined) => T + ) { const { next, parsed, program, configuredProgram } = this.prepareProgram(input) let inferred: Snapshot | undefined try { @@ -199,7 +174,7 @@ export class NativeCompiler { }) sourceFile = inferred.operation.createdPrograms?.[0]?.getSourceFile(fileName) } - return sourceFile ? callback(sourceFile) : undefined + return callback(sourceFile) } finally { inferred?.dispose() if (!configuredProgram) program.dispose() @@ -246,53 +221,6 @@ export class NativeCompiler { } } -function parseAstPath(id: string) { - return id === "" ? [] : id.split(".").map(Number) -} - -function nodeAtPath(sourceFile: Node, path: number[]) { - let node = sourceFile - for (const index of path) { - const child = astChildren(node).find(candidate => candidate.index === index) - if (!child) return undefined - node = child.child - } - return node -} - -function astChildren(node: Node) { - const children: Array<{ child: Node; index: number }> = [] - node.forEachChild(child => { - children.push({ child, index: children.length }) - }) - return children -} - -function describeAstNode( - node: Node, - sourceFile: AstSourceFile, - path: number[], - syntaxKind: typeof Ast.SyntaxKind -): AstNode { - const properties = node as Node & { text?: string; escapedText?: string } - const text = - typeof properties.text === "string" - ? properties.text - : typeof properties.escapedText === "string" - ? properties.escapedText - : "" - const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : "" - const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : "" - return { - detail: [preview, flags].filter(Boolean).join(" · "), - end: node.getEnd(), - hasChildren: node.forEachChild(() => true) === true, - id: path.join("."), - kind: syntaxKind[node.kind] ?? String(node.kind), - start: node.getStart(sourceFile), - } -} - function hasTypeQuery(source: string | undefined): source is string { return source !== undefined && /^\s*\/\/\s*\^\?\s*$/m.test(source) } diff --git a/packages/playground-v2/src/native.ts b/packages/playground-v2/src/native.ts index 974587837d8c..5ca1c0b387f6 100644 --- a/packages/playground-v2/src/native.ts +++ b/packages/playground-v2/src/native.ts @@ -1,6 +1,6 @@ import type { Dependency, SourceFile } from "./ata" import type { NativeCompileInput, NativeCompileResult } from "./native-compiler" -import type { AstNode } from "./ast-types" +import type { AstNode, AstSnapshot } from "./ast-tree" import type { CompilerModules } from "./compiler-distribution" type PendingRequest = { @@ -64,15 +64,15 @@ export class NativeBackend { } astRoot(input: NativeCompileInput, fileName: string) { - return this.request("astRoot", { input, fileName }) + return this.request("astRoot", { input, fileName }) } - astChildren(fileName: string, id: string) { - return this.request("astChildren", { fileName, id }) + astChildren(revision: number, id: string) { + return this.request("astChildren", { revision, id }) } - astPath(fileName: string, position: number) { - return this.request("astPath", { fileName, position }) + astPath(revision: number, position: number) { + return this.request("astPath", { revision, position }) } compile(input: NativeCompileInput): Promise { diff --git a/packages/playground-v2/src/native.worker.ts b/packages/playground-v2/src/native.worker.ts index a2665e6299ac..1e02ee445725 100644 --- a/packages/playground-v2/src/native.worker.ts +++ b/packages/playground-v2/src/native.worker.ts @@ -13,8 +13,8 @@ type Message = } | { id: number; method: "compile"; args: NativeCompileInput } | { id: number; method: "astRoot"; args: { input: NativeCompileInput; fileName: string } } - | { id: number; method: "astChildren"; args: { fileName: string; id: string } } - | { id: number; method: "astPath"; args: { fileName: string; position: number } } + | { id: number; method: "astChildren"; args: { revision: number; id: string } } + | { id: number; method: "astPath"; args: { revision: number; position: number } } | { id: number; method: "discover"; args: SourceFile } let compiler: NativeCompiler | undefined @@ -48,10 +48,10 @@ async function handle(message: Message) { result = compiler.astRoot(message.args.input, message.args.fileName) break case "astChildren": - result = compiler.astChildren(message.args.fileName, message.args.id) + result = compiler.astChildren(message.args.revision, message.args.id) break case "astPath": - result = compiler.astPath(message.args.fileName, message.args.position) + result = compiler.astPath(message.args.revision, message.args.position) break case "discover": result = compiler.discover(message.args) diff --git a/packages/playground-v2/src/strada.ts b/packages/playground-v2/src/strada.ts index 9f26441bbc73..ef366f91a017 100644 --- a/packages/playground-v2/src/strada.ts +++ b/packages/playground-v2/src/strada.ts @@ -1,6 +1,6 @@ import { monaco } from "./tsgo-lsp" import type { StradaLibraryProgress } from "./strada-libraries" -import type { AstNode } from "./ast-types" +import type { AstNode, AstSnapshot } from "./ast-tree" import { isSourceLanguage, languageForFile, sourceLanguages } from "./editor-languages" import { resolveSymlinks, type Symlinks } from "./symlinks" @@ -155,17 +155,15 @@ export class StradaBackend { async astRoot(fileName: string) { await this.updateFiles() - return this.#request("astRoot", { fileName }) + return this.#request("astRoot", { fileName }) } - async astChildren(fileName: string, id: string) { - await this.updateFiles() - return this.#request("astChildren", { fileName, id }) + astChildren(revision: number, id: string) { + return this.#request("astChildren", { revision, id }) } - async astPath(fileName: string, position: number) { - await this.updateFiles() - return this.#request("astPath", { fileName, position }) + astPath(revision: number, position: number) { + return this.#request("astPath", { revision, position }) } async #formatOptions(model: monaco.editor.ITextModel) { diff --git a/packages/playground-v2/src/strada.worker.ts b/packages/playground-v2/src/strada.worker.ts index a84b8f06f3b1..959c76ae2a95 100644 --- a/packages/playground-v2/src/strada.worker.ts +++ b/packages/playground-v2/src/strada.worker.ts @@ -1,5 +1,5 @@ import type { StradaLoadingProgress } from "./strada" -import type { AstNode } from "./ast-types" +import { AstCache, syntaxKindNames } from "./ast-tree" import { loadStradaLibraries } from "./strada-libraries" import { createStradaFileSystem, normalizeStradaPath as normalizePath, parseStradaConfig } from "./strada-project" import { resolveSymlinks, type Symlinks } from "./symlinks" @@ -44,6 +44,8 @@ let filesystem: ReturnType let languageService: import("typescript").LanguageService let inferredLanguageService: import("typescript").LanguageService | undefined const requestedInferredFiles = new Set() +const astCache = new AstCache() +let syntaxKindName: (kind: number) => string | undefined self.addEventListener("message", (event: MessageEvent) => { void handleMessage(event.data) @@ -67,6 +69,7 @@ async function initialize(args: InitMessage["args"]) { // The owning page supplies compiler code from our trusted compiler CDN over // this dedicated worker's private channel. Evaluating that compiler is intentional. ts = new Function(`${args.compilerSource}\nreturn ts;`)() as TS + syntaxKindName = syntaxKindNames(ts.SyntaxKind as unknown as Record) symlinks = args.symlinks ?? {} filesystem = createStradaFileSystem(ts, () => [...files.keys(), ...libraries.keys()], readFile, symlinks) version = args.version @@ -180,96 +183,22 @@ async function dispatch(method: string, args: any) { case "astRoot": return serializeAstRoot(args.fileName) case "astChildren": - return serializeAstChildren(args.fileName, args.id) + return astCache.children(args.revision, args.id) case "astPath": - return serializeAstPath(args.fileName, args.position) + return astCache.path(args.revision, args.position) default: throw new Error(`Unknown Strada request: ${method}`) } } -function serializeAstRoot(fileName: string): AstNode | undefined { +function serializeAstRoot(fileName: string) { const { service, fileName: resolvedFileName } = languageServiceForFile(fileName) const sourceFile = service.getProgram()?.getSourceFile(resolvedFileName) - return sourceFile ? describeAstNode(sourceFile, sourceFile, "") : undefined -} - -function serializeAstChildren(fileName: string, id: string): AstNode[] { - const sourceFile = sourceFileForAst(fileName) - if (!sourceFile) return [] - const path = parseAstPath(id) - const node = nodeAtPath(sourceFile, path) - return node ? astChildren(node).map(({ child, index }) => describeAstNode(child, sourceFile, [...path, index])) : [] -} - -function serializeAstPath(fileName: string, position: number): AstNode[] { - const sourceFile = sourceFileForAst(fileName) - if (!sourceFile) return [] - const path: import("typescript").Node[] = [sourceFile] - let node: import("typescript").Node = sourceFile - let indices: number[] = [] - while (true) { - const child = astChildren(node).find( - ({ child }) => position >= child.getStart(sourceFile) && position <= child.getEnd() - ) - if (!child) break - node = child.child - indices = [...indices, child.index] - path.push(node) - } - return path.map((entry, index) => describeAstNode(entry, sourceFile, indices.slice(0, index))) -} - -function sourceFileForAst(fileName: string): import("typescript").SourceFile | undefined { - const { service, fileName: resolvedFileName } = languageServiceForFile(fileName) - return service.getProgram()?.getSourceFile(resolvedFileName) as import("typescript").SourceFile | undefined -} - -function parseAstPath(id: string) { - return id === "" ? [] : id.split(".").map(Number) -} - -function nodeAtPath(sourceFile: import("typescript").SourceFile, path: number[]) { - let node: import("typescript").Node = sourceFile - for (const index of path) { - const child = astChildren(node).find(candidate => candidate.index === index) - if (!child) return undefined - node = child.child - } - return node -} - -function astChildren(node: import("typescript").Node) { - const children: Array<{ child: import("typescript").Node; index: number }> = [] - node.forEachChild(child => { - children.push({ child, index: children.length }) - }) - return children -} - -function describeAstNode( - node: import("typescript").Node, - sourceFile: import("typescript").SourceFile, - path: number[] | "" -): AstNode { - const kind = ts.SyntaxKind[node.kind] ?? String(node.kind) - const properties = node as import("typescript").Node & { text?: string; escapedText?: string } - const text = - typeof properties.text === "string" - ? properties.text - : typeof properties.escapedText === "string" - ? properties.escapedText - : "" - const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : "" - const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : "" - return { - detail: [preview, flags].filter(Boolean).join(" · "), - end: node.getEnd(), - hasChildren: node.forEachChild(() => true) === true, - id: Array.isArray(path) ? path.join(".") : path, - kind, - start: node.getStart(sourceFile), - } + return astCache.store( + sourceFile, + syntaxKindName, + node => node.getStart(sourceFile) + ) } function updateFiles( From d817e13fcf19f581659ab7fd599958b4f26fb0ad Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:31:22 -0800 Subject: [PATCH 3/3] Correct AST cursor selection at adjacent node boundaries Cursor positions shared by adjacent nodes should identify the node that\nstarts at that position. This keeps the AST selection aligned with\nthe source identifier under the cursor. --- packages/playground-v2/src/ast-tree.ts | 6 ++++-- packages/playground-v2/src/index.html | 12 +++++++++++- packages/playground-v2/src/native-compiler.test.ts | 3 +++ packages/playground-v2/src/native-compiler.ts | 4 ++-- packages/playground-v2/src/strada.worker.ts | 8 ++------ 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/playground-v2/src/ast-tree.ts b/packages/playground-v2/src/ast-tree.ts index d3c818b28032..c76bee93ced0 100644 --- a/packages/playground-v2/src/ast-tree.ts +++ b/packages/playground-v2/src/ast-tree.ts @@ -21,7 +21,7 @@ export type AstSyntaxNode = { // Enum reverse mappings resolve to the marker aliases declared last // (FirstStatement for VariableStatement), so keep the first declared name. -export function syntaxKindNames(syntaxKind: Record) { +export function syntaxKindNames(syntaxKind: object) { const names = new Map() for (const [name, value] of Object.entries(syntaxKind)) { if (typeof value === "number" && !names.has(value)) names.set(value, name) @@ -74,7 +74,9 @@ export class AstCache { const path: AstNode[] = [] while (node) { path.push(summarize(node)) - node = node.children.find(child => position >= child.start && position <= child.end) + node = + node.children.find(child => position >= child.start && position < child.end) ?? + node.children.find(child => position === child.end && child.end > child.start) } return path } diff --git a/packages/playground-v2/src/index.html b/packages/playground-v2/src/index.html index 4c8840b52bee..a994f79d33ad 100644 --- a/packages/playground-v2/src/index.html +++ b/packages/playground-v2/src/index.html @@ -127,7 +127,17 @@

Emit

- +

AST

Active file diff --git a/packages/playground-v2/src/native-compiler.test.ts b/packages/playground-v2/src/native-compiler.test.ts index 45c8d45de29a..3f4f3db78d11 100644 --- a/packages/playground-v2/src/native-compiler.test.ts +++ b/packages/playground-v2/src/native-compiler.test.ts @@ -803,6 +803,9 @@ test("native compiler reuses snapshots across edits, types, and configuration ch assert.equal(compiler.astChildren(ast!.revision, ast!.root.id), undefined) delete input.files[outsideSource] + const adjacent = compiler.astRoot({ ...input, files: { ...input.files, [source]: "a;b;" } }, source) + assert.equal(compiler.astPath(adjacent!.revision, 2)?.at(-1)?.detail, '"b"') + input.files[source] = 'const answer: number = "wrong";\n// ^?\n' const changed = compiler.compile(input) assert(changed.diagnostics.some(diagnostic => diagnostic.code === 2322)) diff --git a/packages/playground-v2/src/native-compiler.ts b/packages/playground-v2/src/native-compiler.ts index 344599f6cefa..d00095aaa93c 100644 --- a/packages/playground-v2/src/native-compiler.ts +++ b/packages/playground-v2/src/native-compiler.ts @@ -34,9 +34,9 @@ export class NativeCompiler { private readonly syntaxKindName: (kind: number) => string | undefined private readonly discoverDependencies: ReturnType - constructor(private readonly api: API, private readonly transport: WasmTransport, private readonly ast: typeof Ast) { + constructor(private readonly api: API, private readonly transport: WasmTransport, ast: typeof Ast) { this.discoverDependencies = corsaDiscovery(api, ast) - this.syntaxKindName = syntaxKindNames(ast.SyntaxKind as unknown as Record) + this.syntaxKindName = syntaxKindNames(ast.SyntaxKind) } discover(file: DependencySourceFile): readonly Dependency[] { diff --git a/packages/playground-v2/src/strada.worker.ts b/packages/playground-v2/src/strada.worker.ts index 959c76ae2a95..b416ed99a380 100644 --- a/packages/playground-v2/src/strada.worker.ts +++ b/packages/playground-v2/src/strada.worker.ts @@ -69,7 +69,7 @@ async function initialize(args: InitMessage["args"]) { // The owning page supplies compiler code from our trusted compiler CDN over // this dedicated worker's private channel. Evaluating that compiler is intentional. ts = new Function(`${args.compilerSource}\nreturn ts;`)() as TS - syntaxKindName = syntaxKindNames(ts.SyntaxKind as unknown as Record) + syntaxKindName = syntaxKindNames(ts.SyntaxKind) symlinks = args.symlinks ?? {} filesystem = createStradaFileSystem(ts, () => [...files.keys(), ...libraries.keys()], readFile, symlinks) version = args.version @@ -194,11 +194,7 @@ async function dispatch(method: string, args: any) { function serializeAstRoot(fileName: string) { const { service, fileName: resolvedFileName } = languageServiceForFile(fileName) const sourceFile = service.getProgram()?.getSourceFile(resolvedFileName) - return astCache.store( - sourceFile, - syntaxKindName, - node => node.getStart(sourceFile) - ) + return astCache.store(sourceFile, syntaxKindName, node => node.getStart(sourceFile)) } function updateFiles(