Skip to content

Commit

Permalink
Merge branch 'main' into main-overleaf
Browse files Browse the repository at this point in the history
  • Loading branch information
aeaton-overleaf committed Nov 7, 2023
2 parents 6a09ea7 + 8491f3d commit 29f7a87
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
## 6.5.4 (2023-09-20)

### Bug fixes

Fix a bug that caused whole-word search to incorrectly check for word boundaries in some circumstances.

## 6.5.3 (2023-09-14)

### Bug fixes

The `gotoLine` dialog is now populated with the current line number when you open it.

## 6.5.2 (2023-08-26)

### Bug fixes

Don't use the very lowest precedence for match highlighting decorations.

## 6.5.1 (2023-08-04)

### Bug fixes

Make `gotoLine` prefer to scroll the target line to the middle of the view.

Fix an issue in `SearchCursor` where character normalization could produce nonsensical matches.

## 6.5.0 (2023-06-05)

### New features

The new `regexp` option to `search` can be used to control whether queries have the regexp flag on by default.

## 6.4.0 (2023-04-25)

### Bug fixes
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codemirror/search",
"version": "6.4.0",
"version": "6.5.4",
"description": "Search functionality for the CodeMirror code editor",
"scripts": {
"test": "cm-runtests",
Expand Down Expand Up @@ -31,7 +31,7 @@
"crelt": "^1.0.5"
},
"devDependencies": {
"@codemirror/buildhelper": "^0.1.5"
"@codemirror/buildhelper": "^1.0.0"
},
"repository": {
"type": "git",
Expand Down
14 changes: 8 additions & 6 deletions src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ export class SearchCursor implements Iterator<{from: number, to: number}>{
for (let i = 0, pos = start;; i++) {
let code = norm.charCodeAt(i)
let match = this.match(code, pos)
if (match) {
this.value = match
return this
}
if (i == norm.length - 1) break
if (i == norm.length - 1) {
if (match) {
this.value = match
return this
}
break
}
if (pos == start && i < str.length && str.charCodeAt(i) == code) pos++
}
}
Expand Down Expand Up @@ -110,7 +112,7 @@ export class SearchCursor implements Iterator<{from: number, to: number}>{
else
this.matches.push(1, pos)
}
if (match && this.test && !this.test(match.from, match.to, this.buffer, this.bufferPos)) match = null
if (match && this.test && !this.test(match.from, match.to, this.buffer, this.bufferStart)) match = null
return match
}

Expand Down
11 changes: 6 additions & 5 deletions src/goto-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {EditorView, Command, Panel, getPanel, showPanel} from "@codemirror/view"
import elt from "crelt"

function createLineDialog(view: EditorView): Panel {
let input = elt("input", {class: "cm-textfield", name: "line"}) as HTMLInputElement
let line = String(view.state.doc.lineAt(view.state.selection.main.head).number)
let input = elt("input", {class: "cm-textfield", name: "line", value: line}) as HTMLInputElement
let dom = elt("form", {
class: "cm-gotoLine",
onkeydown: (event: KeyboardEvent) => {
Expand Down Expand Up @@ -38,10 +39,10 @@ function createLineDialog(view: EditorView): Panel {
line = line * (sign == "-" ? -1 : 1) + startLine.number
}
let docLine = state.doc.line(Math.max(1, Math.min(state.doc.lines, line)))
let selection = EditorSelection.cursor(docLine.from + Math.max(0, Math.min(col, docLine.length)))
view.dispatch({
effects: dialogEffect.of(false),
selection: EditorSelection.cursor(docLine.from + Math.max(0, Math.min(col, docLine.length))),
scrollIntoView: true
effects: [dialogEffect.of(false), EditorView.scrollIntoView(selection.from, {y: 'center'})],
selection,
})
view.focus()
}
Expand Down Expand Up @@ -75,7 +76,7 @@ export const gotoLine: Command = view => {
view.dispatch({effects})
panel = getPanel(view, createLineDialog)
}
if (panel) panel.dom.querySelector("input")!.focus()
if (panel) panel.dom.querySelector("input")!.select()
return true
}

Expand Down
12 changes: 9 additions & 3 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ interface SearchConfig {
/// Defaults to false.
wholeWord?: boolean

/// Used to turn on regular expression search in the default query.
/// Defaults to false.
regexp?: boolean

/// Can be used to override the way the search panel is implemented.
/// Should create a [Panel](#view.Panel) that contains a form
/// which lets the user:
Expand Down Expand Up @@ -59,6 +63,7 @@ const searchConfigFacet: Facet<SearchConfig, Required<SearchConfig>> = Facet.def
top: false,
caseSensitive: false,
literal: false,
regexp: false,
wholeWord: false,
createPanel: view => new SearchPanel(view),
scrollToMatch: range => EditorView.scrollIntoView(range)
Expand Down Expand Up @@ -307,11 +312,11 @@ class RegExpQuery extends QueryType<RegExpResult> {
}

getReplacement(result: RegExpResult) {
return this.spec.unquote(this.spec.replace.replace(/\$([$&\d+])/g, (m, i) =>
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))
: m)
}

matchAll(state: EditorState, limit: number) {
Expand Down Expand Up @@ -534,6 +539,7 @@ function defaultQuery(state: EditorState, fallback?: SearchQuery) {
search: (fallback?.literal ?? config.literal) ? selText : selText.replace(/\n/g, "\\n"),
caseSensitive: fallback?.caseSensitive ?? config.caseSensitive,
literal: fallback?.literal ?? config.literal,
regexp: fallback?.regexp ?? config.regexp,
wholeWord: fallback?.wholeWord ?? config.wholeWord
})
}
Expand Down Expand Up @@ -792,6 +798,6 @@ const baseTheme = EditorView.baseTheme({

const searchExtensions = [
searchState,
Prec.lowest(searchHighlighter),
Prec.low(searchHighlighter),
baseTheme
]
4 changes: 4 additions & 0 deletions test/test-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ describe("SearchCursor", () => {
while (!cursor.nextOverlapping().done) matches.push([cursor.value.from, cursor.value.to])
ist(JSON.stringify(matches), "[[0,4],[2,6],[4,8]]")
})

it("will not match partial normalized content", () => {
testMatches(new SearchCursor(Text.of(["´"]), " "), [])
})
})

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

0 comments on commit 29f7a87

Please sign in to comment.