Compare commits

..
Author SHA1 Message Date
l0ng-ai 55cb9f11f7 feat(cucumber): behave and pytest-bdd step definitions in Python 2026-11-20 23:41:26 +08:00
l0ng-ai 1000ef0fa0 fix(editor): align code folding controls and arrow visibility (#4820)
Settings > Editor > General > Code Folding puts the outline visibility next to Show code folding outline (On hover / Always, new editor setting folding.outlineVisibility, default On hover) and lists the Fold by default options in one column. With Always, open fold arrows stay visible without hovering the gutter.
2026-11-20 23:40:32 +08:00
l0ng-ai 654da3e1ef feat(editor): spinner in progress hints (#4854)
Pending editor hints (Finding navigation target and other progress hints) start with the ring spinner used elsewhere in the UI; final hints are unchanged.
2026-11-20 23:21:16 +08:00
l0ng-ai ba6ccaf9b2 feat(vcs): option to open diffs in a separate window (#4853)
Adds Settings > Version Control > Confirmation > Open diff as editor tab (default on, client-side workflow setting vcs.diffAsEditorTab). When off, diffs opened from the Commit tool window, history, log and Compare show in a separate Diff window; combined multi-file diffs still open as a tab.
2026-11-20 23:17:37 +08:00
20 changed files with 513 additions and 40 deletions

No files matched your search

+37 -1
View File
@@ -197,6 +197,7 @@ pub enum ImportsOnPaste {
#[serde(default, rename_all = "camelCase")]
pub struct FoldingSettings {
pub show_outline: bool,
pub outline_visibility: FoldingOutlineVisibility,
pub file_header: bool,
pub imports: bool,
pub doc_comments: bool,
@@ -204,10 +205,24 @@ pub struct FoldingSettings {
impl Default for FoldingSettings {
fn default() -> Self {
Self { show_outline: true, file_header: true, imports: true, doc_comments: false }
Self {
show_outline: true,
outline_visibility: FoldingOutlineVisibility::Hover,
file_header: true,
imports: true,
doc_comments: false,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum FoldingOutlineVisibility {
#[default]
Hover,
Always,
}
/// Editor › General › Editor Tabs.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
@@ -543,3 +558,24 @@ mod auto_import_tests {
}
}
}
#[cfg(test)]
mod folding_tests {
use super::{FoldingOutlineVisibility, FoldingSettings};
#[test]
fn folding_outline_defaults_to_hover_for_existing_settings() {
let settings: FoldingSettings = serde_json::from_str(r#"{"showOutline":false}"#).unwrap();
assert!(!settings.show_outline);
assert_eq!(settings.outline_visibility, FoldingOutlineVisibility::Hover);
}
#[test]
fn folding_outline_visibility_round_trips() {
for mode in ["hover", "always"] {
let json = serde_json::json!({ "outlineVisibility": mode });
let settings: FoldingSettings = serde_json::from_value(json).unwrap();
assert_eq!(serde_json::to_value(settings).unwrap()["outlineVisibility"], mode);
}
}
}
+25 -4
View File
@@ -1,6 +1,7 @@
//! Cucumber sources: the project's `.feature` files and the JavaScript / TypeScript files
//! (`@cucumber/cucumber`, `playwright-bdd`) and Java / Kotlin files (cucumber-jvm's
//! `@Given("…")` annotations and Java 8 lambdas) that could define steps, with their text. The UI
//! `@Given("…")` annotations and Java 8 lambdas) and Python files (behave and pytest-bdd's
//! `@given('…')` decorators) that could define steps, with their text. The UI
//! owns the Gherkin and step-expression parsing and the index it feeds (as for `routes`),
//! so this side stays a cheap scan.
@@ -21,6 +22,8 @@ const MAX_FILE_BYTES: u64 = 2 << 20;
/// What a step definition call starts with (`Given('…'`, `defineStep(/…/`).
const STEP_CALLS: [&str; 5] = ["Given(", "When(", "Then(", "Step(", "defineStep("];
/** What a Python step decorator starts with (behave also takes the capitalized names). */
const PYTHON_STEP_CALLS: [&str; 8] = ["@given(", "@when(", "@then(", "@step(", "@Given(", "@When(", "@Then(", "@Step("];
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
@@ -38,10 +41,14 @@ fn is_feature(path: &Path) -> bool {
fn is_script(path: &Path) -> bool {
matches!(
path.extension().and_then(|e| e.to_str()),
Some("js" | "jsx" | "mjs" | "cjs" | "ts" | "tsx" | "mts" | "cts" | "java" | "kt")
Some("js" | "jsx" | "mjs" | "cjs" | "ts" | "tsx" | "mts" | "cts" | "java" | "kt" | "py")
)
}
fn is_python(path: &Path) -> bool {
path.extension().and_then(|e| e.to_str()) == Some("py")
}
/// The feature files and step definition files under `root`.
pub(crate) fn sources_in(root: &Path, excludes: &[String]) -> Vec<StepSource> {
let found: Mutex<Vec<StepSource>> = Mutex::new(Vec::new());
@@ -55,7 +62,8 @@ pub(crate) fn sources_in(root: &Path, excludes: &[String]) -> Vec<StepSource> {
return;
}
let Ok(text) = std::fs::read_to_string(path) else { return };
if !feature && !STEP_CALLS.iter().any(|call| text.contains(call)) {
let calls: &[&str] = if is_python(path) { &PYTHON_STEP_CALLS } else { &STEP_CALLS };
if !feature && !calls.iter().any(|call| text.contains(call)) {
return;
}
let Ok(mut found) = found.lock() else { return };
@@ -76,6 +84,7 @@ pub fn cucumber_sources(host: &Host, window: &str) -> IdeResult<Vec<StepSource>>
let mut excludes = project.search_excludes();
// Cucumber's own sources (and every other package's) are not the project's steps.
excludes.push("node_modules".to_owned());
excludes.push("site-packages".to_owned());
let mut out = Vec::new();
for root in std::iter::once(project.root.clone()).chain(project.content_roots()) {
out.extend(sources_in(&root, &excludes));
@@ -104,11 +113,23 @@ mod tests {
std::fs::write(root.join("src/CartSteps.java"), "@Given(\"an empty cart\")\npublic void empty() {}").unwrap();
std::fs::write(root.join("src/App.java"), "class App {}").unwrap();
std::fs::write(root.join("src/notes.md"), "Given('x')").unwrap();
std::fs::write(root.join("steps/cart_steps.py"), "@given('an empty cart')\ndef step_impl(context): pass")
.unwrap();
std::fs::write(root.join("src/util.py"), "def given(x): return x\nWhen = given('x')").unwrap();
std::fs::write(root.join("node_modules/@cucumber/cucumber/lib/index.js"), "Given('x')").unwrap();
let files = sources_in(root, &["node_modules".to_owned()]);
let names: Vec<String> =
files.iter().map(|f| f.path.0.strip_prefix(root).unwrap().to_string_lossy().replace('\\', "/")).collect();
assert_eq!(names, ["features/cart.feature", "src/CartSteps.java", "steps/bdd.mjs", "steps/cart.steps.ts"]);
assert_eq!(
names,
[
"features/cart.feature",
"src/CartSteps.java",
"steps/bdd.mjs",
"steps/cart.steps.ts",
"steps/cart_steps.py"
]
);
assert!(files[0].feature && !files[1].feature);
assert!(files[3].text.contains("an empty cart"));
}
+71
View File
@@ -0,0 +1,71 @@
import { expect, openFromTree, test } from "./fixtures";
test("code folding controls stay grouped in narrow settings", async ({ workbench: page }) => {
await page.keyboard.press("Meta+,");
const dialog = page.getByRole("dialog", { name: "Settings", exact: true });
await dialog.getByRole("textbox", { name: "Search settings" }).fill("Code Folding");
await dialog.getByRole("treeitem", { name: "Code Folding", exact: true }).click();
const label = "Show code folding outline";
const outline = dialog.getByRole("checkbox", { name: label, exact: true });
const visibility = dialog.getByRole("button", { name: label, exact: true });
const defaults = dialog.getByRole("region", { name: "Fold by default:", exact: true });
await expect(visibility).toHaveText("On hover");
const wideOutline = (await outline.boundingBox())!;
expect((await visibility.boundingBox())!.x).toBeGreaterThan(wideOutline.x + wideOutline.width);
await page.setViewportSize({ width: 520, height: 760 });
await expect(visibility).toBeInViewport({ ratio: 0.98 });
const rows = await Promise.all(
["File header", "Imports", "Documentation comments"].map((name) =>
defaults.getByRole("checkbox", { name, exact: true }).boundingBox(),
),
);
expect(rows[0]!.x).toBe(rows[2]!.x);
expect(rows[0]!.y).toBeLessThan(rows[1]!.y);
expect(rows[1]!.y).toBeLessThan(rows[2]!.y);
await outline.uncheck();
await expect(dialog.getByRole("textbox", { name: label, exact: true })).toBeDisabled();
await expect(defaults.getByRole("checkbox", { name: "Imports", exact: true })).toBeEnabled();
});
test("folding visibility applies to an already open editor and keeps folded arrows visible", async ({
workbench: page,
}) => {
const editor = await openFromTree(page, "App.tsx");
const content = editor.locator(".cm-content");
await content.click();
await page.keyboard.press("Meta+a");
await page.keyboard.insertText("export function sum() {\n return 1 + 2;\n}\n");
const arrow = editor.locator(".cm-foldGutter .cm-openidea-foldOpen").first();
await expect(arrow).toHaveCSS("opacity", "0");
const openSettings = async () => {
await page.keyboard.press("Meta+,");
const dialog = page.getByRole("dialog", { name: "Settings", exact: true });
await dialog.getByRole("textbox", { name: "Search settings" }).fill("Code Folding");
await dialog.getByRole("treeitem", { name: "Code Folding", exact: true }).click();
return dialog;
};
const label = "Show code folding outline";
const settings = await openSettings();
await settings.getByRole("button", { name: label, exact: true }).click();
await page.getByRole("option", { name: "Always", exact: true }).click();
await settings.getByRole("button", { name: "OK", exact: true }).click();
await content.hover();
await expect(arrow).toHaveCSS("opacity", "1");
const reopened = await openSettings();
const control = reopened.getByRole("button", { name: label, exact: true });
await expect(control).toHaveText("Always");
await control.click();
await page.getByRole("option", { name: "On hover", exact: true }).click();
await reopened.getByRole("button", { name: "OK", exact: true }).click();
await content.hover();
await expect(arrow).toHaveCSS("opacity", "0");
await arrow.click();
await content.hover();
await expect(editor.locator(".cm-foldGutter .cm-openidea-foldClosed").first()).toHaveCSS("opacity", "1");
await expect(editor.locator(".cm-foldPlaceholder").first()).toBeVisible();
});
+3
View File
@@ -6310,9 +6310,12 @@ export type FoldRange = {
kind: FoldKind,
};
export type FoldingOutlineVisibility = "hover" | "always";
/** Editor › General › Code Folding. */
export type FoldingSettings = {
showOutline?: boolean,
outlineVisibility?: FoldingOutlineVisibility,
fileHeader?: boolean,
imports?: boolean,
docComments?: boolean,
+10 -4
View File
@@ -1,7 +1,7 @@
/**
* Folding: ranges from the backend (`doc_folding`, tree-sitter based) merged
* with CodeMirror's syntax folding (`foldNodeProp`) as fallback, IDEA-style
* chevrons in the gutter (open chevrons only on gutter hover).
* chevrons in the gutter (open chevrons can be shown only on hover).
*/
import {
EditorSelection,
@@ -23,6 +23,7 @@ import {
unfoldEffect,
} from "@codemirror/language";
import type { FoldRange } from "@/platform/ipc";
import type { FoldingOutlineVisibility } from "@/bindings";
import { enclosingCodeBlock } from "./caretCommands";
import { regionMarkerAt, regionNameAt } from "./regions";
@@ -432,9 +433,10 @@ export const expandRecursively = (view: EditorView) =>
view.state.selection.ranges.flatMap((r) => expandRecursivelyEffects(view.state, r.head)),
);
function chevron(open: boolean): HTMLElement {
function chevron(open: boolean, visibility: FoldingOutlineVisibility): HTMLElement {
const span = document.createElement("span");
span.className = open ? "cm-openidea-foldOpen" : "cm-openidea-foldClosed";
span.classList.toggle("cm-openidea-foldOnHover", open && visibility === "hover");
span.title = open ? "Collapse" : "Expand";
const ns = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(ns, "svg");
@@ -477,10 +479,14 @@ export function foldChipWidth(el: HTMLElement): number {
return Math.round(chip.getBoundingClientRect().width);
}
export function foldingExtension(showOutline = true): Extension {
export function foldingExtension(showOutline = true, visibility: FoldingOutlineVisibility = "hover"): Extension {
return [
// The chevron column sits right of contributed icon gutters (IDEA new UI).
showOutline ? Prec.lowest(foldGutter({ markerDOM: chevron, foldingChanged: backendFoldingChanged })) : [],
showOutline
? Prec.lowest(
foldGutter({ markerDOM: (open) => chevron(open, visibility), foldingChanged: backendFoldingChanged }),
)
: [],
backendFolds,
backendFoldService,
codeFolding({
+2 -1
View File
@@ -276,7 +276,8 @@ export function distractionFreeExtension(
}
export function foldingFor(settings: EditorSettings | undefined): Extension {
return foldingExtension(prefsOf(settings).folding.showOutline);
const { showOutline, outlineVisibility } = prefsOf(settings).folding;
return foldingExtension(showOutline, outlineVisibility);
}
export function caretFor(settings: EditorSettings | undefined): Extension {
+3 -3
View File
@@ -303,9 +303,9 @@ export const openideaTheme = EditorView.theme({
cursor: "pointer",
transition: "opacity var(--dur-fast) var(--ease)",
},
".cm-foldGutter .cm-openidea-foldOpen": { opacity: "0", display: "inline-flex" },
".cm-foldGutter .cm-openidea-foldClosed": { display: "inline-flex" },
".cm-gutters:hover .cm-foldGutter .cm-openidea-foldOpen": { opacity: "1" },
".cm-foldGutter .cm-openidea-foldOpen, .cm-foldGutter .cm-openidea-foldClosed": { display: "inline-flex" },
".cm-foldGutter .cm-openidea-foldOnHover": { opacity: "0" },
".cm-gutters:hover .cm-foldGutter .cm-openidea-foldOnHover": { opacity: "1" },
".cm-foldGutter .cm-gutterElement:hover": { color: "var(--fg-secondary)" },
".cm-foldPlaceholder": {
backgroundColor: "var(--editor-fold-chip)",
+2
View File
@@ -30,6 +30,7 @@ describe("pending hints", () => {
vi.advanceTimersByTime(PENDING_HINT_DELAY_MS);
expect(hint()).toBe("Waiting…");
expect(document.querySelector<HTMLElement>(".cm-lsp-hint")?.dataset.pending).toBe("true");
expect(document.querySelector(".cm-lsp-hint .cm-lsp-hint-spinner svg")).not.toBeNull();
done();
expect(hint()).toBeNull();
});
@@ -37,6 +38,7 @@ describe("pending hints", () => {
it("marks only progress as pending", () => {
showHint(view, "Done");
expect(document.querySelector<HTMLElement>(".cm-lsp-hint")?.dataset.pending).toBeUndefined();
expect(document.querySelector(".cm-lsp-hint-spinner")).toBeNull();
});
it("puts what the hint answers in its data attributes", () => {
+2
View File
@@ -7,6 +7,7 @@ import { StateEffect, StateField, type Extension } from "@codemirror/state";
import { EditorView, keymap, showTooltip, type Tooltip } from "@codemirror/view";
import { Prec } from "@codemirror/state";
import { txTemplate } from "@/platform/i18n";
import { spinnerNode } from "@/ui/iconNode";
interface Hint {
pos: number;
@@ -48,6 +49,7 @@ const hintField = StateField.define<Hint | null>({
Object.assign(dom.dataset, h.data);
if (typeof h.content === "string") {
dom.textContent = txTemplate(h.content);
if (h.pending) dom.prepend(spinnerNode(12, "cm-lsp-hint-spinner"));
return { dom };
}
const built = h.content();
+1
View File
@@ -335,6 +335,7 @@ export const lspTheme = EditorView.theme({
boxShadow: "var(--shadow-popup)",
whiteSpace: "pre-wrap",
},
".cm-lsp-hint-spinner": { display: "inline-flex", verticalAlign: "-2px", marginRight: "var(--space-2)" },
// The hint is the tooltip element itself: beat the editor theme's `.cm-tooltip` width.
".cm-tooltip.cm-lsp-hint": { maxWidth: "min(480px, 100%)" },
".cm-lsp-hint-error": {
+2 -2
View File
@@ -42,7 +42,7 @@ import {
usagesOf,
} from "./stepIndex";
const SCRIPT = /\.(?:[cm]?[jt]sx?|java|kt)$/i;
const SCRIPT = /\.(?:[cm]?[jt]sx?|java|kt|py)$/i;
const FEATURE = /\.feature$/i;
const DELAY_MS = 300;
@@ -168,7 +168,7 @@ export function activate(): () => void {
return targets.length ? { from: step.from, to: step.to, targets } : null;
}
if (!SCRIPT.test(doc.path)) return null;
const definition = stepDefinitions(text).find((d) => d.from <= offset && offset < d.to);
const definition = stepDefinitions(text, doc.path).find((d) => d.from <= offset && offset < d.to);
if (!definition) return null;
const targets = await usagesOf(definition);
return targets.length ? { from: definition.from, to: definition.to, targets } : null;
+123
View File
@@ -0,0 +1,123 @@
import { describe, expect, it } from "vitest";
import { definitionInsertions, matchesStep, snippetStyle, stepDefinitions, stepSnippet, type FeatureStep } from "./steps";
const step = (text: string): FeatureStep => ({ from: 0, to: 0, keyword: "Given ", type: "Given", text, matchText: text });
const apply = (text: string, insertions: { at: number; text: string }[]) =>
[...insertions].sort((a, b) => b.at - a.at).reduce((out, i) => out.slice(0, i.at) + i.text + out.slice(i.at), text);
describe("behave steps", () => {
const BEHAVE = `from behave import given, then
@given('an empty cart')
def step_impl(context):
pass
@given(u"a cart with {count:d} items")
def step_impl(context, count):
pass
@then('the total is {amount:f} {currency}')
def step_impl(context, amount, currency):
pass
use_step_matcher("re")
@when(r'^I remove (?P<count>\\d+) items?$')
def step_impl(context, count):
pass
@step("""I check out""")
def step_impl(context):
pass
`;
it("reads parse formats, and regular expressions after use_step_matcher", () => {
const defs = stepDefinitions(BEHAVE, "features/steps/cart_steps.py");
expect(defs.map((d) => d.source)).toEqual([
"an empty cart",
"a cart with {count:d} items",
"the total is {amount:f} {currency}",
"^I remove (?P<count>\\d+) items?$",
"I check out",
]);
const [empty, count, total, remove, checkout] = defs;
expect(BEHAVE.slice(count!.from, count!.to)).toBe('u"a cart with {count:d} items"');
expect(matchesStep(empty!, step("an empty cart"))).toBe(true);
expect(matchesStep(empty!, step("an empty cart now"))).toBe(false);
expect(matchesStep(count!, step("a cart with 3 items"))).toBe(true);
expect(matchesStep(count!, step("a cart with three items"))).toBe(false);
expect(matchesStep(total!, step("the total is 3.50 EUR"))).toBe(true);
expect(matchesStep(remove!, step("I remove 2 items"))).toBe(true);
expect(matchesStep(remove!, step("I remove all items"))).toBe(false);
// `use_step_matcher("re")` stays in effect: a plain string is a regular expression.
expect(matchesStep(checkout!, step("I check out"))).toBe(true);
});
it("writes a pending step_impl and imports the decorator", () => {
expect(snippetStyle("features/steps/cart_steps.py", BEHAVE)).toBe("behave");
const snippet = stepSnippet("When", "I pay {gift} 'now'", "behave");
expect(snippet.text).toBe(
[
"@when('I pay {{gift}} \\'now\\'')",
"def step_impl(context):",
" raise NotImplementedError('STEP: When I pay {gift} \\'now\\'')",
"",
].join("\n"),
);
const { insertions, select } = definitionInsertions(BEHAVE, "When", snippet, "behave");
const out = apply(BEHAVE, insertions);
expect(out.startsWith("from behave import given, then, when\n")).toBe(true);
expect(out.endsWith(` pass\n\n\n${snippet.text}`)).toBe(true);
expect(out.split("\n")[select.line]!.slice(select.from, select.to)).toMatch(/^raise NotImplementedError/);
const created = stepDefinitions(out, "steps.py").find((d) => d.source.startsWith("I pay"))!;
expect(created).toBeDefined();
});
it("matches a created step's braces literally", () => {
const text = `from behave import when\n\n${stepSnippet("When", "I pay {gift}", "behave").text}`;
const [created] = stepDefinitions(text, "steps.py");
expect(matchesStep(created!, step("I pay {gift}"))).toBe(true);
});
});
describe("pytest-bdd steps", () => {
const PYTEST_BDD = `from pytest_bdd import given, parsers, scenarios, when
scenarios("cart.feature")
@given("there are {count} cucumbers")
def literal():
pass
@given(parsers.parse("I have {start:d} cucumbers"), target_fixture="cucumbers")
def start(start):
return start
@when(parsers.re(r"I eat (?P<eat>\\d+) cucumbers"))
def eat(cucumbers, eat):
pass
`;
it("matches plain strings exactly and parsers by their kind", () => {
const [literal, start, eat] = stepDefinitions(PYTEST_BDD, "tests/test_cart.py");
expect(matchesStep(literal!, step("there are {count} cucumbers"))).toBe(true);
expect(matchesStep(literal!, step("there are 5 cucumbers"))).toBe(false);
expect(matchesStep(start!, step("I have 12 cucumbers"))).toBe(true);
expect(matchesStep(eat!, step("I eat 5 cucumbers"))).toBe(true);
expect(matchesStep(eat!, step("I eat 5 cucumbers today"))).toBe(false);
});
it("writes an anonymous pending step", () => {
expect(snippetStyle("tests/test_cart.py", PYTEST_BDD)).toBe("pytest-bdd");
const snippet = stepSnippet("Then", "I have {count} cucumbers", "pytest-bdd");
expect(snippet.text).toBe(
["@then('I have {count} cucumbers')", "def _():", " raise NotImplementedError", ""].join("\n"),
);
const { insertions } = definitionInsertions(PYTEST_BDD, "Then", snippet, "pytest-bdd");
const out = apply(PYTEST_BDD, insertions);
expect(out.startsWith("from pytest_bdd import given, parsers, scenarios, when, then\n")).toBe(true);
const created = stepDefinitions(out, "tests/test_cart.py").at(-1)!;
expect(matchesStep(created, step("I have {count} cucumbers"))).toBe(true);
});
});
+171
View File
@@ -0,0 +1,171 @@
/**
* Python step definitions: behave's `@given('…')` decorators (with its `parse`, `cfparse` and
* `re` step matchers, switched by `use_step_matcher`) and pytest-bdd's (plain strings matched
* exactly, `parsers.parse` / `cfparse` / `re`).
*/
import type { StepDefinition } from "./steps";
export type PythonFramework = "behave" | "pytest-bdd";
type Matcher = "parse" | "re" | "string";
/** pytest-bdd when the file imports it; behave otherwise. */
export function pythonFramework(text: string): PythonFramework {
return /^[ \t]*(?:from|import)[ \t]+pytest_bdd\b/m.test(text) ? "pytest-bdd" : "behave";
}
const DECORATOR = /@(?:given|when|then|step|Given|When|Then|Step)\s*\(\s*/g;
const STEP_MATCHER = /\b(?:use_step_matcher|step_matcher)\s*\(\s*["'](\w+)["']\s*\)/g;
const PARSER = /^(?:parsers\.)?(parse|cfparse|re|string)\s*\(\s*/;
/** The step definitions of a Python file. */
export function pythonStepDefinitions(text: string): StepDefinition[] {
const framework = pythonFramework(text);
// behave's matcher in effect from each `use_step_matcher(…)` call on.
const switches = [...text.matchAll(STEP_MATCHER)].map((m) => ({
at: m.index,
matcher: (m[1] === "re" ? "re" : "parse") as Matcher,
}));
const out: StepDefinition[] = [];
for (const m of text.matchAll(DECORATOR)) {
let at = m.index + m[0].length;
let matcher: Matcher =
framework === "pytest-bdd" ? "string" : (switches.filter((s) => s.at < m.index).pop()?.matcher ?? "parse");
const parser = PARSER.exec(text.slice(at, at + 32));
if (parser) {
matcher = parser[1] === "re" ? "re" : parser[1] === "string" ? "string" : "parse";
at += parser[0].length;
}
const literal = readPythonString(text, at);
if (!literal) continue;
const regexp = matcherRegExp(literal.value, matcher);
if (regexp) out.push({ from: at, to: literal.end, source: literal.value, regexp });
}
return out;
}
/** A string literal at `at` (an `r`/`u`/`b` prefix and triple quotes included), without f-strings. */
export function readPythonString(text: string, at: number): { value: string; end: number } | null {
const prefix = /^[rRuUbB]{0,2}/.exec(text.slice(at, at + 2))![0];
const raw = /r/i.test(prefix);
let i = at + prefix.length;
const quote = text[i];
if (quote !== "'" && quote !== '"') return null;
const triple = text.startsWith(quote.repeat(3), i);
const close = triple ? quote.repeat(3) : quote;
i += close.length;
let value = "";
for (; i < text.length; i++) {
if (text.startsWith(close, i)) return { value, end: i + close.length };
const c = text[i]!;
if (c === "\\" && i + 1 < text.length) {
const next = text[i + 1]!;
// Raw strings keep the backslash; others resolve the escapes a pattern can contain.
value += raw ? c + next : next === "\\" || next === "'" || next === '"' ? next : next === "n" ? "\n" : c + next;
i++;
} else if (c === "\n" && !triple) return null;
else value += c;
}
return null;
}
function matcherRegExp(value: string, matcher: Matcher): RegExp | null {
if (matcher === "string") return new RegExp(`^${escapeRe(value)}$`);
if (matcher === "parse") return parseFormatRegExp(value);
try {
// Python's named groups and back-references; steps match the whole text.
const source = value.replace(/\(\?P<(\w+)>/g, "(?<$1>").replace(/\(\?P=(\w+)\)/g, "\\k<$1>");
return new RegExp(`^(?:${source})$`);
} catch {
return null;
}
}
const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
const NUMBER = "[-+]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][-+]?\\d+)?";
/** The `parse` library's field types (`{count:d}`); others, and untyped fields, match any text. */
const PARSE_TYPES: Record<string, string> = {
d: "[-+]?\\d+",
n: "[-+]?\\d[\\d,]*",
f: NUMBER,
F: NUMBER,
e: NUMBER,
g: NUMBER,
w: "\\w+",
W: "\\W+",
s: "\\s+",
S: "\\S+",
D: "\\D+",
l: "[A-Za-z]+",
};
/**
* A `parse` format (`there are {count:d} cucumbers`) as an anchored, case-insensitive regular
* expression, as `parse` matches. `{{` and `}}` are literal braces; cfparse's cardinality
* (`{names:w+}`) matches any text. Null when a field is not closed.
*/
export function parseFormatRegExp(format: string): RegExp | null {
let out = "";
for (let i = 0; i < format.length; i++) {
const c = format[i]!;
if ((c === "{" || c === "}") && format[i + 1] === c) {
out += `\\${c}`;
i++;
} else if (c === "{") {
const end = format.indexOf("}", i);
if (end < 0) return null;
const field = format.slice(i + 1, end);
const spec = field.includes(":") ? field.slice(field.indexOf(":") + 1) : "";
out += `(${PARSE_TYPES[spec.slice(-1)] && !/[?*+]$/.test(spec) ? PARSE_TYPES[spec.slice(-1)] : ".+?"})`;
i = end;
} else out += escapeRe(c);
}
try {
return new RegExp(`^${out}$`, "i");
} catch {
return null;
}
}
/**
* The pending definition behave or pytest-bdd suggests for an undefined step, at the top level
* of the module. `body` is the line (0-based) the caret goes to.
*/
export function pythonStepSnippet(
keyword: string,
text: string,
framework: PythonFramework,
): { text: string; body: number } {
const decorator = keyword.toLowerCase();
const quoted = (s: string) => `'${s.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
if (framework === "pytest-bdd")
return { text: [`@${decorator}(${quoted(text)})`, "def _():", " raise NotImplementedError", ""].join("\n"), body: 2 };
// behave reads the pattern as a `parse` format: braces in the step are literal.
const format = text.replace(/[{}]/g, "$&$&");
return {
text: [
`@${decorator}(${quoted(format)})`,
"def step_impl(context):",
` raise NotImplementedError(${quoted(`STEP: ${keyword} ${text}`)})`,
"",
].join("\n"),
body: 2,
};
}
/** The `from behave import …` (or `pytest_bdd`) line's insertion that adds `keyword`, if one is missing. */
export function pythonImportInsertion(
fileText: string,
keyword: string,
framework: PythonFramework,
): { at: number; text: string } | null {
const module = framework === "pytest-bdd" ? "pytest_bdd" : "behave";
const line = new RegExp(`^from[ \\t]+${module}[ \\t]+import[ \\t]+([\\w ,\\t]+?)[ \\t]*$`, "m").exec(fileText);
if (!line) return null;
const names = line[1]!.split(",").map((n) => n.trim());
const name = keyword.toLowerCase();
if (names.includes(name) || names.includes("*")) return null;
return { at: line.index + line[0].length, text: `, ${name}` };
}
+6 -4
View File
@@ -35,7 +35,7 @@ export async function dialectOf(text: string): Promise<GherkinDialect> {
/** Whether a change to `path` can change the index: a feature or script file. */
export function isCucumberSource(path: string): boolean {
return /\.(feature|[cm]?[jt]sx?|java|kt)$/i.test(path);
return /\.(feature|[cm]?[jt]sx?|java|kt|py)$/i.test(path);
}
/** Forgets the index, so the next lookup rescans the project. */
@@ -51,7 +51,7 @@ function stepIndex(): Promise<StepIndex> {
const path = ipcPath(s.path);
if (s.feature) index.steps.push({ path, text: s.text, items: featureSteps(s.text, await dialectOf(s.text)) });
else {
const items = stepDefinitions(s.text);
const items = stepDefinitions(s.text, path);
if (items.length) index.definitions.push({ path, text: s.text, items });
}
}
@@ -110,13 +110,15 @@ export async function undefinedSteps(steps: readonly FeatureStep[]): Promise<Fea
/** The file a new definition for a step of `featurePath` goes to: the step file named like the feature, else the one with the most definitions. */
export async function definitionFileFor(featurePath: string): Promise<string | null> {
const { definitions } = await stepIndex();
// `cart.steps.ts`, `CartSteps.java` and `cart_stepdefs.kt` all name the `cart` feature's steps.
// `cart.steps.ts`, `CartSteps.java`, `cart_stepdefs.kt` and pytest-bdd's `test_cart.py` all
// name the `cart` feature's steps.
const stem = (path: string) =>
(path.split("/").pop() ?? "")
.split(".")[0]!
.toLowerCase()
.replace(/[^a-z0-9]/g, "")
.replace(/(?:stepdefinitions|stepdefs|steps)$/, "");
.replace(/(?:stepdefinitions|stepdefs|steps)$/, "")
.replace(/^test(?=.)/, "");
const feature = stem(featurePath);
const named = definitions.find((file) => stem(file.path) === feature);
if (named) return named.path;
+22 -5
View File
@@ -2,8 +2,15 @@
* Cucumber steps, both sides: the step definitions of JavaScript / TypeScript files
* (`Given('a {int} item cart', …)` of `@cucumber/cucumber`, `playwright-bdd`'s `createBdd()`
* functions) and the steps of Gherkin `.feature` files, with Cucumber expression and regular
* expression matching between them.
* expression matching between them. Python files are read by `pythonSteps`.
*/
import {
pythonFramework,
pythonImportInsertion,
pythonStepDefinitions,
pythonStepSnippet,
type PythonFramework,
} from "./pythonSteps";
/**
* A step definition: the pattern literal of a `Given` / `When` / `Then` / `Step` call, or of a
@@ -35,8 +42,9 @@ export interface FeatureStep {
const DEFINE = /\b(?:Given|When|Then|And|But|Step|defineStep)\s*(?:<[^<>()]*>\s*)?\(\s*/g;
/** The step definitions of a script. */
export function stepDefinitions(text: string): StepDefinition[] {
/** The step definitions of a script; `path` tells a Python file from the others. */
export function stepDefinitions(text: string, path = ""): StepDefinition[] {
if (/\.py$/i.test(path)) return pythonStepDefinitions(text);
const out: StepDefinition[] = [];
for (const m of text.matchAll(DEFINE)) {
const at = m.index + m[0].length;
@@ -303,12 +311,15 @@ export function definingKeyword(steps: readonly FeatureStep[], step: FeatureStep
/** Strings and numbers of a step's text, as Cucumber's snippets turn them into parameters. */
const SNIPPET_PARAMETER = /(^|[^\w.-])("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|-?\d+\.\d+|-?\d+)(?![\w.])/g;
export type SnippetStyle = "cucumber" | "playwright-bdd" | "java" | "kotlin";
export type SnippetStyle = "cucumber" | "playwright-bdd" | "java" | "kotlin" | PythonFramework;
const isPython = (style: SnippetStyle): style is PythonFramework => style === "behave" || style === "pytest-bdd";
/** The snippet style of a step definition file. */
export function snippetStyle(path: string, text: string): SnippetStyle {
if (/\.java$/i.test(path)) return "java";
if (/\.kts?$/i.test(path)) return "kotlin";
if (/\.py$/i.test(path)) return pythonFramework(text);
return /\bcreateBdd\s*\(/.test(text) ? "playwright-bdd" : "cucumber";
}
@@ -351,6 +362,7 @@ export function stepSnippet(
text: string,
style: SnippetStyle = "cucumber",
): { text: string; body: number } {
if (isPython(style)) return pythonStepSnippet(keyword, text, style);
const jvm = style === "java" || style === "kotlin";
const types: string[] = [];
let expression = "";
@@ -457,7 +469,12 @@ export function definitionInsertions(
}
} else {
at = fileText.length;
const lead = !fileText ? "" : fileText.endsWith("\n\n") ? "" : fileText.endsWith("\n") ? "\n" : "\n\n";
const imported = isPython(style) ? pythonImportInsertion(fileText, keyword, style) : null;
if (imported) insertions.push(imported);
// Python separates top-level functions with two blank lines.
const gap = isPython(style) ? "\n\n\n" : "\n\n";
const ending = /\n*$/.exec(fileText)![0].length;
const lead = !fileText ? "" : gap.slice(Math.min(ending, gap.length));
insert = lead + snippet.text;
}
insertions.push({ at, text: insert });
+3 -4
View File
@@ -6,12 +6,11 @@ import { defaultKeymap, history, historyKeymap, indentWithTab, redo, undo } from
import { syntaxHighlighting } from "@codemirror/language";
import { caretRowHighlight } from "@/editor/core/cm/caretRow";
import { findExtension, findResultLines, findScope, type FindOptions } from "@/editor/core/cm/find";
import { foldingExtension } from "@/editor/core/cm/folding";
import { openideaHighlightStyle } from "@/editor/core/cm/highlight";
import { overviewLane } from "@/editor/core/cm/lanes";
import { findLanguageDescription, loadSupport } from "@/editor/core/cm/languages";
import { caretExtension, fontZoom, setFontZoom, stepFontZoom } from "@/editor/core/cm/prefs";
import { fontExtension, heavyExtension, lineNumbersExtension } from "@/editor/core/cm/setup";
import { foldingFor, fontExtension, heavyExtension, lineNumbersExtension } from "@/editor/core/cm/setup";
import { openideaTheme } from "@/editor/core/cm/theme";
import { FindPanel, searchAgain, seedFind, type FindTarget } from "@/editor/core/view/FindBar";
import { executeAction } from "@/platform/actions";
@@ -417,7 +416,7 @@ function PluginContentEditorBody({ input, initialData }: { input: EditorInput; i
fontSlot.of(fontExtension(editorSettings())),
caretSlot.of(caretExtension(editorPrefs())),
lineNumbersSlot.of(lineNumbersExtension(editorSettings())),
foldingSlot.of(foldingExtension(editorPrefs().folding.showOutline)),
foldingSlot.of(foldingFor(editorSettings())),
heavySlot.of(heavyExtension(session.currentText.length, editorSettings())),
langSlot.of([]),
readOnlySlot.of([EditorState.readOnly.of(effectiveReadOnly), EditorView.editable.of(!effectiveReadOnly)]),
@@ -482,7 +481,7 @@ function PluginContentEditorBody({ input, initialData }: { input: EditorInput; i
effects.push(
fontSlot.reconfigure(fontExtension(nextEditor)),
lineNumbersSlot.reconfigure(lineNumbersExtension(nextEditor)),
foldingSlot.reconfigure(foldingExtension(nextPrefs.folding.showOutline)),
foldingSlot.reconfigure(foldingFor(nextEditor)),
heavySlot.reconfigure(heavyExtension(view.state.doc.length, nextEditor)),
);
}
+20 -11
View File
@@ -449,20 +449,29 @@ const FOLDING = editorPage({
order: 4,
keywords: ["fold", "collapse"],
sections: [
{ items: [{ t: "check", key: `${P}.folding.showOutline`, label: "Show code folding outline" }] },
{
items: [
{
t: "check",
key: `${P}.folding.showOutline`,
label: "Show code folding outline",
inline: {
kind: "select",
key: `${P}.folding.outlineVisibility`,
options: [
{ value: "hover", label: "On hover" },
{ value: "always", label: "Always" },
],
},
},
],
},
{
title: "Fold by default:",
items: [
{
t: "grid",
columns: [
[
{ t: "check", key: `${P}.folding.fileHeader`, label: "File header" },
{ t: "check", key: `${P}.folding.imports`, label: "Imports" },
],
[{ t: "check", key: `${P}.folding.docComments`, label: "Documentation comments" }],
],
},
{ t: "check", key: `${P}.folding.fileHeader`, label: "File header" },
{ t: "check", key: `${P}.folding.imports`, label: "Imports" },
{ t: "check", key: `${P}.folding.docComments`, label: "Documentation comments" },
],
},
],
+8 -1
View File
@@ -143,7 +143,13 @@ export const EDITOR_EXTRA_DEFAULTS = {
parameterInfoDelay: 1000,
},
autoImport: { addUnambiguous: false, optimizeOnTheFly: false, importsOnPaste: "ask" as "ask" | "always" | "never" },
folding: { showOutline: true, fileHeader: true, imports: true, docComments: false },
folding: {
showOutline: true,
outlineVisibility: "hover" as "hover" | "always",
fileHeader: true,
imports: true,
docComments: false,
},
tabs: {
placement: "top",
multipleRows: false,
@@ -278,6 +284,7 @@ export function validateEditorExtras(raw: unknown): EditorExtras {
const out = coerceLike(raw, EDITOR_EXTRA_DEFAULTS);
if (!["active", "all"].includes(out.general.wheelZoomScope)) out.general.wheelZoomScope = "active";
if (!["ask", "always", "never"].includes(out.autoImport.importsOnPaste)) out.autoImport.importsOnPaste = "ask";
if (!["hover", "always"].includes(out.folding.outlineVisibility)) out.folding.outlineVisibility = "hover";
if (!DOCSTRING_FORMATS.includes(out.docstringFormat)) out.docstringFormat = "rest";
for (const k of ["codeVisionUsagesPosition", "codeVisionAuthorPosition"] as const)
if (!["top", "right"].includes(out.inlay[k])) out.inlay[k] = "top";
+1
View File
@@ -1,6 +1,7 @@
{
"Open diff as editor tab": "Open diff as editor tab",
"When off, diffs you open (changes, history, Compare) show in a separate window.": "When off, diffs you open (changes, history, Compare) show in a separate window.",
"On hover": "On hover",
"Plain-text word lists (one word per line) or Hunspell .dic files; a .dic with its .aff beside it also accepts the inflected forms and compounds its rules allow. Relative paths resolve against the project root, ~/ against the home folder.": "Plain-text word lists (one word per line) or Hunspell .dic files; a .dic with its .aff beside it also accepts the inflected forms and compounds its rules allow. Relative paths resolve against the project root, ~/ against the home folder.",
"Trust Folder": "Trust Folder",
"No files selected": "No files selected",
+1
View File
@@ -1,6 +1,7 @@
{
"Open diff as editor tab": "以编辑器标签页打开差异",
"When off, diffs you open (changes, history, Compare) show in a separate window.": "关闭后,打开的差异(更改、历史记录、比较)会显示在单独的窗口中。",
"On hover": "悬停时",
"Plain-text word lists (one word per line) or Hunspell .dic files; a .dic with its .aff beside it also accepts the inflected forms and compounds its rules allow. Relative paths resolve against the project root, ~/ against the home folder.": "纯文本单词列表(每行一个词)或 Hunspell .dic 文件;旁边有同名 .aff 的 .dic 还会接受其规则允许的变形和复合词。相对路径基于项目根目录解析,~/ 表示主目录。",
"MCP server instructions": "MCP 服务器说明",
"MCP tools (deferred)": "MCP 工具(延迟加载)",