forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.ts
More file actions
87 lines (83 loc) · 3.75 KB
/
base.ts
File metadata and controls
87 lines (83 loc) · 3.75 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { injectable, unmanaged } from 'inversify';
import { DiagnosticSeverity } from 'vscode';
import { IWorkspaceService } from '../../common/application/types';
import { IDisposable, IDisposableRegistry, Resource } from '../../common/types';
import { IServiceContainer } from '../../ioc/types';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { DiagnosticCodes } from './constants';
import { DiagnosticScope, IDiagnostic, IDiagnosticFilterService, IDiagnosticsService } from './types';
@injectable()
export abstract class BaseDiagnostic implements IDiagnostic {
constructor(
public readonly code: DiagnosticCodes,
public readonly message: string,
public readonly severity: DiagnosticSeverity,
public readonly scope: DiagnosticScope,
public readonly resource: Resource,
public readonly invokeHandler: 'always' | 'default' = 'default',
public readonly shouldShowPrompt = true
) {}
}
@injectable()
export abstract class BaseDiagnosticsService implements IDiagnosticsService, IDisposable {
protected static handledDiagnosticCodeKeys: string[] = [];
protected readonly filterService: IDiagnosticFilterService;
constructor(
@unmanaged() private readonly supportedDiagnosticCodes: string[],
@unmanaged() protected serviceContainer: IServiceContainer,
@unmanaged() disposableRegistry: IDisposableRegistry,
@unmanaged() public readonly runInBackground: Boolean = false
) {
this.filterService = serviceContainer.get<IDiagnosticFilterService>(IDiagnosticFilterService);
disposableRegistry.push(this);
}
public abstract diagnose(resource: Resource): Promise<IDiagnostic[]>;
public dispose() {
// Nothing to do, but can be overidden
}
public async handle(diagnostics: IDiagnostic[]): Promise<void> {
if (diagnostics.length === 0) {
return;
}
const diagnosticsToHandle = diagnostics.filter((item) => {
if (item.invokeHandler && item.invokeHandler === 'always') {
return true;
}
const key = this.getDiagnosticsKey(item);
if (BaseDiagnosticsService.handledDiagnosticCodeKeys.indexOf(key) !== -1) {
return false;
}
BaseDiagnosticsService.handledDiagnosticCodeKeys.push(key);
return true;
});
await this.onHandle(diagnosticsToHandle);
}
public async canHandle(diagnostic: IDiagnostic): Promise<boolean> {
sendTelemetryEvent(EventName.DIAGNOSTICS_MESSAGE, undefined, { code: diagnostic.code });
return this.supportedDiagnosticCodes.filter((item) => item === diagnostic.code).length > 0;
}
protected abstract onHandle(diagnostics: IDiagnostic[]): Promise<void>;
/**
* Returns a key used to keep track of whether a diagnostic was handled or not.
* So as to prevent handling/displaying messages multiple times for the same diagnostic.
*
* @protected
* @param {IDiagnostic} diagnostic
* @returns {string}
* @memberof BaseDiagnosticsService
*/
protected getDiagnosticsKey(diagnostic: IDiagnostic): string {
if (diagnostic.scope === DiagnosticScope.Global) {
return diagnostic.code;
}
const workspace = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
const workspaceFolder = diagnostic.resource ? workspace.getWorkspaceFolder(diagnostic.resource) : undefined;
return `${diagnostic.code}dbe75733-0407-4124-a1b2-ca769dc30523${
workspaceFolder ? workspaceFolder.uri.fsPath : ''
}`;
}
}