-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathextension.js
More file actions
284 lines (242 loc) · 11.2 KB
/
extension.js
File metadata and controls
284 lines (242 loc) · 11.2 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
275
276
277
278
279
280
281
282
283
284
//@ts-check
/// <reference path="./lib/index.d.ts" />
const vscode = require('vscode');
const ext = require('./lib/VSCodeHelper');
const uploader = require('./lib/Uploader');
const log = require('./lib/Log');
const statusBar = require('./lib/StatusBarManager');
const localServer = require('./lib/LocalServer');
const uploadObject = require('./lib/UploadObject');
const { isDebugMode } = require('./lib/Constants');
const { getProxyConfiguration } = require('./lib/GetProxyConfiguration');
const { generateDiagnoseLogFile } = require('./lib/EnvironmentProbe');
/** How many ms in 1s */
const SECOND = 1000;
/** shortest time to record coding. 5000 means: All coding record divide by 5000 */
const CODING_SHORTEST_UNIT_MS = 5 * SECOND;
/** at least time to upload a watching(open) record */
const AT_LEAST_WATCHING_TIME = 5 * SECOND;
/**
* means if you are not intently watching time is more than this number
* the watching track will not be continuously but a new record
*/
const MAX_ALLOW_NOT_INTENTLY_MS = 60 * SECOND;
/** if you have time below not coding(pressing your keyboard), the coding track record will be upload and re-track */
const MAX_CODING_WAIT_TIME = 30 * SECOND;
/** If there a event onFileCoding with scheme in here, just ignore this event */
const INVALID_CODING_DOCUMENT_SCHEMES = [
//there are will be a `onDidChangeTextDocument` with document scheme `git-index`
//be emitted when you switch document, so ignore it
'git-index',
//since 1.9.0 vscode changed `git-index` to `git`, OK, they are refactoring around source control
//see more: https://code.visualstudio.com/updates/v1_9#_contributable-scm-providers
'git',
//when you just look up output channel content, there will be a `onDidChangeTextDocument` be emitted
'output',
//This is a edit event emit from you debug console input box
'input',
//This scheme is appeared in vscode global replace diff preview editor
'private',
//This scheme is used for markdown preview document
//It will appear when you edit a markdown with aside preview
'markdown'
];
const EMPTY = { document: null, textEditor: null };
/** more thinking time from user configuration */
let moreThinkingTime = 0;
/** current active document*/
let activeDocument;
/** Tracking data, record document open time, first coding time and last coding time and coding time long */
let trackData = {
openTime: 0,
lastIntentlyTime: 0,
firstCodingTime: 0,
codingLong: 0,
lastCodingTime: 0
};
let resetTrackOpenAndIntentlyTime = (now) => { trackData.openTime = trackData.lastIntentlyTime = now };
/**
* Uploading open track data
* @param {number} now
*/
function uploadOpenTrackData(now) {
//If active document is not a ignore document
if (!isIgnoreDocument(activeDocument)) {
let longest = trackData.lastIntentlyTime + MAX_ALLOW_NOT_INTENTLY_MS + moreThinkingTime,
long = Math.min(now, longest) - trackData.openTime;
uploadObject.generateOpen(activeDocument, trackData.openTime, long)
.then(uploader.upload);
}
resetTrackOpenAndIntentlyTime(now);
}
/** Uploading coding track data and retracking coding track data */
function uploadCodingTrackData() {
//If active document is not a ignore document
if (!isIgnoreDocument(activeDocument)) {
uploadObject.generateCode(activeDocument, trackData.firstCodingTime, trackData.codingLong)
.then(uploader.upload);
}
//Re-tracking coding track data
trackData.codingLong =
trackData.lastCodingTime =
trackData.firstCodingTime = 0;
}
/** Check a TextDocument, Is it a ignore document(null/'inmemory') */
function isIgnoreDocument(doc) {
return !doc || doc.uri.scheme == 'inmemory';
}
/** Handler VSCode Event */
let EventHandler = {
/** @param {vscode.TextEditor} doc */
onIntentlyWatchingCodes: (textEditor) => {
// if (isDebugMode)
// log.debug('watching intently: ' + ext.dumpEditor(textEditor));
if (!textEditor || !textEditor.document)
return;//Empty document
let now = Date.now();
//Long time have not intently watching document
if (now > trackData.lastIntentlyTime + MAX_ALLOW_NOT_INTENTLY_MS + moreThinkingTime) {
uploadOpenTrackData(now);
//uploadOpenTrackDate has same expression as below:
//resetTrackOpenAndIntentlyTime(now);
} else {
trackData.lastIntentlyTime = now;
}
},
/** @param {vscode.TextDocument} doc */
onActiveFileChange: (doc) => {
// if(isDebugMode)
// log.debug('active file change: ' + ext.dumpDocument(doc));
let now = Date.now();
// If there is a TextEditor opened before changed, should upload the track data
if (activeDocument) {
//At least open 5 seconds
if (trackData.openTime < now - AT_LEAST_WATCHING_TIME) {
uploadOpenTrackData(now);
}
//At least coding 1 second
if (trackData.codingLong) {
uploadCodingTrackData();
}
}
activeDocument = ext.cloneTextDocument(doc);
//Retracking file open time again (Prevent has not retracked open time when upload open tracking data has been called)
resetTrackOpenAndIntentlyTime(now);
trackData.codingLong = trackData.lastCodingTime = trackData.firstCodingTime = 0;
},
/** @param {vscode.TextDocument} doc */
onFileCoding: (doc) => {
// onFileCoding is an alias of event `onDidChangeTextDocument`
//
// Here is description of this event excerpt from vscode extension docs page.
// (Link: https://code.visualstudio.com/docs/extensionAPI/vscode-api)
// ```
// An event that is emitted when a text document is changed.
// This usually happens when the contents changes but also when other things like the dirty - state changes.
// ```
// if(isDebugMode)
// log.debug('coding: ' + ext.dumpDocument(doc));
// vscode bug:
// Event `onDidChangeActiveTextEditor` be emitted with empty document when you open "Settings" editor.
// Then Event `onDidChangeTextDocument` be emitted even if you has not edited anything in setting document.
// I ignore empty activeDocument to keeping tracker up and avoiding exception like follow:
// TypeError: Cannot set property 'lineCount' of null // activeDocument.lineCount = ...
if (!activeDocument)
return ;
// Ignore the invalid coding file schemes
if (!doc || INVALID_CODING_DOCUMENT_SCHEMES.indexOf(doc.uri.scheme) >= 0 )
return;
if (isDebugMode) {
// fragment in this if condition is for catching unknown document scheme
let { uri } = doc, { scheme } = uri;
if (scheme != 'file' &&
scheme != 'untitled' &&
scheme != 'debug' &&
//scheme in vscode user settings (or quick search bar in user settings)
// vscode://defaultsettings/{0...N}/settings.json
scheme != 'vscode' &&
//scheme in vscode ineractive playground
scheme != 'walkThroughSnippet') {
// vscode.window.showInformationMessage(`Unknown uri scheme(details in console): ${scheme}: ${uri.toString()}`);
log.debug(ext.dumpDocument(doc));
}
}
let now = Date.now();
//If time is too short to calling this function then just ignore it
if (now - CODING_SHORTEST_UNIT_MS < trackData.lastCodingTime)
return;
// Update document line count
activeDocument.lineCount = doc.lineCount;
//If is first time coding in this file, record time
if (!trackData.firstCodingTime)
trackData.firstCodingTime = now;
//If too long time to recoding, so upload old coding track and retracking
else if (trackData.lastCodingTime < now - MAX_CODING_WAIT_TIME - moreThinkingTime) {//30s
uploadCodingTrackData()
//Reset first coding time
trackData.firstCodingTime = now;
}
trackData.codingLong += CODING_SHORTEST_UNIT_MS;
trackData.lastCodingTime = now;
}
}
/** when extension launch or vscode config change */
function updateConfigurations() {
//CodingTracker Configuration
const extensionCfg = ext.getConfig('codingTracker');
const uploadToken = String(extensionCfg.get('uploadToken'));
const computerId = String(extensionCfg.get('computerId'));
const enableStatusBar = extensionCfg.get('showStatus');
let mtt = parseInt(extensionCfg.get('moreThinkingTime'));
let uploadURL = String(extensionCfg.get('serverURL'));
const httpCfg = ext.getConfig('http');
const baseHttpProxy = httpCfg ? httpCfg.get('proxy') : undefined;
const overwriteHttpProxy = extensionCfg.get('proxy');
const proxy = getProxyConfiguration(baseHttpProxy, overwriteHttpProxy);
// fixed wrong more thinking time configuration value
if (isNaN(mtt)) mtt = 0;
if (mtt < -15 * SECOND) mtt = -15 * SECOND;
moreThinkingTime = mtt;
uploadURL = (uploadURL.endsWith('/') ? uploadURL : (uploadURL + '/')) + 'ajax/upload';
uploader.set(uploadURL, uploadToken, proxy);
uploadObject.init(computerId || `unknown-${require('os').platform()}`);
localServer.updateConfig();
statusBar.init(enableStatusBar);
}
function activate(context) {
generateDiagnoseLogFile();
//Declare for add disposable inside easy
let subscriptions = context.subscriptions;
uploadObject.init();
//Initialize local server(launch local server if localServer config is true)
localServer.init(context);
//Initialize Uploader Module
uploader.init();
//Update configurations first time
updateConfigurations();
//Listening workspace configurations change
vscode.workspace.onDidChangeConfiguration(updateConfigurations);
//Tracking the file display when vscode open
EventHandler.onActiveFileChange( (vscode.window.activeTextEditor || EMPTY).document);
//Listening vscode event to record coding activity
subscriptions.push(vscode.workspace.onDidChangeTextDocument(e => EventHandler.onFileCoding( (e || EMPTY).document) ));
subscriptions.push(vscode.window.onDidChangeActiveTextEditor(e => EventHandler.onActiveFileChange((e || EMPTY).document ) ));
//the below event happen means you change the cursor in the document.
//So It means you are watching so intently in some ways
subscriptions.push(vscode.window.onDidChangeTextEditorSelection(e => EventHandler.onIntentlyWatchingCodes((e || EMPTY).textEditor) ));
// Maybe I will add "onDidChangeVisibleTextEditors" in extension in next version
// For detect more detailed editor information
// But this event will always include debug-input box if you open debug panel one time
// subscriptions.push(vscode.window.onDidChangeVisibleTextEditors(e => log.debug('onDidChangeVisibleTextEditors', e)))
// debug command
// subscriptions.push(vscode.commands.registerCommand('codingTracker.dumpVCSQueue', () => {
// log.debug(require('./lib/vcs/Git')._getVCSInfoQueue);
// }));
}
function deactivate() {
EventHandler.onActiveFileChange(null);
localServer.dispose();
log.end();
}
exports.activate = activate;
exports.deactivate = deactivate;