Skip to content

Commit abfd8c7

Browse files
feat: add correct Range in pom.xml when possible
1 parent 42c9e2e commit abfd8c7

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

src/upgrade/display/diagnosticsManager.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import { Diagnostic, DiagnosticSeverity, type Disposable, languages, Uri, Range } from "vscode";
55
import type { FileIssues } from "../type";
66
import { Upgrade } from "../../constants";
7-
import { buildMessage } from "../utility";
7+
import { buildMessage, normalizePath } from "../utility";
8+
import pomDataManager from "../pomDataManager";
89

910
class DiagnosticsManager implements Disposable {
1011
private diagnostics = languages.createDiagnosticCollection('javaUpgrade');
@@ -15,9 +16,9 @@ class DiagnosticsManager implements Disposable {
1516

1617
public refresh(filePath: string, issues: FileIssues) {
1718
this.diagnostics.set(Uri.parse(filePath), Object.entries(issues).map(([packageId, issue]) => {
19+
const range = pomDataManager.getPomRange(normalizePath(filePath), issue.packageId);
1820
const diagnostic = new Diagnostic(
19-
// TODO: locate the actual version settings
20-
new Range(0, 0, 0, 0),
21+
range ?? new Range(0, 0, 0, 0),
2122
buildMessage(issue),
2223
DiagnosticSeverity.Warning
2324
);

src/upgrade/metadataManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MetadataManager {
5757
return {
5858
name: Upgrade.DIAGNOSTICS_NAME_FOR_JAVA_ENGINE,
5959
supportedVersion: `>=${Upgrade.EARLIEST_JAVA_VERSION_NOT_TO_PROMPT}`,
60-
packageRuleUsed: Upgrade.DIAGNOSTICS_GROUP_ID_FOR_JAVA_ENGINE,
60+
packageRuleUsed: buildPackageId(Upgrade.DIAGNOSTICS_GROUP_ID_FOR_JAVA_ENGINE, "*"),
6161
};
6262
}
6363

src/upgrade/upgradeCodeLensProvider.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,57 @@
33

44
import { CancellationToken, CodeLens, CodeLensProvider, ProviderResult, Range, TextDocument } from "vscode";
55
import { Commands } from "../commands";
6-
import { buildFixPrompt } from "./utility";
6+
import { buildFixPrompt, normalizePath, toFirstLine } from "./utility";
77
import issueManager from "./issueManager";
88
import metadataManager from "./metadataManager";
99
import { Upgrade } from "../constants";
10+
import pomDataManager from "./pomDataManager";
1011

1112
export default class UpgradeCodeLensProvider implements CodeLensProvider {
1213
provideCodeLenses(document: TextDocument, _token: CancellationToken): ProviderResult<CodeLens[]> {
1314
const documentPath = document.uri.toString();
1415
const issues = issueManager.getIssues(documentPath);
15-
return Object.values(issues).map((issue) => {
16+
const topOfFileCodeLens: CodeLens[] = [];
17+
const inlineCodeLens: CodeLens[] = [];
18+
Object.values(issues).forEach((issue) => {
1619
const metadata = metadataManager.getMetadataById(issue.packageId);
1720
if (!metadata) {
1821
return;
1922
}
20-
const codeLens = new CodeLens(new Range(0, 0, 0, 0), {
21-
title: "Upgrade",
22-
command: Commands.VIEW_TRIGGER_JAVA_UPGRADE_TOOL,
23-
tooltip: `Upgrade ${metadata.name} with GitHub Copilot`,
24-
arguments: [buildFixPrompt(issue)],
25-
});
23+
const range = pomDataManager.getPomRange(normalizePath(documentPath), issue.packageId);
24+
if (range) {
25+
const codeLens = new CodeLens(
26+
toFirstLine(range),
27+
{
28+
title: "Upgrade",
29+
command: Commands.VIEW_TRIGGER_JAVA_UPGRADE_TOOL,
30+
tooltip: `Upgrade ${metadata.name} with GitHub Copilot`,
31+
arguments: [buildFixPrompt(issue)],
32+
});
33+
inlineCodeLens.push(codeLens);
34+
} else {
35+
const codeLens = new CodeLens(
36+
new Range(0, 0, 0, 0),
37+
{
38+
title: "Upgrade",
39+
command: Commands.VIEW_TRIGGER_JAVA_UPGRADE_TOOL,
40+
tooltip: `Upgrade ${metadata.name} with GitHub Copilot`,
41+
arguments: [buildFixPrompt(issue)],
42+
});
43+
topOfFileCodeLens.push(codeLens);
44+
}
45+
});
2646

27-
return codeLens;
28-
})
29-
.filter((x): x is CodeLens => Boolean(x))
30-
.sort((a, b) => {
31-
// always show Java engine upgrade first
47+
return [
48+
...topOfFileCodeLens.sort((a, b) => {
49+
// always show Java engine upgrade first, if any
3250
if (a.command?.title === `Upgrade ${Upgrade.DIAGNOSTICS_NAME_FOR_JAVA_ENGINE}`) return -1;
3351
if (b.command?.title === `Upgrade ${Upgrade.DIAGNOSTICS_NAME_FOR_JAVA_ENGINE}`) return 1;
3452
return (a.command?.title ?? "") < (b.command?.title ?? "") ? -1 : 1;
3553
})
36-
.slice(0, 1); // give 1 Code Lens action at most
54+
// give 1 top-of-file Code Lens action at most
55+
.slice(0, 1),
56+
...inlineCodeLens,
57+
];
3758
}
3859
}

src/upgrade/upgradeManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import metadataManager from "./metadataManager";
1515
import UpgradeCodeActionProvider from "./upgradeCodeActionProvider";
1616
import UpgradeCodeLensProvider from "./upgradeCodeLensProvider";
1717
import { buildPackageId } from "./utility";
18+
import pomDataManager from "./pomDataManager";
1819

1920
const DEFAULT_UPGRADE_PROMPT = "Upgrade Java project dependency";
2021

@@ -72,6 +73,7 @@ class UpgradeManager {
7273
if (!pomPath) {
7374
return;
7475
}
76+
await pomDataManager.parsePom(pomPath);
7577
this.checkJavaVersion(project, pomPath);
7678
const packageData = await Jdtls.getPackageData({ kind: NodeKind.Project, projectUri: project.uri });
7779
packageData

src/upgrade/utility.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import { Uri } from "vscode";
5-
import { UpgradeIssue, UpgradeReason } from "./type";
4+
import { Range, Uri } from "vscode";
5+
import { type UpgradeIssue, UpgradeReason } from "./type";
66

77
export function buildMessage(issue: UpgradeIssue): string {
88
const { packageId, packageDisplayName, currentVersion, reason } = issue;
@@ -48,4 +48,8 @@ export function buildPackageId(groupId: string, artifactId: string): string {
4848

4949
export function normalizePath(path: string): string {
5050
return Uri.parse(path).toString();
51+
}
52+
53+
export function toFirstLine(range: Range): Range {
54+
return new Range(range.start.line, 0, range.start.line, 0);
5155
}

0 commit comments

Comments
 (0)