Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main-overleaf
Browse files Browse the repository at this point in the history
  • Loading branch information
aeaton-overleaf committed Sep 25, 2024
2 parents 29f7a87 + d0797ab commit d1d7333
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 6.5.6 (2024-02-07)

### Bug fixes

Make `highlightSelectionMatches` include whitespace in the selection in its matches.

Fix a bug that caused `SearchCursor` to return invalid ranges when matching astral chars that the the normalizer normalized to single-code-unit chars.

## 6.5.5 (2023-11-27)

### Bug fixes

Fix a bug that caused codes like `\n` to be unescaped in strings inserted via replace placeholders like `$&`.

Use the keybinding Mod-Alt-g for `gotoLine` to the search keymap, to make it usable for people whose keyboard layout uses Alt/Option-g to type some character.

## 6.5.4 (2023-09-20)

### 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.4",
"version": "6.5.6",
"description": "Search functionality for the CodeMirror code editor",
"scripts": {
"test": "cm-runtests",
Expand Down
8 changes: 4 additions & 4 deletions src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class SearchCursor implements Iterator<{from: number, to: number}>{
let norm = this.normalize(str)
for (let i = 0, pos = start;; i++) {
let code = norm.charCodeAt(i)
let match = this.match(code, pos)
let match = this.match(code, pos, this.bufferPos + this.bufferStart)
if (i == norm.length - 1) {
if (match) {
this.value = match
Expand All @@ -89,13 +89,13 @@ export class SearchCursor implements Iterator<{from: number, to: number}>{
}
}

private match(code: number, pos: number) {
private match(code: number, pos: number, end: number) {
let match: null | {from: number, to: number} = null
for (let i = 0; i < this.matches.length; i += 2) {
let index = this.matches[i], keep = false
if (this.query.charCodeAt(index) == code) {
if (index == this.query.length - 1) {
match = {from: this.matches[i + 1], to: pos + 1}
match = {from: this.matches[i + 1], to: end}
} else {
this.matches[i]++
keep = true
Expand All @@ -108,7 +108,7 @@ export class SearchCursor implements Iterator<{from: number, to: number}>{
}
if (this.query.charCodeAt(0) == code) {
if (this.query.length == 1)
match = {from: pos, to: pos + 1}
match = {from: pos, to: end}
else
this.matches.push(1, pos)
}
Expand Down
19 changes: 11 additions & 8 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,11 @@ export class StringQuery extends QueryType<SearchResult> {

nextMatch(state: EditorState, curFrom: number, curTo: number) {
let cursor = stringCursor(this.spec, state, curTo, state.doc.length).nextOverlapping()
if (cursor.done) cursor = stringCursor(this.spec, state, 0, curFrom).nextOverlapping()
return cursor.done ? null : cursor.value
if (cursor.done) {
let end = Math.min(state.doc.length, curFrom + this.spec.unquoted.length)
cursor = stringCursor(this.spec, state, 0, end).nextOverlapping()
}
return cursor.done || cursor.value.from == curFrom && cursor.value.to == curTo ? null : cursor.value
}

// Searching in reverse is, rather than implementing an inverted search
Expand All @@ -233,8 +236,10 @@ export class StringQuery extends QueryType<SearchResult> {
}

prevMatch(state: EditorState, curFrom: number, curTo: number) {
return this.prevMatchInRange(state, 0, curFrom) ||
this.prevMatchInRange(state, curTo, state.doc.length)
let found = this.prevMatchInRange(state, 0, curFrom)
if (!found)
found = this.prevMatchInRange(state, Math.max(0, curTo - this.spec.unquoted.length), state.doc.length)
return found && (found.from != curFrom || found.to != curTo) ? found : null
}

getReplacement(_result: SearchResult) { return this.spec.unquote(this.spec.replace) }
Expand Down Expand Up @@ -590,17 +595,15 @@ export const closeSearchPanel: Command = view => {
/// - Mod-f: [`openSearchPanel`](#search.openSearchPanel)
/// - F3, Mod-g: [`findNext`](#search.findNext)
/// - Shift-F3, Shift-Mod-g: [`findPrevious`](#search.findPrevious)
/// - Alt-g: [`gotoLine`](#search.gotoLine)
/// - Mod-Alt-g: [`gotoLine`](#search.gotoLine)
/// - Mod-d: [`selectNextOccurrence`](#search.selectNextOccurrence)
export const searchKeymap: readonly KeyBinding[] = [
{key: "Mod-f", run: openSearchPanel, scope: "editor search-panel"},
{key: "F3", run: findNext, shift: findPrevious, scope: "editor search-panel", preventDefault: true},
{key: "Mod-g", run: findNext, shift: findPrevious, scope: "editor search-panel", preventDefault: true},
{key: "Escape", run: closeSearchPanel, scope: "editor search-panel"},
{key: "Mod-Shift-l", run: selectSelectionMatches},
// This keybinding causes issues with entering @ on certain keyboard layouts
// https://github.com/overleaf/internal/issues/12119
// {key: "Alt-g", run: gotoLine},
{key: "Mod-Alt-g", run: gotoLine},
{key: "Mod-d", run: selectNextOccurrence, preventDefault: true},
]

Expand Down
6 changes: 3 additions & 3 deletions src/selection-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ const matchHighlighter = ViewPlugin.fromClass(class {
if (conf.wholeWords) {
query = state.sliceDoc(range.from, range.to) // TODO: allow and include leading/trailing space?
check = state.charCategorizer(range.head)
if (!(insideWordBoundaries(check, state, range.from, range.to)
&& insideWord(check, state, range.from, range.to))) return Decoration.none
if (!(insideWordBoundaries(check, state, range.from, range.to) &&
insideWord(check, state, range.from, range.to))) return Decoration.none
} else {
query = state.sliceDoc(range.from, range.to).trim()
query = state.sliceDoc(range.from, range.to)
if (!query) return Decoration.none
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/test-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ describe("SearchCursor", () => {
it("will not match partial normalized content", () => {
testMatches(new SearchCursor(Text.of(["´"]), " "), [])
})

it("produces the correct range for astral chars that get normalized to non-astral", () => {
testMatches(new SearchCursor(Text.of(["𝜎"]), "𝜎"), [[0, 2]])
})
})

describe("RegExpCursor", () => {
Expand Down

0 comments on commit d1d7333

Please sign in to comment.