Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge latest changes from upstream (v 6.5.8) #1

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 6.5.8 (2024-11-22)

### Bug fixes

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.

## 6.5.7 (2024-11-01)

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codemirror/search",
"version": "6.5.7",
"version": "6.5.8",
"description": "Search functionality for the CodeMirror code editor",
"scripts": {
"test": "cm-runtests",
Expand Down
21 changes: 13 additions & 8 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,15 @@ class RegExpQuery extends QueryType<RegExpResult> {
}

getReplacement(result: RegExpResult) {
return this.spec.unquote(this.spec.replace).replace(/\$([$&\d+])/g, (m, i) =>
i == "$" ? "$"
: i == "&" ? result.match[0]
: i != "0" && +i < result.match.length ? result.match[i]
: m)
return this.spec.unquote(this.spec.replace).replace(/\$([$&]|\d+)/g, (m, i) => {
if (i == "&") return result.match[0]
if (i == "$") return "$"
for (let l = i.length; l > 0; l--) {
let n = +i.slice(0, l)
if (n > 0 && n < result.match.length) return result.match[n] + i.slice(l)
}
return m
})
}

matchAll(state: EditorState, limit: number) {
Expand Down Expand Up @@ -470,8 +474,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 +487,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