forked from tariky/easy-snippet-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
161 lines (131 loc) · 5.75 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
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
var vscode = require('vscode');
var process = require('process');
var fs = require('fs');
var util = require('util');
var os = require('os');
function activate(context) {
let disposable = vscode.commands.registerCommand('extension.petMaker', function () {
let editor = vscode.window.activeTextEditor;
let selected = editor.selection;
let selectedText = editor.document.getText(selected);
let cleanCode = selectedText;
let snippetObject = {};
vscode.languages.getLanguages()
.then((listOfLanguages) => {
return vscode.window.showQuickPick(listOfLanguages, { placeHolder: vscode.window.activeTextEditor.document.languageId });
})
.then(selectedLanguage => {
snippetObject.lang = selectedLanguage;
return vscode.window.showInputBox({ prompt: "Enter snippet name" });
})
.then((snippetName) => {
snippetObject.name = snippetName;
return vscode.window.showInputBox({ prompt: "Enter snippet prefix" });
})
.then(snippetPrefix => {
snippetObject.prefix = snippetPrefix;
return vscode.window.showInputBox({ prompt: "Enter snippet description" });
})
.then(snippetDesc => {
snippetObject.description = snippetDesc;
})
.then(() => {
let extansionPath;
let delimiter = "/";
let vscodeRelease = vscode.env.appName;
// Check for insider version
if(vscodeRelease === "Visual Studio Code - Insiders") {
[extansionPath,delimiter] = pickingRelease("Code - Insiders");
} else {
[extansionPath,delimiter] = pickingRelease("Code");
}
var userSnippetsFile = extansionPath + util.format("snippets%s.json", delimiter + snippetObject.lang);
if( snippetObject.name !== undefined && snippetObject.prefix !== undefined ) {
fs.readFile(userSnippetsFile, (err, txt) => {
if(txt) {
var check = txt.toString();
}
if(err) {
fs.open(userSnippetsFile, "w+", (err, fd) => {
if (err) {
return;
} else {
initFile(snippetObject, cleanCode, userSnippetsFile);
}
})
} else if(!check) {
initFile(snippetObject, cleanCode, userSnippetsFile);
} else {
// TODO: Refactoring, code is damn mess.
var savedSnippets = txt.toString();
var restoreObject = JSON.parse(savedSnippets);
if (restoreObject[snippetObject.name] !== undefined || restoreObject[snippetObject.name] === null) {
vscode.window.showErrorMessage("Snippet with this name already exists");
return;
} else {
// Access to current file and append new object member
restoreObject[snippetObject.name] = {
prefix: snippetObject.prefix,
body: buildBodyFromText(cleanCode),
description: snippetObject.description
}
var snippetString = JSON.stringify(restoreObject, null, 2);
var toSave = snippetString;
fs.writeFile(userSnippetsFile, toSave, (err) => {
if(err) vscode.window.showErrorMessage("Error while saving new snippet!");
})
}
}
})
} else {
vscode.window.showWarningMessage("You need to enter Snippet name and Snippet prefix.");
}
})
context.subscriptions.push(disposable);
});
}
function buildBodyFromText(text) {
return text.split("\n");
}
function pickingRelease(name) {
const osName = os.type();
let delimiter = "/";
let extansionPath;
switch (osName) {
case ("Darwin"): {
extansionPath = process.env.HOME + "/Library/Application Support/" + name + "/User/";
break;
}
case ("Linux"): {
extansionPath = process.env.HOME + "/.config/" + name + "/User/";
break;
}
case ("Windows_NT"): {
extansionPath = process.env.APPDATA + "\\" + name + "\\User\\";
delimiter = "\\";
break;
}
default: {
extansionPath = process.env.HOME + "/.config/" + name + "/User/";
break;
}
}
return [extansionPath,delimiter];
}
function initFile(snippObj, body, saveLocation) {
var snippets = {}
snippets[snippObj.name] = {
prefix: snippObj.prefix,
body: buildBodyFromText(body),
description: snippObj.description
}
var snippetString = JSON.stringify(snippets, null, 2);
var toSave = snippetString;
fs.writeFile(saveLocation, toSave, (err) => {
if(err) vscode.window.showErrorMessage("Error while saving new snippet!");
})
}
exports.activate = activate;
function deactivate() {
}
exports.deactivate = deactivate;