дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
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
Цей коміт міститься в:
1 джерело
709cde907e
коміт
87b659b3ac
5 files changed
+29
-5
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -27,7 +27,13 @@ import {
|
||||
import { closeChooser, useChooser } from "../popup/store";
|
||||
import { liveTemplates, removeTemplate, saveTemplate } from "../live/store";
|
||||
|
||||
const MODES: Record<string, string> = { typescript: "TypeScript", rust: "Rust", python: "Python", java: "Java" };
|
||||
const MODES: Record<string, string> = {
|
||||
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", "<?php\n\nfinal class A\n{\n pubsf");
|
||||
expect(tab(v)).toBe(true);
|
||||
await tick();
|
||||
expect(v.state.doc.line(5).text).toBe(" public static function ()");
|
||||
});
|
||||
|
||||
it("expands both occurrences selected with Alt+J twice and Right, then undoes them together", async () => {
|
||||
const doc = "function first() {\n log\n}\nfunction second() {\n log\n}";
|
||||
const v = await editor("typescript", doc, doc.indexOf("log") + 1, [history(), occurrenceSearch]);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Заблокувати користувача