Repository navigation
fix: refocus search bar in code editor on Ctrl/Cmd + F - #6980
Conversation
WalkthroughCodeEditor adds a ref to CodeMirrorSearch and changes Cmd/Ctrl‑F handling to show the search bar and call its focus() via the ref. CodeMirrorSearch is converted to forwardRef and exposes a focus() imperative API that focuses/selects its internal input. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CodeEditor as CodeEditor (component)
participant CodeMirrorSearch as CodeMirrorSearch (component)
participant Editor as CodeMirror (editor)
User->>CodeEditor: Press Cmd/Ctrl-F
CodeEditor->>CodeEditor: setState(searchBarVisible = true)
CodeEditor->>CodeMirrorSearch: ref.current.focus()
CodeMirrorSearch->>CodeMirrorSearch: focus() -> inputRef.focus()/select()
CodeMirrorSearch->>Editor: (search input updates) perform search / highlight
Editor-->>CodeMirrorSearch: search results / matches
CodeMirrorSearch-->>User: show matches, navigation controls
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/bruno-app/src/components/CodeEditor/index.js`:
- Around line 97-100: Remove the leftover debug log from the 'Cmd-F' key
handler: in the object mapping for keyboard shortcuts (the 'Cmd-F' entry) delete
the console.log('Searching...') call and leave the setState call that toggles
searchBarVisible and sets searchFocusTrigger so behavior matches the 'Ctrl-F'
handler.
🧹 Nitpick comments (2)
packages/bruno-app/src/components/CodeMirrorSearch/index.js (2)
117-119: Consider removingvisibleandfocusTriggerfrom doSearch dependencies.These dependencies were added but
doSearchdoesn't use them directly. The focus concern is already handled by the separate effect above. Including them here may cause unnecessary search re-executions when the search bar opens or when the hotkey is pressed repeatedly.♻️ Proposed fix
useEffect(() => { doSearch(0, debouncedSearchText); - }, [debouncedSearchText, doSearch, visible, focusTrigger]); + }, [debouncedSearchText, doSearch]);
178-180:autoFocusis redundant with the new programmatic focus effect.Since lines 110-115 now handle focusing the input programmatically when
visiblechanges, theautoFocusattribute is no longer necessary and can be removed for clarity.♻️ Proposed fix
<input ref={inputRef} - autoFocus type="text"
|
The approach depends on |
| this.setState({ searchBarVisible: true }); | ||
| } | ||
| this.setState({ searchBarVisible: true }, () => { | ||
| this.searchBarRef.current?.focus(); |
There was a problem hiding this comment.
this.searchBarRef.current may or may not exist after the state is flushed, the mounting might happen after the callback comes back.
Instead run the focus in the ref callback itself
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/bruno-app/src/components/CodeMirrorSearch/index.js`:
- Around line 118-120: The useEffect is calling doSearch(0, debouncedSearchText)
but doSearch only accepts a single parameter (newIndex); remove the dead second
argument so the effect calls doSearch(0) (or alternatively, if the intention was
to pass the search text, update the doSearch signature where it’s defined).
Update the call in the useEffect that references doSearch and
debouncedSearchText to match the doSearch(newIndex) signature to eliminate the
extra argument.
🧹 Nitpick comments (2)
packages/bruno-app/src/components/CodeMirrorSearch/index.js (2)
110-116: Consider adding a dependency array touseImperativeHandle.Omitting the dependency array causes the imperative handle to be recreated on every render. Since
inputRefis stable andfocuscaptures it by reference at call time, an empty array suffices.♻️ Proposed fix
useImperativeHandle(ref, () => ({ focus: () => { if (inputRef.current) { inputRef.current.focus(); } } - })); + }), []);
11-11: Optional: AdddisplayNamefor easier debugging.When using
forwardRef, setting adisplayNamehelps identify the component in React DevTools and error stack traces.♻️ Proposed addition after line 209
CodeMirrorSearch.displayName = 'CodeMirrorSearch';Also applies to: 209-209
| useEffect(() => { | ||
| doSearch(0, debouncedSearchText); | ||
| }, [debouncedSearchText, doSearch]); |
There was a problem hiding this comment.
Extra argument passed to doSearch.
doSearch accepts only one parameter (newIndex), but it's being called with debouncedSearchText as a second argument. This is dead code and may cause confusion.
🐛 Proposed fix
useEffect(() => {
- doSearch(0, debouncedSearchText);
+ doSearch(0);
}, [debouncedSearchText, doSearch]);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| useEffect(() => { | |
| doSearch(0, debouncedSearchText); | |
| }, [debouncedSearchText, doSearch]); | |
| useEffect(() => { | |
| doSearch(0); | |
| }, [debouncedSearchText, doSearch]); |
🤖 Prompt for AI Agents
In `@packages/bruno-app/src/components/CodeMirrorSearch/index.js` around lines 118
- 120, The useEffect is calling doSearch(0, debouncedSearchText) but doSearch
only accepts a single parameter (newIndex); remove the dead second argument so
the effect calls doSearch(0) (or alternatively, if the intention was to pass the
search text, update the doSearch signature where it’s defined). Update the call
in the useEffect that references doSearch and debouncedSearchText to match the
doSearch(newIndex) signature to eliminate the extra argument.
Description
Fixes : #6930
Contribution Checklist:
Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.
Publishing to New Package Managers
Please see here for more information.
Summary by CodeRabbit
New Features
Bug Fixes