From 4a3ba14e8ea2ea27031532f9b98e4bd73d874eea Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Mon, 4 Dec 2017 23:47:33 +1100 Subject: [PATCH] ref #5 Experimenting save highlights feature --- lib/app-integrator.js | 1 + lib/command-factory.js | 29 +++++++++++++++++++++++- lib/commands/save-all-highlights.js | 16 +++++++++++++ lib/config-store.js | 7 ++++++ lib/config-target-picker.js | 35 +++++++++++++++++++++++++++++ package.json | 9 ++++++++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 lib/commands/save-all-highlights.js create mode 100644 lib/config-target-picker.js diff --git a/lib/app-integrator.js b/lib/app-integrator.js index 524b54d..67f34eb 100644 --- a/lib/app-integrator.js +++ b/lib/app-integrator.js @@ -29,6 +29,7 @@ class AppIntegrator { const commandMap = new Map([ [`${Const.EXTENSION_ID}.highlightUsingRegex`, factory.createHighlightUsingRegex()], [`${Const.EXTENSION_ID}.clearAllHighlight`, factory.createRemoveAllHighlightsCommand()], + [`${Const.EXTENSION_ID}.saveAllHighlights`, factory.createSaveAllHighlightsCommand()], [`${Const.EXTENSION_ID}.toggleCaseSensitivity`, factory.createToggleCaseSensitivityCommand()], [`${Const.EXTENSION_ID}.toggleModeForCaseSensitivity`, factory.createToggleCaseSensitivityModeCommand()], [`${Const.EXTENSION_ID}.toggleWholeMatch`, factory.createToggleWholeMatchCommand()], diff --git a/lib/command-factory.js b/lib/command-factory.js index de5b152..9b01e26 100644 --- a/lib/command-factory.js +++ b/lib/command-factory.js @@ -2,6 +2,7 @@ const ColourRegistry = require('./colour-registry'); const CommandWrapper = require('./command-wrapper'); const ConfigStore = require('./config-store'); +const ConfigTargetPicker = require('./config-target-picker'); const Debouncer = require('./debouncer'); const DecorationOperatorFactory = require('./decoration-operator-factory'); const DecorationRefresher = require('./decoration-refresher'); @@ -16,6 +17,7 @@ const PatternFactory = require('./pattern-factory'); 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 TextDecorator = require('./text-decorator'); const TextEditorFactory = require('./text-editor-factory'); const TextLocationRegistry = require('./text-location-registry'); @@ -94,6 +96,15 @@ class CommandFactory { return this._wrapCommand(command); } + createSaveAllHighlightsCommand() { + const command = new SaveAllHighlightsCommand({ + configStore: this._getConfigStore(), + configTargetPicker: this._getConfigTargetPicker(), + decorationRegistry: this._getDecorationRegistry() + }); + return this._wrapCommand(command); + } + createToggleCaseSensitivityCommand() { const command = new ToggleCaseSensitivityCommand({ decorationOperatorFactory: this._getDecorationOperatorFactory(), @@ -183,10 +194,26 @@ class CommandFactory { } _getConfigStore() { - this._configStore = this._configStore || new ConfigStore({workspace: this._vscode.workspace}); + this._configStore = this._configStore || this._createConfigStore(); return this._configStore; } + _createConfigStore() { + return new ConfigStore({ + workspace: this._vscode.workspace, + configTargetPicker: this._getConfigTargetPicker() + }); + } + + _getConfigTargetPicker() { + this._configTargetPicker = this._configTargetPicker || this._createConfigTargetPicker(); + return this._configTargetPicker; + } + + _createConfigTargetPicker() { + return new ConfigTargetPicker({windowComponent: this._getWindowComponent()}); + } + _getDecorationRegistry() { this._decorationRegistry = this._decorationRegistry || this._createDecorationRegistry(); return this._decorationRegistry; diff --git a/lib/commands/save-all-highlights.js b/lib/commands/save-all-highlights.js new file mode 100644 index 0000000..0035870 --- /dev/null +++ b/lib/commands/save-all-highlights.js @@ -0,0 +1,16 @@ + +class SaveAllHighlightsCommand { + + constructor(params) { + this._configStore = params.configStore; + this._decorationRegistry = params.decorationRegistry; + } + + execute() { + const decorations = this._decorationRegistry.retrieveAll(); + return this._configStore.set('savedHighlights', decorations); + } + +} + +module.exports = SaveAllHighlightsCommand; diff --git a/lib/config-store.js b/lib/config-store.js index f17105b..c62bb34 100644 --- a/lib/config-store.js +++ b/lib/config-store.js @@ -5,6 +5,7 @@ class ConfigStore { constructor(params) { this._workspace = params.workspace; + this._configTargetPicker = params.configTargetPicker; } get(configName) { @@ -12,6 +13,12 @@ class ConfigStore { return extensionConfig.get(configName); } + async set(configName, configValue) { + const configTarget = await this._configTargetPicker.pick(); + const extensionConfig = this._workspace.getConfiguration(Const.EXTENSION_ID); + return extensionConfig.update(configName, configValue, configTarget); + } + } module.exports = ConfigStore; diff --git a/lib/config-target-picker.js b/lib/config-target-picker.js new file mode 100644 index 0000000..1f42569 --- /dev/null +++ b/lib/config-target-picker.js @@ -0,0 +1,35 @@ + +const ConfigurationTarget = { + Global: 1, + Workspace: 2 +}; + +class ConfigurationTargetPicker { + + constructor(params) { + this._windowComponent = params.windowComponent; + } + + pick() { + const selectItems = this._buildQuickPickItems(); + const options = {placeHolder: 'Select which scope of settings to save highlights to'}; + return this._windowComponent.showQuickPick(selectItems, options) + .then(item => item ? item.value : null); + } + + _buildQuickPickItems() { + return [ + { + label: 'Global', + value: ConfigurationTarget.Global + }, + { + label: 'Workspace', + value: ConfigurationTarget.Workspace + } + ]; + } + +} + +module.exports = ConfigurationTargetPicker; diff --git a/package.json b/package.json index f2736ed..e3ae282 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,10 @@ "null" ], "default": 300 + }, + "textmarker.savedHighlights": { + "description": "Highlights that get applied when opening editor", + "type": "object" } } }, @@ -114,6 +118,11 @@ "title": "Clear All Highlights", "category": "TextMarker" }, + { + "command": "textmarker.saveAllHighlights", + "title": "Save All Highlights", + "category": "TextMarker" + }, { "command": "textmarker.updateHighlight", "title": "Update Highlight",