diff --git a/lib/app-integrator.js b/lib/app-integrator.js index 67f34eb..47277f4 100644 --- a/lib/app-integrator.js +++ b/lib/app-integrator.js @@ -12,7 +12,7 @@ class AppIntegrator { this._registerCommands(context); this._registerTextEditorCommands(context); this._registerEventListeners(context); - this._prepareStatusBarItems(); + this._prepareExtensionEventsDrivenItems(); this._broadcastReady(); } @@ -56,7 +56,8 @@ class AppIntegrator { }); } - _prepareStatusBarItems() { + _prepareExtensionEventsDrivenItems() { + this._commandFactory.createSavedHighlightsRestorer(); this._commandFactory.createToggleCaseSensitivityModeButton(); this._commandFactory.createToggleWholeMatchModeButton(); } diff --git a/lib/command-factory.js b/lib/command-factory.js index 9b01e26..edc0a48 100644 --- a/lib/command-factory.js +++ b/lib/command-factory.js @@ -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'); @@ -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; diff --git a/lib/saved-highlights-restorer.js b/lib/saved-highlights-restorer.js new file mode 100644 index 0000000..8b21629 --- /dev/null +++ b/lib/saved-highlights-restorer.js @@ -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; diff --git a/test/acceptance/lib/fake-vscode-builder.js b/test/acceptance/lib/fake-vscode-builder.js index 1aa5695..4c24f28 100644 --- a/test/acceptance/lib/fake-vscode-builder.js +++ b/test/acceptance/lib/fake-vscode-builder.js @@ -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) { diff --git a/test/unit/saved-highlights-restorer.test.js b/test/unit/saved-highlights-restorer.test.js new file mode 100644 index 0000000..1de9615 --- /dev/null +++ b/test/unit/saved-highlights-restorer.test.js @@ -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); + }); + +});