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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: scroll to variable (#1365)
Myles Scolnicka6d3b249df5c
diff --git a/frontend/src/components/dependency-graph/dependency-graph-minimap.tsx b/frontend/src/components/dependency-graph/dependency-graph-minimap.tsxindex b4f54f3542..b5d1ce1381 100644--- a/frontend/src/components/dependency-graph/dependency-graph-minimap.tsx+++ b/frontend/src/components/dependency-graph/dependency-graph-minimap.tsx@@ -24,7 +24,7 @@ import { Atom } from "jotai";import { NodeData, VerticalElementsBuilder } from "./elements";import useEvent from "react-use-event-hook";-import { scrollToCell } from "../editor/links/cell-link";+import { scrollAndHighlightCell } from "../editor/links/cell-link";import { GraphSelectionPanel } from "./panels";import { useFitToViewOnDimensionChange } from "./utils/useFitToViewOnDimensionChange";@@ -132,7 +132,7 @@ export const DependencyGraphMinimap: React.FC<PropsWithChildren<Props>> = ({setEdges([]);}}onNodeDoubleClick={(_event, node) => {- scrollToCell(node.id as CellId, "focus");+ scrollAndHighlightCell(node.id as CellId, "focus");}}onEdgeClick={(_event, edge) => {setSelectedEdge(edge);diff --git a/frontend/src/components/dependency-graph/dependency-graph-tree.tsx b/frontend/src/components/dependency-graph/dependency-graph-tree.tsxindex ebd6d7585d..9d057e08cd 100644--- a/frontend/src/components/dependency-graph/dependency-graph-tree.tsx+++ b/frontend/src/components/dependency-graph/dependency-graph-tree.tsx@@ -23,7 +23,7 @@ import { NodeData, TreeElementsBuilder } from "./elements";import { layoutElements } from "./utils/layout";import { GraphSelection, GraphSettings, LayoutDirection } from "./types";import useEvent from "react-use-event-hook";-import { scrollToCell } from "../editor/links/cell-link";+import { scrollAndHighlightCell } from "../editor/links/cell-link";import { GraphSelectionPanel } from "./panels";import { useFitToViewOnDimensionChange } from "./utils/useFitToViewOnDimensionChange";@@ -113,7 +113,7 @@ export const DependencyGraphTree: React.FC<PropsWithChildren<Props>> = ({});}}onNodeDoubleClick={(_event, node) => {- scrollToCell(node.id as CellId, "focus");+ scrollAndHighlightCell(node.id as CellId, "focus");}}fitView={true}onNodesChange={onNodesChange}diff --git a/frontend/src/components/dependency-graph/panels.tsx b/frontend/src/components/dependency-graph/panels.tsxindex aad374b69e..61010ed8ea 100644--- a/frontend/src/components/dependency-graph/panels.tsx+++ b/frontend/src/components/dependency-graph/panels.tsx@@ -22,6 +22,9 @@ import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover";import { Checkbox } from "../ui/checkbox";import { Label } from "../ui/label";import { ConnectionCellActionsDropdown } from "../editor/cell/cell-actions";+import { getCellEditorView } from "@/core/cells/cells";+import { CellId } from "@/core/cells/ids";+import { goToDefinition } from "@/core/codemirror/find-replace/search-highlight";interface Props {view: GraphLayoutView;@@ -113,6 +116,14 @@ export const GraphSelectionPanel: React.FC<{return null;}+ // Highlight the variable in the cell editor+ const highlightInCell = (cellId: CellId, variableName: string) => {+ const editorView = getCellEditorView(cellId);+ if (editorView) {+ goToDefinition(editorView, variableName);+ }+ };+const renderSelection = () => {if (selection.type === "node") {const variablesUsed = Object.values(variables).filter((variable) =>@@ -122,7 +133,10 @@ export const GraphSelectionPanel: React.FC<{variable.declaredBy.includes(selection.id),);- const renderVariables = (variables: Variable[]) => (+ const renderVariables = (+ variables: Variable[],+ direction: "in" | "out",+ ) => (<>{variables.length === 0 && (<div className="text-muted-foreground text-sm text-center">--</div>@@ -141,7 +155,19 @@ export const GraphSelectionPanel: React.FC<{</span></div><div className="truncate col-span-2 gap-1 items-center">- <CellLinkList maxCount={3} cellIds={variable.usedBy} />+ <CellLinkList+ skipScroll={true}+ onClick={() =>+ highlightInCell(+ direction === "in"+ ? variable.declaredBy[0]+ : variable.usedBy[0],+ variable.name,+ )+ }+ maxCount={3}+ cellIds={variable.usedBy}+ /></div></React.Fragment>))}@@ -167,7 +193,7 @@ export const GraphSelectionPanel: React.FC<{<ArrowRightFromLineIcon className="w-4 h-4" />Outputs</span>- {renderVariables(variablesDeclared)}+ {renderVariables(variablesDeclared, "out")}</div><hr className="border-divider my-3" /><div className="flex flex-col gap-2">@@ -175,7 +201,7 @@ export const GraphSelectionPanel: React.FC<{<ArrowRightToLineIcon className="w-4 h-4" />Inputs</span>- {renderVariables(variablesUsed)}+ {renderVariables(variablesUsed, "in")}</div></div></>@@ -202,6 +228,9 @@ export const GraphSelectionPanel: React.FC<{<VariableNamedeclaredBy={variable.declaredBy}name={variable.name}+ onClick={() => {+ highlightInCell(variable.declaredBy[0], variable.name);+ }}/><div className="truncate text-foreground/60 font-mono">{variable.dataType}diff --git a/frontend/src/components/editor/links/cell-link-list.tsx b/frontend/src/components/editor/links/cell-link-list.tsxindex 489b7ab14b..183caefe07 100644--- a/frontend/src/components/editor/links/cell-link-list.tsx+++ b/frontend/src/components/editor/links/cell-link-list.tsx@@ -12,6 +12,7 @@ import { useCellIds } from "@/core/cells/cells";interface Props {maxCount: number;cellIds: CellId[];+ skipScroll?: boolean;onClick?: (cellId: CellId) => void;}@@ -19,6 +20,7 @@ export const CellLinkList: React.FC<Props> = ({maxCount,cellIds,onClick,+ skipScroll,}) => {const cellIndex = useCellIds();const sortedCellIds = [...cellIds].sort((a, b) => {@@ -37,6 +39,7 @@ export const CellLinkList: React.FC<Props> = ({variant="focus"key={cellId}cellId={cellId}+ skipScroll={skipScroll}className="whitespace-nowrap"onClick={onClick ? () => onClick(cellId) : undefined}/>diff --git a/frontend/src/components/editor/links/cell-link.tsx b/frontend/src/components/editor/links/cell-link.tsxindex 14242af64a..8361e6b96d 100644--- a/frontend/src/components/editor/links/cell-link.tsx+++ b/frontend/src/components/editor/links/cell-link.tsx@@ -8,13 +8,15 @@ import { useCellIds, useCellNames } from "@/core/cells/cells";interface Props {cellId: CellId;className?: string;+ shouldScroll?: boolean;+ skipScroll?: boolean;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, onClick } = props;+ const { className, cellId, variant, onClick, skipScroll } = props;const cellName = useCellNames()[cellId] ?? "";const cellIndex = useCellIds().indexOf(cellId);@@ -27,7 +29,7 @@ export const CellLink = (props: Props): JSX.Element => {onClick={(e) => {e.stopPropagation();e.preventDefault();- const succeeded = scrollToCell(cellId, variant);+ const succeeded = scrollAndHighlightCell(cellId, variant, skipScroll);if (succeeded) {onClick?.();}@@ -45,9 +47,10 @@ export const CellLinkError = (return <CellLink {...props} variant={"destructive"} />;};-export function scrollToCell(+export function scrollAndHighlightCell(cellId: CellId,variant?: "destructive" | "focus",+ skipScroll?: boolean,): boolean {const cellHtmlId = HTMLCellId.create(cellId);const cell: HTMLElement | null = document.getElementById(cellHtmlId);@@ -56,7 +59,9 @@ export function scrollToCell(Logger.error(`Cell ${cellHtmlId} not found on page.`);return false;} else {- cell.scrollIntoView({ behavior: "smooth", block: "center" });+ if (!skipScroll) {+ cell.scrollIntoView({ behavior: "smooth", block: "center" });+ }if (variant === "destructive") {cell.classList.add("error-outline");diff --git a/frontend/src/components/variables/common.tsx b/frontend/src/components/variables/common.tsxindex 352186f06b..0dee013458 100644--- a/frontend/src/components/variables/common.tsx+++ b/frontend/src/components/variables/common.tsx@@ -3,19 +3,28 @@ import React from "react";import { toast } from "../ui/use-toast";import { Badge } from "../ui/badge";-interface Props {+interface Props extends React.HTMLAttributes<HTMLDivElement> {name: string;declaredBy: string[];}-export const VariableName: React.FC<Props> = ({ name, declaredBy }) => {+export const VariableName: React.FC<Props> = ({+ name,+ declaredBy,+ onClick,+ ...rest+}) => {return (- <div className="max-w-[130px]">+ <div className="max-w-[130px]" {...rest}><Badgetitle={name}variant={declaredBy.length > 1 ? "destructive" : "outline"}className="rounded-sm text-ellipsis block overflow-hidden max-w-fit cursor-pointer font-medium"- onClick={() => {+ onClick={(evt) => {+ if (onClick) {+ onClick(evt);+ return;+ }navigator.clipboard.writeText(name);toast({ title: "Copied to clipboard" });}}diff --git a/frontend/src/components/variables/variables-table.tsx b/frontend/src/components/variables/variables-table.tsxindex 74d15c6269..a0b695d151 100644--- a/frontend/src/components/variables/variables-table.tsx+++ b/frontend/src/components/variables/variables-table.tsx@@ -137,6 +137,7 @@ const COLUMNS = [<CellLinkvariant="focus"cellId={declaredBy[0]}+ skipScroll={true}onClick={() => highlightInCell(declaredBy[0])}/>) : (@@ -147,6 +148,7 @@ const COLUMNS = [variant="focus"key={cellId}cellId={cellId}+ skipScroll={true}className="whitespace-nowrap text-destructive"onClick={() => highlightInCell(cellId)}/>@@ -164,6 +166,7 @@ const COLUMNS = [<CellLinkListmaxCount={3}cellIds={usedBy}+ skipScroll={true}onClick={highlightInCell}/></div>diff --git a/frontend/src/core/codemirror/find-replace/search-highlight.ts b/frontend/src/core/codemirror/find-replace/search-highlight.tsindex d80a1224f5..8f31fb1bf2 100644--- a/frontend/src/core/codemirror/find-replace/search-highlight.ts+++ b/frontend/src/core/codemirror/find-replace/search-highlight.ts@@ -12,6 +12,7 @@ import { QueryType, asQueryCreator } from "./query";import { store } from "@/core/state/jotai";import { findReplaceAtom } from "./state";import { getAllEditorViews } from "@/core/cells/cells";+import { syntaxTree } from "@codemirror/language";const setSearchQuery = StateEffect.define<SearchQuery>();@@ -148,28 +149,55 @@ export const highlightTheme = EditorView.baseTheme({});/**- * 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) {+export function goToDefinition(+ view: EditorView,+ variableName: string,+): boolean {const state = view.state;- const search = new SearchQuery({- search: variableName,- caseSensitive: true,- regexp: false,- replace: "",- wholeWord: true,+ const tree = syntaxTree(state);++ let found = false;+ let from = 0;++ tree.iterate({+ enter: (node) => {+ if (found) {+ return false;+ } // Stop traversal if found++ // Check if the node is an identifier and matches the variable name+ if (+ node.name === "VariableName" &&+ state.doc.sliceString(node.from, node.to) === variableName+ ) {+ from = node.from;+ found = true;+ return false; // Stop traversal+ }++ // Skip comments and strings+ if (node.name === "Comment" || node.name === "String") {+ return false;+ }+ },});- const query = asQueryCreator(search).create();- const result = query.nextMatch(state, 0, 0);- if (result) {++ if (found) {view.focus();view.dispatch({selection: {- anchor: result.from,- head: result.from,+ anchor: from,+ head: from,},+ // Unfortunately, EditorView.scrollIntoView does+ // not support smooth scrolling.+ effects: EditorView.scrollIntoView(from, {+ y: "center",+ }),});+ return true;}+ return false;}diff --git a/marimo/_smoke_tests/bugs/1362.py b/marimo/_smoke_tests/bugs/1362.pynew file mode 100644index 0000000000..1f997c8938--- /dev/null+++ b/marimo/_smoke_tests/bugs/1362.py@@ -0,0 +1,160 @@+import marimo++__generated_with = "0.5.2"+app = marimo.App()+++def __():+ """+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell++ """+ qwerty = 10+ return qwerty,+++def __(qwerty):+ """+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell+ hello, this is a cell++ """+ a = f"{qwerty} 10"+ return a,+++def __(a):+ a+ return+++if __name__ == "__main__":+ app.run()