Skip to content

Commit

Permalink
Fix cursor result ranges for astal chars that get normalized to regul…
Browse files Browse the repository at this point in the history
…ar ones

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

Closes codemirror/dev#1334
  • Loading branch information
marijnh committed Feb 7, 2024
1 parent e766a89 commit 47eb18e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
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
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 47eb18e

Please sign in to comment.