-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.js
65 lines (54 loc) · 1.79 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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require("vscode");
const convert = require("./convert");
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const convertCommand = vscode.commands.registerCommand(
"extension.convertCSStoJS",
() => {
const editor = vscode.window.activeTextEditor;
// return if there's no editor or it's not a javascript file
if (
!editor ||
!/javascript|typescript/.test(editor.document.languageId)
) {
return;
}
const selection = editor.selection;
const lineText = editor.document.lineAt(selection.start.line).text;
const selectedText = editor.document.getText(selection);
const convertableText = selectedText || lineText;
const range = rangeFactory(selection, selectedText.length);
editor.edit((builder) =>
builder.replace(range, convert(convertableText))
);
}
);
context.subscriptions.push(convertCommand);
}
// this method is called when your extension is deactivated
function deactivate() {}
function rangeFactory(selection, length) {
if (length === 0) {
selection.start._character = 0;
selection.end._character = vscode.window.activeTextEditor.document.lineAt(
selection.start.line
).text.length;
}
return new vscode.Range(
positionFactory(selection.start),
positionFactory(selection.end)
);
}
function positionFactory(positionObj) {
return new vscode.Position(positionObj._line, positionObj._character);
}
module.exports = {
activate,
deactivate,
};