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.

improvement: move cursor to variable when clicked in variable explorer (#760)

Myles Scolnickb097005c5b23
diff --git a/frontend/src/components/editor/links/cell-link.tsx b/frontend/src/components/editor/links/cell-link.tsx
index 524bb532ef..4f167d190e 100644
--- a/frontend/src/components/editor/links/cell-link.tsx
+++ b/frontend/src/components/editor/links/cell-link.tsx
@@ -8,12 +8,13 @@ import { useCellIds, useCellNames } from "@/core/cells/cells";
interface Props {
cellId: CellId;
className?: string;
+ onClick?: () => void;
variant?: "destructive" | "focus";
}
/* Component that adds a link to a cell, with styling. */
export const CellLink = (props: Props): JSX.Element => {
- const { className, cellId, variant } = props;
+ const { className, cellId, variant, onClick } = props;
const cellName = useCellNames()[cellId] ?? "";
const cellIndex = useCellIds().indexOf(cellId);
const cellHtmlId = HTMLCellId.create(cellId);
@@ -47,6 +48,8 @@ export const CellLink = (props: Props): JSX.Element => {
cell.classList.remove("focus-outline");
}, 1500);
}
+
+ onClick?.();
}
}}
>
diff --git a/frontend/src/components/variables/variables-table.tsx b/frontend/src/components/variables/variables-table.tsx
index 004b80bed2..854b0d64f3 100644
--- a/frontend/src/components/variables/variables-table.tsx
+++ b/frontend/src/components/variables/variables-table.tsx
@@ -26,6 +26,8 @@ import {
} from "@tanstack/react-table";
import { DataTableColumnHeader } from "../data-table/column-header";
import { sortBy } from "lodash-es";
+import { getCellEditorView } from "@/core/cells/cells";
+import { goToDefinition } from "@/core/codemirror/find-replace/search-highlight";
interface Props {
className?: string;
@@ -111,7 +113,7 @@ const COLUMNS = [
}),
columnDefOf({
id: ColumnIds.defs,
- accessorFn: (v) => [v.declaredBy, v.usedBy] as const,
+ accessorFn: (v) => [v.declaredBy, v.usedBy, v.name] as const,
enableSorting: true,
sortingFn: "basic",
header: ({ column }) => (
@@ -126,7 +128,15 @@ const COLUMNS = [
/>
),
cell: ({ getValue }) => {
- const [declaredBy, usedBy] = getValue();
+ const [declaredBy, usedBy, name] = getValue();
+
+ // Highlight the variable in the cell editor
+ const highlightInCell = (cellId: CellId) => {
+ const editorView = getCellEditorView(cellId);
+ if (editorView) {
+ goToDefinition(editorView, name);
+ }
+ };
return (
<div className="flex flex-col gap-1 py-1">
@@ -136,7 +146,11 @@ const COLUMNS = [
</span>
{declaredBy.length === 1 ? (
- <CellLink variant="focus" cellId={declaredBy[0]} />
+ <CellLink
+ variant="focus"
+ cellId={declaredBy[0]}
+ onClick={() => highlightInCell(declaredBy[0])}
+ />
) : (
<div className="text-destructive flex flex-row gap-2">
{declaredBy.slice(0, 3).map((cellId, idx) => (
@@ -146,6 +160,7 @@ const COLUMNS = [
key={cellId}
cellId={cellId}
className="whitespace-nowrap text-destructive"
+ onClick={() => highlightInCell(cellId)}
/>
{idx < declaredBy.length - 1 && ", "}
</span>
@@ -165,6 +180,7 @@ const COLUMNS = [
key={cellId}
cellId={cellId}
className="whitespace-nowrap"
+ onClick={() => highlightInCell(cellId)}
/>
{idx < usedBy.length - 1 && ", "}
</span>
diff --git a/frontend/src/core/codemirror/find-replace/search-highlight.ts b/frontend/src/core/codemirror/find-replace/search-highlight.ts
index 1c23d07a40..d80a1224f5 100644
--- a/frontend/src/core/codemirror/find-replace/search-highlight.ts
+++ b/frontend/src/core/codemirror/find-replace/search-highlight.ts
@@ -146,3 +146,30 @@ export const highlightTheme = EditorView.baseTheme({
backgroundColor: "#6199ff88 !important",
},
});
+
+/**
+ * Poor-man's go to definition.
+ *
+ * This function will select the first occurrence of the given variable name.
+ */
+export function goToDefinition(view: EditorView, variableName: string) {
+ const state = view.state;
+ const search = new SearchQuery({
+ search: variableName,
+ caseSensitive: true,
+ regexp: false,
+ replace: "",
+ wholeWord: true,
+ });
+ const query = asQueryCreator(search).create();
+ const result = query.nextMatch(state, 0, 0);
+ if (result) {
+ view.focus();
+ view.dispatch({
+ selection: {
+ anchor: result.from,
+ head: result.from,
+ },
+ });
+ }
+}
diff --git a/frontend/src/css/Cell.css b/frontend/src/css/Cell.css
index 15131f56a6..01d2f5c814 100644
--- a/frontend/src/css/Cell.css
+++ b/frontend/src/css/Cell.css
@@ -161,10 +161,11 @@
/* Focus state */
&.focus-outline {
- border-color: var(--blue-8);
+ border-color: var(--blue-8) !important;
/* custom shadow until our theme overrides can support colored shadow */
--tw-shadow: 0 4px 0px -1px var(--blue-8), 0 2px 4px -2px var(--blue-8) !important;
+ box-shadow: var(--tw-shadow) !important;
@apply shadow-lg;
}