Skip to content

Commit 991c776

Browse files
fix: use WeakMap to map Diagnostic and UpgradeIssue
1 parent 6c2d33d commit 991c776

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

src/upgrade/display/diagnosticsManager.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,38 @@
22
// Licensed under the MIT license.
33

44
import { Diagnostic, DiagnosticSeverity, type Disposable, languages, Uri, Range } from "vscode";
5-
import type { FileIssues } from "../type";
5+
import type { FileIssues, UpgradeIssue } from "../type";
66
import { Upgrade } from "../../constants";
77
import { buildMessage } from "../utility";
88

99
class DiagnosticsManager implements Disposable {
1010
private diagnostics = languages.createDiagnosticCollection('javaUpgrade');
11+
private diagIssueMap = new WeakMap<Diagnostic, UpgradeIssue>();
1112

1213
dispose() {
1314
this.diagnostics.dispose();
1415
}
1516

16-
refresh(filePath: string, issues: FileIssues) {
17-
this.diagnostics.set(Uri.file(filePath), Object.entries(issues).map(([groupId, issue]) => {
17+
public refresh(filePath: string, issues: FileIssues) {
18+
this.diagnostics.set(Uri.file(filePath), Object.entries(issues).map(([packageId, issue]) => {
1819
const diagnostic = new Diagnostic(
1920
// TODO: locate the actual version settings
2021
new Range(0, 0, 0, 0),
2122
buildMessage(issue),
2223
DiagnosticSeverity.Warning
2324
);
25+
this.diagIssueMap.set(diagnostic, issue);
2426

25-
diagnostic.code = JSON.stringify(issue);
27+
diagnostic.code = packageId;
2628
diagnostic.source = Upgrade.PROMOTION_DIAGNOSTIC_SOURCE;
2729

2830
return diagnostic;
2931
}));
3032
}
33+
34+
public getIssue(diagnostic: Diagnostic) {
35+
return this.diagIssueMap.get(diagnostic);
36+
}
3137
}
3238

3339
const diagnosticsManager = new DiagnosticsManager();

src/upgrade/upgradeCodeActionProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { CancellationToken, CodeAction, CodeActionContext, CodeActionKind, CodeA
55
import { Upgrade } from "../constants";
66
import { Commands } from "../commands";
77
import metadataManager from "./metadataManager";
8-
import { buildFixPrompt, tryParse } from "./utility";
9-
import type { UpgradeIssue } from "./type";
8+
import { buildFixPrompt } from "./utility";
9+
import diagnosticsManager from "./display/diagnosticsManager";
1010

1111
export default class UpgradeCodeActionProvider implements CodeActionProvider {
1212
provideCodeActions(_document: TextDocument, _range: Range | Selection, context: CodeActionContext, _token: CancellationToken): ProviderResult<(CodeAction | Command)[]> {
1313
const actions: CodeAction[] = [];
1414

1515
for (const diagnostic of context.diagnostics) {
1616
if (diagnostic.source === Upgrade.PROMOTION_DIAGNOSTIC_SOURCE) {
17-
const issue = tryParse<UpgradeIssue>(String(diagnostic.code));
17+
const issue = diagnosticsManager.getIssue(diagnostic);
1818
if (!issue) {
1919
return;
2020
}

src/upgrade/utility.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,4 @@ export function buildFixPrompt(issue: UpgradeIssue): string {
3838
return [`Upgrade Java version.`, ...suffix].join(" ");
3939
}
4040
}
41-
}
42-
43-
export function tryParse<T>(x: string): T | undefined {
44-
try {
45-
return JSON.parse(x)
46-
} catch (_) {
47-
return undefined
48-
}
49-
}
41+
}

0 commit comments

Comments
 (0)