forked from emmetio/brackets-emmet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
288 lines (248 loc) · 6.88 KB
/
editor.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Emmet Editor interface implementation for Brackets.
* Interface is optimized for multiple cursor usage: authors
* should run acttion multiple times and update `_selection.index`
* property on each iteration.
*/
define(function(require, exports, module) {
var emmet = require('emmet/emmet');
var utils = require('emmet/utils/common');
var editorUtils = require('emmet/utils/editor');
var actionUtils = require('emmet/utils/action');
var tabStops = require('emmet/assets/tabStops');
var Editor = brackets.getModule('editor/Editor').Editor;
/**
* Normalizes text before it goes to editor: replaces indentation
* and newlines with ones used in editor
* @param {String} text Text to normalize
* @param {Editor} editor Brackets editor instance
* @return {String}
*/
function normalize(text) {
var indentation = '\t';
if (!Editor.getUseTabChar()) {
indentation = '';
var units = Editor.getSpaceUnits();
while (units--) {
indentation += ' ';
}
}
return editorUtils.normalize(text, {
indentation: indentation,
newline: '\n'
});
}
function visualize(str) {
return str
.replace(/\t/g, '\\t')
.replace(/\n/g, '\\n')
.replace(/\s/g, '\\s');
}
return {
editor: null,
modeMap: {
'text/html': 'html',
'application/xml': 'xml',
'text/xsl': 'xsl',
'text/css': 'css',
'text/x-less': 'less',
'text/x-scss': 'scss',
'text/x-sass': 'sass',
'javascript': 'jsx',
'text/javascript': 'jsx'
},
setup: function(editor, singleSelectionMode) {
this.editor = editor;
var bufRanges = editor.getSelections();
this._selection = {
index: 0,
saved: new Array(bufRanges),
bufferRanges: bufRanges,
indexRanges: bufRanges.map(function(r) {
return {
start: editor.indexFromPos(r.start),
end: editor.indexFromPos(r.end)
};
}),
batch: []
};
if (singleSelectionMode) {
this._selection.index = bufRanges.length - 1;
}
},
/**
* Executes given function for every selection
* @param {Function} fn
*/
exec: function(fn, skipSelSet) {
var sel = this._selection;
var ix = sel.bufferRanges.length - 1;
var success = true;
sel.saved = [];
while (ix >= 0) {
sel.index = ix;
if (fn(ix, sel.indexRanges[ix], sel.bufferRanges[ix]) === false) {
success = false;
break;
}
ix--;
}
if (!skipSelSet && success && sel.saved.length > 1) {
this.editor.setSelections(sel.saved);
}
},
_saveSelection: function(delta) {
var sel = this._selection;
sel.saved[sel.index] = this.editor.getSelection();
if (delta) {
var i = sel.index, r;
while (++i < sel.saved.length) {
r = sel.saved[i];
r.start.line += delta;
r.end.line += delta;
}
}
},
_convertRange: function(sel) {
return {
start: this.editor.indexFromPos(sel.start),
end: this.editor.indexFromPos(sel.end)
};
},
_posFromIndex: function(index) {
// XXX: shouldn’t use private editor._codeMirror here,
// Brackets must provide `posFromIndex()` method alias
return this.editor._codeMirror.posFromIndex(index);
},
/**
* Returns list of selections for current CodeMirror instance.
* @return {Array}
*/
selectionList: function() {
return this._selection.indexRanges;
},
getCaretPos: function() {
return this.getSelectionRange().start;
},
setCaretPos: function(pos) {
this.createSelection(pos);
},
/**
* Returns current selection range (for current selection index)
* @return {Object}
*/
getSelectionRange: function() {
var sel = this._selection;
return sel.indexRanges[sel.index];
},
getSelectionBufferRange: function() {
var sel = this._selection;
return sel.bufferRanges[sel.index];
},
createSelection: function(start, end) {
end = end || start;
start = this._posFromIndex(start);
end = this._posFromIndex(end);
this.editor.setSelection(start, end);
this._selection.batch.unshift({start: start, end: end});
// var sels = this._selection.bufferRanges;
// sels[this._selection.index] = {
// start: this._posFromIndex(start),
// end: this._posFromIndex(end)
// };
// this.editor.setSelections(sels);
},
applyBatchedSelections: function() {
if (this._selection.batch.length) {
this.editor.setSelections(this._selection.batch);
}
},
/**
* Returns current selection
* @return {String}
*/
getSelection: function() {
var sel = this.getSelectionBufferRange();
return this.editor.document.getRange(sel.start, sel.end);
},
_currentLineRange: function() {
var sel = this.getSelectionBufferRange();
return this.editor.convertToLineSelections([sel])[0].selectionForEdit;
},
getCurrentLineRange: function() {
return this._convertRange(this._currentLineRange());
},
getCurrentLine: function() {
var lineRange = this._currentLineRange();
return this.editor.document.getRange(lineRange.start, lineRange.end);
},
getContent: function() {
return this.editor.document.getText();
},
replaceContent: function(value, start, end, noIndent) {
if (typeof end == 'undefined') {
end = (typeof start == 'undefined') ? this.getContent().length : start;
}
if (typeof start == 'undefined') {
start = 0;
}
value = normalize(value);
// indent new value
if (!noIndent) {
var pad = utils.getLinePaddingFromPosition(this.getContent(), start);
value = utils.padString(value, pad);
}
// find new caret position
var tabstopData = tabStops.extract(value, {
escape: function(ch) {
return ch;
}
});
value = tabstopData.text;
var firstTabStop = tabstopData.tabstops[0] || {start: value.length, end: value.length};
firstTabStop.start += start;
firstTabStop.end += start;
var doc = this.editor.document;
start = this._posFromIndex(start);
end = this._posFromIndex(end);
var oldValue = doc.getRange(start, end);
doc.replaceRange(value, start, end);
this.createSelection(firstTabStop.start, firstTabStop.end);
this._saveSelection(utils.splitByLines(value).length - utils.splitByLines(oldValue).length);
return value;
},
getSyntax: function() {
var sel = this.getSelectionBufferRange();
var mode = this.editor.getModeForRange(sel.start, sel.end).name;
if (!mode) {
mode = this.editor.getModeForDocument();
}
return this.modeMap[mode] || mode;
},
/**
* Returns current output profile name (@see emmet#setupProfile)
* @return {String}
*/
getProfileName: function() {
return actionUtils.detectProfile(this);
},
/**
* Ask user to enter something
* @param {String} title Dialog title
* @return {String} Entered data
*/
prompt: function(title) {
return prompt(title);
},
/**
* Returns current editor's file path
* @return {String}
*/
getFilePath: function() {
if (this.editor.document.isUntitled()) {
return null;
}
return this.editor.document.file.fullPath;
}
};
});