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
imporvement: highlight matches across all cells (instead of 1 cell) (#686)
Myles Scolnicke6b1830eb211
diff --git a/frontend/src/components/find-replace/find-replace.tsx b/frontend/src/components/find-replace/find-replace.tsxindex ca4cfe395a..ebb598d17a 100644--- a/frontend/src/components/find-replace/find-replace.tsx+++ b/frontend/src/components/find-replace/find-replace.tsx@@ -28,6 +28,10 @@ import { useHotkey } from "@/hooks/useHotkey";import { EditorView } from "@codemirror/view";import { FocusScope } from "@react-aria/focus";import { toast } from "../ui/use-toast";+import {+ clearGlobalSearchQuery,+ setGlobalSearchQuery,+} from "@/core/codemirror/find-replace/search-highlight";export const FindReplace: React.FC = () => {const [isFocused, setIsFocused] = useState(false);@@ -48,17 +52,20 @@ export const FindReplace: React.FC = () => {useEffect(() => {if (!state.isOpen) {+ clearGlobalSearchQuery();return;}if (state.findText === "") {setMatches(undefined);+ clearGlobalSearchQuery();return;}const matches = getMatches();// False count means an invalid regexsetMatches(matches === false ? undefined : matches);+ setGlobalSearchQuery();}, [// Re-search when any of these changestate.findText,diff --git a/frontend/src/core/codemirror/find-replace/extension.ts b/frontend/src/core/codemirror/find-replace/extension.tsindex 326b971711..623b648be2 100644--- a/frontend/src/core/codemirror/find-replace/extension.ts+++ b/frontend/src/core/codemirror/find-replace/extension.ts@@ -6,6 +6,11 @@ import {} from "@codemirror/search";import { keymap } from "@codemirror/view";import { closeFindReplacePanel, openFindReplacePanel } from "./state";+import {+ highlightTheme,+ searchHighlighter,+ searchState,+} from "./search-highlight";export function findReplaceBundle() {return [@@ -28,5 +33,8 @@ export function findReplaceBundle() {},]),highlightSelectionMatches(),+ searchHighlighter,+ searchState,+ highlightTheme,];}diff --git a/frontend/src/core/codemirror/find-replace/navigate.ts b/frontend/src/core/codemirror/find-replace/navigate.tsindex 2ba4c84dd7..d1974bf973 100644--- a/frontend/src/core/codemirror/find-replace/navigate.ts+++ b/frontend/src/core/codemirror/find-replace/navigate.ts@@ -1,32 +1,15 @@/* Copyright 2024 Marimo. All rights reserved. */import { store } from "@/core/state/jotai";-import { SearchCursor, SearchQuery } from "@codemirror/search";-import { EditorSelection, EditorState } from "@codemirror/state";+import { SearchQuery } from "@codemirror/search";+import { EditorSelection } from "@codemirror/state";import { EditorView } from "@codemirror/view";import { findReplaceAtom } from "./state";import { getAllEditorViews } from "@/core/cells/cells";+import { QueryType, asQueryCreator } from "./query";-type SearchResult = typeof SearchCursor.prototype.value;--// Copied from https://github.com/codemirror/search/blob/6.5.2/src/search.ts#L164-interface QueryType<Result extends SearchResult = SearchResult> {- nextMatch(state: EditorState, curFrom: number, curTo: number): Result | null;- prevMatch(state: EditorState, curFrom: number, curTo: number): Result | null;- getReplacement(result: Result): string;- matchAll(state: EditorState, limit: number): readonly Result[] | null;- highlight(- state: EditorState,- from: number,- to: number,- add: (from: number, to: number) => void- ): void;-}--interface QueryCreator {- create(): QueryType;-}--function searchCommand<T>(f: (state: { query: QueryType }) => T) {+function searchCommand<T>(+ f: (state: { query: QueryType; search: SearchQuery }) => T+) {return () => {const state = store.get(findReplaceAtom);const search = new SearchQuery({@@ -37,7 +20,7 @@ function searchCommand<T>(f: (state: { query: QueryType }) => T) {wholeWord: state.wholeWord,});return search.valid- ? f({ query: (search as unknown as QueryCreator).create() })+ ? f({ query: asQueryCreator(search).create(), search }): false;};}diff --git a/frontend/src/core/codemirror/find-replace/query.ts b/frontend/src/core/codemirror/find-replace/query.tsnew file mode 100644index 0000000000..2f2800fb07--- /dev/null+++ b/frontend/src/core/codemirror/find-replace/query.ts@@ -0,0 +1,30 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { invariant } from "@/utils/invariant";+import { SearchCursor, SearchQuery } from "@codemirror/search";+import { EditorState } from "@codemirror/state";++type SearchResult = typeof SearchCursor.prototype.value;++// Copied from https://github.com/codemirror/search/blob/6.5.2/src/search.ts#L164+export interface QueryType<Result extends SearchResult = SearchResult> {+ nextMatch(state: EditorState, curFrom: number, curTo: number): Result | null;+ prevMatch(state: EditorState, curFrom: number, curTo: number): Result | null;+ getReplacement(result: Result): string;+ matchAll(state: EditorState, limit: number): readonly Result[] | null;+ highlight(+ state: EditorState,+ from: number,+ to: number,+ add: (from: number, to: number) => void+ ): void;+ spec: SearchQuery;+}++export interface QueryCreator {+ create(): QueryType;+}++export function asQueryCreator(query: SearchQuery): QueryCreator {+ invariant("create" in query, 'Expected query to have a "create" method');+ return query as unknown as QueryCreator;+}diff --git a/frontend/src/core/codemirror/find-replace/search-highlight.ts b/frontend/src/core/codemirror/find-replace/search-highlight.tsnew file mode 100644index 0000000000..9a0f81214f--- /dev/null+++ b/frontend/src/core/codemirror/find-replace/search-highlight.ts@@ -0,0 +1,148 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { SearchQuery } from "@codemirror/search";+import { RangeSetBuilder, StateEffect, StateField } from "@codemirror/state";+import {+ Decoration,+ ViewPlugin,+ DecorationSet,+ EditorView,+ ViewUpdate,+} from "@codemirror/view";+import { QueryType, asQueryCreator } from "./query";+import { store } from "@/core/state/jotai";+import { findReplaceAtom } from "./state";+import { getAllEditorViews } from "@/core/cells/cells";++const setSearchQuery = StateEffect.define<SearchQuery>();++/**+ * Set the global search query to the current find/replace state.+ */+export function setGlobalSearchQuery() {+ const state = store.get(findReplaceAtom);+ const views = getAllEditorViews();+ for (const view of views) {+ if (view.state.readOnly) {+ continue;+ }+ view.dispatch({+ effects: setSearchQuery.of(+ new SearchQuery({+ search: state.findText,+ caseSensitive: state.caseSensitive,+ regexp: state.regexp,+ replace: state.replaceText,+ wholeWord: state.wholeWord,+ })+ ),+ });+ }+}++/**+ * Clear the global search query.+ */+export function clearGlobalSearchQuery() {+ const views = getAllEditorViews();+ for (const view of views) {+ if (view.state.readOnly) {+ continue;+ }+ view.dispatch({+ effects: setSearchQuery.of(new SearchQuery({ search: "" })),+ });+ }+}++export const searchState: StateField<QueryType> = StateField.define<QueryType>({+ create() {+ const state = store.get(findReplaceAtom);+ const search = new SearchQuery({+ search: state.findText,+ caseSensitive: state.caseSensitive,+ regexp: state.regexp,+ replace: state.replaceText,+ wholeWord: state.wholeWord,+ });++ return asQueryCreator(search).create();+ },+ update(value, tr) {+ for (const effect of tr.effects) {+ if (effect.is(setSearchQuery)) {+ value = asQueryCreator(effect.value).create();+ }+ }+ return value;+ },+});++const matchMark = Decoration.mark({+ class: "cm-searchMatch",+});+const selectedMatchMark = Decoration.mark({+ class: "cm-searchMatch cm-searchMatch-selected",+});++const HighlightMargin = 250;++// Adapted from from https://github.com/codemirror/search/blob/e766a897aef7515f7ded46ee60b60b875241239e/src/search.ts++export const searchHighlighter = ViewPlugin.fromClass(+ class {+ decorations: DecorationSet;++ constructor(readonly view: EditorView) {+ this.decorations = this.highlight(view.state.field(searchState));+ }++ update(update: ViewUpdate) {+ const state = update.state.field(searchState);+ if (+ state != update.startState.field(searchState) ||+ update.docChanged ||+ update.selectionSet ||+ update.viewportChanged+ ) {+ this.decorations = this.highlight(state);+ }+ }++ highlight(query: QueryType) {+ if (!query.spec.valid) {+ return Decoration.none;+ }+ const { view } = this;+ const builder = new RangeSetBuilder<Decoration>();+ const ranges = view.visibleRanges;+ const l = ranges.length;+ for (let i = 0; i < l; i++) {+ let { to } = ranges[i];+ const { from } = ranges[i];+ while (i < l - 1 && to > ranges[i + 1].from - 2 * HighlightMargin) {+ to = ranges[++i].to;+ }+ query.highlight(view.state, from, to, (from, to) => {+ const selected = view.state.selection.ranges.some(+ (r) => r.from == from && r.to == to+ );+ builder.add(from, to, selected ? selectedMatchMark : matchMark);+ });+ }+ return builder.finish();+ }+ },+ {+ decorations: (v) => v.decorations,+ }+);++export const highlightTheme = EditorView.baseTheme({+ "&light .cm-searchMatch": { backgroundColor: "#99ff7780" },+ "&dark .cm-searchMatch": { backgroundColor: "#22bb0070" },++ "&light .cm-searchMatch-selected": { backgroundColor: "transparent" },+ "&dark .cm-searchMatch.cm-searchMatch-selected": {+ backgroundColor: "#6199ff88 !important",+ },+});