-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
91 lines (76 loc) · 3.29 KB
/
extension.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
// VS Code API Reference: https://code.visualstudio.com/api/references/vscode-api
const vscode = require('vscode');
let emmetHelper;
function getEmmetHelper() {
// Lazy load vscode-emmet-helper instead of importing it
// directly to reduce the start-up time of the extension
if (!emmetHelper) {
emmetHelper = require('vscode-emmet-helper');
}
return emmetHelper;
}
function expand(abbr) {
let helper = getEmmetHelper(),
expandOptions = helper.getExpandOptions('html'), // TODO: consider editor.document.languageId
parsedAbbr = helper.parseAbbreviation(abbr);
return helper.expandAbbreviation(parsedAbbr, expandOptions);
}
function expandPreview(abbr) {
let result = expand(abbr);
// replace tabstops with their placeholders (if any)
return result.replace(/\$\{\d+\:?(.*?)\}/g, '$1');
}
function activate(context) {
const disposable = vscode.commands.registerCommand('ysemeniuk.emmetLive', () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('Emmet Live: no active text editor');
return; // No open text editor
}
let originalText = editor.document.getText(editor.selection);
// if selection is empty then try to select current line
// TODO: improve this behavior with emmetHelper.extractAbbreviation and/or emmetHelper.extractAbbreviationFromText
if (!originalText) {
let currentLineIndex = editor.selection.active.line;
let currentLine = editor.document.lineAt(currentLineIndex);
originalText = currentLine ? currentLine.text : originalText;
// select current line
let selectionFrom = new vscode.Position(currentLineIndex, 0);
let selectionTo = new vscode.Position(currentLineIndex, originalText.length);
editor.selections = [new vscode.Selection(selectionFrom, selectionTo)];
}
let recordUndoStops = true;
vscode.window.showInputBox({
placeHolder: 'Emmet Abbreviation',
prompt: 'Type your Emmet abbreviation',
value: originalText.trim(),
validateInput: function (abbr) {
try {
let result = expandPreview(abbr);
editor.edit((editBuilder) => {
editBuilder.replace(editor.selection, result);
}, { undoStopAfter: false, undoStopBefore: recordUndoStops });
// record only first edit
recordUndoStops = false;
} catch (e) {
return 'Invalid Emmet abbreviation';
}
}
}).then((abbr) => {
if (abbr) {
let result = expand(abbr);
editor.insertSnippet(new vscode.SnippetString(result));
} else {
// if input dismissed (abbr is undefined) then restore original text
editor.edit((editBuilder) => {
editBuilder.replace(editor.selection, originalText);
}, { undoStopAfter: false, undoStopBefore: false });
}
});
});
context.subscriptions.push(disposable);
}
function deactivate() {
}
exports.activate = activate;
exports.deactivate = deactivate;