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
This commit is contained in:
l0ng-ai
2026-11-16 10:56:23 +08:00
committed by GitHub
parent 148e45745a
commit b60dad7076
8 changed files with 116 additions and 0 deletions
+2
View File
@@ -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"),
+6
View File
@@ -0,0 +1,6 @@
id = "gherkin"
name = "Gherkin"
extensions = ["feature"]
line-comment = "#"
indent = { tab-size = 2, insert-spaces = true }
ui-mode = "Gherkin"
+44
View File
@@ -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 <method>
When I pay with "<method>":
| 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 <method>")).toBe("| card |");
expect(foldAt('When I pay with "<method>":')).toBe("| 10 |");
expect(foldAt('"""')).toBe('"""');
expect(foldAt("Given a cart")).toBeNull();
expect(foldAt("@smoke")).toBeNull();
});
});
+51
View File
@@ -0,0 +1,51 @@
/**
* Gherkin (`.feature`): the legacy-mode highlighting (keywords in every Gherkin
* language, tags, tables, doc strings, `<placeholders>`) 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)]);
}
+5
View File
@@ -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"],
+2
View File
@@ -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",
+3
View File
@@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<g stroke-linejoin="round"><path d="M14 4C14 2.89543 13.1046 2 12 2H4C2.89543 2 2 2.89543 2 4V12C2 13.1046 2.89543 14 4 14H12C13.1046 14 14 13.1046 14 12V4Z" fill="#4E9A42"/><ellipse cx="8" cy="8" rx="2.4" ry="4.2" transform="rotate(35 8 8)" stroke="#2B2D30"/><path d="M7.4 6.6H7.6M8.4 9.4H8.6" stroke="#2B2D30" stroke-linecap="round"/></g>
</svg>

After

Width:  |  Height:  |  Size: 444 B

@@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<g stroke-linejoin="round"><path d="M14 4C14 2.89543 13.1046 2 12 2H4C2.89543 2 2 2.89543 2 4V12C2 13.1046 2.89543 14 4 14H12C13.1046 14 14 13.1046 14 12V4Z" fill="#5FAD52"/><ellipse cx="8" cy="8" rx="2.4" ry="4.2" transform="rotate(35 8 8)" stroke="#2B2D30"/><path d="M7.4 6.6H7.6M8.4 9.4H8.6" stroke="#2B2D30" stroke-linecap="round"/></g>
</svg>

After

Width:  |  Height:  |  Size: 444 B