Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: dismiss Jedi signature hint on Escape and closing paren (#10162)

Shahmir Varqha8c60aef46801
diff --git a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
index 5fb8dc6013..10f7b3d92e 100644
--- a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
+++ b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
@@ -12,6 +12,7 @@ import { MockRequestClient } from "@/__mocks__/requests";
import { aiCompletionCellAtom } from "@/core/ai/state";
import type { CellActions } from "@/core/cells/cells";
import { notebookAtom } from "@/core/cells/cells";
+import { signatureHintField } from "@/core/codemirror/completion/signature-hint";
import {
configOverridesAtom,
platformAtom,
@@ -1825,6 +1826,38 @@ describe("useCellEditorNavigationProps", () => {
expect(focusCell).not.toHaveBeenCalled();
});
+ it("should dismiss the signature hint when Escape is pressed without exiting to command mode", () => {
+ mockSimplifySelection.mockReturnValue(false);
+ mockCompletionStatus.mockReturnValue(null);
+
+ const dispatch = vi.fn();
+ const mockEditorView = {
+ current: {
+ state: {
+ selection: { main: { from: 9, to: 9, empty: true } },
+ field: vi.fn((field: unknown) =>
+ field === signatureHintField ? { pos: 9 } : false,
+ ),
+ },
+ dispatch,
+ } as unknown as EditorView,
+ };
+
+ const { result } = renderWithProvider(() =>
+ useCellEditorNavigationProps(mockCellId, mockEditorView),
+ );
+
+ const mockEvent = Mocks.keyboardEvent({ key: "Escape" });
+
+ act(() => {
+ result.current.onKeyDown?.(mockEvent);
+ });
+
+ // The hint is dismissed (dispatch called) but we stay in edit mode.
+ expect(dispatch).toHaveBeenCalled();
+ expect(focusCell).not.toHaveBeenCalled();
+ });
+
it("should continue propagation for other keys", () => {
const mockEditorView = { current: null };
const { result } = renderWithProvider(() =>
diff --git a/frontend/src/components/editor/navigation/navigation.ts b/frontend/src/components/editor/navigation/navigation.ts
index 824f7c9147..cb3a2065a1 100644
--- a/frontend/src/components/editor/navigation/navigation.ts
+++ b/frontend/src/components/editor/navigation/navigation.ts
@@ -22,6 +22,10 @@ import {
} from "@/core/cells/pending-cut-service";
import { usePendingDeleteService } from "@/core/cells/pending-delete-service";
import { scrollCellIntoView } from "@/core/cells/scrollCellIntoView";
+import {
+ closeSignatureHint,
+ signatureHintField,
+} from "@/core/codemirror/completion/signature-hint";
import {
hotkeysAtom,
isAiFeatureEnabled,
@@ -713,7 +717,9 @@ export function useCellEditorNavigationProps(
return;
}
- const hasSignatureHelp = state.field(signatureHelpTooltipField, false);
+ const hasSignatureHelp =
+ Boolean(state.field(signatureHelpTooltipField, false)) ||
+ Boolean(state.field(signatureHintField, false));
const hasAutocompletePopup = completionStatus(state) !== null;
if (hasSignatureHelp) {
closeSignatureHelp(view);
@@ -784,4 +790,5 @@ export function closeSignatureHelp(view: EditorView) {
if (view.state.field(signatureHelpTooltipField, false)) {
view.dispatch({ effects: setSignatureHelpTooltip.of(null) });
}
+ closeSignatureHint(view);
}
diff --git a/frontend/src/core/codemirror/completion/__tests__/signature-hint.test.ts b/frontend/src/core/codemirror/completion/__tests__/signature-hint.test.ts
index e89efaa250..74127b3447 100644
--- a/frontend/src/core/codemirror/completion/__tests__/signature-hint.test.ts
+++ b/frontend/src/core/codemirror/completion/__tests__/signature-hint.test.ts
@@ -5,6 +5,7 @@ import { EditorView, type Tooltip } from "@codemirror/view";
import { describe, expect, it } from "vitest";
import {
asSignatureHint,
+ closeSignatureHint,
setSignatureHintEffect,
signatureHintField,
} from "../signature-hint";
@@ -51,12 +52,79 @@ describe("signatureHintField", () => {
expect(state.field(signatureHintField)).toBeNull();
});
- it("keeps and re-anchors the tooltip across edits", () => {
+ it("keeps and re-anchors the tooltip across edits inside the call", () => {
let state = stateWithHint("plt.plot(", 9);
- // Insert before the tooltip position; it should shift to stay anchored.
- state = state.update({ changes: { from: 0, insert: "xy" } }).state;
+ // Insert before the tooltip position while the cursor stays inside the
+ // call; the anchor should shift but the hint should remain.
+ state = state.update({
+ changes: { from: 0, insert: "xy" },
+ selection: { anchor: 11 },
+ }).state;
expect(state.field(signatureHintField)?.pos).toBe(11);
});
+
+ it("dismisses the tooltip when the closing paren is typed", () => {
+ let state = stateWithHint("plt.plot(", 9);
+ // Type the closing paren; the cursor is now outside the call.
+ state = state.update({
+ changes: { from: 9, insert: ")" },
+ selection: { anchor: 10 },
+ }).state;
+ expect(state.field(signatureHintField)).toBeNull();
+ });
+
+ it("dismisses the tooltip when the anchored call closes inside grouping parens", () => {
+ // Regression for the `(plt.plot())` case: the outer grouping paren must not
+ // keep the (now-closed) plt.plot hint alive.
+ let state = stateWithHint("(plt.plot(", 10);
+ // Close plt.plot's call; the outer `(` is still open but we've left the
+ // anchored call.
+ state = state.update({
+ changes: { from: 10, insert: ")" },
+ selection: { anchor: 11 },
+ }).state;
+ expect(state.field(signatureHintField)).toBeNull();
+ });
+
+ it("keeps the tooltip while typing a nested call inside the anchored call", () => {
+ // Cursor inside the anchored call of `f(g(<cursor>`; opening/typing a nested
+ // call stays inside the anchored call, so the hint should remain.
+ let state = stateWithHint("f(g(", 4);
+ state = state.update({
+ changes: { from: 4, insert: "x(" },
+ selection: { anchor: 6 },
+ }).state;
+ expect(state.field(signatureHintField)?.pos).toBe(4);
+ });
+
+ it("keeps the tooltip when a nested call closes inside the anchored call", () => {
+ const anchor = "f(".length;
+ let state = stateWithHint("f(g(x", anchor);
+ state = state.update({
+ changes: { from: 5, insert: ")" },
+ selection: { anchor: 6 },
+ }).state;
+ expect(state.field(signatureHintField)?.pos).toBe(anchor);
+ });
+
+ it("dismisses the tooltip when the closing paren is typed in a large multi-line call", () => {
+ const anchor = "f(".length;
+ const prefix = `f(\n${" x,\n".repeat(25)}`;
+ let state = EditorState.create({
+ doc: prefix,
+ selection: { anchor: prefix.length },
+ extensions: [signatureHintField],
+ });
+ state = state.update({
+ effects: setSignatureHintEffect.of(fakeTooltip(anchor)),
+ }).state;
+ const head = prefix.length;
+ state = state.update({
+ changes: { from: head, insert: ")" },
+ selection: { anchor: head + 1 },
+ }).state;
+ expect(state.field(signatureHintField)).toBeNull();
+ });
});
describe("asSignatureHint", () => {
@@ -92,3 +160,29 @@ describe("asSignatureHint", () => {
expect(wrapped.above).toBe(true);
});
});
+
+describe("closeSignatureHint", () => {
+ it("returns false when no hint is showing", () => {
+ const view = new EditorView({
+ state: EditorState.create({ extensions: [signatureHintField] }),
+ });
+ expect(closeSignatureHint(view)).toBe(false);
+ expect(view.state.field(signatureHintField)).toBeNull();
+ view.destroy();
+ });
+
+ it("dismisses the hint and returns true when one is showing", () => {
+ const view = new EditorView({
+ state: EditorState.create({
+ doc: "plt.plot(",
+ extensions: [signatureHintField],
+ }),
+ });
+ view.dispatch({ effects: setSignatureHintEffect.of(fakeTooltip(9)) });
+ expect(view.state.field(signatureHintField)?.pos).toBe(9);
+
+ expect(closeSignatureHint(view)).toBe(true);
+ expect(view.state.field(signatureHintField)).toBeNull();
+ view.destroy();
+ });
+});
diff --git a/frontend/src/core/codemirror/completion/signature-hint.ts b/frontend/src/core/codemirror/completion/signature-hint.ts
index c5413fbcdf..15aebb80eb 100644
--- a/frontend/src/core/codemirror/completion/signature-hint.ts
+++ b/frontend/src/core/codemirror/completion/signature-hint.ts
@@ -1,12 +1,61 @@
/* Copyright 2026 Marimo. All rights reserved. */
+import type { EditorState } from "@codemirror/state";
import { StateEffect, StateField } from "@codemirror/state";
-import { showTooltip, type Tooltip } from "@codemirror/view";
+import { type EditorView, showTooltip, type Tooltip } from "@codemirror/view";
/**
* Effect to set (or clear, with `null`) the floating signature hint.
*/
export const setSignatureHintEffect = StateEffect.define<Tooltip | null>();
+// Bound the scan so large cells stay cheap on every keystroke.
+const MAX_LINES_BACK = 20;
+
+/**
+ * Whether the cursor is still inside the anchored call (just inside its `(`).
+ *
+ * Anchor-relative paren scan, bounded to {@link MAX_LINES_BACK} lines.
+ * Good enough for hint dismissal — not a full parse (ignores strings/comments).
+ */
+function isCursorInsideAnchoredCall(options: {
+ state: EditorState;
+ anchor: number;
+ head: number;
+}): boolean {
+ const { state, anchor, head } = options;
+ if (head < anchor) {
+ return false;
+ }
+
+ const headLine = state.doc.lineAt(head).number;
+ const anchorLine = state.doc.lineAt(anchor).number;
+ const startLine = Math.max(anchorLine, headLine - MAX_LINES_BACK + 1);
+ const from = Math.max(anchor, state.doc.line(startLine).from);
+
+ // If the anchor is outside the bounded window, assume its `(` is still open.
+ const assumedOpen = from > anchor;
+ let balance = assumedOpen ? 1 : 0;
+ const iter = state.doc.iterRange(from, head);
+ for (;;) {
+ const { value, done } = iter.next();
+ if (done) {
+ break;
+ }
+ for (const char of value) {
+ if (char === "(") {
+ balance++;
+ } else if (char === ")") {
+ balance--;
+ const closed = assumedOpen ? balance <= 0 : balance < 0;
+ if (closed) {
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+}
+
/**
* Wrap a tooltip so it renders like the completion popup's info box.
*
@@ -34,13 +83,7 @@ export function asSignatureHint(tooltip: Tooltip): Tooltip {
* Holds the floating "signature hint" shown after typing `(` or `,` inside a
* call on the non-LSP (Jedi) completion path.
*
- * The LSP path has its own signature help; this fills the gap for users
- * without a language server. The completion source (`pythonCompletionSource`)
- * drives it: it dispatches `setSignatureHintEffect` with the tooltip when the
- * backend returns a signature and with `null` otherwise. The hint is also
- * cleared when the cursor moves via a selection-only change (e.g. clicking
- * away or arrowing out of the call), and kept anchored across edits so it
- * doesn't flicker while a fresh result is in flight.
+ * The LSP path has its own signature help; this fills the gap for users without a language server.
*/
export const signatureHintField = StateField.define<Tooltip | null>({
create: () => null,
@@ -57,12 +100,36 @@ export const signatureHintField = StateField.define<Tooltip | null>({
if (tr.selection && !tr.docChanged) {
return null;
}
- // Keep the hint anchored across edits; the completion source refreshes or
- // clears it as new results arrive.
+ // Dismiss once the cursor leaves the anchored call (e.g. the closing paren
+ // is typed). Otherwise keep the hint anchored across edits so it doesn't
+ // flicker while a fresh result is in flight; the completion source refreshes
+ // or clears it as results arrive.
if (tr.docChanged) {
- return { ...tooltip, pos: tr.changes.mapPos(tooltip.pos) };
+ const anchor = tr.changes.mapPos(tooltip.pos);
+ if (
+ !isCursorInsideAnchoredCall({
+ state: tr.state,
+ anchor,
+ head: tr.state.selection.main.head,
+ })
+ ) {
+ return null;
+ }
+ return { ...tooltip, pos: anchor };
}
return tooltip;
},
provide: (field) => showTooltip.from(field),
});
+
+/**
+ * Dismiss the floating signature hint if one is showing.
+ * Returns `true` if a hint was dismissed.
+ */
+export function closeSignatureHint(view: EditorView): boolean {
+ if (view.state.field(signatureHintField, false)) {
+ view.dispatch({ effects: setSignatureHintEffect.of(null) });
+ return true;
+ }
+ return false;
+}