diff --git a/src/editor/core/cm/editing.ts b/src/editor/core/cm/editing.ts index 20f002f7d..53533e435 100644 --- a/src/editor/core/cm/editing.ts +++ b/src/editor/core/cm/editing.ts @@ -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 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+)*`; diff --git a/src/editor/core/cm/structural.test.ts b/src/editor/core/cm/structural.test.ts index 8a0a00783..a177f76fc 100644 --- a/src/editor/core/cm/structural.test.ts +++ b/src/editor/core/cm/structural.test.ts @@ -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 `};`", () => {