diff --git a/src/lib/commands/go-to-highlight.ts b/src/lib/commands/go-to-highlight.ts new file mode 100644 index 0000000..941730b --- /dev/null +++ b/src/lib/commands/go-to-highlight.ts @@ -0,0 +1,40 @@ +import {CommandLike} from '../editor-components/vscode'; +import TextLocationRegistry from '../text-location-registry'; +import TextEditor from '../text-editor'; +import DecorationRegistry from '../decoration-registry'; +import WindowComponent from '../editor-components/window'; +import DecorationOperatorFactory from '../decoration-operator-factory'; +import PatternFactory from '../pattern-factory'; +import MatchingModeRegistry from '../matching-mode-registry'; +import {FlatRange} from '../models/flat-range'; +import {Option} from 'fp-ts/lib/Option'; + +export abstract class GoToHighlightCommand implements CommandLike { + protected readonly textLocationRegistry: TextLocationRegistry; + private readonly decorationOperatorFactory: DecorationOperatorFactory; + private readonly patternFactory: PatternFactory; + + protected abstract findTargetLocation(editor: TextEditor): Option; + + protected constructor(matchingModeRegistry: MatchingModeRegistry, + textLocationRegistry: TextLocationRegistry, + decorationRegistry: DecorationRegistry, + windowComponent: WindowComponent) { + this.decorationOperatorFactory = new DecorationOperatorFactory(decorationRegistry, textLocationRegistry, windowComponent); + this.textLocationRegistry = textLocationRegistry; + this.patternFactory = new PatternFactory(matchingModeRegistry); + } + + execute(editor: TextEditor) { + const decorationId = this.textLocationRegistry.queryDecorationId(editor.id, editor.selection).toUndefined(); + if (!decorationId) this.addDecoration(editor); + this.findTargetLocation(editor).map(range => { editor.selection = range; }); + } + + private addDecoration(textEditor: TextEditor) { + if (!textEditor.selectedText) return; + const pattern = this.patternFactory.create({phrase: textEditor.selectedText}); + const decorationOperator = this.decorationOperatorFactory.createForVisibleEditors(); + decorationOperator.addDecoration(pattern); + } +} diff --git a/src/lib/commands/go-to-next-highlight.ts b/src/lib/commands/go-to-next-highlight.ts index 44ac40e..ae3bf2a 100644 --- a/src/lib/commands/go-to-next-highlight.ts +++ b/src/lib/commands/go-to-next-highlight.ts @@ -1,37 +1,21 @@ -import {CommandLike} from '../editor-components/vscode'; import TextLocationRegistry from '../text-location-registry'; import TextEditor from '../text-editor'; import DecorationRegistry from '../decoration-registry'; import WindowComponent from '../editor-components/window'; -import DecorationOperatorFactory from '../decoration-operator-factory'; -import PatternFactory from '../pattern-factory'; import MatchingModeRegistry from '../matching-mode-registry'; +import {GoToHighlightCommand} from './go-to-highlight'; -export class GoToNextHighlightCommand implements CommandLike { - private readonly textLocationRegistry: TextLocationRegistry; - private readonly decorationOperatorFactory: DecorationOperatorFactory; - private readonly patternFactory: PatternFactory; +export class GoToNextHighlightCommand extends GoToHighlightCommand { constructor(matchingModeRegistry: MatchingModeRegistry, textLocationRegistry: TextLocationRegistry, decorationRegistry: DecorationRegistry, windowComponent: WindowComponent) { - this.decorationOperatorFactory = new DecorationOperatorFactory(decorationRegistry, textLocationRegistry, windowComponent); - this.textLocationRegistry = textLocationRegistry; - this.patternFactory = new PatternFactory(matchingModeRegistry); + super(matchingModeRegistry, textLocationRegistry, decorationRegistry, windowComponent); } - execute(editor: TextEditor) { - const decorationId = this.textLocationRegistry.queryDecorationId(editor.id, editor.selection).toUndefined(); - if (!decorationId) this.addDecoration(editor); - const next = this.textLocationRegistry.findNextOccurence(editor.id, editor.selection); - next.map(range => { editor.selection = range; }); + protected findTargetLocation(editor: TextEditor) { + return this.textLocationRegistry.findNextOccurence(editor.id, editor.selection); } - private addDecoration(textEditor: TextEditor) { - if (!textEditor.selectedText) return; - const pattern = this.patternFactory.create({phrase: textEditor.selectedText}); - const decorationOperator = this.decorationOperatorFactory.createForVisibleEditors(); - decorationOperator.addDecoration(pattern); - } } diff --git a/src/lib/commands/go-to-previous-highlight.ts b/src/lib/commands/go-to-previous-highlight.ts index 05ce2b5..bac2729 100644 --- a/src/lib/commands/go-to-previous-highlight.ts +++ b/src/lib/commands/go-to-previous-highlight.ts @@ -1,37 +1,21 @@ -import {CommandLike} from '../editor-components/vscode'; import TextLocationRegistry from '../text-location-registry'; import TextEditor from '../text-editor'; import DecorationRegistry from '../decoration-registry'; import WindowComponent from '../editor-components/window'; -import DecorationOperatorFactory from '../decoration-operator-factory'; -import PatternFactory from '../pattern-factory'; import MatchingModeRegistry from '../matching-mode-registry'; +import {GoToHighlightCommand} from './go-to-highlight'; -export class GoToPreviousHighlightCommand implements CommandLike { - private readonly textLocationRegistry: TextLocationRegistry; - private readonly decorationOperatorFactory: DecorationOperatorFactory; - private readonly patternFactory: PatternFactory; +export class GoToPreviousHighlightCommand extends GoToHighlightCommand { constructor(matchingModeRegistry: MatchingModeRegistry, textLocationRegistry: TextLocationRegistry, decorationRegistry: DecorationRegistry, windowComponent: WindowComponent) { - this.decorationOperatorFactory = new DecorationOperatorFactory(decorationRegistry, textLocationRegistry, windowComponent); - this.textLocationRegistry = textLocationRegistry; - this.patternFactory = new PatternFactory(matchingModeRegistry); + super(matchingModeRegistry, textLocationRegistry, decorationRegistry, windowComponent); } - execute(editor: TextEditor) { - const decorationId = this.textLocationRegistry.queryDecorationId(editor.id, editor.selection).toUndefined(); - if (!decorationId) this.addDecoration(editor); - const next = this.textLocationRegistry.findPreviousOccurence(editor.id, editor.selection); - next.map(range => { editor.selection = range; }); + protected findTargetLocation(editor: TextEditor) { + return this.textLocationRegistry.findPreviousOccurence(editor.id, editor.selection); } - private addDecoration(textEditor: TextEditor) { - if (!textEditor.selectedText) return; - const pattern = this.patternFactory.create({phrase: textEditor.selectedText}); - const decorationOperator = this.decorationOperatorFactory.createForVisibleEditors(); - decorationOperator.addDecoration(pattern); - } }