Avoid creating duplicate spell check marks on the edited line (#21331)

GitOrigin-RevId: 12726b47450be23f5a412a4c50cc2420af6f425f
This commit is contained in:
Alf Eaton 2024-10-24 09:39:52 +01:00 committed by Copybot
parent 5ec52e2e5b
commit 4bbde479a5

View file

@ -1,4 +1,4 @@
import { StateField, StateEffect } from '@codemirror/state'
import { StateField, StateEffect, Line } from '@codemirror/state'
import { EditorView, Decoration, DecorationSet } from '@codemirror/view'
import { updateAfterAddingIgnoredWord } from './ignored-words'
import { Word } from './spellchecker'
@ -36,9 +36,27 @@ export const misspelledWordsField = StateField.define<DecorationSet>({
for (const effect of transaction.effects) {
if (effect.is(addMisspelledWords)) {
const { doc } = transaction.state
// collect the lines that contained mispelled words, so existing marks can be removed
const affectedLines = new Map<number, Line>()
for (const word of effect.value) {
if (!affectedLines.has(word.lineNumber)) {
affectedLines.set(word.lineNumber, doc.line(word.lineNumber))
}
}
// Merge the new misspelled words into the existing set of marks
marks = marks.update({
add: effect.value.map(word => createMark(word)), // TODO: make sure these positions are still accurate
filter(from, to) {
for (const line of affectedLines.values()) {
if (to > line.from && from < line.to) {
return false
}
}
return true
},
add: effect.value.map(word => createMark(word)),
sort: true,
})
} else if (effect.is(updateAfterAddingIgnoredWord)) {