Skip to content
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
26 changes: 4 additions & 22 deletions apps/web/src/components/ui/search-tab/SearchTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { DecorationSet } from '@codemirror/view'
import { StateEffect, StateField } from '@codemirror/state'
import { Decoration, EditorView } from '@codemirror/view'
import { CaseSensitive, ChevronDown, ChevronRight, ChevronUp, Regex, Replace, ReplaceAll, WholeWord, X } from '@lucide/vue'
import { findLiteralMatches } from '@/lib/editor/findLiteralMatches'

const props = defineProps<{
editorView: EditorView
Expand Down Expand Up @@ -149,13 +150,13 @@ function findAllMatches() {
searchTo = selectionRange.value.to
}

const content = props.editorView.state.sliceDoc(searchFrom, searchTo)
const searchTerm = searchWord.value
const _matchPositions: Array<Array<{ line: number, ch: number }>> = []
let _matchPositions: Array<Array<{ line: number, ch: number }>> = []

if (searchTerm) {
if (isRegex.value) {
try {
const content = props.editorView.state.sliceDoc(searchFrom, searchTo)
const flags = `gm${isCaseSensitive.value ? `` : `i`}`
const regex = new RegExp(searchTerm, flags)
let match
Expand Down Expand Up @@ -184,26 +185,7 @@ function findAllMatches() {
}
}
else {
const lines = content.split(`\n`)
const searchTermForCompare = isCaseSensitive.value ? searchTerm : searchTerm.toLowerCase()

lines.forEach((line, lineIndex) => {
const lineForCompare = isCaseSensitive.value ? line : line.toLowerCase()
let startIndex = 0
let index = lineForCompare.indexOf(searchTermForCompare, startIndex)

while (index !== -1) {
const actualLineObj = props.editorView.state.doc.lineAt(searchFrom)
const actualLineNumber = actualLineObj.number - 1 + lineIndex

_matchPositions.push([
{ line: actualLineNumber, ch: index },
{ line: actualLineNumber, ch: index + searchTerm.length },
])
startIndex = index + searchTerm.length
index = lineForCompare.indexOf(searchTermForCompare, startIndex)
}
})
_matchPositions = findLiteralMatches(props.editorView.state, searchTerm, searchFrom, searchTo, isCaseSensitive.value)
}
}

Expand Down
42 changes: 42 additions & 0 deletions apps/web/src/lib/editor/findLiteralMatches.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { EditorState } from '@codemirror/state'
import { describe, expect, it } from 'vitest'
import { findLiteralMatches } from './findLiteralMatches'

describe(`findLiteralMatches`, () => {
it(`keeps matches inside a selection that starts and ends mid-line`, () => {
const text = `intro\ncat outside: CAT cat\ncat | cat outside`
const state = EditorState.create({ doc: text })
const matches = findLiteralMatches(state, `cat`, text.indexOf(`CAT`), text.indexOf(` |`), false)

expect(matches).toEqual([
[{ line: 1, ch: 13 }, { line: 1, ch: 16 }],
[{ line: 1, ch: 17 }, { line: 1, ch: 20 }],
[{ line: 2, ch: 0 }, { line: 2, ch: 3 }],
])
})

it(`keeps whole-document matches case-sensitive and non-overlapping`, () => {
const state = EditorState.create({ doc: `Aa aaa` })
expect(findLiteralMatches(state, `aa`, 0, state.doc.length, true)).toEqual([
[{ line: 0, ch: 3 }, { line: 0, ch: 5 }],
])
})

it(`returns original columns when lowercasing changes the length`, () => {
const state = EditorState.create({ doc: `xİcatZ\nxi\u0307Z` })
expect(findLiteralMatches(state, `cat`, 1, 5, false)).toEqual([
[{ line: 0, ch: 2 }, { line: 0, ch: 5 }],
])
const second = state.doc.line(2)
expect(findLiteralMatches(state, `İ`, second.from + 1, second.to - 1, false)).toEqual([
[{ line: 1, ch: 1 }, { line: 1, ch: 3 }],
])
})

it(`does not match the same original character twice after lowercasing`, () => {
const state = EditorState.create({ doc: `xİİİZ` })
expect(findLiteralMatches(state, `\u0307i`, 1, 4, false)).toEqual([
[{ line: 0, ch: 1 }, { line: 0, ch: 3 }],
])
})
})
43 changes: 43 additions & 0 deletions apps/web/src/lib/editor/findLiteralMatches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { EditorState } from '@codemirror/state'

// Lowercasing can expand a character (İ becomes i + a combining dot).
// Map each resulting UTF-16 unit back to its column in the editor.
function sourceColumns(line: string) {
const columns: number[] = []
for (let column = 0; column < line.length; column++) {
const width = line[column].toLowerCase().length
for (let i = 0; i < width; i++)
columns.push(column)
}
return columns
}

export function findLiteralMatches(state: EditorState, query: string, from: number, to: number, caseSensitive: boolean) {
const matches: { line: number, ch: number }[][] = []
if (!query)
return matches

const firstLine = state.doc.lineAt(from)
const term = caseSensitive ? query : query.toLowerCase()
state.sliceDoc(from, to).split(`\n`).forEach((line, lineIndex) => {
const text = caseSensitive ? line : line.toLowerCase()
const columns = text.length === line.length ? undefined : sourceColumns(line)
const lineNumber = firstLine.number - 1 + lineIndex
const columnOffset = lineIndex === 0 ? from - firstLine.from : 0
let index = text.indexOf(term)
while (index !== -1) {
const last = index + term.length - 1
const lastColumn = columns?.[last] ?? last
matches.push([
{ line: lineNumber, ch: columnOffset + (columns?.[index] ?? index) },
{ line: lineNumber, ch: columnOffset + lastColumn + 1 },
])
let next = last + 1
// The match consumes the whole original character even if only part of it matched.
while (columns?.[next] === lastColumn)
next++
index = text.indexOf(term, next)
}
})
return matches
}