-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref #14 Extracted common logic as GoToHighlight super class
- Loading branch information
Showing
3 changed files
with
50 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |