329 lines
8.8 KiB
TypeScript
329 lines
8.8 KiB
TypeScript
import { Editor, EditorPosition, EditorSelection } from 'obsidian'
|
|
import { selectionToRange } from './Utils'
|
|
|
|
// ============================================================
|
|
// Selection occurrence tracking (ported from obsidian-editor-shortcuts)
|
|
// ============================================================
|
|
|
|
/*
|
|
Properties used to distinguish between selections that are programmatic
|
|
(expanding from a cursor selection) vs. manual (using a mouse / Shift + arrow
|
|
keys). This controls the match behaviour for selectWordOrNextOccurrence.
|
|
*/
|
|
let isManualSelection = true
|
|
let isProgrammaticSelectionChange = false
|
|
|
|
export const setIsManualSelection = (value: boolean) => {
|
|
isManualSelection = value
|
|
}
|
|
|
|
export const setIsProgrammaticSelectionChange = (value: boolean) => {
|
|
isProgrammaticSelectionChange = value
|
|
}
|
|
|
|
export const getIsManualSelection = () => isManualSelection
|
|
export const getIsProgrammaticSelectionChange = () => isProgrammaticSelectionChange
|
|
|
|
/**
|
|
* Check if all selections have the same text content
|
|
*/
|
|
function hasSameSelectionContent(
|
|
editor: Editor,
|
|
selections: EditorSelection[],
|
|
): boolean {
|
|
return (
|
|
new Set(
|
|
selections.map((selection) => {
|
|
const { from, to } = selectionToRange(selection)
|
|
return editor.getRange(from, to)
|
|
}),
|
|
).size === 1
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Get the search text from the current selections.
|
|
* If autoExpand is true and no text is selected, expands to word at cursor.
|
|
*/
|
|
function getSearchText({
|
|
editor,
|
|
allSelections,
|
|
autoExpand,
|
|
}: {
|
|
editor: Editor
|
|
allSelections: EditorSelection[]
|
|
autoExpand: boolean
|
|
}): { searchText: string; singleSearchText: boolean } {
|
|
const singleSearchText = hasSameSelectionContent(editor, allSelections)
|
|
const firstSelection = allSelections[0]
|
|
const { from, to } = selectionToRange(firstSelection)
|
|
let searchText = editor.getRange(from, to)
|
|
if (searchText.length === 0 && autoExpand) {
|
|
const wordRange = editor.wordAt(from)
|
|
if (wordRange) {
|
|
searchText = editor.getRange(wordRange.from, wordRange.to)
|
|
}
|
|
}
|
|
return { searchText, singleSearchText }
|
|
}
|
|
|
|
/**
|
|
* Escapes any special regex characters in the given string.
|
|
*/
|
|
const escapeRegex = (input: string) =>
|
|
input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
|
|
/**
|
|
* Constructs a custom regex query with word boundaries because `\b` in JS
|
|
* doesn't match word boundaries for unicode characters, even with the unicode flag.
|
|
*
|
|
* Adapted from https://shiba1014.medium.com/regex-word-boundaries-with-unicode-207794f6e7ed.
|
|
*/
|
|
const withWordBoundaries = (input: string) =>
|
|
`(?<=\\W|^)${input}(?=\\W|$)`
|
|
|
|
/**
|
|
* Find all match positions of searchText in the document content.
|
|
* When searchWithinWords is false, only whole-word matches are returned.
|
|
*/
|
|
function findAllMatches({
|
|
searchText,
|
|
searchWithinWords,
|
|
documentContent,
|
|
}: {
|
|
searchText: string
|
|
searchWithinWords: boolean
|
|
documentContent: string
|
|
}): RegExpMatchArray[] {
|
|
const escapedSearchText = escapeRegex(searchText)
|
|
const searchExpression = new RegExp(
|
|
searchWithinWords
|
|
? escapedSearchText
|
|
: withWordBoundaries(escapedSearchText),
|
|
'g',
|
|
)
|
|
return Array.from(documentContent.matchAll(searchExpression))
|
|
}
|
|
|
|
/**
|
|
* Find all match positions as editor selections.
|
|
*/
|
|
function findAllMatchPositions({
|
|
editor,
|
|
searchText,
|
|
searchWithinWords,
|
|
documentContent,
|
|
}: {
|
|
editor: Editor
|
|
searchText: string
|
|
searchWithinWords: boolean
|
|
documentContent: string
|
|
}): EditorSelection[] {
|
|
const matches = findAllMatches({
|
|
searchText,
|
|
searchWithinWords,
|
|
documentContent,
|
|
})
|
|
const matchPositions: EditorSelection[] = []
|
|
for (const match of matches) {
|
|
matchPositions.push({
|
|
anchor: editor.offsetToPos(match.index!),
|
|
head: editor.offsetToPos(match.index! + searchText.length),
|
|
})
|
|
}
|
|
return matchPositions
|
|
}
|
|
|
|
/**
|
|
* Find the next match position after a given position.
|
|
* If no match is found after, wraps around to the beginning.
|
|
* Already-selected positions are skipped.
|
|
*/
|
|
function findNextMatchPosition({
|
|
editor,
|
|
latestMatchPos,
|
|
searchText,
|
|
searchWithinWords,
|
|
documentContent,
|
|
}: {
|
|
editor: Editor
|
|
latestMatchPos: EditorPosition
|
|
searchText: string
|
|
searchWithinWords: boolean
|
|
documentContent: string
|
|
}): EditorSelection | null {
|
|
const latestMatchOffset = editor.posToOffset(latestMatchPos)
|
|
const matches = findAllMatches({
|
|
searchText,
|
|
searchWithinWords,
|
|
documentContent,
|
|
})
|
|
|
|
// Find first match after current position
|
|
for (const match of matches) {
|
|
if (match.index! > latestMatchOffset) {
|
|
return {
|
|
anchor: editor.offsetToPos(match.index!),
|
|
head: editor.offsetToPos(match.index! + searchText.length),
|
|
}
|
|
}
|
|
}
|
|
|
|
// Circle back to search from the top, skipping already-selected positions
|
|
const selectionIndexes = editor.listSelections().map((selection) => {
|
|
const { from } = selectionToRange(selection)
|
|
return editor.posToOffset(from)
|
|
})
|
|
|
|
for (const match of matches) {
|
|
if (!selectionIndexes.includes(match.index!)) {
|
|
return {
|
|
anchor: editor.offsetToPos(match.index!),
|
|
head: editor.offsetToPos(match.index! + searchText.length),
|
|
}
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* Find the previous match position before a given position.
|
|
* If no match is found before, wraps around to the end.
|
|
* Already-selected positions are skipped.
|
|
*/
|
|
function findPrevMatchPosition({
|
|
editor,
|
|
latestMatchPos,
|
|
searchText,
|
|
searchWithinWords,
|
|
documentContent,
|
|
}: {
|
|
editor: Editor
|
|
latestMatchPos: EditorPosition
|
|
searchText: string
|
|
searchWithinWords: boolean
|
|
documentContent: string
|
|
}): EditorSelection | null {
|
|
const latestMatchOffset = editor.posToOffset(latestMatchPos)
|
|
const matches = findAllMatches({
|
|
searchText,
|
|
searchWithinWords,
|
|
documentContent,
|
|
})
|
|
|
|
// Find last match before current position
|
|
for (let i = matches.length - 1; i >= 0; i--) {
|
|
if (matches[i].index! < latestMatchOffset) {
|
|
return {
|
|
anchor: editor.offsetToPos(matches[i].index!),
|
|
head: editor.offsetToPos(matches[i].index! + searchText.length),
|
|
}
|
|
}
|
|
}
|
|
|
|
// Circle back from the bottom, skipping already-selected positions
|
|
const selectionIndexes = editor.listSelections().map((selection) => {
|
|
const { from } = selectionToRange(selection)
|
|
return editor.posToOffset(from)
|
|
})
|
|
|
|
for (let i = matches.length - 1; i >= 0; i--) {
|
|
if (!selectionIndexes.includes(matches[i].index!)) {
|
|
return {
|
|
anchor: editor.offsetToPos(matches[i].index!),
|
|
head: editor.offsetToPos(matches[i].index! + searchText.length),
|
|
}
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* Select the next or previous occurrence of the current selection text.
|
|
* When nothing is selected, expands to the word under cursor first.
|
|
*
|
|
* Ported from obsidian-editor-shortcuts `selectWordOrNextOccurrence`.
|
|
*/
|
|
export function selectWordOrNextOccurrence(
|
|
editor: Editor,
|
|
direction: 'next' | 'prev',
|
|
): void {
|
|
setIsProgrammaticSelectionChange(true)
|
|
const allSelections = editor.listSelections()
|
|
const { searchText, singleSearchText } = getSearchText({
|
|
editor,
|
|
allSelections,
|
|
autoExpand: false,
|
|
})
|
|
|
|
if (searchText.length > 0 && singleSearchText) {
|
|
// All selections have the same content — find next/prev occurrence
|
|
const latestSelection =
|
|
direction === 'next'
|
|
? allSelections[allSelections.length - 1]
|
|
: allSelections[0]
|
|
const { from: latestMatchPos } = selectionToRange(latestSelection)
|
|
const findFn =
|
|
direction === 'next' ? findNextMatchPosition : findPrevMatchPosition
|
|
const nextMatch = findFn({
|
|
editor,
|
|
latestMatchPos,
|
|
searchText,
|
|
searchWithinWords: isManualSelection,
|
|
documentContent: editor.getValue(),
|
|
})
|
|
const newSelections = nextMatch
|
|
? allSelections.concat(nextMatch)
|
|
: allSelections
|
|
editor.setSelections(newSelections)
|
|
const lastSelection = newSelections[newSelections.length - 1]
|
|
editor.scrollIntoView(selectionToRange(lastSelection))
|
|
} else {
|
|
// No text selected — expand each cursor to its word
|
|
const newSelections: EditorSelection[] = []
|
|
for (const selection of allSelections) {
|
|
const { from, to } = selectionToRange(selection)
|
|
// Don't modify existing range selections
|
|
if (from.line !== to.line || from.ch !== to.ch) {
|
|
newSelections.push(selection)
|
|
} else {
|
|
const wordAt = editor.wordAt(from)
|
|
if (wordAt) {
|
|
newSelections.push({ anchor: wordAt.from, head: wordAt.to })
|
|
} else {
|
|
newSelections.push(selection)
|
|
}
|
|
setIsManualSelection(false)
|
|
}
|
|
}
|
|
editor.setSelections(newSelections)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Select all occurrences of the current selection text in the document.
|
|
* If nothing is selected, expands to the word under cursor first.
|
|
*
|
|
* Ported from obsidian-editor-shortcuts `selectAllOccurrences`.
|
|
*/
|
|
export function selectAllOccurrences(editor: Editor): void {
|
|
const allSelections = editor.listSelections()
|
|
const { searchText, singleSearchText } = getSearchText({
|
|
editor,
|
|
allSelections,
|
|
autoExpand: true,
|
|
})
|
|
if (!singleSearchText) {
|
|
return
|
|
}
|
|
const matches = findAllMatchPositions({
|
|
editor,
|
|
searchText,
|
|
searchWithinWords: true,
|
|
documentContent: editor.getValue(),
|
|
})
|
|
editor.setSelections(matches)
|
|
}
|