Skip to content

Commit fa2ec9b

Browse files
committed
refactor: set display name on java server side
1 parent 18527a1 commit fa2ec9b

File tree

12 files changed

+40
-39
lines changed

12 files changed

+40
-39
lines changed

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/model/PackageNode.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020
import java.util.Objects;
2121

22+
import org.apache.commons.lang3.StringUtils;
2223
import org.eclipse.core.resources.IFile;
2324
import org.eclipse.core.resources.IFolder;
2425
import org.eclipse.core.resources.IProject;
@@ -273,11 +274,32 @@ public static PackageRootNode createNodeForPackageFragmentRoot(IPackageFragmentR
273274
for (IClasspathAttribute attribute : resolvedClasspathEntry.getExtraAttributes()) {
274275
node.setMetaDataValue(attribute.getName(), attribute.getValue());
275276
}
277+
278+
String computedDisplayName = computeDisplayName(node);
279+
if (StringUtils.isNotBlank(computedDisplayName)) {
280+
node.setDisplayName(computedDisplayName);
281+
}
276282
}
277283

278284
return node;
279285
}
280286

287+
private static String computeDisplayName(PackageRootNode node) {
288+
if (node.getMetaData() == null || node.getMetaData().isEmpty()) {
289+
return node.getName();
290+
}
291+
292+
String version = (String) node.getMetaData().get("maven.version");
293+
String groupId = (String) node.getMetaData().get("maven.groupId");
294+
String artifactId = (String) node.getMetaData().get("maven.artifactId");
295+
296+
if (StringUtils.isBlank(version) || StringUtils.isBlank(groupId) || StringUtils.isBlank(artifactId)) {
297+
return node.getName();
298+
}
299+
300+
return groupId + ":" + artifactId + ":" + version;
301+
}
302+
281303
/**
282304
* Get the correspond node of classpath, it may be container or a package root.
283305
*

src/views/containerNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export class ContainerNode extends DataNode {
4343
return this._containerType;
4444
}
4545

46-
public getLabel(): string {
47-
return this._nodeData.displayName ?? this._nodeData.name;
46+
public isMavenType(): boolean {
47+
return this._containerType == ContainerType.Maven;
4848
}
4949

5050
protected async loadData(): Promise<INodeData[]> {

src/views/dataNode.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export abstract class DataNode extends ExplorerNode {
1717

1818
public getTreeItem(): TreeItem | Promise<TreeItem> {
1919
const item = new TreeItem(
20-
this.getLabel(),
20+
this._nodeData.displayName || this._nodeData.name,
2121
this.hasChildren() ? TreeItemCollapsibleState.Collapsed : TreeItemCollapsibleState.None,
2222
);
2323
item.description = this.description;
@@ -42,6 +42,10 @@ export abstract class DataNode extends ExplorerNode {
4242
return item;
4343
}
4444

45+
public getDisplayName(): string {
46+
return this._nodeData.displayName || this._nodeData.name;
47+
}
48+
4549
public get nodeData(): INodeData {
4650
return this._nodeData;
4751
}

src/views/dependencyDataProvider.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RelativePattern, TreeDataProvider, TreeItem, Uri, window, workspace,
88
} from "vscode";
99
import { instrumentOperationAsVsCodeCommand, sendError } from "vscode-extension-telemetry-wrapper";
10-
import { contextManager } from "../../extension.bundle";
10+
import { ContainerNode, contextManager } from "../../extension.bundle";
1111
import { Commands } from "../commands";
1212
import { Context } from "../constants";
1313
import { appendOutput, executeExportJarTask } from "../tasks/buildArtifact/BuildArtifactTaskProvider";
@@ -124,10 +124,12 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
124124
const children = (!this._rootItems || !element) ?
125125
await this.getRootNodes() : await element.getChildren();
126126

127-
if (children) {
128-
children.sort((a, b) => {
129-
return a.getLabel().localeCompare(b.getLabel());
130-
});
127+
if (children && element instanceof ContainerNode) {
128+
if (element.isMavenType()) {
129+
children.sort((a, b) => {
130+
return a.getDisplayName().localeCompare(b.getDisplayName());
131+
});
132+
}
131133
}
132134

133135
explorerNodeCache.saveNodes(children || []);

src/views/documentSymbolNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class DocumentSymbolNode extends ExplorerNode {
2828
super(parent);
2929
}
3030

31-
public getLabel(): string {
31+
public getDisplayName(): string {
3232
return this.symbolInfo.name;
3333
}
3434

@@ -43,7 +43,7 @@ export class DocumentSymbolNode extends ExplorerNode {
4343
}
4444

4545
public getTreeItem(): TreeItem | Promise<TreeItem> {
46-
const item = new TreeItem(this.getLabel(),
46+
const item = new TreeItem(this.getDisplayName(),
4747
this.symbolInfo?.children?.length ? TreeItemCollapsibleState.Collapsed
4848
: TreeItemCollapsibleState.None);
4949
item.iconPath = this.iconPath;

src/views/explorerNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ export abstract class ExplorerNode {
3434

3535
public abstract computeContextValue(): string | undefined;
3636

37-
public abstract getLabel(): string;
37+
public abstract getDisplayName(): string;
3838
}

src/views/fileNode.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ export class FileNode extends DataNode {
1313
super(nodeData, parent);
1414
}
1515

16-
public getLabel(): string {
17-
return this._nodeData.displayName ?? this._nodeData.name;
18-
}
19-
2016
protected hasChildren(): boolean {
2117
return false;
2218
}

src/views/folderNode.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ export class FolderNode extends DataNode {
1515
super(nodeData, parent);
1616
}
1717

18-
public getLabel(): string {
19-
return this._nodeData.displayName ?? this._nodeData.name;
20-
}
21-
2218
protected async loadData(): Promise<INodeData[]> {
2319
return Jdtls.getPackageData({
2420
kind: NodeKind.Folder,

src/views/packageNode.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ export class PackageNode extends DataNode {
2222
return parentData.entryKind === PackageRootKind.K_SOURCE || parentData.kind === NodeKind.Project;
2323
}
2424

25-
public getLabel(): string {
26-
return this._nodeData.displayName ?? this._nodeData.name;
27-
}
28-
2925
protected async loadData(): Promise<INodeData[]> {
3026
return Jdtls.getPackageData({
3127
kind: NodeKind.Package,

src/views/packageRootNode.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { INodeData, NodeKind } from "../java/nodeData";
88
import { IPackageRootNodeData, PackageRootKind } from "../java/packageRootNodeData";
99
import { Settings } from "../settings";
1010
import { isTest } from "../utility";
11-
import { ContainerNode, ContainerType } from "./containerNode";
11+
import { ContainerNode } from "./containerNode";
1212
import { DataNode } from "./dataNode";
1313
import { ExplorerNode } from "./explorerNode";
1414
import { ProjectNode } from "./projectNode";
@@ -20,13 +20,6 @@ export class PackageRootNode extends DataNode {
2020
super(nodeData, parent);
2121
}
2222

23-
public getLabel(): string {
24-
if (this._nodeData.metaData?.['maven.groupId']) {
25-
return `${this._nodeData.metaData?.['maven.groupId']}:${this._nodeData.metaData?.['maven.artifactId']}:${this._nodeData.metaData?.['maven.version']}`;
26-
}
27-
return this._nodeData.displayName ?? this._nodeData.name;
28-
}
29-
3023
public isSourceRoot(): boolean {
3124
return (<IPackageRootNodeData>this.nodeData).entryKind === PackageRootKind.K_SOURCE;
3225
}

0 commit comments

Comments
 (0)