From f2923ef940da0976fb7f10e5236a8db49e82fd59 Mon Sep 17 00:00:00 2001 From: Marko Milic Date: Sun, 27 Oct 2024 12:59:17 +0100 Subject: [PATCH] refactor: set display name on java server side --- .../jdtls/ext/core/model/PackageNode.java | 22 +++++++++++++++++++ src/views/dataNode.ts | 6 ++++- src/views/dependencyDataProvider.ts | 12 +++++----- src/views/documentSymbolNode.ts | 4 ++-- src/views/explorerNode.ts | 2 +- src/views/fileNode.ts | 4 ---- src/views/folderNode.ts | 4 ---- src/views/packageNode.ts | 4 ---- src/views/packageRootNode.ts | 9 +------- src/views/projectNode.ts | 4 ---- src/views/workspaceNode.ts | 4 ---- 11 files changed, 38 insertions(+), 37 deletions(-) diff --git a/jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/model/PackageNode.java b/jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/model/PackageNode.java index 7f146d8d..92fa4cf7 100644 --- a/jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/model/PackageNode.java +++ b/jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/model/PackageNode.java @@ -19,6 +19,7 @@ import java.util.Map; import java.util.Objects; +import org.apache.commons.lang3.StringUtils; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; @@ -273,11 +274,32 @@ public static PackageRootNode createNodeForPackageFragmentRoot(IPackageFragmentR for (IClasspathAttribute attribute : resolvedClasspathEntry.getExtraAttributes()) { node.setMetaDataValue(attribute.getName(), attribute.getValue()); } + + String computedDisplayName = computeDisplayName(node); + if (StringUtils.isNotBlank(computedDisplayName)) { + node.setDisplayName(computedDisplayName); + } } return node; } + private static String computeDisplayName(PackageRootNode node) { + if (node.getMetaData() == null || node.getMetaData().isEmpty()) { + return node.getName(); + } + + String version = (String) node.getMetaData().get("maven.version"); + String groupId = (String) node.getMetaData().get("maven.groupId"); + String artifactId = (String) node.getMetaData().get("maven.artifactId"); + + if (StringUtils.isBlank(version) || StringUtils.isBlank(groupId) || StringUtils.isBlank(artifactId)) { + return node.getName(); + } + + return groupId + ":" + artifactId + ":" + version; + } + /** * Get the correspond node of classpath, it may be container or a package root. * diff --git a/src/views/dataNode.ts b/src/views/dataNode.ts index e0292a0e..200abf13 100644 --- a/src/views/dataNode.ts +++ b/src/views/dataNode.ts @@ -17,7 +17,7 @@ export abstract class DataNode extends ExplorerNode { public getTreeItem(): TreeItem | Promise { const item = new TreeItem( - this.getLabel(), + this._nodeData.displayName || this._nodeData.name, this.hasChildren() ? TreeItemCollapsibleState.Collapsed : TreeItemCollapsibleState.None, ); item.description = this.description; @@ -42,6 +42,10 @@ export abstract class DataNode extends ExplorerNode { return item; } + public getDisplayName(): string { + return this._nodeData.displayName || this._nodeData.name; + } + public get nodeData(): INodeData { return this._nodeData; } diff --git a/src/views/dependencyDataProvider.ts b/src/views/dependencyDataProvider.ts index 9cfe445d..766dd684 100644 --- a/src/views/dependencyDataProvider.ts +++ b/src/views/dependencyDataProvider.ts @@ -7,7 +7,7 @@ import { RelativePattern, TreeDataProvider, TreeItem, Uri, window, workspace, } from "vscode"; import { instrumentOperationAsVsCodeCommand, sendError } from "vscode-extension-telemetry-wrapper"; -import { contextManager } from "../../extension.bundle"; +import { ContainerNode, contextManager } from "../../extension.bundle"; import { Commands } from "../commands"; import { Context } from "../constants"; import { appendOutput, executeExportJarTask } from "../tasks/buildArtifact/BuildArtifactTaskProvider"; @@ -124,10 +124,12 @@ export class DependencyDataProvider implements TreeDataProvider { const children = (!this._rootItems || !element) ? await this.getRootNodes() : await element.getChildren(); - if (children) { - children.sort((a, b) => { - return a.getLabel().localeCompare(b.getLabel()); - }); + if (children && element instanceof ContainerNode) { + if (element.isMavenType()) { + children.sort((a, b) => { + return a.getDisplayName().localeCompare(b.getDisplayName()); + }); + } } explorerNodeCache.saveNodes(children || []); diff --git a/src/views/documentSymbolNode.ts b/src/views/documentSymbolNode.ts index 3ad00e3a..7552116e 100644 --- a/src/views/documentSymbolNode.ts +++ b/src/views/documentSymbolNode.ts @@ -28,7 +28,7 @@ export class DocumentSymbolNode extends ExplorerNode { super(parent); } - public getLabel(): string { + public getDisplayName(): string { return this.symbolInfo.name; } @@ -43,7 +43,7 @@ export class DocumentSymbolNode extends ExplorerNode { } public getTreeItem(): TreeItem | Promise { - const item = new TreeItem(this.getLabel(), + const item = new TreeItem(this.getDisplayName(), this.symbolInfo?.children?.length ? TreeItemCollapsibleState.Collapsed : TreeItemCollapsibleState.None); item.iconPath = this.iconPath; diff --git a/src/views/explorerNode.ts b/src/views/explorerNode.ts index 5b271531..1dcac373 100644 --- a/src/views/explorerNode.ts +++ b/src/views/explorerNode.ts @@ -34,5 +34,5 @@ export abstract class ExplorerNode { public abstract computeContextValue(): string | undefined; - public abstract getLabel(): string; + public abstract getDisplayName(): string; } diff --git a/src/views/fileNode.ts b/src/views/fileNode.ts index e593f10c..851afafa 100644 --- a/src/views/fileNode.ts +++ b/src/views/fileNode.ts @@ -13,10 +13,6 @@ export class FileNode extends DataNode { super(nodeData, parent); } - public getLabel(): string { - return this._nodeData.displayName ?? this._nodeData.name; - } - protected hasChildren(): boolean { return false; } diff --git a/src/views/folderNode.ts b/src/views/folderNode.ts index 45529b31..609b799b 100644 --- a/src/views/folderNode.ts +++ b/src/views/folderNode.ts @@ -15,10 +15,6 @@ export class FolderNode extends DataNode { super(nodeData, parent); } - public getLabel(): string { - return this._nodeData.displayName ?? this._nodeData.name; - } - protected async loadData(): Promise { return Jdtls.getPackageData({ kind: NodeKind.Folder, diff --git a/src/views/packageNode.ts b/src/views/packageNode.ts index 83f7589c..a6151435 100644 --- a/src/views/packageNode.ts +++ b/src/views/packageNode.ts @@ -22,10 +22,6 @@ export class PackageNode extends DataNode { return parentData.entryKind === PackageRootKind.K_SOURCE || parentData.kind === NodeKind.Project; } - public getLabel(): string { - return this._nodeData.displayName ?? this._nodeData.name; - } - protected async loadData(): Promise { return Jdtls.getPackageData({ kind: NodeKind.Package, diff --git a/src/views/packageRootNode.ts b/src/views/packageRootNode.ts index 15581c86..63de2c65 100644 --- a/src/views/packageRootNode.ts +++ b/src/views/packageRootNode.ts @@ -8,7 +8,7 @@ import { INodeData, NodeKind } from "../java/nodeData"; import { IPackageRootNodeData, PackageRootKind } from "../java/packageRootNodeData"; import { Settings } from "../settings"; import { isTest } from "../utility"; -import { ContainerNode, ContainerType } from "./containerNode"; +import { ContainerNode } from "./containerNode"; import { DataNode } from "./dataNode"; import { ExplorerNode } from "./explorerNode"; import { ProjectNode } from "./projectNode"; @@ -20,13 +20,6 @@ export class PackageRootNode extends DataNode { super(nodeData, parent); } - public getLabel(): string { - if (this._nodeData.metaData?.['maven.groupId']) { - return `${this._nodeData.metaData?.['maven.groupId']}:${this._nodeData.metaData?.['maven.artifactId']}:${this._nodeData.metaData?.['maven.version']}`; - } - return this._nodeData.displayName ?? this._nodeData.name; - } - public isSourceRoot(): boolean { return (this.nodeData).entryKind === PackageRootKind.K_SOURCE; } diff --git a/src/views/projectNode.ts b/src/views/projectNode.ts index 2bd341c4..f48ef75a 100644 --- a/src/views/projectNode.ts +++ b/src/views/projectNode.ts @@ -54,10 +54,6 @@ export class ProjectNode extends DataNode { return false; } - public getLabel(): string { - return this._nodeData.displayName ?? this._nodeData.name; - } - protected async loadData(): Promise { return Jdtls.getPackageData({ kind: NodeKind.Project, projectUri: this.nodeData.uri }); } diff --git a/src/views/workspaceNode.ts b/src/views/workspaceNode.ts index 6cbd1913..4cb8331f 100644 --- a/src/views/workspaceNode.ts +++ b/src/views/workspaceNode.ts @@ -14,10 +14,6 @@ export class WorkspaceNode extends DataNode { super(nodeData, parent); } - public getLabel(): string { - return this._nodeData.displayName ?? this._nodeData.name; - } - protected async loadData(): Promise { if (!this.nodeData.uri) { return undefined;