-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
229 lines (195 loc) · 6.23 KB
/
index.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
'use strict';
import StreamReader from './lib/stream-reader';
import isAtHTMLTag from './lib/is-html';
import { isQuote } from './lib/quotes';
const code = ch => ch.charCodeAt(0);
const SQUARE_BRACE_L = code('[');
const SQUARE_BRACE_R = code(']');
const ROUND_BRACE_L = code('(');
const ROUND_BRACE_R = code(')');
const CURLY_BRACE_L = code('{');
const CURLY_BRACE_R = code('}');
const specialChars = new Set('#.*:$-_!@%^+>/'.split('').map(code));
const bracePairs = new Map()
.set(SQUARE_BRACE_L, SQUARE_BRACE_R)
.set(ROUND_BRACE_L, ROUND_BRACE_R)
.set(CURLY_BRACE_L, CURLY_BRACE_R);
const defaultOptions = {
syntax: 'markup',
lookAhead: null,
prefix: ''
};
/**
* Extracts Emmet abbreviation from given string.
* The goal of this module is to extract abbreviation from current editor’s line,
* e.g. like this: `<span>.foo[title=bar|]</span>` -> `.foo[title=bar]`, where
* `|` is a current caret position.
* @param {String} line A text line where abbreviation should be expanded
* @param {Number} [pos] Caret position in line. If not given, uses end-of-line
* @param {Object} [options]
* @param {Boolean} [options.lookAhead] Allow parser to look ahead of `pos` index for
* searching of missing abbreviation parts. Most editors automatically inserts
* closing braces for `[`, `{` and `(`, which will most likely be right after
* current caret position. So in order to properly expand abbreviation, user
* must explicitly move caret right after auto-inserted braces. With this option
* enabled, parser will search for closing braces right after `pos`. Default is `true`
* @param {String} [options.syntax] Name of context syntax of expanded abbreviation.
* Either 'markup' (default) or 'stylesheet'. In 'stylesheet' syntax, braces `[]`
* and `{}` are not supported thus not extracted.
* @param {String} [options.prefix] A string that should precede abbreviation in
* order to make it successfully extracted. If given, the abbreviation will be
* extracted from the nearest `prefix` occurrence.
* @return {Object} Object with `abbreviation` and its `location` in given line
* if abbreviation can be extracted, `null` otherwise
*/
export default function extractAbbreviation(line, pos, options) {
// make sure `pos` is within line range
pos = Math.min(line.length, Math.max(0, pos == null ? line.length : pos));
if (typeof options === 'boolean') {
options = Object.assign({}, defaultOptions, { lookAhead: options });
} else {
options = Object.assign({}, defaultOptions, options);
}
if (options.lookAhead == null || options.lookAhead === true) {
pos = offsetPastAutoClosed(line, pos, options);
}
let c;
const start = getStartOffset(line, pos, options.prefix);
if (start === -1) {
return null;
}
const stream = new StreamReader(line, start);
stream.pos = pos;
const stack = [];
while (!stream.sol()) {
c = stream.peek();
if (isCloseBrace(c, options.syntax)) {
stack.push(c);
} else if (isOpenBrace(c, options.syntax)) {
if (stack.pop() !== bracePairs.get(c)) {
// unexpected brace
break;
}
} else if (has(stack, SQUARE_BRACE_R) || has(stack, CURLY_BRACE_R)) {
// respect all characters inside attribute sets or text nodes
stream.pos--;
continue;
} else if (isAtHTMLTag(stream) || !isAbbreviation(c)) {
break;
}
stream.pos--;
}
if (!stack.length && stream.pos !== pos) {
// found something, remove some invalid symbols from the
// beginning and return abbreviation
const abbreviation = line.slice(stream.pos, pos).replace(/^[*+>^]+/, '');
return {
abbreviation,
location: pos - abbreviation.length,
start: options.prefix
? start - options.prefix.length
: pos - abbreviation.length,
end: pos
};
}
}
/**
* Returns new `line` index which is right after characters beyound `pos` that
* editor will likely automatically close, e.g. }, ], and quotes
* @param {String} line
* @param {Number} pos
* @return {Number}
*/
function offsetPastAutoClosed(line, pos, options) {
// closing quote is allowed only as a next character
if (isQuote(line.charCodeAt(pos))) {
pos++;
}
// offset pointer until non-autoclosed character is found
while (isCloseBrace(line.charCodeAt(pos), options.syntax)) {
pos++;
}
return pos;
}
/**
* Returns start offset (left limit) in `line` where we should stop looking for
* abbreviation: it’s nearest to `pos` location of `prefix` token
* @param {String} line
* @param {Number} pos
* @param {String} prefix
* @return {Number}
*/
function getStartOffset(line, pos, prefix) {
if (!prefix) {
return 0;
}
const stream = new StreamReader(line);
const compiledPrefix = String(prefix).split('').map(code);
stream.pos = pos;
let result;
while (!stream.sol()) {
if (consumePair(stream, SQUARE_BRACE_R, SQUARE_BRACE_L) || consumePair(stream, CURLY_BRACE_R, CURLY_BRACE_L)) {
continue;
}
result = stream.pos;
if (consumeArray(stream, compiledPrefix)) {
return result;
}
stream.pos--;
}
return -1;
}
/**
* Consumes full character pair, if possible
* @param {StreamReader} stream
* @param {Number} close
* @param {Number} open
* @return {Boolean}
*/
function consumePair(stream, close, open) {
const start = stream.pos;
if (stream.eat(close)) {
while (!stream.sol()) {
if (stream.eat(open)) {
return true;
}
stream.pos--;
}
}
stream.pos = start;
return false;
}
/**
* Consumes all character codes from given array, right-to-left, if possible
* @param {StreamReader} stream
* @param {Number[]} arr
*/
function consumeArray(stream, arr) {
const start = stream.pos;
let consumed = false;
for (let i = arr.length - 1; i >= 0 && !stream.sol(); i--) {
if (!stream.eat(arr[i])) {
break;
}
consumed = i === 0;
}
if (!consumed) {
stream.pos = start;
}
return consumed;
}
function has(arr, value) {
return arr.indexOf(value) !== -1;
}
function isAbbreviation(c) {
return (c > 64 && c < 91) // uppercase letter
|| (c > 96 && c < 123) // lowercase letter
|| (c > 47 && c < 58) // number
|| specialChars.has(c); // special character
}
function isOpenBrace(c, syntax) {
return c === ROUND_BRACE_L || (syntax === 'markup' && (c === SQUARE_BRACE_L || c === CURLY_BRACE_L));
}
function isCloseBrace(c, syntax) {
return c === ROUND_BRACE_R || (syntax === 'markup' && (c === SQUARE_BRACE_R || c === CURLY_BRACE_R));
}