forked from adobe/brackets
/
Prefs.js
127 lines (113 loc) · 5.12 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* 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"),
strings = brackets.getModule("strings"),
store = {},
foldsKey = "folds";
//default preference values
prefs.definePreference("enabled", "boolean", true,
{name: strings.ENABLE_CODE_FOLDING, description: strings.ENABLE_CODE_FOLDING});
prefs.definePreference("minFoldSize", "number", 2,
{name: strings.MIN_FOLD_SIZE, description: strings.MIN_FOLD_SIZE_HELP});
prefs.definePreference("saveFoldStates", "boolean", true,
{name: strings.SAVE_FOLD_STATES, description: strings.SAVE_FOLD_STATES_HELP});
prefs.definePreference("alwaysUseIndentFold", "boolean", true,
{name: strings.ALWAYS_USE_INDENT_FOLD, description: strings.ALWAYS_USE_INDENT_FOLD_HELP});
prefs.definePreference("enableRegionFolding", "boolean", true,
{name: strings.ENABLE_REGION_FOLDING, description: strings.ENABLE_REGION_FOLDING});
prefs.definePreference("fadeFoldButtons", "boolean", false,
{name: strings.FADE_FOLD_BUTTONS, description: strings.FADE_FOLD_BUTTONS_HELP});
prefs.definePreference("maxFoldLevel", "number", 2,
{name: strings.MAX_FOLD_LEVEL, description: strings.MAX_FOLD_LEVEL_HELP});
prefs.definePreference("folds", "object", {});
/**
Simplifies the fold ranges into an array of pairs of numbers.
@param {!{number: {from: {ch, line}, to: {ch, line}} folds the raw fold ranges indexed by line numbers
@returns {number: Array<Array<number>>} an object whose keys are line numbers and the values are array
of two 2-element arrays. First array contains [from.line, from.ch] and the second contains [to.line, to.ch]
*/
function simplify(folds) {
if (!folds) {
return;
}
var res = {}, range;
Object.keys(folds).forEach(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;
}
/**
Inflates the fold ranges stored as simplified numeric arrays. The inflation converts the data into
objects whose keys are line numbers and whose values are objects in the format {from: {line, ch}, to: {line, ch}}.
@param {number: Array<Array<number>>} folds the simplified fold ranges
@returns {number: {from: {line, ch}, to: {line, ch}}}
*/
function inflate(folds) {
if (!folds) {
return;
}
//transform the folds into objects with from and to properties
var ranges = {}, obj;
Object.keys(folds).forEach(function (line) {
obj = folds[line];
ranges[line] = {from: {line: obj[0][0], ch: obj[0][1]}, to: {line: obj[1][0], ch: obj[1][1]}};
});
return ranges;
}
/**
Gets the line folds saved for the specified path.
@param {string} path the document path
@returns {number: {from: {line, ch}, to: {line, ch}}} the line folds for the document at the specified path
*/
function get(path) {
store = (prefs.get(foldsKey) || {});
return inflate(store[path]);
}
/**
Saves the line folds specified
*/
function set(path, folds) {
store[path] = simplify(folds);
prefs.set(foldsKey, store);
}
/**
Get the code folding setting with the specified key from the store
@param {!string} key The key for the setting to retrieve
@returns {string} the setting with the specified key
*/
function getSetting(key) {
return prefs.get(key, PreferencesManager.CURRENT_PROJECT);
}
/**
Gets all the values for code folding settings.
@returns {object} all settings saved in the preferences.
*/
function getAllSettings() {
var res = {};
prefs.getKeys().forEach(function (key) {
res[key] = getSetting(key);
});
return res;
}
/**
Clears all the saved line folds for all documents.
*/
function clearAllFolds() {
prefs.set(foldsKey, {});
}
module.exports.get = get;
module.exports.set = set;
module.exports.getSetting = getSetting;
module.exports.getAllSettings = getAllSettings;
module.exports.clearAllFolds = clearAllFolds;
});