Skip to content

Commit

Permalink
ref #5 Experimenting save highlights feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ryu1kn committed Dec 4, 2017
1 parent 8dd27c9 commit 4a3ba14
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/app-integrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down
29 changes: 28 additions & 1 deletion lib/command-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions lib/commands/save-all-highlights.js
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions lib/config-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ class ConfigStore {

constructor(params) {
this._workspace = params.workspace;
this._configTargetPicker = params.configTargetPicker;
}

get(configName) {
const extensionConfig = this._workspace.getConfiguration(Const.EXTENSION_ID);
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;
35 changes: 35 additions & 0 deletions lib/config-target-picker.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"null"
],
"default": 300
},
"textmarker.savedHighlights": {
"description": "Highlights that get applied when opening editor",
"type": "object"
}
}
},
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 4a3ba14

Please sign in to comment.