-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathextension.js
More file actions
274 lines (227 loc) · 9.24 KB
/
extension.js
File metadata and controls
274 lines (227 loc) · 9.24 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
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
268
269
270
271
272
273
274
// 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');
var Codealike = require('@codealike/codealike-core').Codealike;
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBarItem.command = "codealike.connect";
statusBarItem.show();
// if there is a folder loaded, initialize codealike
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
if (rootPath) {
statusBarItem.text = "Codealike is initializing...";
// initialize plugin for current client and version
Codealike.initialize('vscode', '0.0.27');
Codealike.registerStateSubscriber((state) => {
if (state.isTracking) {
if (state.networkStatus === 'OnLine') {
statusBarItem.text = "Codealike is tracking on-line";
}
else {
statusBarItem.text = "Codealike is tracking off-line";
}
}
else {
statusBarItem.text = "Click here to configure Codealike";
}
});
// try to connect
Codealike.connect()
.then(
() => {
startTrackingProject();
},
() => {
stopTrackingProject();
}
);
}else{
}
}
// 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 "codealike-code" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
var disposable = vscode.commands.registerCommand('codealike.connect', function () {
// The code you place here will be executed every time your command is executed
// else, ask for codealike user token
vscode.window.showInputBox({
prompt: 'Your Codealike API Token',
ignoreFocusOut: true,
placeHolder: "Enter here your Codealike API token, or clean it to disconnect",
value: Codealike.getUserToken()
})
.then(
(token) => {
// if user has pressed esc, do nothing
if (token === undefined)
return;
// set user token configuration
Codealike.setUserToken(token);
// try to connect
if (token) {
Codealike.connect()
.then(
() => {
startTrackingProject();
},
() => {
stopTrackingProject();
}
);
}
else {
stopTrackingProject();
Codealike.disconnect();
}
}
);
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
setTimeout(function() {
Codealike.dispose();
}, 0);
return undefined;
}
exports.deactivate = deactivate;
function stopTrackingProject() {
Codealike.stopTracking();
}
function startTrackingProject() {
if (!vscode.workspace.workspaceFolders)
return;
// start tracking project
Codealike
.configure(vscode.workspace.workspaceFolders[0].uri.fsPath) // get currently active work spaces...
.then(
(configuration) => {
// calculate when workspace started loading
let currentDate = new Date();
// start tracking project
Codealike.startTracking(configuration, currentDate);
},
(error) => {
vscode.window.showErrorMessage(error);
}
);
vscode.debug.onDidStartDebugSession(() => {
Codealike.trackDebuggingState();
});
vscode.debug.onDidTerminateDebugSession(() => {
Codealike.trackCodingState();
});
//Given an input line and results , find the corresponding class and symbol
function _findClassAndMember(results ,line) {
const clsSymbol = {
className: null,
member: null
}
if (!results || !line) {
return clsSymbol;
}
results.forEach(element => {
if (!element) return;
if (!element?.location || !element?.location.range) return;
if (!element?.location.range._start?.line) {
return;
}
const rangeLine = element.location.range._start.line;
if (rangeLine > line) {
return;
}
if (element.kind == vscode.SymbolKind.Class) {
clsSymbol.className = element.name;
}
if (element.kind == vscode.SymbolKind.Method) {
clsSymbol.member = element.name;
}
});
return clsSymbol;
}
vscode.workspace.onDidChangeTextDocument((event) => {
vscode
.commands
.executeCommand('vscode.executeDocumentSymbolProvider', event.document.uri)
.then(function(result) {
if (!event.contentChanges || event.contentChanges.length == 0)
return;
var line = event.contentChanges[0]?.range?.start?.line;
/* OLD CODE COMMENTED AND MOVED TO COMMON FUNCTION
var className = null;
var member = null;
if (result) {
result.forEach(function(element) {
if (!element || element.location.range._start.line > line)
return;
if (element.kind == vscode.SymbolKind.Class) {
className = element.name;
}
if (element.kind == vscode.SymbolKind.Method) {
member = element.name;
}
}, this);
}*/
const clsSymbol = _findClassAndMember(result, line);
let context = {
file: event.document.fileName,
line: line,
className: clsSymbol.className, //className,
member: clsSymbol.member //member
}
Codealike.trackCodingEvent(context);
},
function(error) {
console.warn("Error trying to track coding event", error);
}
);
});
vscode.window.onDidChangeTextEditorSelection((event) => {
// TODO: pending to check events like cursor keys as focus
if (event.kind === 2) {
vscode
.commands
.executeCommand('vscode.executeDocumentSymbolProvider', event.textEditor.document.uri)
.then(function(result) {
if (!event.selections || event.selections.length == 0)
return;
var line = event.selections[0]?.active?.line;
/* OLD CODE COMMENTED AND MOVED TO COMMON FUNCTION
var className = null;
var member = null;
if (result) {
result.forEach(function(element) {
if (!element || element.location.range._start.line > line)
return;
if (element.kind == vscode.SymbolKind.Class) {
className = element.name;
}
if (element.kind == vscode.SymbolKind.Method) {
member = element.name;
}
}, this);
} */
const clsSymbol = _findClassAndMember(result, line);
let context = {
file: event.textEditor.document.fileName,
line: line,
className: clsSymbol.className, //className,
member: clsSymbol.member //member
}
Codealike.trackFocusEvent(context);
},
function(error) {
console.warn("Error trying to track focus event", error);
}
);
}
});
}