Skip to content

Commit

Permalink
ref #5 Extracted collection management logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ryu1kn committed Feb 16, 2018
1 parent 4e082c0 commit 9e134e5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/decoration-registry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

const _ = require('lodash');
const OverviewRulerLane = require('vscode').OverviewRulerLane;
const TextDecorationCollection = require('./text-decoration-collection');

const OVERVIEW_RULER_COLOUR = 'violet';

Expand All @@ -11,53 +12,54 @@ class DecorationRegistry {
this._window = params.window;
this._generateUuid = params.generateUuid;

this._textDecorationMap = {};
this._textDecorationMap = new TextDecorationCollection();
}

inquireById(decorationId) {
return this._buildDecorationFromId(decorationId);
}

inquireByPattern(pattern) {
const decorationId = _.findKey(
this._textDecorationMap,
decoration => decoration && decoration.pattern.equalTo(pattern)
);
return decorationId ? this._buildDecorationFromId(decorationId) : null;
const isSamePattern = decoration => decoration.pattern.equalTo(pattern);
const decoration = this._textDecorationMap.find(isSamePattern);
return decoration && this._buildDecorationInfo(decoration);
}

_buildDecorationFromId(decorationId) {
const decoration = Object.assign({id: decorationId}, this._textDecorationMap[decorationId]);
_buildDecorationInfo(decoration) {
return _.pick(decoration, ['id', 'decorationType', 'pattern']);
}

_buildDecorationFromId(decorationId) {
const decoration = this._textDecorationMap.get(decorationId);
return decoration && this._buildDecorationInfo(decoration);
}

issue(pattern) {
const decoration = this.inquireByPattern(pattern);
if (decoration) return null;

const colour = this._colourRegistry.issue();
const id = this._generateUuid();
const decorationType = this._generateDecorationType(colour);
this._textDecorationMap[id] = {pattern, colour, decorationType};
this._textDecorationMap.add({id, pattern, colour, decorationType});
return this._buildDecorationFromId(id);
}

updatePattern(decorationId, newPattern) {
this._textDecorationMap[decorationId].pattern = newPattern;
return this._buildDecorationFromId(decorationId);
const decoration = this._textDecorationMap.get(decorationId);
decoration.pattern = newPattern;
return this._buildDecorationInfo(decoration);
}

revoke(decorationId) {
const decoration = this._textDecorationMap[decorationId];
const decoration = this._textDecorationMap.get(decorationId);
this._colourRegistry.revoke(decoration.colour);
this._textDecorationMap[decorationId] = null;
this._textDecorationMap.remove(decorationId);
}

retrieveAll() {
return _.reduce(this._textDecorationMap,
(result, value, key) => value ? result.concat(this._buildDecorationFromId(key)) : result,
[]
);
return this._textDecorationMap.toList()
.map(this._buildDecorationInfo);
}

_generateDecorationType(colour) {
Expand Down
37 changes: 37 additions & 0 deletions lib/text-decoration-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

const _ = require('lodash');

class TextDecorationCollection {

constructor() {
this._map = Object.create(null);
}

add({id, pattern, colour, decorationType}) {
this._map[id] = {pattern, colour, decorationType};
}

get(id) {
const value = this._map[id];
return value ? Object.assign({id}, value) : null;
}

remove(id) {
this._map[id] = null;
}

find(predicate) {
const id = _.findKey(this._map, decoration => decoration && predicate(decoration));
return id ? Object.assign({id}, this._map[id]) : null;
}

toList() {
return _.reduce(this._map,
(result, value, key) => value ? [...result, Object.assign({id: key}, value)] : result,
[]
);
}

}

module.exports = TextDecorationCollection;

0 comments on commit 9e134e5

Please sign in to comment.