From 87b659b3acbed4459154a7deebf71a28c77f8895 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Thu, 3 Dec 2026 14:49:44 +0800 Subject: [PATCH] fix(templates): expand live templates in PHP files (#7713) The PHP grammar names its document node Template, and the check for being inside a comment or string treated any node named like a template string as one, so the whole PHP file counted as a string: code live templates (pubsf, prif, fore, ...) never expanded with Tab or showed in completion, and postfix templates were off too. The document node no longer counts. The same name check in Emmet's JSX context, Move Caret to Code Block Start/End and Move Element Left/Right gets the same exception. Fixes #7660 --- src/editor/core/cm/caretCommands.ts | 4 +++- src/editor/core/cm/moveElement.ts | 4 +++- src/features/templates/__tests__/editor.test.ts | 16 +++++++++++++++- src/features/templates/editorContext.ts | 8 +++++++- src/features/templates/emmet/editor.ts | 2 +- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/editor/core/cm/caretCommands.ts b/src/editor/core/cm/caretCommands.ts index 5b1aba7ec..45f28d963 100644 --- a/src/editor/core/cm/caretCommands.ts +++ b/src/editor/core/cm/caretCommands.ts @@ -146,7 +146,9 @@ function braceAt(state: EditorState, pos: number): "{" | "}" | null { const ch = state.sliceDoc(pos, pos + 1); if (ch !== "{" && ch !== "}") return null; const tree = syntaxTree(state); - if (tree.length && NOT_CODE.test(tree.resolveInner(pos, 1).name)) return null; + const node = tree.resolveInner(pos, 1); + // The document node is code even when named like a string (PHP's `Template`). + if (tree.length && !node.type.isTop && NOT_CODE.test(node.name)) return null; return ch; } diff --git a/src/editor/core/cm/moveElement.ts b/src/editor/core/cm/moveElement.ts index ac53f0615..9e78aab62 100644 --- a/src/editor/core/cm/moveElement.ts +++ b/src/editor/core/cm/moveElement.ts @@ -15,7 +15,9 @@ const MAX_SCAN = 20_000; function inCode(state: EditorState, pos: number): boolean { const tree = syntaxTree(state); - return !tree.length || !NOT_CODE.test(tree.resolveInner(pos, 1).name); + const node = tree.resolveInner(pos, 1); + // The document node is code even when named like a string (PHP's `Template`). + return !tree.length || node.type.isTop || !NOT_CODE.test(node.name); } /** `[open, close]` offsets of the innermost bracket pair around `pos` that holds a comma list. */ diff --git a/src/features/templates/__tests__/editor.test.ts b/src/features/templates/__tests__/editor.test.ts index fbe614b6e..45c975ab3 100644 --- a/src/features/templates/__tests__/editor.test.ts +++ b/src/features/templates/__tests__/editor.test.ts @@ -27,7 +27,13 @@ import { import { closeChooser, useChooser } from "../popup/store"; import { liveTemplates, removeTemplate, saveTemplate } from "../live/store"; -const MODES: Record = { typescript: "TypeScript", rust: "Rust", python: "Python", java: "Java" }; +const MODES: Record = { + typescript: "TypeScript", + rust: "Rust", + python: "Python", + java: "Java", + php: "PHP", +}; let view: EditorView | null = null; afterEach(() => { @@ -80,6 +86,14 @@ describe("Tab expansion", () => { expect(v.state.selection.main.head).toBe("function f() {\n console.log(".length); }); + it("expands a PHP template in a class body", async () => { + // The PHP grammar's document node is named `Template`: it is code, not a template string. + const v = await editor("php", " { const doc = "function first() {\n log\n}\nfunction second() {\n log\n}"; const v = await editor("typescript", doc, doc.indexOf("log") + 1, [history(), occurrenceSearch]); diff --git a/src/features/templates/editorContext.ts b/src/features/templates/editorContext.ts index aa22640a4..f81caedfc 100644 --- a/src/features/templates/editorContext.ts +++ b/src/features/templates/editorContext.ts @@ -51,11 +51,17 @@ export function enclosingName(state: EditorState, pos: number, kind: "class" | " return found ?? ""; } +// The document node is never one, even when named so (PHP's `Template`). const COMMENT_OR_STRING = /Comment|String|Docstring|Template|Char(acter)?Literal|RawString/i; export function inCommentOrString(state: EditorState, pos: number): boolean { for (let n: SyntaxNode | null = syntaxTree(state).resolveInner(pos, -1); n; n = n.parent) { - if (COMMENT_OR_STRING.test(n.name) && n.from < pos && (n.to > pos || !/String|Literal/.test(n.name))) { + if ( + !n.type.isTop && + COMMENT_OR_STRING.test(n.name) && + n.from < pos && + (n.to > pos || !/String|Literal/.test(n.name)) + ) { // Directly after a closed string the caret is outside it. if (/String|Literal/i.test(n.name) && n.to === pos) return false; return true; diff --git a/src/features/templates/emmet/editor.ts b/src/features/templates/emmet/editor.ts index 960232e39..b54e6582f 100644 --- a/src/features/templates/emmet/editor.ts +++ b/src/features/templates/emmet/editor.ts @@ -25,7 +25,7 @@ const HTML_TAGS = new Set( function jsxAbbreviationContext(state: EditorState, from: number, abbr: string): boolean { const node = syntaxTree(state).resolveInner(from, 1); if (node.name === "JSXText") return true; - if (/String|Comment|Template|JSX/.test(node.name)) return false; + if (!node.type.isTop && /String|Comment|Template|JSX/.test(node.name)) return false; if (!/(?:\breturn|=>)\s*\(?\s*$/.test(state.sliceDoc(Math.max(0, from - 200), from))) return false; return /[.#>+*^[{:]/.test(abbr) || HTML_TAGS.has(abbr) || /^[A-Z]/.test(abbr); }