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); }