forked from poidstotal/stataRun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstataRun.js
168 lines (142 loc) · 5.29 KB
/
stataRun.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
// 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');
let fs = require('fs')
const sendCode = require('./sendCode');
function saveToFile(code) {
if (code) {
var temp = require('os').tmpdir()
var filePath = temp+"/"+ Date.now()
filePath +='.do'
//var filePath="/Volumes/MediaDocs/Codes/stata-run/mydofile.do";
fs.writeFile(filePath, code + "\n", (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
const doFileCommand = 'do '+filePath;
return sendCode.send(doFileCommand);
//vscode.window.showInformationMessage(filePath);
}
else {
return;// Document is empty
}
}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function CheckEditor(editor) {
if (!editor) {
let mgs = 'No Editor is opened. Open a compatible file and try again'
vscode.window.showWarningMessage(mgs);
}
else {
return editor;
}
}
function ShowError() {
let mgs = 'The editor look empty, please adding some stata code or command'
vscode.window.showErrorMessage(mgs);
}
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 "stata-run" is now active!');
let runAll = vscode.commands.registerCommand('stataRun.runAll', function () {
// Run Full file
let editor = CheckEditor(vscode.window.activeTextEditor)
if (editor){
let code = editor.document.getText()
if (code.length > 0){
saveToFile(code)
}
else {
ShowError()
}
}
context.subscriptions.push(runAll);
});
let runSelection = vscode.commands.registerCommand('stataRun.runSelection', function () {
// Run Selection text
let editor = CheckEditor(vscode.window.activeTextEditor)
if (editor){
let selection = editor.selection;
let code = editor.document.getText(selection);
if (code){
saveToFile(code)
}
else {
ShowError()
}
}
context.subscriptions.push(runSelection);
});
let runDown = vscode.commands.registerCommand('stataRun.runDown', function () {
// Run Selection from current line to bottom
let editor = CheckEditor(vscode.window.activeTextEditor)
if (editor){
const position = editor.selection.active.line
const lines = editor.document.lineCount -1
const first = new vscode.Position(position,0)
const lastpos= editor.document.lineAt(lines)
const last = new vscode.Position(lines,lastpos.range.end.character)
if (first != last) {
const range = new vscode.Range(first,last);
var code = editor.document.getText(range);
}
if (code){
saveToFile(code)
}
else {
ShowError()
}
}
context.subscriptions.push(runDown);
});
let runCurrent = vscode.commands.registerCommand('stataRun.runCurrent', function () {
// Run Selection from current line to bottom
let editor = CheckEditor(vscode.window.activeTextEditor)
if (editor){
const position = editor.selection.active.line
const first = new vscode.Position(position,0)
const lastpos= editor.document.lineAt(position)
const last = new vscode.Position(position,lastpos.range.end.character)
if (first != last) {
const range = new vscode.Range(first,last);
var code = editor.document.getText(range);
}
if (code){
saveToFile(code)
}
else {
ShowError()
}
}
context.subscriptions.push(runCurrent);
});
let runFront= vscode.commands.registerCommand('stataRun.runFront', function () {
// Run Selection from current line to bottom
let editor = CheckEditor(vscode.window.activeTextEditor)
if (editor){
const position = editor.selection.active.line
const first = new vscode.Position(0,0)
const lastpos= editor.document.lineAt(position)
const last = new vscode.Position(position,lastpos.range.end.character)
if (first != last) {
const range = new vscode.Range(first,last);
var code = editor.document.getText(range);
}
if (code){
saveToFile(code)
}
else {
ShowError()
}
}
context.subscriptions.push(runFront);
});
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
// this method is called when your extension is deactivated
}
exports.deactivate = deactivate;