From b60dad70765d9942f6b1fb49223bef19ef33c0c2 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Mon, 16 Nov 2026 10:56:23 +0800 Subject: [PATCH] feat(languages): Gherkin feature files (#3645) .feature files are recognised as Gherkin: syntax colours for keywords in every Gherkin language, tags, tables, doc strings and outline placeholders; line comments, two-space indentation, a file icon, and folding of each section, step argument and doc string by indentation. Part of #797 --- crates/ide-language/src/registry.rs | 2 + languages/gherkin.toml | 6 +++ src/editor/core/cm/gherkin.test.ts | 44 +++++++++++++++++++ src/editor/core/cm/gherkin.ts | 51 ++++++++++++++++++++++ src/editor/core/cm/languages.ts | 5 +++ src/ui/icons.tsx | 2 + src/ui/openidea/fileTypes/gherkin.svg | 3 ++ src/ui/openidea/fileTypes/gherkin_dark.svg | 3 ++ 8 files changed, 116 insertions(+) create mode 100644 languages/gherkin.toml create mode 100644 src/editor/core/cm/gherkin.test.ts create mode 100644 src/editor/core/cm/gherkin.ts create mode 100644 src/ui/openidea/fileTypes/gherkin.svg create mode 100644 src/ui/openidea/fileTypes/gherkin_dark.svg diff --git a/crates/ide-language/src/registry.rs b/crates/ide-language/src/registry.rs index 6f12ca20e..f304cac60 100644 --- a/crates/ide-language/src/registry.rs +++ b/crates/ide-language/src/registry.rs @@ -33,6 +33,7 @@ const BUILTIN: &[(&str, &str)] = &[ ("gdscript", include_str!("../../../languages/gdscript.toml")), ("gdshader", include_str!("../../../languages/gdshader.toml")), ("go", include_str!("../../../languages/go.toml")), + ("gherkin", include_str!("../../../languages/gherkin.toml")), ("gomod", include_str!("../../../languages/gomod.toml")), ("gowork", include_str!("../../../languages/gowork.toml")), ("godot-resource", include_str!("../../../languages/godot-resource.toml")), @@ -1057,6 +1058,7 @@ mod tests { ("CMakeLists.txt", "cmake"), ("api.proto", "proto"), ("schema.graphql", "graphql"), + ("checkout.feature", "gherkin"), ("schema.prisma", "prisma"), ("main.tf", "terraform"), ("prod.tfvars", "terraform-vars"), diff --git a/languages/gherkin.toml b/languages/gherkin.toml new file mode 100644 index 000000000..88d57e268 --- /dev/null +++ b/languages/gherkin.toml @@ -0,0 +1,6 @@ +id = "gherkin" +name = "Gherkin" +extensions = ["feature"] +line-comment = "#" +indent = { tab-size = 2, insert-spaces = true } +ui-mode = "Gherkin" diff --git a/src/editor/core/cm/gherkin.test.ts b/src/editor/core/cm/gherkin.test.ts new file mode 100644 index 000000000..9e54385e3 --- /dev/null +++ b/src/editor/core/cm/gherkin.test.ts @@ -0,0 +1,44 @@ +import { foldable } from "@codemirror/language"; +import { EditorState } from "@codemirror/state"; +import { describe, expect, it } from "vitest"; +import { gherkinSupport } from "./gherkin"; + +const DOC = `@smoke +Feature: Checkout + As a shopper + + Background: + Given a cart + + Scenario Outline: pay with + When I pay with "": + | amount | + | 10 | + Then I see: + """ + Thanks + """ + + Examples: + | method | + | card | +`; + +describe("Gherkin", () => { + const state = EditorState.create({ doc: DOC, extensions: gherkinSupport() }); + const foldAt = (lineText: string) => { + const line = state.doc.line(DOC.split("\n").findIndex((l) => l.trim() === lineText) + 1); + const r = foldable(state, line.from, line.to); + return r ? state.doc.lineAt(r.to).text.trim() : null; + }; + + it("folds sections, step arguments and doc strings by indentation", () => { + expect(foldAt("Feature: Checkout")).toBe("| card |"); + expect(foldAt("Background:")).toBe("Given a cart"); + expect(foldAt("Scenario Outline: pay with ")).toBe("| card |"); + expect(foldAt('When I pay with "":')).toBe("| 10 |"); + expect(foldAt('"""')).toBe('"""'); + expect(foldAt("Given a cart")).toBeNull(); + expect(foldAt("@smoke")).toBeNull(); + }); +}); diff --git a/src/editor/core/cm/gherkin.ts b/src/editor/core/cm/gherkin.ts new file mode 100644 index 000000000..a2cfa9202 --- /dev/null +++ b/src/editor/core/cm/gherkin.ts @@ -0,0 +1,51 @@ +/** + * Gherkin (`.feature`): the legacy-mode highlighting (keywords in every Gherkin + * language, tags, tables, doc strings, ``) plus folding of each + * section, step argument and doc string by indentation, as Cucumber's outline does. + */ +import { LanguageSupport, StreamLanguage, foldService } from "@codemirror/language"; +import { gherkin } from "@codemirror/legacy-modes/mode/gherkin"; +import type { EditorState } from "@codemirror/state"; + +export const gherkinLanguage = StreamLanguage.define(gherkin); + +const indentOf = (text: string) => /^\s*/.exec(text)![0].replace(/\t/g, " ").length; + +/** + * A header line — `Feature:`, `Scenario:`, a step ending in `:` before its table, an + * opening `"""` — folds the more indented lines below it, up to the last of them. + */ +export function gherkinFold( + state: EditorState, + lineStart: number, + lineEnd: number, +): { from: number; to: number } | null { + const doc = state.doc; + const header = doc.lineAt(lineStart); + const text = header.text.trim(); + if (!text || text.startsWith("#") || text.startsWith("|") || text.startsWith("@")) return null; + const docString = /^("""|```)/.exec(text); + if (docString) { + // The matching closing fence, at any indentation. + for (let n = header.number + 1; n <= doc.lines; n++) { + const line = doc.line(n); + if (line.text.trim().startsWith(docString[1]!)) + return n > header.number + 1 ? { from: lineEnd, to: line.to } : null; + } + return null; + } + if (!text.endsWith(":") && !/^[^\s:]+(\s[^\s:]+)*:\s/.test(text)) return null; + const own = indentOf(header.text); + let last = header.number; + for (let n = header.number + 1; n <= doc.lines; n++) { + const line = doc.line(n); + if (!line.text.trim()) continue; + if (indentOf(line.text) <= own && !line.text.trim().startsWith("#")) break; + last = n; + } + return last > header.number ? { from: lineEnd, to: doc.line(last).to } : null; +} + +export function gherkinSupport(): LanguageSupport { + return new LanguageSupport(gherkinLanguage, [foldService.of(gherkinFold)]); +} diff --git a/src/editor/core/cm/languages.ts b/src/editor/core/cm/languages.ts index 0d3f516e5..0cc1ba0b8 100644 --- a/src/editor/core/cm/languages.ts +++ b/src/editor/core/cm/languages.ts @@ -92,6 +92,11 @@ const OWN_MODES = [ extensions: ["rst"], load: () => import("./restructuredtext").then((m) => new LanguageSupport(m.restructuredtextLanguage)), }), + LanguageDescription.of({ + name: "Gherkin", + extensions: ["feature"], + load: () => import("./gherkin").then((m) => m.gherkinSupport()), + }), LanguageDescription.of({ name: "Log", extensions: ["log"], diff --git a/src/ui/icons.tsx b/src/ui/icons.tsx index 7a3f068e3..101ad3cc7 100644 --- a/src/ui/icons.tsx +++ b/src/ui/icons.tsx @@ -179,6 +179,7 @@ const ICONS = { "file-gdshader": "openidea/fileTypes/gdshader", "file-asciidoc": "openidea/fileTypes/asciidoc", "file-d": "openidea/fileTypes/d", + "file-gherkin": "openidea/fileTypes/gherkin", "file-prisma": "openidea/fileTypes/prisma", "file-slint": "openidea/fileTypes/slint", "file-dart": "openidea/fileTypes/dart", @@ -1148,6 +1149,7 @@ const EXT_ICONS = table({ "file-gdshader": "gdshader gdshaderinc", "file-asciidoc": "adoc asciidoc", "file-d": "d di", + "file-gherkin": "feature", "file-prisma": "prisma", "file-slint": "slint", "file-dart": "dart", diff --git a/src/ui/openidea/fileTypes/gherkin.svg b/src/ui/openidea/fileTypes/gherkin.svg new file mode 100644 index 000000000..2ea889391 --- /dev/null +++ b/src/ui/openidea/fileTypes/gherkin.svg @@ -0,0 +1,3 @@ + + + diff --git a/src/ui/openidea/fileTypes/gherkin_dark.svg b/src/ui/openidea/fileTypes/gherkin_dark.svg new file mode 100644 index 000000000..d30a835e9 --- /dev/null +++ b/src/ui/openidea/fileTypes/gherkin_dark.svg @@ -0,0 +1,3 @@ + + +