From 4bbde479a5e19cbc597dc7a51ab8024ee77855fa Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Thu, 24 Oct 2024 09:39:52 +0100 Subject: [PATCH] Avoid creating duplicate spell check marks on the edited line (#21331) GitOrigin-RevId: 12726b47450be23f5a412a4c50cc2420af6f425f --- .../extensions/spelling/misspelled-words.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/services/web/frontend/js/features/source-editor/extensions/spelling/misspelled-words.ts b/services/web/frontend/js/features/source-editor/extensions/spelling/misspelled-words.ts index 66240c9074..7767402d8c 100644 --- a/services/web/frontend/js/features/source-editor/extensions/spelling/misspelled-words.ts +++ b/services/web/frontend/js/features/source-editor/extensions/spelling/misspelled-words.ts @@ -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({ 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() + 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)) {