-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
267 lines (244 loc) · 8.3 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
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "cpppp" is now active!');
context.subscriptions.push(vscode.commands.registerCommand('extension.addHeaderGuard', addHeaderGuard));
context.subscriptions.push(vscode.commands.registerCommand('extension.genGet', genGetSet(['get'])));
context.subscriptions.push(vscode.commands.registerCommand('extension.genSet', genGetSet(['set'])));
context.subscriptions.push(vscode.commands.registerCommand('extension.genGetAndSet', genGetSet(['get', 'set'])));
context.subscriptions.push(vscode.commands.registerCommand('extension.GenClear', genGetSet(['clear'])));
}
function addHeaderGuard() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
let headerPath = editor.document.fileName;
if (!headerPath.endsWith('.h')) {
vscode.window.showInformationMessage('Not a cpp header file.');
return;
}
const relativePath = vscode.workspace.asRelativePath(headerPath);
const guard = relativePath.toUpperCase().replace(/[^\w]/g, '_');
const startGuard = [
'#ifndef ', guard, '\n',
'#define ', guard, '\n',
'\n', '\n', '\n'
].join('');
const endGuard = [
'\n',
'#endif ', '// ', guard
].join('');
const startPosition = new vscode.Position(0, 0),
endPosition = new vscode.Position(editor.document.lineCount, 0);
editor.edit(function (editBuilder) {
editBuilder.insert(startPosition, startGuard);
editBuilder.insert(endPosition, endGuard);
})
}
function parseLine(line_) {
let line = line_.trim()
if (line.startsWith('//')) {
return null;
}
let index = 0;
let code = {}
let brackets = 0
while (index < line.length) {
if (line[index] === ' ') {
if (brackets === 0) {
code.type = line.substring(0, index).replace(/\s/g, '');
break;
} else {
// pass
}
} else if (line[index] === '<') {
brackets++;
} else if (line[index] === '>') {
brackets--;
} else {
// pass
}
index++;
}
if (index === line.length) {
return null;
}
line = line.substring(index).trimLeft()
index = 0;
while (index < line.length) {
if (line[index] === ';') {
code.var = line.substring(0, index).replace(/\s/g, '');
if (code.var && (code.var[0] === '*' || code.var[0] === '&')) {
// pointer or reference
code.type += code.var[0];
code.var = code.var.substring(1);
}
break;
} else {
// pass
}
index++;
}
if (index != line.length) {
code.comment = line.substring(index).trimLeft();
}
code.varLite = code.var.substring(0, code.var.length - 1)
return code;
}
const PRIMARY_TYPE = [
'bool',
'double', 'float',
'int', 'uint', 'int32_t', 'uint32_t', 'int64_t', 'uint64_t',
]
function isPrimaryType(varType) {
return PRIMARY_TYPE.indexOf(varType) !== -1;
}
function isPointerType(varType) {
return varType.endsWith('*');
}
function isBoolType(varType) {
return PRIMARY_TYPE.indexOf(varType) === 0;
}
function isFloatingPointType(varType) {
return PRIMARY_TYPE.indexOf(varType) >= 1 && PRIMARY_TYPE.indexOf(varType) <= 2;
}
function isIntegerType(varType) {
return PRIMARY_TYPE.indexOf(varType) >= 3 && PRIMARY_TYPE.indexOf(varType) <= 8;
}
function genSet(variable, primaryType) {
if (primaryType === undefined) {
primaryType = isPrimaryType(variable.type);
}
let pointerType = isPointerType(variable.type);
let code;
if (primaryType) {
code = [
'void set_', variable.varLite, '(', variable.type, ' ', variable.varLite, ')',
' { ', variable.var, ' = ', variable.varLite, '; }',
]
} else if (pointerType) {
code = [
'void set_', variable.varLite, '(', variable.type, ' const ', variable.varLite, ')',
' { ', variable.var, ' = ', variable.varLite, '; }',
]
} else {
code = [
'void set_', variable.varLite, '(const ', variable.type, ' &', variable.varLite, ')',
' { ', variable.var, ' = ', variable.varLite, '; }',
]
}
return code.join('');
}
function genGet(variable, primaryType) {
if (primaryType === undefined) {
primaryType = isPrimaryType(variable.type);
}
let pointerType = isPointerType(variable.type);
if (primaryType) {
return [
variable.type, ' ', variable.varLite, '() const ',
'{ return ', variable.var, '; }',
].join('');
} else if (pointerType) {
return [
[
variable.type, ' ', variable.varLite, '() ',
'{ return ', variable.var, '; }',
],
[
'const ', variable.type, ' ', variable.varLite, '() const ',
'{ return ', variable.var, '; }',
]].map(function (code) {
return code.join('');
}).join('\n');
} else {
return [
[
variable.type, '& ', variable.varLite, '() ',
'{ return ', variable.var, '; }',
],
[
'const ', variable.type, '& ', variable.varLite, '() const ',
'{ return ', variable.var, '; }',
]].map(function (code) {
return code.join('');
}).join('\n');
}
}
function genGetSet(props) {
return function () {
let editor = vscode.window.activeTextEditor;
let selection = editor.selection;
if (!selection) {
vscode.window.showInformationMessage('No variables selected.');
return;
}
let codes = [
'',
];
if (props.indexOf('get') >= 0 || props.indexOf('set') >= 0) {
codes.push('// getters and setters');
} else if (props.indexOf('clear') >= 0) {
codes.push('// clear');
}
for (let index = selection.start.line; index <= selection.end.line; index++) {
let line = editor.document.lineAt(index).text;
let variable = parseLine(line);
if (!variable) {
continue;
}
let code = '// ' + variable.var;
let primaryType = isPrimaryType(variable.type);
if (props.indexOf('get') >= 0) {
code += '\n' + genGet(variable, primaryType);
}
if (props.indexOf('set') >= 0) {
code += '\n' + genSet(variable, primaryType);
}
if (props.indexOf('clear') >= 0) {
code += '\n' + GenClear(variable);
}
codes.push(code);
}
codes.push('')
const content = codes.join('\n\n');
const insertPosition = new vscode.Position(selection.end.line + 1, 0);
editor.edit(function(editBuilder) {
editBuilder.insert(insertPosition, content);
})
}
}
function GenClear(variable) {
if (isBoolType(variable.type)) {
return [
variable.varLite, ' = false;',
].join('');
} else if (isFloatingPointType(variable.type)) {
return [
variable.varLite, ' = 0.0;',
].join('');
} else if (isIntegerType(variable.type)) {
return [
variable.varLite, ' = 0;',
].join('');
} else if (isPointerType(variable.type)) {
return [
variable.varLite, ' = NULL;',
].join('');
} else {
return [
variable.varLite, '.clear();',
].join('');
}
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;