fix(editor): give a completed foreach its body (#5123)

⌘⇧↵ after `foreach ($xs as $x)` in PHP produced `foreach ($xs as $x);` with the caret on the next line: a semicolon that terminates an empty loop. `foreach` was missing from the block-head keywords, so the line was treated as a plain statement.

`foreach` (PHP, C#) and PHP's `elseif` are now block heads: the line gets `{` / caret / `}`, and a bare `foreach` gets `(|)` like `for`.
This commit is contained in:
l0ng-ai
2026-11-22 13:21:34 +08:00
committed by GitHub
parent bf4fff8fcf
commit ca7f26f484
2 changed files with 19 additions and 2 deletions
+2 -2
View File
@@ -584,7 +584,7 @@ export const KEYWORD_BLOCK_LANGS = new Set([
const KEYWORD_HEAD =
/\b(do(\s*\|[^|]*\|)?|then|where|of)$|^\s*(def|defp|defmodule|class|module|function|local function|if|unless|elsif|elseif|else|while|until|for|case|begin)\b/;
const BLOCK_HEAD =
/^\s*(if|else|for|while|do|switch|try|catch|finally|fn|fun|func|function|class|struct|enum|impl|trait|interface|object|match|when|loop|unsafe|mod|namespace)\b/;
/^\s*(if|else|elseif|for|foreach|while|do|switch|try|catch|finally|fn|fun|func|function|class|struct|enum|impl|trait|interface|object|match|when|loop|unsafe|mod|namespace)\b/;
/** Languages whose `struct` / `class` / `union` / `enum` definitions end with `};`. */
const C_TYPE_LANGS = new Set(["c", "cpp", "objective-c", "objective-cpp"]);
/** `struct S`, `class A : public B`, `enum class E : int`, `template <typename T> class A`. */
@@ -613,7 +613,7 @@ const PAREN_CONDITION_LANGS = new Set([
"objective-cpp",
]);
/** A condition keyword with nothing after it yet (`for`, `} else if`). */
export const BARE_CONDITION = /^\s*(\}\s*)?(else\s+)?(if|for|while|switch|catch|synchronized)$/;
export const BARE_CONDITION = /^\s*(\}\s*)?(else\s+)?(if|elseif|for|foreach|while|switch|catch|synchronized)$/;
const MODIFIERS =
"public|protected|private|internal|static|final|abstract|sealed|non-sealed|open|data|export|default|async|override|partial|readonly|unsafe|const|extern|inline|virtual|synchronized|native|strictfp|transient";
const ANNOTATIONS = String.raw`(@[\w.]+(\([^)]*\))?\s+)*`;
+17
View File
@@ -270,6 +270,23 @@ describe("completeStatementSpec (text rules)", () => {
});
});
describe("completeStatementSpec: foreach and elseif", () => {
const ext = [comments("//"), indentUnit.of(" ")];
it("gives a PHP or C# foreach a body instead of ending it with `;`", () => {
for (const [head, language] of [
["foreach ($xs as $x)", "php"],
["foreach ($map as $k => $v)", "php"],
["elseif ($x)", "php"],
["foreach (var x in xs)", "csharp"],
] as const) {
const s = state(`${head}|`, ext);
expect(withCaret(s, completeStatementSpec(s, language))).toBe(`${head} {\n |\n}`);
}
const bare = state("foreach|", ext);
expect(withCaret(bare, completeStatementSpec(bare, "php"))).toBe("foreach (|) {\n}");
});
});
describe("completeStatementSpec: C and C++ type definitions", () => {
const ext = [comments("//"), indentUnit.of(" ")];
it("ends a struct, class, union or enum with `};`", () => {