diff --git a/packages/playground-v2/src/ast-tree.ts b/packages/playground-v2/src/ast-tree.ts new file mode 100644 index 000000000000..c76bee93ced0 --- /dev/null +++ b/packages/playground-v2/src/ast-tree.ts @@ -0,0 +1,113 @@ +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: object) { + 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) ?? + node.children.find(child => position === child.end && child.end > child.start) + } + 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/index.html b/packages/playground-v2/src/index.html index 3c93de346730..a994f79d33ad 100644 --- a/packages/playground-v2/src/index.html +++ b/packages/playground-v2/src/index.html @@ -124,6 +124,29 @@

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..4173810ed190 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-tree" 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,20 @@ 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 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") const currentFile = getElement("current-file") @@ -441,10 +457,12 @@ inputEditor.onDidChangeModel(() => { trackedEditorLocation = nextLocation updateActiveFile() updateNavigationButtons() + scheduleAstRefresh() }) inputEditor.onDidChangeCursorSelection(() => { trackedEditorLocation = getEditorLocation() scheduleProjectPersistence() + scheduleAstCursorFollow() }) window.addEventListener("pagehide", () => { if (playgroundSettings.saveToUrl) persistProjectState() @@ -478,6 +496,7 @@ refreshCompilerOverrides() function loadLayoutState(): LayoutState { const defaults: LayoutState = { + astVisible: false, emitVisible: true, filesVisible: true, filesWidth: 208, @@ -488,6 +507,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,7 +694,9 @@ 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) + updateAstActivity() showFilesButton.hidden = layoutState.filesVisible showOutputButton.hidden = layoutState.outputVisible @@ -686,6 +708,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 +890,12 @@ 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() + persistLayoutState() +}) toggleEmitButton.addEventListener("click", () => { layoutState.emitVisible = !layoutState.emitVisible applyLayoutState() @@ -956,6 +988,7 @@ async function initializeNativeCompiler() { discoverDependencies = file => backend.discover(file) compileActiveProject = compileNativeProject compilerReady = true + scheduleAstRefresh() startLanguageServer(module, libFiles) await compileNativeProject() void refreshTypeAcquisition() @@ -1035,6 +1068,7 @@ async function initializeStradaCompiler(requestedVersion: string) { compileActiveProject = compileStradaProject compilerReady = true lspReady = true + scheduleAstRefresh() await compileStradaProject() void refreshTypeAcquisition() renderStatus() @@ -2300,6 +2334,353 @@ function diagnosticSeverity(category: number) { } } +type AstTreeEntry = { + children?: HTMLUListElement + element: HTMLLIElement + loading?: Promise + node: AstNode + row: HTMLDivElement + select: HTMLButtonElement + toggle?: HTMLButtonElement +} + +type AstTreeState = { + model: monaco.editor.ITextModel + modelVersion: number + revision: number +} + +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() + 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())) { + showAstMessage("AST view is available for TypeScript and JavaScript files.") + return + } + if (!compilerReady) { + showAstMessage("Waiting for the compiler.") + return + } + if (astTree.hidden) showAstMessage("Loading AST…") + const modelVersion = model.getVersionId() + try { + 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 + } + astTreeState = { model, modelVersion, revision: snapshot.revision } + astEntries.clear() + astTree.replaceChildren() + astTree.hidden = false + astTree.inert = false + astMessage.hidden = true + renderAstEntry(astTree, snapshot.root) + await followAstCursor() + } catch (error) { + reportAstError(version, error) + } +} + +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(revision: number, id: string) { + if (useNativeCompiler) return nativeBackend?.astChildren(revision, id) + return stradaBackend?.astChildren(revision, id) +} + +function requestAstPath(revision: number, position: number) { + if (useNativeCompiler) return nativeBackend?.astPath(revision, position) + return stradaBackend?.astPath(revision, 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.tabIndex = -1 + toggle.setAttribute("aria-label", `Expand ${node.kind}`) + toggle.appendChild(createText("span", "▸", "ast-node-arrow")) + toggle.addEventListener("pointerenter", () => 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.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", () => { + 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() + }) + row.appendChild(select) + element.appendChild(row) + parent.appendChild(element) + const entry: AstTreeEntry = { element, node, row, select, toggle } + toggle?.addEventListener("click", () => runAstTask(() => toggleAstEntry(entry))) + astEntries.set(node.id, entry) +} + +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 list = document.createElement("ul") + list.className = "ast-node-children" + list.setAttribute("role", "group") + for (const child of children) renderAstEntry(list, child) + list.hidden = !isAstEntryExpanded(entry) + entry.element.appendChild(list) + entry.children = list +} + +function selectAstNode(node: AstNode) { + 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)) + astSelection = { end, id: node.id, start } + markActiveAstEntry(node.id) + const range = spanRange(model, start, end) + inputEditor.setSelection(range) + inputEditor.revealRangeInCenter(range, monaco.editor.ScrollType.Smooth) +} + +function highlightAstNode(node: AstNode) { + 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, [ + { + 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) + if (!astTreeState) return + astCursorTimer = window.setTimeout(() => runAstTask(followAstCursor), 80) +} + +async function followAstCursor() { + const state = astTreeState + const selection = inputEditor.getSelection() + if (!state || !selection || !isCurrentAst(state)) return + const cursorVersion = ++astCursorVersion + 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 (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 = 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() { const renderVersion = ++emitRenderVersion emitOutput.replaceChildren() @@ -2577,6 +2958,7 @@ function updateActiveFile() { else button.removeAttribute("aria-current") } if (projectModel) persistProjectState() + scheduleAstRefresh() } function restoreProjectSelection(state: ProjectState) { @@ -2628,6 +3010,7 @@ function registerProjectModel(model: monaco.editor.ITextModel) { nativeCompileVersion++ stradaCompileVersion++ scheduleProjectPersistence() + 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 ad6ba2999363..3f4f3db78d11 100644 --- a/packages/playground-v2/src/native-compiler.test.ts +++ b/packages/playground-v2/src/native-compiler.test.ts @@ -782,6 +782,30 @@ 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 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;" + 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] + + 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 45db1f90548b..d00095aaa93c 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 { 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 } from "./ata" +import { corsaDiscovery, type Dependency, type SourceFile as DependencySourceFile } from "./ata" import type { Symlinks } from "./symlinks" export type NativeCompileInput = { @@ -29,17 +30,20 @@ export class NativeCompiler { private snapshot: Snapshot | undefined private readonly files = new Map() private symlinkKey = "" + 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, ast: typeof Ast) { this.discoverDependencies = corsaDiscovery(api, ast) + this.syntaxKindName = syntaxKindNames(ast.SyntaxKind) } - discover(file: SourceFile): readonly Dependency[] { + discover(file: DependencySourceFile): readonly Dependency[] { return this.discoverDependencies(file) } - compile(input: NativeCompileInput): NativeCompileResult { + private prepareProgram(input: NativeCompileInput) { const symlinkKey = JSON.stringify(input.symlinks ?? {}) if (this.snapshot && this.symlinkKey !== symlinkKey) { this.snapshot.dispose() @@ -64,16 +68,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 +103,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 +145,42 @@ export class NativeCompiler { } } + astRoot(input: NativeCompileInput, fileName: string): AstSnapshot | undefined { + return this.withAstSource(input, fileName, sourceFile => + this.astCache.store(sourceFile, this.syntaxKindName, node => node.getStart(sourceFile)) + ) + } + + astChildren(revision: number, id: string): AstNode[] | undefined { + return this.astCache.children(revision, id) + } + + astPath(revision: number, position: number): AstNode[] | undefined { + return this.astCache.path(revision, position) + } + + private withAstSource( + input: NativeCompileInput, + fileName: string, + callback: (sourceFile: AstSourceFile | undefined) => 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 callback(sourceFile) + } finally { + inferred?.dispose() + if (!configuredProgram) program.dispose() + } + } + close() { this.snapshot?.dispose() this.snapshot = undefined diff --git a/packages/playground-v2/src/native.ts b/packages/playground-v2/src/native.ts index 384b40ea3ab6..5ca1c0b387f6 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, AstSnapshot } from "./ast-tree" 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(revision: number, id: string) { + return this.request("astChildren", { revision, id }) + } + + astPath(revision: number, position: number) { + return this.request("astPath", { revision, 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..1e02ee445725 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: { revision: number; id: string } } + | { id: number; method: "astPath"; args: { revision: number; 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.revision, message.args.id) + break + case "astPath": + result = compiler.astPath(message.args.revision, 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..ef366f91a017 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, AstSnapshot } from "./ast-tree" import { isSourceLanguage, languageForFile, sourceLanguages } from "./editor-languages" import { resolveSymlinks, type Symlinks } from "./symlinks" @@ -152,6 +153,19 @@ export class StradaBackend { return this.#request("readFile", { fileName }) } + async astRoot(fileName: string) { + await this.updateFiles() + return this.#request("astRoot", { fileName }) + } + + astChildren(revision: number, id: string) { + return this.#request("astChildren", { revision, id }) + } + + astPath(revision: number, position: number) { + return this.#request("astPath", { revision, 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..b416ed99a380 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 { 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" @@ -43,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) @@ -66,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) symlinks = args.symlinks ?? {} filesystem = createStradaFileSystem(ts, () => [...files.keys(), ...libraries.keys()], readFile, symlinks) version = args.version @@ -176,11 +180,23 @@ async function dispatch(method: string, args: any) { } case "readFile": return readFile(args.fileName) + case "astRoot": + return serializeAstRoot(args.fileName) + case "astChildren": + return astCache.children(args.revision, args.id) + case "astPath": + return astCache.path(args.revision, args.position) default: throw new Error(`Unknown Strada request: ${method}`) } } +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)) +} + 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;