From 348d451723235eb5c5c7925059c0bd6707555122 Mon Sep 17 00:00:00 2001 From: rivea0 <58330360+rivea0@users.noreply.github.com> Date: Thu, 3 Jul 2025 11:30:42 +0300 Subject: [PATCH] feat: add a setting to manually clear the highlights, make automatically clearing highlights the default when there is a change in editor --- README.md | 2 +- src/main.ts | 63 ++++++++++++++++++++----------------------------- src/settings.ts | 50 +++++++++++++++++++++++++++++++++++++++ src/types.ts | 37 +++++++++++++++-------------- 4 files changed, 95 insertions(+), 57 deletions(-) create mode 100644 src/settings.ts diff --git a/README.md b/README.md index c342b83..0df386f 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ You can choose "Enter match syntax" to write your syntax and click "Find matches You can bind hotkeys for the commands by going to **Community plugins**, find MatchSyntax in Installed Plugins, and choosing "Hotkeys" button. -At the moment, highlights will stay until you explicitly clear them. +Highlights are cleared automatically when there is a change in editor. You can override this behavior by setting "Manually clear the highlights" on inside plugin settings. ### TODO and Ideas - Custom tags diff --git a/src/main.ts b/src/main.ts index 6cba1cc..8b9f021 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,20 @@ -import { App, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; +import { Notice, Plugin } from 'obsidian'; import { EditorView, ViewPlugin, Decoration } from '@codemirror/view'; import { MatchTextModal } from './modal'; import { getMatchRanges } from './utils'; import MatchSyntaxString from './syntaxObj'; -import type { PluginValue, DecorationSet, PluginSpec, ViewUpdate } from '@codemirror/view'; +import type { + PluginValue, + DecorationSet, + PluginSpec, + ViewUpdate, +} from '@codemirror/view'; import type { MatchSyntaxPluginSettings, IRange } from './types'; +import { keepHighlights, MatchSyntaxSettingTab } from './settings'; const DEFAULT_SETTINGS: MatchSyntaxPluginSettings = { showNumberOfMatchesNotification: true, + manuallyClearHighlights: false, }; const SYNTAX = new MatchSyntaxString(); @@ -40,8 +47,12 @@ export default class MatchSyntaxPlugin extends Plugin { plugin.makeDeco(ranges); } if (this.settings.showNumberOfMatchesNotification) { - const numberOfMatches = ranges.length - new Notice(`${numberOfMatches} match${numberOfMatches === 1 ? '' : 'es'} found.`); + const numberOfMatches = ranges.length; + new Notice( + `${numberOfMatches} match${ + numberOfMatches === 1 ? '' : 'es' + } found.` + ); } }).open(); }, @@ -56,13 +67,13 @@ export default class MatchSyntaxPlugin extends Plugin { const plugin = cm6Editor.plugin(highlightViewPlugin); if (plugin) { if (plugin.decorations === Decoration.none) { - new Notice('No decorations found!'); + new Notice('No highlights found!'); } else { SYNTAX.clearValue(); plugin.clearDeco(); } } - } + }, }); } @@ -82,35 +93,6 @@ export default class MatchSyntaxPlugin extends Plugin { } } -class MatchSyntaxSettingTab extends PluginSettingTab { - plugin: MatchSyntaxPlugin; - - constructor(app: App, plugin: MatchSyntaxPlugin) { - super(app, plugin); - this.plugin = plugin; - } - - display(): void { - const { containerEl } = this; - - containerEl.empty(); - - new Setting(containerEl) - .setName('Notify the number of matches found') - .setDesc( - 'The number of match results will be shown in a notification when you search for a match syntax' - ) - .addToggle((toggle) => { - toggle - .setValue(this.plugin.settings.showNumberOfMatchesNotification) - .onChange(async (value) => { - this.plugin.settings.showNumberOfMatchesNotification = value; - await this.plugin.saveSettings(); - }); - }); - } -} - class HighlighterPlugin implements PluginValue { decorations: DecorationSet; constructor(view: EditorView) { @@ -120,9 +102,14 @@ class HighlighterPlugin implements PluginValue { 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); + if (!keepHighlights) { + SYNTAX.clearValue(); + this.clearDeco(); + } else { + const docEl = viewUpdate.view.state.doc; + const ranges = getMatchRanges(docEl, SYNTAX.getValue()); + this.makeDeco(ranges); + } } } } diff --git a/src/settings.ts b/src/settings.ts new file mode 100644 index 0000000..a06bb79 --- /dev/null +++ b/src/settings.ts @@ -0,0 +1,50 @@ +import { App, PluginSettingTab, Setting } from 'obsidian'; +import MatchSyntaxPlugin from './main'; + +let keepHighlights = false; + +export class MatchSyntaxSettingTab extends PluginSettingTab { + plugin: MatchSyntaxPlugin; + + constructor(app: App, plugin: MatchSyntaxPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const { containerEl } = this; + + containerEl.empty(); + + new Setting(containerEl) + .setName('Notify the number of matches found') + .setDesc( + 'The number of match results will be shown in a notification when you search for a match syntax' + ) + .addToggle((toggle) => { + toggle + .setValue(this.plugin.settings.showNumberOfMatchesNotification) + .onChange(async (value) => { + this.plugin.settings.showNumberOfMatchesNotification = value; + await this.plugin.saveSettings(); + }); + }); + + new Setting(containerEl) + .setName('Manually clear the highlights') + .setDesc( + 'Do not automatically clear highlights when there is a change in the editor' + ) + .addToggle((toggle) => { + toggle + .setValue(this.plugin.settings.manuallyClearHighlights) + .onChange(async (value) => { + this.plugin.settings.manuallyClearHighlights = value; + await this.plugin.saveSettings(); + keepHighlights = !keepHighlights; + }); + }); + } +} + +export { keepHighlights }; diff --git a/src/types.ts b/src/types.ts index bdddb16..209f81a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,31 +1,32 @@ export interface MatchSyntaxPluginSettings { showNumberOfMatchesNotification: boolean; + manuallyClearHighlights: boolean; } export interface IOffsetOutput { - text: string, + text: string; terms: { - text: string, - pre: string, - post: string, - tags: string[], - normal: string, - index: number[], - id: string, - chunk: string, - dirty: boolean, - offset: IOffset - }[], - offset: IOffset + text: string; + pre: string; + post: string; + tags: string[]; + normal: string; + index: number[]; + id: string; + chunk: string; + dirty: boolean; + offset: IOffset; + }[]; + offset: IOffset; } export interface IRange { - from: number, - to: number + from: number; + to: number; } interface IOffset { - index: number, - start: number, - length: number + index: number; + start: number; + length: number; }