Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai ac1ee9d651 fix(editor): let other X11 apps keep the primary selection while the window is inactive
Another application taking the primary selection makes WebKitGTK clear the
page's selection. An editor in an inactive window is still the active
element, so CodeMirror wrote its selection back and claimed the primary
selection again: terminals and other apps could not keep a selection while
an OpenIDEA editor had one. On Linux the editor now accepts the cleared
selection while its window is inactive, keeping its carets.

Fixes #6309
2026-11-29 20:03:24 +08:00
3 змінених файлів з 121 додано та 0 видалено

No files matched your search

+74
Переглянути файл
@@ -0,0 +1,74 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { EditorSelection, EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { usePlatform } from "@/platform/platformInfo";
import { primarySelectionYield, yieldPrimarySelection } from "./primarySelection";
function editor(selection: EditorSelection, extensions = [primarySelectionYield]) {
const parent = document.createElement("div");
document.body.appendChild(parent);
const view = new EditorView({
parent,
state: EditorState.create({
doc: "first line\nsecond line\nthird line",
selection,
extensions: [EditorState.allowMultipleSelections.of(true), ...extensions],
}),
});
view.contentDOM.focus();
return view;
}
describe("primary selection in an inactive window", () => {
const initialOs = usePlatform.getState().os;
afterEach(() => {
vi.restoreAllMocks();
usePlatform.setState({ os: initialOs });
document.body.innerHTML = "";
});
it("gives up the editor's selection when the page's selection was cleared, keeping the carets", () => {
const view = editor(EditorSelection.create([EditorSelection.range(0, 5), EditorSelection.range(11, 17)], 1));
vi.spyOn(document, "hasFocus").mockReturnValue(false);
document.getSelection()!.removeAllRanges();
expect(yieldPrimarySelection(view)).toBe(true);
expect(view.state.selection.ranges.map((r) => [r.anchor, r.head])).toEqual([
[5, 5],
[17, 17],
]);
expect(view.state.selection.mainIndex).toBe(1);
view.destroy();
});
it("keeps the selection while the window is active, the range is still in the page or the editor lost focus", () => {
const view = editor(EditorSelection.single(0, 5));
document.getSelection()!.removeAllRanges();
const hasFocus = vi.spyOn(document, "hasFocus").mockReturnValue(true);
expect(yieldPrimarySelection(view)).toBe(false);
hasFocus.mockReturnValue(false);
const text = view.contentDOM.querySelector(".cm-line")!.firstChild!;
document.getSelection()!.setBaseAndExtent(text, 0, text, 5);
expect(yieldPrimarySelection(view)).toBe(false);
document.getSelection()!.removeAllRanges();
view.contentDOM.blur();
expect(yieldPrimarySelection(view)).toBe(false);
expect(view.state.selection.main.empty).toBe(false);
view.destroy();
});
it("runs on selection changes only on Linux", () => {
vi.spyOn(document, "hasFocus").mockReturnValue(false);
usePlatform.setState({ os: "macos" });
const mac = editor(EditorSelection.single(0, 5));
document.getSelection()!.removeAllRanges();
document.dispatchEvent(new Event("selectionchange"));
expect(mac.state.selection.main.empty).toBe(false);
mac.destroy();
usePlatform.setState({ os: "linux" });
const linux = editor(EditorSelection.single(0, 5));
document.getSelection()!.removeAllRanges();
document.dispatchEvent(new Event("selectionchange"));
expect(linux.state.selection.main).toMatchObject({ anchor: 5, head: 5 });
linux.destroy();
});
});
+45
Переглянути файл
@@ -0,0 +1,45 @@
import { EditorSelection } from "@codemirror/state";
import { ViewPlugin, type EditorView } from "@codemirror/view";
import { usePlatform } from "@/platform/platformInfo";
/**
* X11: when another application takes the primary selection, WebKitGTK clears the page's
* selection. An editor in an inactive window is still the document's active element, so
* CodeMirror writes its selection back into the page, which claims the primary selection
* again: no other application could keep a selection while an editor had one. While the
* window is inactive the editor accepts the cleared selection (its carets stay where they
* were), as X11 and GTK applications give up theirs.
*/
export function yieldPrimarySelection(view: EditorView, doc: Document = view.contentDOM.ownerDocument): boolean {
if (doc.hasFocus() || doc.activeElement !== view.contentDOM) return false;
const { selection } = view.state;
if (selection.ranges.every((r) => r.empty)) return false;
const dom = doc.getSelection();
// The editor's own range is still in the page: nothing was taken from it.
if (dom && dom.rangeCount > 0 && !dom.isCollapsed && view.contentDOM.contains(dom.anchorNode)) return false;
view.dispatch({
selection: EditorSelection.create(
selection.ranges.map((r) => EditorSelection.cursor(r.head, r.assoc)),
selection.mainIndex,
),
});
return true;
}
/** Installs [`yieldPrimarySelection`] on Linux, ahead of CodeMirror's own `selectionchange` handling. */
export const primarySelectionYield = ViewPlugin.fromClass(
class {
private readonly doc: Document | null;
private readonly onSelectionChange = () => void yieldPrimarySelection(this.view);
constructor(private readonly view: EditorView) {
this.doc = usePlatform.getState().os === "linux" ? view.contentDOM.ownerDocument : null;
// Capture runs before CodeMirror's listener on the same document.
this.doc?.addEventListener("selectionchange", this.onSelectionChange, true);
}
destroy() {
this.doc?.removeEventListener("selectionchange", this.onSelectionChange, true);
}
},
);
+2
Переглянути файл
@@ -60,6 +60,7 @@ import { imeSyllableSpaces } from "./imeCommit";
import { stickySelectionField } from "./stickySelection";
import { relativeLineNumbers } from "./relativeLineNumbers";
import { clipboardDocumentPath, clipboardFilesExtension } from "./clipboard";
import { primarySelectionYield } from "./primarySelection";
import type { LanguageInfo } from "../backend";
import { tx } from "@/platform/i18n";
@@ -406,6 +407,7 @@ export function baseExtensions(o: SetupOptions): Extension {
gutterLineDrag,
clipboardDocumentPath.of(o.ctx.path),
clipboardFilesExtension,
primarySelectionYield,
indentOnInput(),
syntaxHighlightField,
syntaxHighlighting(openideaHighlightStyle),