Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(mcp): report window focus changes immediately (#2431)

l0ng-aiad0b9e478efb
diff --git a/src/features/mcp/index.ts b/src/features/mcp/index.ts
index 0d56f41f4..757e7579a 100644
--- a/src/features/mcp/index.ts
+++ b/src/features/mcp/index.ts
@@ -29,6 +29,7 @@ export function activate(): () => void {
let disposed = false;
let timer: ReturnType<typeof setTimeout> | null = null;
const report = () => {
+ if (timer) clearTimeout(timer);
timer = null;
if (!useProject.getState().project) return;
const paths = openEditorPaths();
@@ -92,8 +93,8 @@ export function activate(): () => void {
schedule();
}
});
- window.addEventListener("focus", schedule);
- window.addEventListener("blur", schedule);
+ window.addEventListener("focus", report);
+ window.addEventListener("blur", report);
return () => {
disposed = true;
unload();
@@ -105,8 +106,8 @@ export function activate(): () => void {
offEvents();
offRemote();
offProject();
- window.removeEventListener("focus", schedule);
- window.removeEventListener("blur", schedule);
+ window.removeEventListener("focus", report);
+ window.removeEventListener("blur", report);
if (timer) clearTimeout(timer);
};
}
diff --git a/src/features/mcp/reporters.test.ts b/src/features/mcp/reporters.test.ts
index 8246df1a3..5f94a1f83 100644
--- a/src/features/mcp/reporters.test.ts
+++ b/src/features/mcp/reporters.test.ts
@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const state = vi.hoisted(() => ({
paths: ["/p/a.txt"],
+ selection: { from: 3, to: 5 },
remote: false,
root: "/p",
registryChanged: () => {},
@@ -17,7 +18,7 @@ vi.mock("@/editor/api", () => ({
ctx: { path: "/p/a.txt" },
view: {
state: {
- selection: { main: { from: 3, to: 5 } },
+ selection: { main: state.selection },
doc: {
lineAt: () => ({ number: 1, from: 0 }),
sliceString: () => "hi",
@@ -74,6 +75,7 @@ describe("MCP editor reports", () => {
vi.useFakeTimers();
vi.clearAllMocks();
state.paths = ["/p/a.txt"];
+ state.selection = { from: 3, to: 5 };
state.remote = false;
state.root = "/p";
vi.spyOn(document, "hasFocus").mockReturnValue(true);
@@ -108,6 +110,33 @@ describe("MCP editor reports", () => {
expect(state.reportOpen).toHaveBeenCalledTimes(1);
expect(state.reportFocused).toHaveBeenCalledTimes(1);
});
+ it.each([true, false])("reports window focus %s immediately during continuous editing", async (focused) => {
+ vi.mocked(document.hasFocus).mockReturnValue(!focused);
+ await vi.advanceTimersByTimeAsync(300);
+ state.reportFocused.mockClear();
+ state.editorChanged();
+ await vi.advanceTimersByTimeAsync(100);
+
+ vi.mocked(document.hasFocus).mockReturnValue(focused);
+ window.dispatchEvent(new Event(focused ? "focus" : "blur"));
+ expect(state.reportFocused).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ path: "/p/a.txt" }), focused);
+
+ for (let from = 4; from < 9; from++) {
+ state.selection = { from, to: from + 2 };
+ state.editorChanged();
+ await vi.advanceTimersByTimeAsync(100);
+ expect(state.reportFocused).toHaveBeenCalledTimes(1);
+ }
+ await vi.advanceTimersByTimeAsync(200);
+ expect(state.reportFocused).toHaveBeenCalledTimes(2);
+ expect(state.reportFocused).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ selection: { start: { line: 0, col: 8 }, end: { line: 0, col: 10 } },
+ }),
+ focused,
+ );
+ expect(state.reportOpen).toHaveBeenCalledTimes(1);
+ });
it("reports again after reopening the same project", async () => {
await vi.advanceTimersByTimeAsync(300);
state.projectChanged();