Skip to content

Commit

Permalink
ref #5 Restore saved highlights on extension start up
Browse files Browse the repository at this point in the history
  • Loading branch information
ryu1kn committed Feb 17, 2018
1 parent c509f1d commit a3f10a4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/app-integrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AppIntegrator {
this._registerCommands(context);
this._registerTextEditorCommands(context);
this._registerEventListeners(context);
this._prepareStatusBarItems();
this._prepareExtensionEventsDrivenItems();
this._broadcastReady();
}

Expand Down Expand Up @@ -56,7 +56,8 @@ class AppIntegrator {
});
}

_prepareStatusBarItems() {
_prepareExtensionEventsDrivenItems() {
this._commandFactory.createSavedHighlightsRestorer();
this._commandFactory.createToggleCaseSensitivityModeButton();
this._commandFactory.createToggleWholeMatchModeButton();
}
Expand Down
10 changes: 10 additions & 0 deletions lib/command-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const PatternVariationReader = require('./pattern-variation-reader');
const RegexReader = require('./regex-reader');
const RemoveAllHighlightsCommand = require('./commands/remove-all-highlights');
const SaveAllHighlightsCommand = require('./commands/save-all-highlights');
const SavedHighlightsRestorer = require('./saved-highlights-restorer');
const TextDecorator = require('./text-decorator');
const TextEditorFactory = require('./text-editor-factory');
const TextLocationRegistry = require('./text-location-registry');
Expand Down Expand Up @@ -165,6 +166,15 @@ class CommandFactory {
});
}

createSavedHighlightsRestorer() {
return new SavedHighlightsRestorer({
configStore: this._getConfigStore(),
decorationOperatorFactory: this._getDecorationOperatorFactory(),
eventBus: this.getEventBus(),
patternFactory: this._getPatternFactory()
});
}

createToggleCaseSensitivityModeButton() {
const alignment = this._vscode.StatusBarAlignment.Right;
const priority = BASE_STATUS_BAR_PRIORITY + 1;
Expand Down
32 changes: 32 additions & 0 deletions lib/saved-highlights-restorer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

const {Event} = require('./const');

class SavedHighlightsRestorer {

constructor(params) {
this._configStore = params.configStore;
this._decorationOperatorFactory = params.decorationOperatorFactory;
this._patternFactory = params.patternFactory;
this._eventBus = params.eventBus;

this._registerListeners();
}

_registerListeners() {
this._eventBus.on(Event.EXTENSION_READY, this._restore.bind(this));
}

_restore() {
const decorationsData = this._configStore.get('savedHighlights');
const decorationOperator = this._decorationOperatorFactory.createForVisibleEditors();
decorationsData.forEach(decorationData => this._addDecoration(decorationData, decorationOperator));
}

_addDecoration(decorationData, decorationOperator) {
const pattern = this._patternFactory.create(decorationData.pattern);
decorationOperator.addDecoration(pattern);
}

}

module.exports = SavedHighlightsRestorer;
3 changes: 2 additions & 1 deletion test/acceptance/lib/fake-vscode-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class FakeVscodeBuilder {
build(params) {
const commands = {};
const textMarkerConfig = {
highlightColors: ['COLOUR_A', 'COLOUR_B']
highlightColors: ['COLOUR_A', 'COLOUR_B'],
savedHighlights: []
};
return {
Range: function (startPos, endPos) {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/saved-highlights-restorer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

const EventEmitter = require('events');
const {Event} = require('../../lib/const');
const SavedHighlightsRestorer = require('../../lib/saved-highlights-restorer');

suite('SavedHighlightsRestorer', () => {

test('it restores saved highlights', done => {
const eventBus = new EventEmitter();
const configStore = {get: stubWithArgs(['savedHighlights'], [{pattern: 'PATTERN_DATA'}])};
const decorationOperator = {addDecoration: sinon.spy()};
const decorationOperatorFactory = {createForVisibleEditors: () => decorationOperator};
const patternFactory = {create: stubWithArgs(['PATTERN_DATA'], 'PATTERN')};
new SavedHighlightsRestorer({eventBus, configStore, decorationOperatorFactory, patternFactory}); // eslint-disable-line no-new

eventBus.on(Event.EXTENSION_READY, () => {
expect(decorationOperator.addDecoration).to.have.been.calledWith('PATTERN');
done();
});

eventBus.emit(Event.EXTENSION_READY);
});

});

0 comments on commit a3f10a4

Please sign in to comment.