-
Notifications
You must be signed in to change notification settings - Fork 768
/
editor.js
403 lines (332 loc) · 9.44 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
var _completions = {};
// some caching data used during action sessions
// make sure to call pyResetCache() before each new function call
var __cache = {};
var editorUtils = emmet.utils.editor;
var actionUtils = emmet.utils.action;
var range = emmet.require('assets/range.js');
var tabStops = emmet.tabStops;
var utils = emmet.utils.common;
var htmlMatcher = emmet.htmlMatcher;
var resources = emmet.resources;
var cssResolver = emmet.require('resolver/css.js');
var abbreviationParser = emmet.require('parser/abbreviation.js');
var expandAbbreviationAction = emmet.require('action/expandAbbreviation.js');
var updateTagAction = emmet.require('action/updateTag.js');
function activeView() {
return sublime.active_window().active_view();
}
var editorProxy = {
getSelectionRange: function() {
var view = activeView();
var sel = view.sel()[0];
return {
start: sel.begin(),
end: sel.end()
};
},
createSelection: function(start, end) {
var view = activeView();
view.sel().clear();
view.sel().add(new sublime.Region(start, end || start));
view.show(view.sel());
},
getCurrentLineRange: function() {
var view = activeView();
var selection = view.sel()[0];
var line = view.line(selection);
return {
start: line.begin(),
end: line.end()
};
},
getCaretPos: function() {
var view = activeView();
var sel = view.sel();
return sel && sel[0] ? sel[0].begin() : 0;
},
setCaretPos: function(pos){
this.createSelection(pos, pos);
},
getCurrentLine: function() {
var view = activeView();
return view.substr(view.line(view.sel()[0]));
},
replaceContent: function(value, start, end, noIndent) {
if (typeof end === 'undefined')
end = typeof start === 'undefined' ? this.getContent().length : start;
if (typeof start === 'undefined') start = 0;
// update tabstops: make sure all caret placeholder are unique
// by default, abbreviation parser generates all unlinked (un-mirrored)
// tabstops as ${0}, so we have upgrade all caret tabstops with unique
// positions but make sure that all other tabstops are not linked accidentally
value = pyPreprocessText(value);
value = editorUtils.normalize(value);
sublimeReplaceSubstring(start, end, value, !!noIndent);
},
getContent: function() {
var view = activeView();
return view.substr(new sublime.Region(0, view.size()));
},
getSyntax: function() {
return pyGetSyntax();
},
getProfileName: function() {
var view = activeView();
var pos = this.getCaretPos();
var m = function(sel) {
return view.match_selector(pos, sel);
}
if (m('text.html') && sublimeGetOption('autodetect_xhtml', false) && actionUtils.isXHTML(this)) {
return 'xhtml';
}
if (m('string.quoted.double.block.python')
|| m('source.coffee string')
|| (m('source.php string') && !sublimeGetOption('php_single_line'))
|| m('string.unquoted.heredoc')) {
// use html's default profile for:
// * Python's multiline block
// * CoffeeScript string
// * PHP heredoc
return pyDetectProfile();
}
if (m('source string')) {
return 'line';
}
return pyDetectProfile();
},
prompt: function(title) {
return pyEditor.prompt();
},
getSelection: function() {
var view = activeView();
return view.sel() ? view.substr(view.sel()[0]) : '';
},
getFilePath: function() {
return activeView().file_name();
}
};
function pyPreprocessText(value) {
var base = 1000;
var zeroBase = 0;
var lastZero = null;
var tabstopOptions = {
tabstop: function(data) {
var group = parseInt(data.group, 10);
var isZero = group === 0;
if (isZero)
group = ++zeroBase;
else
group += base;
var placeholder = data.placeholder;
if (placeholder) {
// recursively update nested tabstops
placeholder = tabStops.processText(placeholder, tabstopOptions);
}
var result = '${' + group + (placeholder ? ':' + placeholder : '') + '}';
if (isZero) {
lastZero = range.create(data.start, result);
}
return result
},
escape: function(ch) {
if (ch == '$') {
return '\\$';
}
if (ch == '\\') {
return '\\\\';
}
return ch;
}
};
value = tabStops.processText(value, tabstopOptions);
if (sublimeGetOption('insert_final_tabstop', false) && !/\$\{0\}$/.test(value)) {
value += '${0}';
} else if (lastZero) {
value = utils.replaceSubstring(value, '${0}', lastZero);
}
return value;
}
function pyExpandAsYouType(abbr, options) {
options = options || {};
var ix = (options.index || 0);
var cacheKey = 'expandParams' + ix;
if (!(cacheKey in __cache)) {
var capturePos = options.selectedRange
? options.selectedRange.begin()
: editorProxy.getCaretPos();
__cache[cacheKey] = {
syntax: editorProxy.getSyntax(),
profile: editorProxy.getProfileName() || null,
counter: ix + 1,
contextNode: actionUtils.captureContext(editorProxy, capturePos)
};
if (options.selectedContent) {
__cache[cacheKey].pastedContent = utils.escapeText(options.selectedContent);
}
}
try {
var result = abbreviationParser.expand(abbr, __cache[cacheKey]);
return pyPreprocessText(result);
} catch(e) {
return '';
}
}
function pyUpdateAsYouType(abbr, options) {
options = options || {};
var ix = (options.index || 0);
var cacheKey = 'updateParams' + ix;
if (!(cacheKey in __cache)) {
var capturePos = options.selectedRange
? options.selectedRange.begin()
: editorProxy.getCaretPos();
__cache[cacheKey] = {
counter: ix + 1,
content: editorProxy.getContent(),
ctx: actionUtils.captureContext(editorProxy, capturePos)
};
}
// try {
var cache = __cache[cacheKey];
if (!cache.ctx) {
return null;
}
var tag = updateTagAction.getUpdatedTag(abbr, cache.ctx, cache.content, {
counter: cache.counter
});
if (!tag) {
return null;
}
var out = [{
start: cache.ctx.match.open.range.start,
end: cache.ctx.match.open.range.end,
content: tag.source
}];
if (tag.name() != cache.ctx.name && cache.ctx.match.close) {
out.unshift({
start: cache.ctx.match.close.range.start,
end: cache.ctx.match.close.range.end,
content: '</' + tag.name() + '>'
});
}
return out;
// } catch(e) {
// console.log(e);
// return null;
// }
}
function pyCaptureWrappingRange() {
var info = editorUtils.outputInfo(editorProxy);
var range = editorProxy.getSelectionRange();
var startOffset = range.start;
var endOffset = range.end;
if (startOffset == endOffset) {
// no selection, find tag pair
var match = htmlMatcher.find(info.content, startOffset);
if (!match) {
// nothing to wrap
return null;
}
var narrowedSel = utils.narrowToNonSpace(info.content, match.range);
startOffset = narrowedSel.start;
endOffset = narrowedSel.end;
}
return [startOffset, endOffset];
}
function pyGetTagNameRanges(pos) {
var ranges = [];
var info = editorUtils.outputInfo(editorProxy);
// search for tag
try {
var tag = htmlMatcher.tag(info.content, pos);
if (tag) {
var open = tag.open.range;
var tagName = /^<([\w\-\:]+)/i.exec(open.substring(info.content))[1];
ranges.push([open.start + 1, open.start + 1 + tagName.length]);
if (tag.close) {
ranges.push([tag.close.range.start + 2, tag.close.range.start + 2 + tagName.length]);
}
}
} catch (e) {}
return ranges;
}
function pyGetTagRanges() {
var ranges = [];
var info = editorUtils.outputInfo(editorProxy);
// search for tag
try {
var tag = htmlMatcher.tag(info.content, editorProxy.getCaretPos());
if (tag) {
ranges.push(tag.open.range.toArray());
if (tag.close) {
ranges.push(tag.close.range.toArray());
}
}
} catch (e) {}
return ranges;
}
function pyExtractAbbreviation() {
return expandAbbreviationAction.findAbbreviation(editorProxy);
}
function pyHasSnippet(name) {
return !!resources.findSnippet(editorProxy.getSyntax(), name);
}
/**
* Get all available CSS completions. This method is optimized for CSS
* only since it should contain snippets only so it's not required
* to do extra parsing
*/
function pyGetCSSCompletions(dialect) {
dialect = dialect || pyGetSyntax();
if (!_completions[dialect]) {
var all = resources.getAllSnippets(dialect);
_completions[dialect] = Object.keys(all).map(function(k) {
var v = all[k];
var snippetValue = typeof v.parsedValue == 'object'
? v.parsedValue.data
: v.value;
var snippet = cssResolver.transformSnippet(snippetValue, false, dialect);
return {
k: v.nk,
label: snippet.replace(/\:\s*\$\{0\}\s*;?$/, ''),
v: cssResolver.expandToSnippet(v.nk, dialect)
};
});
}
return _completions[dialect];
}
/**
* Returns current syntax name
* @return {String}
*/
function pyGetSyntax() {
var view = activeView();
var pt = view.sel()[0].begin();
var scope = 'scope_name' in view ? view.scope_name(pt) : view.syntax_name(pt);
if (~scope.indexOf('xsl')) {
return 'xsl';
}
if (!/\bstring\b/.test(scope) && /\bsource\.jsx?\b/.test(scope)) {
return 'jsx';
}
var syntax = 'html';
if (!/\bstring\b/.test(scope) && /\bsource\.([\w\-]+)/.test(scope) && resources.hasSyntax(RegExp.$1)) {
syntax = RegExp.$1;
} else if (/\b(less|scss|sass|css|stylus|postcss)\b/.test(scope)) {
// detect CSS-like syntaxes independently,
// since it may cause collisions with some highlighters
syntax = RegExp.$1;
if (syntax === 'postcss') {
syntax = 'css';
}
} else if (/\b(html|xml|haml|slim|jade|pug)\b/.test(scope)) {
syntax = RegExp.$1;
}
return actionUtils.detectSyntax(editorProxy, syntax);
}
function pyDetectProfile(syntax) {
return actionUtils.detectProfile(editorProxy, syntax);
}
function pyResetCache() {
__cache = {};
}