-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreateMethod.js
More file actions
74 lines (54 loc) · 1.83 KB
/
CreateMethod.js
File metadata and controls
74 lines (54 loc) · 1.83 KB
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
const vscode = require("vscode");
module.exports = class CreateMethod {
async create() {
const editor = this.activeEditor();
if (editor === undefined) {
return;
}
let activeDocument = this.activeDocument().uri;
if (activeDocument === undefined) {
return;
}
await this.insertMethod(activeDocument);
}
async insertMethod(activeDocument) {
let doc = await vscode.workspace.openTextDocument(activeDocument);
let currentPosition = this.activeEditor().selection;
let methodName = this.activeDocument().getText(currentPosition);
if (!methodName) {
// method not selected
currentPosition = this.activeEditor().selection.active;
let currentLineText = doc.lineAt(currentPosition.line).text.trim();
methodName = currentLineText.match(/->([\w-]+)\(/)[1];
}
for (let line = doc.lineCount - 1; line >= 0; line--) {
let textLine = doc.lineAt(line).text.trim();
console.log(1);
if (/\}/.test(textLine)) {
this.insertNewMethod(line, methodName);
break;
}
}
}
insertNewMethod(line, methodName) {
let method = `\n\tprotected function ${methodName}()\n`
+ "\t{\n"
+ "\t\t\/\/\n"
+ "\t}\n";
this.activeEditor().edit(edit => {
edit.insert(new vscode.Position(line, 0), method);
});
}
range(line) {
return new vscode.Range(
new vscode.Position(line, 0),
new vscode.Position(line, 0)
);
}
activeEditor() {
return vscode.window.activeTextEditor;
}
activeDocument() {
return this.activeEditor().document;
}
};