Skip to content

Commit

Permalink
Fix selection update after a replace with a variable-length query
Browse files Browse the repository at this point in the history
FIX: Fix a bug that put the selection in the wrong place after running
`replaceNext` with a regexp query that could match strings of different length.

See https://discuss.codemirror.net/t/search-replacenext-does-not-update-selection-correctly-when-regexp-option-is-used/8832
  • Loading branch information
marijnh committed Nov 22, 2024
1 parent 0ca3343 commit ff4dc94
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,9 @@ export const selectSelectionMatches: StateCommand = ({state, dispatch}) => {
export const replaceNext = searchCommand((view, {query}) => {
let {state} = view, {from, to} = state.selection.main
if (state.readOnly) return false
let next = query.nextMatch(state, from, from)
if (!next) return false
let match = query.nextMatch(state, from, from)
if (!match) return false
let next: SearchResult | null = match
let changes = [], selection: EditorSelection | undefined, replacement: Text | undefined
let effects: StateEffect<unknown>[] = []
if (next.from == from && next.to == to) {
Expand All @@ -482,7 +483,7 @@ export const replaceNext = searchCommand((view, {query}) => {
state.phrase("replaced match on line $", state.doc.lineAt(from).number) + "."))
}
if (next) {
let off = changes.length == 0 || changes[0].from >= next.to ? 0 : next.to - next.from - replacement!.length
let off = changes.length == 0 || changes[0].from >= match.to ? 0 : match.to - match.from - replacement!.length
selection = EditorSelection.single(next.from - off, next.to - off)
effects.push(announceMatch(view, next))
effects.push(state.facet(searchConfigFacet).scrollToMatch(selection.main, view))
Expand Down

0 comments on commit ff4dc94

Please sign in to comment.