diff --git a/e2e/stickyLinesLayout.spec.ts b/e2e/stickyLinesLayout.spec.ts index 0e02c70d66..55116501f0 100644 --- a/e2e/stickyLinesLayout.spec.ts +++ b/e2e/stickyLinesLayout.spec.ts @@ -1,6 +1,6 @@ import type { EditorView } from "@codemirror/view"; import type { Locator } from "@playwright/test"; -import { expect, test } from "./fixtures"; +import { expect, stubDesktop, stubSettingsWrites, test } from "./fixtures"; function codeStart(line: Locator) { return line.evaluate((element) => { @@ -19,6 +19,8 @@ function codeStart(line: Locator) { } test("Sticky Lines follows editor text, line numbers and the viewport below Find", async ({ workbench: page }) => { + await stubDesktop(page); + await stubSettingsWrites(page); await page.setViewportSize({ width: 1200, height: 760 }); const header = "for (const value of entries)"; const path = "/dev/acme-api/sticky-layout.ts"; @@ -126,4 +128,17 @@ test("Sticky Lines follows editor text, line numbers and the viewport below Find await find.press("Escape"); await pinned.locator(".cm-openidea-stickyText").click({ position: { x: 32, y: 4 } }); await expect(content.locator(".cm-activeLine")).toContainText(header); + + await page.evaluate(async () => { + const actionsModule = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ actionsModule); + await executeAction("editor.disableStickyLinesForLanguage"); + }); + await expect(sticky).toHaveCount(0); + const disabled = await page.evaluate(async () => { + const projectModule = "/src/platform/project.ts"; + const { useProject } = await import(/* @vite-ignore */ projectModule); + return useProject.getState().settings?.editor?.codeEditing?.stickyLinesDisabledLanguages; + }); + expect(disabled).toContain("typescript"); }); diff --git a/src/editor/core/actions.ts b/src/editor/core/actions.ts index 075f0f2090..841954557b 100644 --- a/src/editor/core/actions.ts +++ b/src/editor/core/actions.ts @@ -778,6 +778,37 @@ export async function toggleSchemeSwitch(key: SchemeSwitch, on = !editorPrefs()[ } } +/** IDEA's StickyLinesDisableForLangAction: the active document's known base language, if enabled. */ +function activeStickyLinesLanguage(): string | null { + // A notebook cell has no base document: never change the last text tab's language setting. + if (!appearanceSwitches.stickyLines.on() || focusedCellEditor()) return null; + return activeSlot()?.model.info?.id || null; +} + +async function disableStickyLinesForLanguage(): Promise { + const language = activeStickyLinesLanguage(); + if (!language) return; + const disabled = editorPrefs().codeEditing.stickyLinesDisabledLanguages; + if (disabled.includes(language)) return; + try { + useProject.setState({ + settings: await call( + commands.settingsSet( + "global", + "editor.codeEditing.stickyLinesDisabledLanguages", + JSON.stringify([...disabled, language]), + ), + ), + }); + } catch (e) { + notifications().notify({ + level: "error", + title: "Cannot change the setting", + body: e instanceof Error ? e.message : String(e), + }); + } +} + const appearanceSwitch = (id: string, s: AppearanceSwitch, keywords: string[]): Action => ({ id, title: s.title, @@ -896,6 +927,16 @@ export function registerEditorActions(): () => void { appearanceSwitch("editor.toggleLineNumbers", appearanceSwitches.lineNumbers, ["gutter", "hide line numbers"]), appearanceSwitch("editor.toggleIndentGuides", appearanceSwitches.indentGuides, ["indent lines", "indentation"]), appearanceSwitch("editor.toggleStickyLines", appearanceSwitches.stickyLines, ["sticky scroll"]), + { + id: "editor.disableStickyLinesForLanguage", + title: "Disable Sticky Lines for Language", + description: "Disable sticky lines for the current document's language without changing the global setting.", + category: "View", + keywords: ["sticky scroll", "language"], + when: "textEditorActive", + enabled: () => activeStickyLinesLanguage() !== null, + run: disableStickyLinesForLanguage, + }, appearanceSwitch("editor.toggleWhitespaces", appearanceSwitches.whitespaces, [ "render whitespace", "spaces", diff --git a/src/editor/core/appearanceSwitches.test.ts b/src/editor/core/appearanceSwitches.test.ts index 8651c654f9..567ac51987 100644 --- a/src/editor/core/appearanceSwitches.test.ts +++ b/src/editor/core/appearanceSwitches.test.ts @@ -1,4 +1,12 @@ -import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { useContextKeys } from "@/platform/context"; +import type { EditorSlot } from "./model/slot"; + +let activeSlot: Partial | null = null; +vi.mock("./controller", async (importOriginal) => ({ + ...(await importOriginal()), + editorController: () => ({ activeSlot }), +})); /** Writes the dotted key into the current settings, as the host does. */ const ipc = vi.hoisted(() => ({ @@ -29,7 +37,9 @@ beforeAll(() => { }); afterAll(() => dispose()); afterEach(() => { + activeSlot = null; useProject.setState({ settings: null }); + useContextKeys.setState({}, true); vi.clearAllMocks(); }); @@ -71,6 +81,82 @@ describe("editor appearance toggle actions", () => { expect(resolveActionId("EditorGutterToggleGlobalLineNumbers")).toBe("editor.toggleLineNumbers"); expect(resolveActionId("EditorGutterToggleGlobalIndentLines")).toBe("editor.toggleIndentGuides"); expect(resolveActionId("EditorGutterToggleGlobalStickyLines")).toBe("editor.toggleStickyLines"); + expect(resolveActionId("EditorStickyLinesDisableForLang")).toBe("editor.disableStickyLinesForLanguage"); + }); +}); + +describe("Disable Sticky Lines for Language", () => { + const id = "editor.disableStickyLinesForLanguage"; + const key = "editor.codeEditing.stickyLinesDisabledLanguages"; + const typescript = { id: "typescript", name: "TypeScript" }; + const activate = (info: { id: string; name: string } | undefined) => { + activeSlot = { disposed: false, model: { info } } as never; + }; + const setEditorSettings = (disabledLanguages: string[], stickyScroll = true) => { + useProject.setState({ + settings: { editor: { stickyScroll, codeEditing: { stickyLinesDisabledLanguages: disabledLanguages } } } as never, + }); + }; + + beforeEach(() => useContextKeys.setState({ textEditorActive: true })); + + it("is a View action mapped from IDEA", () => { + expect(getAction(id)).toMatchObject({ title: "Disable Sticky Lines for Language", category: "View" }); + expect(resolveActionId("EditorStickyLinesDisableForLang")).toBe(id); + }); + + it("is unavailable when global sticky lines are off", async () => { + activate(typescript); + setEditorSettings(["python"], false); + expect(await executeAction(id)).toBe(false); + expect(ipc.settingsSet).not.toHaveBeenCalled(); + }); + + it("is unavailable without a known active document language", async () => { + setEditorSettings(["python"]); + expect(await executeAction(id)).toBe(false); + activate(undefined); + expect(await executeAction(id)).toBe(false); + expect(ipc.settingsSet).not.toHaveBeenCalled(); + }); + + it("writes the current language id once while preserving other disabled languages", async () => { + activate(typescript); + setEditorSettings(["python", "go"]); + + expect(await executeAction(id)).toBe(true); + expect(ipc.settingsSet).toHaveBeenCalledTimes(1); + expect(ipc.settingsSet).toHaveBeenCalledWith("global", key, JSON.stringify(["python", "go", "typescript"])); + expect(useProject.getState().settings?.editor).toMatchObject({ + stickyScroll: true, + codeEditing: { stickyLinesDisabledLanguages: ["python", "go", "typescript"] }, + }); + }); + + it("is idempotent across repeated execution", async () => { + activate(typescript); + setEditorSettings(["python"]); + + expect(await executeAction(id)).toBe(true); + expect(await executeAction(id)).toBe(true); + expect(ipc.settingsSet).toHaveBeenCalledTimes(1); + expect(ipc.settingsSet).toHaveBeenCalledWith("global", key, JSON.stringify(["python", "typescript"])); + }); + + it("leaves the global Show Sticky Lines toggle independent", async () => { + activate(typescript); + setEditorSettings(["python"]); + const globalToggle = getAction("editor.toggleStickyLines"); + + await executeAction(id); + expect(actionToggled(globalToggle)).toBe(true); + await executeAction("editor.toggleStickyLines"); + expect(ipc.settingsSet).toHaveBeenLastCalledWith("global", "editor.stickyScroll", "false"); + expect(actionToggled(globalToggle)).toBe(false); + expect(useProject.getState().settings?.editor).toMatchObject({ + codeEditing: { stickyLinesDisabledLanguages: ["python", "typescript"] }, + }); + expect(await executeAction(id)).toBe(false); }); }); diff --git a/src/platform/ideaActions.ts b/src/platform/ideaActions.ts index cf77ac14d9..11bd15aad2 100644 --- a/src/platform/ideaActions.ts +++ b/src/platform/ideaActions.ts @@ -157,6 +157,7 @@ export const IDEA_ACTIONS: Readonly> = { EditorGutterToggleGlobalLineNumbers: "editor.toggleLineNumbers", EditorGutterToggleGlobalIndentLines: "editor.toggleIndentGuides", EditorGutterToggleGlobalStickyLines: "editor.toggleStickyLines", + EditorStickyLinesDisableForLang: "editor.disableStickyLinesForLanguage", ActivateProjectToolWindow: "toolwindow.project", HideAllWindows: "toolwindow.hideAll", HideActiveWindow: "toolwindow.hideActive", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index 41066e96bc..2bf4744bb1 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -3019,6 +3019,8 @@ "Soft-Wrap": "軟換行", "Show Line Numbers": "顯示行號", "Show Indent Guides": "顯示縮排參考線", + "Disable Sticky Lines for Language": "為目前語言停用粘滯行", + "Disable sticky lines for the current document's language without changing the global setting.": "為目前文件的語言停用粘滯行,不變更全域設定。", "Show Sticky Lines": "顯示粘滯行", "Show Whitespaces": "顯示空白", "Use Soft Wraps": "使用軟換行", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 06ed628680..63caa97ba1 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -3674,6 +3674,8 @@ "Soft-Wrap": "软换行", "Show Line Numbers": "显示行号", "Show Indent Guides": "显示缩进参考线", + "Disable Sticky Lines for Language": "为当前语言禁用粘滞行", + "Disable sticky lines for the current document's language without changing the global setting.": "为当前文档的语言禁用粘滞行,不更改全局设置。", "Show Sticky Lines": "显示粘滞行", "Show Whitespaces": "显示空白", "Use Soft Wraps": "使用软换行",