From 126f1c9d0a47d23c88c282ae7167120abe78ad81 Mon Sep 17 00:00:00 2001 From: rivea0 <58330360+rivea0@users.noreply.github.com> Date: Wed, 2 Jul 2025 18:05:01 +0300 Subject: [PATCH] fix: update highlights when there is a change in editor --- src/main.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 74245b8..6cba1cc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,7 @@ import { EditorView, ViewPlugin, Decoration } from '@codemirror/view'; import { MatchTextModal } from './modal'; import { getMatchRanges } from './utils'; import MatchSyntaxString from './syntaxObj'; -import type { PluginValue, DecorationSet, PluginSpec } from '@codemirror/view'; +import type { PluginValue, DecorationSet, PluginSpec, ViewUpdate } from '@codemirror/view'; import type { MatchSyntaxPluginSettings, IRange } from './types'; const DEFAULT_SETTINGS: MatchSyntaxPluginSettings = { @@ -116,6 +116,17 @@ class HighlighterPlugin implements PluginValue { constructor(view: EditorView) { this.decorations = Decoration.none; } + + update(viewUpdate: ViewUpdate) { + if (viewUpdate.docChanged) { + if (this.decorations !== Decoration.none) { + const docEl = viewUpdate.view.state.doc; + const ranges = getMatchRanges(docEl, SYNTAX.getValue()); + this.makeDeco(ranges); + } + } + } + makeDeco(ranges: IRange[]) { const deco = []; const highlightDeco = Decoration.mark({ @@ -132,6 +143,10 @@ class HighlighterPlugin implements PluginValue { clearDeco() { this.decorations = Decoration.none; } + + destroy() { + this.clearDeco(); + } } const pluginSpec: PluginSpec = {