-
-
Notifications
You must be signed in to change notification settings - Fork 520
/
config.ts
494 lines (413 loc) · 15 KB
/
config.ts
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import type { Abbreviation } from '@emmetio/abbreviation';
import markupSnippets from './snippets/html.json' assert { type: 'json' };
import stylesheetSnippets from './snippets/css.json' assert { type: 'json' };
import xslSnippets from './snippets/xsl.json' assert { type: 'json' };
import pugSnippets from './snippets/pug.json' assert { type: 'json' };
import variables from './snippets/variables.json' assert { type: 'json' };
import type { CSSSnippet } from './stylesheet/snippets.js';
export type SyntaxType = 'markup' | 'stylesheet';
export type FieldOutput = (index: number, placeholder: string, offset: number, line: number, column: number) => string;
export type TextOutput = (text: string, offset: number, line: number, column: number) => string;
export type StringCase = '' | 'lower' | 'upper';
export interface SnippetsMap {
[name: string]: string;
}
export interface AbbreviationContext {
name: string;
attributes?: { [name: string]: string | null };
}
/**
* Raw config which contains per-syntax options. `markup` and `syntax` keys are
* reserved for global settings for all markup and stylesheet syntaxes
*/
export interface GlobalConfig {
[syntax: string]: Partial<BaseConfig>;
}
export interface BaseConfig {
/* Type of abbreviation context, default is `markup` */
type: SyntaxType;
/** Options for abbreviation output */
options: Partial<Options>;
/** Substitutions for variable names */
variables: SnippetsMap;
/** Abbreviation name to snippets mapping */
snippets: SnippetsMap;
}
interface ResolvedConfig extends BaseConfig {
/** Host syntax */
syntax: string;
/**
* Context of abbreviation. For markup abbreviation, it contains parent tag
* name with attributes, for stylesheet abbreviation it contains property name
* if abbreviation is expanded as value
*/
context?: AbbreviationContext;
/** Text to wrap with abbreviation */
text?: string | string[];
/** Max amount of repeated elements (fool proof) */
maxRepeat?: number;
/**
* Object for storing internal cache data to be shared across Emmet methods
* invocation. If provided, Emmet will store compute-intensive data in this
* object and will re-use it during editor session.
* Every time user settings are changed, you should empty cache by passing
* new object.
*/
cache?: Cache;
/**
* A callback for internal warnings or errors (for example, when parsing invalid abbreviation)
*/
warn?: (message: string, err?: Error) => void
}
export type Config = ResolvedConfig & { options: Options };
export type UserConfig = Partial<ResolvedConfig>;
export interface Cache {
stylesheetSnippets?: CSSSnippet[];
markupSnippets?: { [name: string]: Abbreviation | null };
}
export interface Options {
/////////////////////
// Generic options //
/////////////////////
/** A list of inline-level elements */
inlineElements: string[];
////////////////////
// Output options //
////////////////////
/** A string for one level indent */
'output.indent': string;
/**
* A string for base indent, e.g. context indentation which will be added
* for every generated line
*/
'output.baseIndent': string;
/** A string to use as a new line */
'output.newline': string;
/** Tag case: lower, upper or '' (keep as-is) */
'output.tagCase': StringCase;
/** Attribute name case: lower, upper or '' (keep as-is) */
'output.attributeCase': StringCase;
/** Attribute value quotes: 'single' or 'double' */
'output.attributeQuotes': 'single' | 'double';
/** Enable output formatting (indentation and line breaks) */
'output.format': boolean;
/** When enabled, automatically adds inner line breaks for leaf (e.g. without children) nodes */
'output.formatLeafNode': boolean;
/** A list of tag names that should not get inner indentation */
'output.formatSkip': string[];
/** A list of tag names that should *always* get inner indentation. */
'output.formatForce': string[];
/**
* How many inline sibling elements should force line break for each tag.
* Set to `0` to output all inline elements without formatting.
* Set to `1` to output all inline elements with formatting (same as block-level).
*/
'output.inlineBreak': number;
/**
* Produce compact notation of boolean attributes: attributes which doesn’t have value.
* With this option enabled, outputs `<div contenteditable>` instead of
* `<div contenteditable="contenteditable">`
*/
'output.compactBoolean': boolean;
/** A list of boolean attributes */
'output.booleanAttributes': string[];
/** Reverses attribute merging directions when resolving snippets */
'output.reverseAttributes': boolean;
/** Style of self-closing tags: html (`<br>`), xml (`<br/>`) or xhtml (`<br />`) */
'output.selfClosingStyle': 'html' | 'xml' | 'xhtml';
/**
* A function that takes field index and optional placeholder and returns
* a string field (tabstop) for host editor. For example, a TextMate-style
* field is `$index` or `${index:placeholder}`
* @param index Field index
* @param placeholder Field placeholder (default value), if any
* @param offset Current character offset from the beginning of generated content
* @param line Current line of generated output
* @param column Current column in line
*/
'output.field': FieldOutput;
/**
* A function for processing text chunk passed to `OutputStream`.
* May be used by editor for escaping characters, if necessary
*/
'output.text': TextOutput;
////////////////////
// Markup options //
////////////////////
/**
* Automatically update value of <a> element's href attribute
* if inserting URL or email
*/
'markup.href': boolean;
/**
* Attribute name mapping. Can be used to change attribute names for output.
* For example, `class` -> `className` in JSX. If a key ends with `*`, this
* value will be used for multi-attributes: currentry, it’s a `class` and `id`
* since `multiple` marker is added for shorthand attributes only.
* Example: `{ "class*": "styleName" }`
*/
'markup.attributes'?: Record<string, string>;
/**
* Prefixes for attribute values.
* If specified, a value is treated as prefix for object notation and
* automatically converts attribute value into expression if `jsx` is enabled.
* Same as in `markup.attributes` option, a `*` can be used.
*/
'markup.valuePrefix'?: Record<string, string>;
////////////////////////////////
// Element commenting options //
////////////////////////////////
/**
* Enable/disable element commenting: generate comments before open and/or
* after close tag
*/
'comment.enabled': boolean;
/**
* Attributes that should trigger node commenting on specific node,
* if commenting is enabled
*/
'comment.trigger': string[];
/**
* Template string for comment to be placed *before* opening tag
*/
'comment.before': string;
/**
* Template string for comment to be placed *after* closing tag.
* Example: `\n<!-- /[#ID][.CLASS] -->`
*/
'comment.after': string;
/////////////////
// BEM options //
/////////////////
/** Enable/disable BEM addon */
'bem.enabled': boolean;
/** A string for separating elements in output class */
'bem.element': string;
/** A string for separating modifiers in output class */
'bem.modifier': string;
/////////////////
// JSX options //
/////////////////
/** Enable/disable JSX addon */
'jsx.enabled': boolean;
////////////////////////
// Stylesheet options //
////////////////////////
/** List of globally available keywords for properties */
'stylesheet.keywords': string[];
/**
* List of unitless properties, e.g. properties where numeric values without
* explicit unit will be outputted as is, without default value
*/
'stylesheet.unitless': string[];
/** Use short hex notation where possible, e.g. `#000` instead of `#000000` */
'stylesheet.shortHex': boolean;
/** A string between property name and value */
'stylesheet.between': string;
/** A string after property value */
'stylesheet.after': string;
/** A unit suffix to output by default after integer values, 'px' by default */
'stylesheet.intUnit': string;
/** A unit suffix to output by default after float values, 'em' by default */
'stylesheet.floatUnit': string;
/**
* Aliases for custom units in abbreviation. For example, `r: 'rem'` will
* output `10rem` for abbreviation `10r`
*/
'stylesheet.unitAliases': SnippetsMap;
/** Output abbreviation as JSON object properties (for CSS-in-JS syntaxes) */
'stylesheet.json': boolean;
/** Use double quotes for JSON values */
'stylesheet.jsonDoubleQuotes': boolean;
/**
* A float number between 0 and 1 to pick fuzzy-matched abbreviations.
* Lower value will pick more abbreviations (and less accurate)
*/
'stylesheet.fuzzySearchMinScore': number;
/**
* Force strict abbreviation match. If Emmet is unable to match abbreviation
* with existing snippets, it will convert it to CSS property (`false`)
* or skip it (`true`). E.g. `foo-bar` will expand to `foo: bar` if this option
* is disabled or empty string if enabled
*/
'stylesheet.strictMatch': boolean;
}
/**
* Default syntaxes for abbreviation types
*/
export const defaultSyntaxes: { [name in SyntaxType]: string } = {
markup: 'html',
stylesheet: 'css'
};
/**
* List of all known syntaxes
*/
export const syntaxes = {
markup: ['html', 'xml', 'xsl', 'jsx', 'js', 'pug', 'slim', 'haml', 'vue', 'svelte'],
stylesheet: ['css', 'sass', 'scss', 'less', 'sss', 'stylus']
};
export const defaultOptions: Options = {
'inlineElements': [
'a', 'abbr', 'acronym', 'applet', 'b', 'basefont', 'bdo',
'big', 'br', 'button', 'cite', 'code', 'del', 'dfn', 'em', 'font', 'i',
'iframe', 'img', 'input', 'ins', 'kbd', 'label', 'map', 'object', 'q',
's', 'samp', 'select', 'small', 'span', 'strike', 'strong', 'sub', 'sup',
'textarea', 'tt', 'u', 'var'
],
'output.indent': '\t',
'output.baseIndent': '',
'output.newline': '\n',
'output.tagCase': '',
'output.attributeCase': '',
'output.attributeQuotes': 'double',
'output.format': true,
'output.formatLeafNode': false,
'output.formatSkip': ['html'],
'output.formatForce': ['body'],
'output.inlineBreak': 3,
'output.compactBoolean': false,
'output.booleanAttributes': [
'contenteditable', 'seamless', 'async', 'autofocus',
'autoplay', 'checked', 'controls', 'defer', 'disabled', 'formnovalidate',
'hidden', 'ismap', 'loop', 'multiple', 'muted', 'novalidate', 'readonly',
'required', 'reversed', 'selected', 'typemustmatch'
],
'output.reverseAttributes': false,
'output.selfClosingStyle': 'html',
'output.field': (index, placeholder) => placeholder,
'output.text': text => text,
'markup.href': true,
'comment.enabled': false,
'comment.trigger': ['id', 'class'],
'comment.before': '',
'comment.after': '\n<!-- /[#ID][.CLASS] -->',
'bem.enabled': false,
'bem.element': '__',
'bem.modifier': '_',
'jsx.enabled': false,
'stylesheet.keywords': ['auto', 'inherit', 'unset', 'none'],
'stylesheet.unitless': ['z-index', 'line-height', 'opacity', 'font-weight', 'zoom', 'flex', 'flex-grow', 'flex-shrink'],
'stylesheet.shortHex': true,
'stylesheet.between': ': ',
'stylesheet.after': ';',
'stylesheet.intUnit': 'px',
'stylesheet.floatUnit': 'em',
'stylesheet.unitAliases': { e: 'em', p: '%', x: 'ex', r: 'rem' },
'stylesheet.json': false,
'stylesheet.jsonDoubleQuotes': false,
'stylesheet.fuzzySearchMinScore': 0,
'stylesheet.strictMatch': false
};
export const defaultConfig: Config = {
type: 'markup',
syntax: 'html',
variables,
snippets: {},
options: defaultOptions
};
/**
* Default per-syntax config
*/
export const syntaxConfig: GlobalConfig = {
markup: {
snippets: parseSnippets(markupSnippets),
},
xhtml: {
options: {
'output.selfClosingStyle': 'xhtml'
}
},
xml: {
options: {
'output.selfClosingStyle': 'xml'
}
},
xsl: {
snippets: parseSnippets(xslSnippets),
options: {
'output.selfClosingStyle': 'xml'
}
},
jsx: {
options: {
'jsx.enabled': true,
'markup.attributes': {
'class': 'className',
'class*': 'styleName',
'for': 'htmlFor'
},
'markup.valuePrefix': {
'class*': 'styles'
}
}
},
vue: {
options: {
'markup.attributes': {
'class*': ':class',
}
}
},
svelte: {
options: {
'jsx.enabled': true
}
},
pug: {
snippets: parseSnippets(pugSnippets)
},
stylesheet: {
snippets: parseSnippets(stylesheetSnippets)
},
sass: {
options: {
'stylesheet.after': ''
}
},
stylus: {
options: {
'stylesheet.between': ' ',
'stylesheet.after': '',
}
}
};
/**
* Parses raw snippets definitions with possibly multiple keys into a plan
* snippet map
*/
export function parseSnippets(snippets: SnippetsMap): SnippetsMap {
const result: SnippetsMap = {};
Object.keys(snippets).forEach(k => {
for (const name of k.split('|')) {
result[name] = snippets[k];
}
});
return result;
}
export default function resolveConfig(config: UserConfig = {}, globals: GlobalConfig = {}): Config {
const type: SyntaxType = config.type || 'markup';
const syntax: string = config.syntax || defaultSyntaxes[type];
return {
...defaultConfig,
...config,
type,
syntax,
variables: mergedData(type, syntax, 'variables', config, globals),
snippets: mergedData(type, syntax, 'snippets', config, globals),
options: mergedData(type, syntax, 'options', config, globals)
};
}
function mergedData<K extends keyof BaseConfig>(type: SyntaxType, syntax: string, key: K, config: UserConfig, globals: GlobalConfig = {}): Config[K] {
const typeDefaults = syntaxConfig[type];
const typeOverride = globals[type];
const syntaxDefaults = syntaxConfig[syntax];
const syntaxOverride = globals[syntax];
return {
...(defaultConfig[key] as object),
...(typeDefaults && typeDefaults[key] as object),
...(syntaxDefaults && syntaxDefaults[key] as object),
...(typeOverride && typeOverride[key] as object),
...(syntaxOverride && syntaxOverride[key] as object),
...(config[key] as object)
} as Config[K];
}