Fix layout-aware find in the source editor (#8088)

* fix(editor): make find shortcut layout-aware

* fix(editor): cover layout-aware find surfaces
This commit is contained in:
Neil
2026-07-10 03:56:55 -07:00
committed by GitHub
parent c5669cceb5
commit 0f0e32952e
7 changed files with 350 additions and 18 deletions
@@ -27,7 +27,7 @@ import { DiffSectionHeader } from './DiffSectionHeader'
import type { DiffSection } from './diff-section-types'
import type { DiffComment } from '../../../../shared/types'
import { isDiffComment } from '@/lib/diff-comment-compat'
import { installEditorSaveShortcut } from './editor-shortcuts'
import { installEditorSaveShortcut, installMonacoEditorFindShortcut } from './editor-shortcuts'
import { DiffSectionBody } from './DiffSectionBody'
import { useDiffSectionLayoutMetrics } from './useDiffSectionLayoutMetrics'
import { disposeUnattachedMonacoModelPaths } from './diff-monaco-model-disposal'
@@ -343,9 +343,12 @@ export function DiffSectionItem({
}
modifiedEditorsRef.current.set(index, modified)
const original = editor.getOriginalEditor()
const cleanupSaveShortcut = installEditorSaveShortcut(modified.getContainerDomNode(), () =>
handleSectionSaveRef.current(index)
)
const cleanupOriginalFindShortcut = installMonacoEditorFindShortcut(original)
const cleanupModifiedFindShortcut = installMonacoEditorFindShortcut(modified)
const modelContentSub = modified.onDidChangeModelContent(() => {
const current = modified.getValue()
setSections((prev) => {
@@ -380,9 +383,11 @@ export function DiffSectionItem({
})
})
modified.onDidDispose(() => {
// Why: editable diff sections own both the save shortcut and model-change
// subscription for this Monaco editor instance.
// Why: editable diff sections own both panes' shortcut bridges and the
// model subscription for the lifetime of this Monaco diff instance.
cleanupSaveShortcut()
cleanupOriginalFindShortcut()
cleanupModifiedFindShortcut()
modelContentSub.dispose()
})
}
@@ -16,7 +16,7 @@ import {
import { applyDiffEditorLineNumberOptions } from './diff-editor-line-number-options'
import type { DiffComment } from '../../../../shared/types'
import { isDiffComment } from '@/lib/diff-comment-compat'
import { installEditorSaveShortcut } from './editor-shortcuts'
import { installEditorSaveShortcut, installMonacoEditorFindShortcut } from './editor-shortcuts'
import { diffEditorScrollbarOptions } from './diff-editor-scrollbar-options'
import { LargeDiffFallback } from './LargeDiffFallback'
import { getLargeDiffRenderLimit } from './large-diff-render-limit'
@@ -336,15 +336,19 @@ export default function DiffViewer({
onSaveRef.current?.(modifiedEditor.getValue())
}
)
const cleanupOriginalFindShortcut = installMonacoEditorFindShortcut(originalEditor)
const cleanupModifiedFindShortcut = installMonacoEditorFindShortcut(modifiedEditor)
// Track changes
const modelContentSub = modifiedEditor.onDidChangeModelContent(() => {
onContentChangeRef.current?.(modifiedEditor.getValue())
})
modifiedEditor.onDidDispose(() => {
// Why: editable diff views own both the save shortcut and
// model-change subscription for this Monaco editor instance.
// Why: editable diff views own both panes' shortcut bridges and the
// model subscription for the lifetime of this Monaco diff instance.
cleanupSaveShortcut()
cleanupOriginalFindShortcut()
cleanupModifiedFindShortcut()
modelContentSub.dispose()
})
@@ -51,7 +51,11 @@ import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip
import { ShortcutKeyCombo } from '@/components/ShortcutKeyCombo'
import { useShortcutKeyDetails, type ShortcutKeyComboDetails } from '@/hooks/useShortcutLabel'
import { registerPendingEditorFlush } from './editor-pending-flush'
import { editorShortcutMatches, installEditorSaveShortcut } from './editor-shortcuts'
import {
editorShortcutMatches,
installEditorSaveShortcut,
installMonacoEditorFindShortcut
} from './editor-shortcuts'
import { getIpynbCodeCellEditorHeight, getIpynbCodeCellPreviewLines } from './ipynb-code-cell-lines'
import MonacoCodeExcerpt from './MonacoCodeExcerpt'
import {
@@ -349,13 +353,15 @@ function CodeCell({
void onSaveRequestRef.current()
}
)
const cleanupFindShortcut = installMonacoEditorFindShortcut(editorInstance)
const blurSub = editorInstance.onDidBlurEditorWidget(() => {
onDeactivateRef.current()
})
editorInstance.onDidDispose(() => {
// Why: the inline source editor owns both the save shortcut and blur
// subscription for this Monaco editor instance.
// Why: the inline source editor owns its shortcut bridges and blur
// subscription for the lifetime of this Monaco editor instance.
cleanupSaveShortcut()
cleanupFindShortcut()
blurSub.dispose()
})
editorInstance.addCommand(monacoInstance.KeyCode.Escape, () => {
@@ -44,7 +44,7 @@ import {
getDiffCommentPopoverTop
} from '../diff-comments/diff-comment-popover-position'
import { isLinuxUserAgent } from '../terminal-pane/pane-helpers'
import { installEditorSaveShortcut } from './editor-shortcuts'
import { installEditorSaveShortcut, installMonacoEditorFindShortcut } from './editor-shortcuts'
import { Plus } from 'lucide-react'
import {
getMonacoMarkdownSelectionAnnotationTarget,
@@ -390,13 +390,12 @@ export default function MonacoEditor({
return model.getValueInRange(selection)
})
const cleanupSaveShortcut = installEditorSaveShortcut(
editorInstance.getContainerDomNode(),
() => {
const value = editorInstance.getValue()
propsRef.current.onSave(value)
}
)
const editorDomNode = editorInstance.getContainerDomNode()
const cleanupSaveShortcut = installEditorSaveShortcut(editorDomNode, () => {
const value = editorInstance.getValue()
propsRef.current.onSave(value)
})
const cleanupFindShortcut = installMonacoEditorFindShortcut(editorInstance)
const searchInFilesAction = editorInstance.addAction({
id: 'orca.searchInFiles',
label: translate('auto.components.editor.MonacoEditor.fd68ae03b3', 'Search in Files'),
@@ -442,7 +441,6 @@ export default function MonacoEditor({
}
})
}
const editorDomNode = editorInstance.getContainerDomNode()
editorDomNode.addEventListener('paste', onLargeTextPaste, { capture: true })
// Track cursor line for "copy path to line" feature
@@ -496,6 +494,7 @@ export default function MonacoEditor({
scrollStateSub.dispose()
gutterMouseDownSub.dispose()
cleanupSaveShortcut()
cleanupFindShortcut()
editorDomNode.removeEventListener('paste', onLargeTextPaste, { capture: true })
searchInFilesAction.dispose()
autoHeightSub?.dispose()
@@ -0,0 +1,198 @@
// @vitest-environment happy-dom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const shortcutState = vi.hoisted(() => ({
keybindings: {} as Record<string, string[]>,
platform: 'darwin' as NodeJS.Platform
}))
vi.mock('@/lib/shortcut-platform', () => ({
getShortcutPlatform: () => shortcutState.platform
}))
vi.mock('@/store', () => ({
useAppStore: {
getState: () => ({ keybindings: shortcutState.keybindings })
}
}))
import { installEditorFindShortcut, installMonacoEditorFindShortcut } from './editor-shortcuts'
type ShortcutFixture = {
container: HTMLDivElement
dispose: () => void
input: HTMLTextAreaElement
onDownstreamKeyDown: ReturnType<typeof vi.fn>
onFind: ReturnType<typeof vi.fn>
}
function createShortcutFixture(): ShortcutFixture {
const container = document.createElement('div')
const input = document.createElement('textarea')
const onDownstreamKeyDown = vi.fn()
const onFind = vi.fn()
container.appendChild(input)
document.body.appendChild(container)
input.addEventListener('keydown', onDownstreamKeyDown)
return {
container,
dispose: installEditorFindShortcut(container, onFind),
input,
onDownstreamKeyDown,
onFind
}
}
function dispatchKeyDown(target: HTMLElement, init: KeyboardEventInit): KeyboardEvent {
const event = new KeyboardEvent('keydown', {
...init,
bubbles: true,
cancelable: true
})
target.dispatchEvent(event)
return event
}
beforeEach(() => {
shortcutState.keybindings = {}
shortcutState.platform = 'darwin'
})
afterEach(() => {
document.body.replaceChildren()
})
describe('installEditorFindShortcut', () => {
it.each([
{ label: 'macOS', platform: 'darwin' as const, modifier: { metaKey: true } },
{ label: 'Linux', platform: 'linux' as const, modifier: { ctrlKey: true } },
{ label: 'Windows', platform: 'win32' as const, modifier: { ctrlKey: true } }
])('matches logical f on physical KeyU for $label', ({ platform, modifier }) => {
shortcutState.platform = platform
const fixture = createShortcutFixture()
const event = dispatchKeyDown(fixture.input, {
key: 'f',
code: 'KeyU',
...modifier
})
expect(event.defaultPrevented).toBe(true)
expect(fixture.onFind).toHaveBeenCalledTimes(1)
expect(fixture.onDownstreamKeyDown).not.toHaveBeenCalled()
fixture.dispose()
})
it('consumes the QWERTY shortcut before Monaco can handle it again', () => {
const fixture = createShortcutFixture()
const event = dispatchKeyDown(fixture.input, {
key: 'f',
code: 'KeyF',
metaKey: true
})
expect(event.defaultPrevented).toBe(true)
expect(fixture.onFind).toHaveBeenCalledTimes(1)
expect(fixture.onDownstreamKeyDown).not.toHaveBeenCalled()
fixture.dispose()
})
it('consumes matched repeats without invoking find again', () => {
const fixture = createShortcutFixture()
const initialEvent = dispatchKeyDown(fixture.input, {
key: 'f',
code: 'KeyF',
metaKey: true
})
const repeatEvent = dispatchKeyDown(fixture.input, {
key: 'f',
code: 'KeyF',
metaKey: true,
repeat: true
})
expect(initialEvent.defaultPrevented).toBe(true)
expect(repeatEvent.defaultPrevented).toBe(true)
expect(fixture.onFind).toHaveBeenCalledTimes(1)
expect(fixture.onDownstreamKeyDown).not.toHaveBeenCalled()
fixture.dispose()
})
it.each([
{ label: 'ordinary f typing', init: { key: 'f', code: 'KeyU' } },
{
label: 'an unrelated shortcut',
init: { key: 'g', code: 'KeyG', metaKey: true }
}
])('leaves $label untouched', ({ init }) => {
const fixture = createShortcutFixture()
const event = dispatchKeyDown(fixture.input, init)
expect(event.defaultPrevented).toBe(false)
expect(fixture.onFind).not.toHaveBeenCalled()
expect(fixture.onDownstreamKeyDown).toHaveBeenCalledTimes(1)
fixture.dispose()
})
it('honors a custom editor.find binding', () => {
shortcutState.keybindings = { 'editor.find': ['Mod+G'] }
const fixture = createShortcutFixture()
const defaultEvent = dispatchKeyDown(fixture.input, {
key: 'f',
code: 'KeyF',
metaKey: true
})
const customEvent = dispatchKeyDown(fixture.input, {
key: 'g',
code: 'KeyU',
metaKey: true
})
expect(defaultEvent.defaultPrevented).toBe(false)
expect(customEvent.defaultPrevented).toBe(true)
expect(fixture.onFind).toHaveBeenCalledTimes(1)
expect(fixture.onDownstreamKeyDown).toHaveBeenCalledTimes(1)
fixture.dispose()
})
it('removes the listener when disposed', () => {
const fixture = createShortcutFixture()
fixture.dispose()
const event = dispatchKeyDown(fixture.input, {
key: 'f',
code: 'KeyU',
metaKey: true
})
expect(event.defaultPrevented).toBe(false)
expect(fixture.onFind).not.toHaveBeenCalled()
expect(fixture.onDownstreamKeyDown).toHaveBeenCalledTimes(1)
})
it('runs Monaco existing find action through the shared bridge', () => {
const container = document.createElement('div')
const input = document.createElement('textarea')
const run = vi.fn()
const getAction = vi.fn((_id: string) => ({ run }))
container.appendChild(input)
document.body.appendChild(container)
const dispose = installMonacoEditorFindShortcut({
getAction,
getContainerDomNode: () => container
})
dispatchKeyDown(input, { key: 'f', code: 'KeyU', metaKey: true })
expect(getAction).toHaveBeenCalledWith('actions.find')
expect(run).toHaveBeenCalledTimes(1)
dispose()
})
})
@@ -28,3 +28,31 @@ export function installEditorSaveShortcut(target: HTMLElement, onSave: () => voi
target.addEventListener('keydown', handleKeyDown, true)
return () => target.removeEventListener('keydown', handleKeyDown, true)
}
export function installEditorFindShortcut(target: HTMLElement, onFind: () => void): () => void {
const handleKeyDown = (event: KeyboardEvent): void => {
if (!editorShortcutMatches('editor.find', event)) {
return
}
event.preventDefault()
event.stopPropagation()
// Why: matched repeats must stay consumed so Monaco cannot reopen or reset find.
if (!event.repeat) {
onFind()
}
}
target.addEventListener('keydown', handleKeyDown, true)
return () => target.removeEventListener('keydown', handleKeyDown, true)
}
type MonacoFindShortcutEditor = {
getAction: (id: string) => { run: () => void | Promise<void> } | null
getContainerDomNode: () => HTMLElement
}
export function installMonacoEditorFindShortcut(editor: MonacoFindShortcutEditor): () => void {
return installEditorFindShortcut(editor.getContainerDomNode(), () => {
void editor.getAction('actions.find')?.run()
})
}