Skip to content

Commit 93ee6b3

Browse files
author
Arvid Paeglit
committed
refactored and simplified file watcher
1 parent 35e1a21 commit 93ee6b3

10 files changed

Lines changed: 91 additions & 185 deletions

File tree

src/deepcode/DeepCodeExtension.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { emitter } from '@deepcode/tsc';
44

55
import { ExtensionInterface } from '../interfaces/DeepCodeInterfaces';
66
import DeepCodeLib from './lib/modules/DeepCodeLib';
7+
import createFileWatcher from './lib/watchers/FilesWatcher';
8+
import { ISupportedFiles } from '@deepcode/tsc';
79

810
import {
911
DEEPCODE_START_COMMAND,
@@ -18,7 +20,7 @@ import {
1820
} from './constants/commands';
1921
import { openDeepcodeSettingsCommand, createDCIgnoreCommand } from './utils/vscodeCommandsUtils';
2022
import { errorsLogs } from './messages/errorsServerLogMessages';
21-
import { DEEPCODE_VIEW_SUPPORT, DEEPCODE_VIEW_ANALYSIS } from './constants/views';
23+
import { DEEPCODE_VIEW_SUPPORT, DEEPCODE_VIEW_ANALYSIS, DEEPCODE_ANALYSIS_STATUS } from './constants/views';
2224
import { SupportProvider } from './view/SupportProvider';
2325
import { IssueProvider } from './view/IssueProvider';
2426

@@ -34,6 +36,7 @@ class DeepCodeExtension extends DeepCodeLib implements ExtensionInterface {
3436
}
3537

3638
public activate(context: vscode.ExtensionContext): void {
39+
emitter.on(emitter.events.supportedFilesLoaded, this.onSupportedFilesLoaded.bind(this));
3740
emitter.on(emitter.events.scanFilesProgress, this.onScanFilesProgress.bind(this));
3841
emitter.on(emitter.events.createBundleProgress, this.onCreateBundleProgress.bind(this));
3942
emitter.on(emitter.events.uploadBundleProgress, this.onUploadBundleProgress.bind(this));
@@ -146,6 +149,15 @@ class DeepCodeExtension extends DeepCodeLib implements ExtensionInterface {
146149
public deactivate(): void {
147150
emitter.removeAllListeners();
148151
}
152+
153+
onSupportedFilesLoaded(data: ISupportedFiles | null) {
154+
this.updateStatus(DEEPCODE_ANALYSIS_STATUS.FILTERS, !!data ? 'Loaded extentions' : 'Loading supported extensions');
155+
156+
// Setup file watcher
157+
if (!this.filesWatcher && data) {
158+
this.filesWatcher = createFileWatcher(this, data);
159+
}
160+
}
149161
}
150162

151163
export default DeepCodeExtension;

src/deepcode/constants/views.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const DEEPCODE_MODE_CODES = {
2626
};
2727

2828
export const DEEPCODE_ANALYSIS_STATUS = {
29+
FILTERS: 'LOADING SUPPORTED extentions',
2930
COLLECTING: 'Collecting files',
3031
BUNDLING: 'Creating file bundles',
3132
UPLOADING: 'Uploading files',

src/deepcode/lib/modules/BaseDeepCodeModule.ts

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
errorType,
99
} from "../../../interfaces/DeepCodeInterfaces";
1010
import DeepCodeAnalyzer from "../analyzer/DeepCodeAnalyzer";
11-
import DeepCodeStatusBarItem from "../statusBarItem/DeepCodeStatusBarItem";
12-
import DeepCodeFilesWatcher from "../watchers/DeepCodeFilesWatcher";
11+
import DeepCodeStatusBarItem from '../statusBarItem/DeepCodeStatusBarItem';
1312
import DeepCodeEditorsWatcher from "../watchers/EditorsWatcher";
1413
import DeepCodeSettingsWatcher from "../watchers/DeepCodeSettingsWatcher";
1514
import { PendingTask, PendingTaskInterface } from "../../utils/pendingTask";
@@ -24,31 +23,30 @@ import { IFileBundle } from '@deepcode/tsc';
2423
export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleInterface {
2524
analyzer: AnalyzerInterface;
2625
statusBarItem: StatusBarItemInterface;
27-
filesWatcher: DeepCodeWatcherInterface;
26+
filesWatcher: vscode.FileSystemWatcher;
2827
editorsWatcher: DeepCodeWatcherInterface;
2928
settingsWatcher: DeepCodeWatcherInterface;
3029

3130
// Views and analysis progress
3231
refreshViewEmitter: vscode.EventEmitter<any>;
33-
analysisStatus = '';
32+
analysisStatus = '';
3433
analysisProgress = '';
3534
private initializedView: PendingTaskInterface;
3635
private progressBadge: PendingTaskInterface | undefined;
3736
private shouldShowProgressBadge = false;
38-
private viewContext: {[key: string]: unknown};
37+
private viewContext: { [key: string]: unknown };
3938

4039
remoteBundle: IFileBundle;
40+
changedFiles: Set<string> = new Set();
4141

4242
// These attributes are used in tests
4343
staticToken = '';
44-
staticBaseURL = '';
4544
defaultBaseURL = 'https://www.deepcode.ai';
4645
staticUploadApproved = false;
4746

4847
constructor() {
4948
this.analyzer = new DeepCodeAnalyzer();
5049
this.statusBarItem = new DeepCodeStatusBarItem();
51-
this.filesWatcher = new DeepCodeFilesWatcher();
5250
this.editorsWatcher = new DeepCodeEditorsWatcher();
5351
this.settingsWatcher = new DeepCodeSettingsWatcher();
5452
this.refreshViewEmitter = new vscode.EventEmitter<any>();
@@ -60,7 +58,7 @@ export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleIn
6058

6159
get baseURL(): string {
6260
// @ts-ignore */}
63-
return this.staticBaseURL || vscode.workspace.getConfiguration('deepcode').get('url') || this.defaultBaseURL;
61+
return vscode.workspace.getConfiguration('deepcode').get('url') || this.defaultBaseURL;
6462
}
6563

6664
get termsConditionsUrl(): string {
@@ -72,7 +70,7 @@ export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleIn
7270
return this.staticToken || vscode.workspace.getConfiguration('deepcode').get('token');
7371
}
7472

75-
async setToken(token: string): Promise<void> {
73+
async setToken(token: string): Promise<void> {
7674
this.staticToken = '';
7775
await vscode.workspace.getConfiguration('deepcode').update('token', token, true);
7876
}
@@ -82,7 +80,11 @@ export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleIn
8280
}
8381

8482
get uploadApproved(): boolean {
85-
return this.staticUploadApproved || this.source !== IDE_NAME || !!(vscode.workspace.getConfiguration('deepcode').get<boolean>('uploadApproved'));
83+
return (
84+
this.staticUploadApproved ||
85+
this.source !== IDE_NAME ||
86+
!!vscode.workspace.getConfiguration('deepcode').get<boolean>('uploadApproved')
87+
);
8688
}
8789

8890
async setUploadApproved(value = true): Promise<void> {
@@ -110,7 +112,7 @@ export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleIn
110112
}
111113

112114
async setContext(key: string, value: unknown): Promise<void> {
113-
console.log("DeepCode context", key, value);
115+
console.log('DeepCode context', key, value);
114116
const oldValue = this.viewContext[key];
115117
this.viewContext[key] = value;
116118
await setContext(key, value);
@@ -119,10 +121,10 @@ export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleIn
119121
}
120122

121123
private trackContextChange(key: string, value: unknown, oldValue: unknown) {
122-
let event = "";
124+
let event = '';
123125
let shouldWaitForView = true;
124-
let options: Record<string,any> | undefined;
125-
switch(key) {
126+
let options: Record<string, any> | undefined;
127+
switch (key) {
126128
case DEEPCODE_CONTEXT.LOGGEDIN: {
127129
if (oldValue !== undefined) {
128130
if (!value && oldValue) event = TELEMETRY_EVENTS.viewLoginView;
@@ -157,21 +159,15 @@ export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleIn
157159
}
158160
// We want to fire the event only when the user actually sees the View
159161
if (event) {
160-
if (shouldWaitForView) this.initializedView.waiter.then(
161-
() => this.processEvent(event, options)
162-
);
162+
if (shouldWaitForView) this.initializedView.waiter.then(() => this.processEvent(event, options));
163163
else this.processEvent(event, options);
164164
}
165165
}
166166

167167
get shouldShowAnalysis(): boolean {
168-
return !this.viewContext[
169-
DEEPCODE_CONTEXT.ERROR
170-
] && [
171-
DEEPCODE_CONTEXT.LOGGEDIN,
172-
DEEPCODE_CONTEXT.APPROVED,
173-
].every(
174-
(c) => !!this.viewContext[c]
168+
return (
169+
!this.viewContext[DEEPCODE_CONTEXT.ERROR] &&
170+
[DEEPCODE_CONTEXT.LOGGEDIN, DEEPCODE_CONTEXT.APPROVED].every(c => !!this.viewContext[c])
175171
);
176172
}
177173

@@ -189,17 +185,19 @@ export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleIn
189185
if (value) {
190186
// Using closure on this to allow partial binding in arbitrary positions
191187
const self = this;
192-
this.initializedView.waiter.then(
193-
() => vscode.window.withProgress(
194-
{ location: { viewId: DEEPCODE_VIEW_ANALYSIS } },
195-
() => self.getProgressBadgePromise()
188+
this.initializedView.waiter
189+
.then(() =>
190+
vscode.window.withProgress({ location: { viewId: DEEPCODE_VIEW_ANALYSIS } }, () =>
191+
self.getProgressBadgePromise(),
192+
),
196193
)
197-
).then(
198-
() => {},
199-
(error) => self.processError(error, {
200-
message: errorsLogs.loadingBadge,
201-
})
202-
);
194+
.then(
195+
() => {},
196+
error =>
197+
self.processError(error, {
198+
message: errorsLogs.loadingBadge,
199+
}),
200+
);
203201
} else {
204202
if (this.progressBadge && !this.progressBadge.isCompleted) {
205203
this.progressBadge.complete();
@@ -216,18 +214,12 @@ export default abstract class BaseDeepCodeModule implements BaseDeepCodeModuleIn
216214
refreshViews = _.throttle(
217215
(content?: any): void => this.refreshViewEmitter.fire(content || undefined),
218216
REFRESH_VIEW_DEBOUNCE_INTERVAL,
219-
{ 'leading': true }
217+
{ leading: true },
220218
);
221219

222-
abstract processError(
223-
error: errorType,
224-
options?: { [key: string]: any }
225-
): Promise<void>;
220+
abstract processError(error: errorType, options?: { [key: string]: any }): Promise<void>;
226221

227-
abstract processEvent(
228-
event: string,
229-
options?: { [key: string]: any }
230-
): Promise<void>;
222+
abstract processEvent(event: string, options?: { [key: string]: any }): Promise<void>;
231223

232224
abstract startExtension(): Promise<void>;
233225
}

src/deepcode/lib/modules/BundlesModule.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import { errorsLogs } from "../../messages/errorsServerLogMessages";
99

1010
import { analyzeFolders } from '@deepcode/tsc';
1111

12-
abstract class BundlesModule extends LoginModule
13-
implements BundlesModuleInterface {
14-
12+
abstract class BundlesModule extends LoginModule implements BundlesModuleInterface {
1513
runningAnalysis = false;
1614

1715
files: string[] = [];
@@ -37,9 +35,9 @@ abstract class BundlesModule extends LoginModule
3735
this.updateStatus(DEEPCODE_ANALYSIS_STATUS.UPLOADING, `${processed}/${total}`);
3836
}
3937

40-
onAnalyseProgress(data: { status: string, progress: number }) {
38+
onAnalyseProgress(data: { status: string; progress: number }) {
4139
// console.log(`ANALYSE PROGRESS - ${data.progress}`);
42-
this.updateStatus(DEEPCODE_ANALYSIS_STATUS.ANALYZING, `${Math.round(100*data.progress)}%`);
40+
this.updateStatus(DEEPCODE_ANALYSIS_STATUS.ANALYZING, `${Math.round(100 * data.progress)}%`);
4341
}
4442

4543
onError(error: Error) {
@@ -60,16 +58,7 @@ abstract class BundlesModule extends LoginModule
6058
if (this.runningAnalysis) return;
6159
this.runningAnalysis = true;
6260

63-
// this.filesWatcher.activate(this);
64-
65-
// const removedFiles = (this.files || []).filter(f => !bundle.includes(f));
66-
67-
const result = await analyzeFolders(this.baseURL, this.token, false, 1, paths).catch(
68-
// no need to wait for processError since catch is called asynchronously as well
69-
(error) => this.processError(error, {
70-
message: errorsLogs.analyse
71-
})
72-
);
61+
const result = await analyzeFolders(this.baseURL, this.token, false, 1, paths);
7362

7463
if (result) {
7564
this.remoteBundle = result;
@@ -83,7 +72,7 @@ abstract class BundlesModule extends LoginModule
8372
} else {
8473
await this.setContext(DEEPCODE_CONTEXT.WORKSPACE_FOUND, false);
8574
}
86-
} catch(err) {
75+
} catch (err) {
8776
await this.processError(err, {
8877
message: errorsLogs.failedAnalysis,
8978
});

src/deepcode/lib/modules/DeepCodeLib.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as _ from "lodash";
22
import { DeepCodeLibInterface } from "../../../interfaces/DeepCodeInterfaces";
33
import BundlesModule from "./BundlesModule";
4-
import { DEEPCODE_CONTEXT, DEEPCODE_MODE_CODES } from "../../constants/views";
4+
import { DEEPCODE_CONTEXT, DEEPCODE_MODE_CODES } from '../../constants/views';
55
import { errorsLogs } from "../../messages/errorsServerLogMessages";
66
import {
77
EXECUTION_DEBOUNCE_INTERVAL,
@@ -46,7 +46,6 @@ export default class DeepCodeLib extends BundlesModule implements DeepCodeLibInt
4646
}
4747
await this.setContext(DEEPCODE_CONTEXT.LOGGEDIN, true);
4848

49-
5049
const uploadApproved = await this.checkApproval();
5150
if (!uploadApproved) {
5251
return;
@@ -59,9 +58,8 @@ export default class DeepCodeLib extends BundlesModule implements DeepCodeLibInt
5958
this.resetTransientErrors();
6059
await this.setLoadingBadge(false);
6160
await this.startAnalysis();
62-
6361
} catch (err) {
64-
console.log('failed debounced function')
62+
console.log('failed debounced function');
6563
await this.processError(err, {
6664
message: errorsLogs.failedExecutionDebounce,
6765
});
@@ -70,11 +68,7 @@ export default class DeepCodeLib extends BundlesModule implements DeepCodeLibInt
7068

7169
// This function is called by commands, error handlers, etc.
7270
// We should avoid having duplicate parallel executions.
73-
public startExtension = _.debounce(
74-
this.startExtension_.bind(this),
75-
EXECUTION_DEBOUNCE_INTERVAL,
76-
{ leading: true },
77-
);
71+
public startExtension = _.debounce(this.startExtension_.bind(this), EXECUTION_DEBOUNCE_INTERVAL, { leading: true });
7872

7973
async setMode(mode: string): Promise<void> {
8074
if (!Object.values(DEEPCODE_MODE_CODES).includes(mode)) return;

0 commit comments

Comments
 (0)