Skip to content

Commit

Permalink
ref #14 Extracted common logic as GoToHighlight super class
Browse files Browse the repository at this point in the history
  • Loading branch information
ryu1kn committed Aug 5, 2018
1 parent 745a64d commit c4ae914
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
40 changes: 40 additions & 0 deletions src/lib/commands/go-to-highlight.ts
Original file line number Diff line number Diff line change
@@ -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<FlatRange>;

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);
}
}
26 changes: 5 additions & 21 deletions src/lib/commands/go-to-next-highlight.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 5 additions & 21 deletions src/lib/commands/go-to-previous-highlight.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c4ae914

Please sign in to comment.