forked from thehogfather/brackets-code-folding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prefs.js
69 lines (64 loc) · 2.51 KB
/
Prefs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Wrapper around brackets pref system to ensure preferences are stored in in one single object instead of using multiple keys. This is to make it easy for the user who edits their preferences file to easily manage the potentially numerous lines of preferences generated by the persisting code-folding state.
* @author Patrick Oladimeji
* @date 3/22/14 20:39:53 PM
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets*/
define(function (require, exports, module) {
"use strict";
var PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
_prefs = PreferencesManager.getExtensionPrefs("code-folding"),
stateManager = PreferencesManager.stateManager.getPrefixedSystem("code-folding"),
DefaultSettings = require("DefaultSettings"),
store = {},
settings = {},
folds = "folds";
function simplify(folds) {
if (!folds) { return folds; }
var res = {}, range;
Object.keys(folds).map(function (line) {
range = folds[line];
res[line] = Array.isArray(range) ? range : [[range.from.line, range.from.ch], [range.to.line, range.to.ch]];
});
return res;
}
function inflate(folds) {
if (!folds) { return folds; }
//transform the folds into objects with from and to properties while gracefully upgrading old preferences
var ranges = {}, obj;
Object.keys(folds).forEach(function (line) {
obj = folds[line];
ranges[line] = !Array.isArray(obj) ? obj :
{from: {line: obj[0][0], ch: obj[0][1]}, to: {line: obj[1][0], ch: obj[1][1]}};
});
return ranges;
}
module.exports = {
get: function (id) {
store = (stateManager.get(folds) || {});
return inflate(store[id]);
},
set: function (id, value) {
store[id] = simplify(value);
stateManager.set(folds, store);
stateManager.save();
},
getSetting: function (key) {
settings = (stateManager.get("settings") || DefaultSettings);
return settings[key];
},
setSetting: function (key, value) {
settings[key] = value;
stateManager.set("settings", settings);
stateManager.save();
},
getAllSettings: function () {
var res = {}, self = this;
Object.keys(DefaultSettings).forEach(function (key) {
res[key] = self.getSetting(key);
});
return res;
}
};
});