forked from sergeche/emmet-sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
325 lines (268 loc) · 7.84 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
function activeView() {
return sublime.active_window().active_view();
}
var editorProxy = emmet.exec(function(require, _) {
return {
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 (_.isUndefined(end))
end = _.isUndefined(start) ? this.getContent().length : start;
if (_.isUndefined(start)) 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);
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();
if (view.match_selector(pos, 'text.html')
&& sublimeGetOption('autodetect_xhtml', false)
&& require('actionUtils').isXHTML(this)) {
return 'xhtml';
}
if (view.match_selector(pos, 'string.quoted.double.block.python')
|| view.match_selector(pos, 'source.coffee string')
|| view.match_selector(pos, 'string.unquoted.heredoc')) {
// use html's default profile for:
// * Python's multiline block
// * CoffeeScript string
// * PHP heredoc
return pyDetectProfile();
}
if (view.score_selector(pos, '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();
}
};
});
var _completions = {};
function require(name) {
return emmet.require(name);
}
function pyPreprocessText(value) {
var base = 1000;
var zeroBase = 0;
var lastZero = null;
var range = require('range');
var ts = require('tabStops');
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 = ts.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 = ts.processText(value, tabstopOptions);
if (sublimeGetOption('insert_final_tabstop', false) && !/\$\{0\}$/.test(value)) {
value += '${0}';
} else if (lastZero) {
value = require('utils').replaceSubstring(value, '${0}', lastZero);
}
return value;
}
function pyExpandAbbreviationAsYouType(abbr) {
var info = require('editorUtils').outputInfo(editorProxy);
try {
var result = emmet.expandAbbreviation(abbr, info.syntax, info.profile,
require('actionUtils').captureContext(editorProxy));
return pyPreprocessText(result);
} catch (e) {
return '';
}
}
function pyWrapAsYouType(abbr, content) {
var info = require('editorUtils').outputInfo(editorProxy);
content = require('utils').escapeText(content);
var ctx = require('actionUtils').captureContext(editorProxy);
try {
var result = require('wrapWithAbbreviation').wrap(abbr, content, info.syntax, info.profile, ctx);
return pyPreprocessText(result);
} catch(e) {
return '';
}
}
function pyCaptureWrappingRange() {
var info = require('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 = require('htmlMatcher').find(info.content, startOffset);
if (!match) {
// nothing to wrap
return null;
}
/** @type Range */
var utils = require('utils');
var narrowedSel = utils.narrowToNonSpace(info.content, match.range);
startOffset = narrowedSel.start;
endOffset = narrowedSel.end;
}
return [startOffset, endOffset];
}
function pyGetTagNameRanges(pos) {
var ranges = [];
var info = require('editorUtils').outputInfo(editorProxy);
// search for tag
try {
var tag = require('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 = require('editorUtils').outputInfo(editorProxy);
// search for tag
try {
var tag = require('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 require('expandAbbreviation').findAbbreviation(editorProxy);
}
function pyHasSnippet(name) {
return !!emmet.require('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 = require('resources').getAllSnippets(dialect);
var css = require('cssResolver');
_completions[dialect] = _.map(all, function(v, k) {
var snippetValue = typeof v.parsedValue == 'object'
? v.parsedValue.data
: v.value;
var snippet = css.transformSnippet(snippetValue, false, dialect);
return {
k: v.nk,
label: snippet.replace(/\:\s*\$\{0\}\s*;?$/, ''),
v: css.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';
}
var syntax = 'html';
if (!/\bstring\b/.test(scope) && /\bsource\.([\w\-]+)/.test(scope) && require('resources').hasSyntax(RegExp.$1)) {
syntax = RegExp.$1;
} else if (/\b(less|scss|sass|css|stylus)\b/.test(scope)) {
// detect CSS-like syntaxes independently,
// since it may cause collisions with some highlighters
syntax = RegExp.$1;
} else if (/\b(html|xml|haml)\b/.test(scope)) {
syntax = RegExp.$1;
}
return require('actionUtils').detectSyntax(editorProxy, syntax);
}
function pyDetectProfile(argument) {
return require('actionUtils').detectProfile(editorProxy);
}