-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathInspectActionCodeLensProvider.ts
More file actions
44 lines (38 loc) · 2.04 KB
/
Copy pathInspectActionCodeLensProvider.ts
File metadata and controls
44 lines (38 loc) · 2.04 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
import { CodeLens, CodeLensProvider, Event, EventEmitter, ExtensionContext, TextDocument, Uri, languages } from "vscode";
import { getTopLevelClassesOfDocument, logger } from "../utils";
import { COMMAND_IGNORE_INSPECTIONS, COMMAND_INSPECT_CLASS } from "./commands";
import InspectionCache from "./InspectionCache";
export class InspectActionCodeLensProvider implements CodeLensProvider {
private inspectCodeLenses: Map<Uri, CodeLens[]> = new Map();
private emitter: EventEmitter<void> = new EventEmitter<void>();
public readonly onDidChangeCodeLenses: Event<void> = this.emitter.event;
public install(context: ExtensionContext): InspectActionCodeLensProvider {
logger.debug('[InspectCodeLensProvider] install...');
context.subscriptions.push(
languages.registerCodeLensProvider({ language: 'java' }, this)
);
return this;
}
public async rerender(document: TextDocument) {
if (document.languageId !== 'java') return;
logger.debug('[InspectCodeLensProvider] rerender inspect codelenses...');
const topLevelCodeLenses: CodeLens[] = [];
const classes = await getTopLevelClassesOfDocument(document);
classes.map(clazz => new CodeLens(clazz.range, {
title: "✨ Rewrite with new Java syntax",
command: COMMAND_INSPECT_CLASS,
arguments: [document, clazz]
})).forEach(codeLens => topLevelCodeLenses.push(codeLens));
const results = await Promise.all(classes.map(clazz => InspectionCache.hasCache(document, clazz)));
classes.filter((_, i) => results[i]).map(clazz => new CodeLens(clazz.range, {
title: "Ignore all",
command: COMMAND_IGNORE_INSPECTIONS,
arguments: [document, clazz]
})).forEach(codeLens => topLevelCodeLenses.push(codeLens));
this.inspectCodeLenses.set(document.uri, topLevelCodeLenses);
this.emitter.fire();
}
public provideCodeLenses(document: TextDocument): CodeLens[] {
return this.inspectCodeLenses.get(document.uri) ?? [];
}
}