Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine the context value of the nodes in the project explorer #311

Merged
merged 6 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
*/
public class PackageNode {

/**
* Nature Id for the IProject
*/
private static final String NATURE_ID = "NatureId";

public final static String K_TYPE_KIND = "TypeKind";

/**
Expand Down Expand Up @@ -146,6 +151,11 @@ public static PackageNode createNodeForProject(IJavaElement javaElement) {
IProject proj = javaElement.getJavaProject().getProject();
PackageNode projectNode = new PackageNode(proj.getName(), proj.getFullPath().toPortableString(), NodeKind.PROJECT);
projectNode.setUri(proj.getLocationURI().toString());
try {
projectNode.setMetaDataValue(NATURE_ID, proj.getDescription().getNatureIds());
} catch (CoreException e) {
// do nothing
}
return projectNode;
}

Expand Down
33 changes: 9 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@
"category": "Java",
"icon": "$(add)"
},
{
"command": "java.project.maven.addDependency",
"title": "%contributes.commands.java.project.maven.addDependency%",
"category": "Java",
"icon": "$(add)"
},
{
"command": "java.project.removeLibrary",
"title": "%contributes.commands.java.project.removeLibrary%",
Expand Down Expand Up @@ -217,10 +211,6 @@
"command": "java.project.addLibraries",
"when": "never"
},
{
"command": "java.project.maven.addDependency",
"when": "never"
},
{
"command": "java.project.removeLibrary",
"when": "never"
Expand Down Expand Up @@ -296,52 +286,47 @@
"view/item/context": [
{
"command": "java.view.package.revealFileInOS",
"when": "view == javaProjectExplorer && viewItem =~ /java:.*?\\+uri/",
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
"group": "path@10"
},
{
"command": "java.view.package.copyFilePath",
"when": "view == javaProjectExplorer && viewItem =~ /java:.*?\\+uri/",
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
"group": "path@20"
},
{
"command": "java.view.package.copyRelativeFilePath",
"when": "view == javaProjectExplorer && viewItem =~ /java:.*?\\+uri/",
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
"group": "path@25"
},
{
"command": "java.view.package.newJavaClass",
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot).*\\+uri/",
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
"group": "new@10"
},
{
"command": "java.view.package.newPackage",
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot).*\\+uri/",
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
"group": "new@20"
},
{
"command": "java.project.addLibraries",
"when": "view == javaProjectExplorer && viewItem =~ /java:container\/referenced-libraries$/",
"when": "view == javaProjectExplorer && viewItem =~ /java:container(?=.*?\\b\\+referencedLibrary\\b)/",
"group": "inline@0"
},
{
"command": "java.project.removeLibrary",
"when": "view == javaProjectExplorer && viewItem =~ /java:jar\/referenced-libraries\\+uri$/",
"when": "view == javaProjectExplorer && viewItem =~ /java:jar(?=.*?\\b\\+referencedLibrary\\b)(?=.*?\\b\\+uri\\b)/",
"group": "inline"
},
{
"command": "java.project.refreshLibraries",
"when": "view == javaProjectExplorer && viewItem =~ /java:container\/referenced-libraries$/",
"when": "view == javaProjectExplorer && viewItem =~ /java:container(?=.*?\\b\\+referencedLibrary\\b)/",
"group": "inline@1"
},
{
"command": "java.project.maven.addDependency",
"when": "view == javaProjectExplorer && mavenEnabled && viewItem =~ /container\/maven-dependencies/",
"group": "inline@0"
},
{
"command": "java.view.package.exportJar",
"when": "view == javaProjectExplorer && viewItem =~ /java:workspace.*?\\+uri/ && java:serverMode!= LightWeight",
"when": "view == javaProjectExplorer && viewItem =~ /java:workspace(?=.*?\\b\\+uri\\b)/ && java:serverMode!= LightWeight",
"group": "inline"
}
]
Expand Down
1 change: 0 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"description": "Manage Java projects in Visual Studio Code",
"contributes.commands.java.project.create": "Create Java Project...",
"contributes.commands.java.project.addLibraries": "Add a jar file or a folder to project classpath",
"contributes.commands.java.project.maven.addDependency": "Add a new dependency to the Maven project",
"contributes.commands.java.project.removeLibrary": "Remove jar file from project classpath",
"contributes.commands.java.view.package.refresh": "Refresh",
"contributes.commands.java.project.build.workspace": "Build Workspace",
Expand Down
1 change: 0 additions & 1 deletion package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"description": "在 Visual Studio Code 中管理 Java 项目",
"contributes.commands.java.project.create": "创建 Java 项目...",
"contributes.commands.java.project.addLibraries": "将一个 Jar 文件或一个目录添加到 Java 项目类路径中",
"contributes.commands.java.project.maven.addDependency": "为该 Maven 项目增加依赖库",
"contributes.commands.java.project.removeLibrary": "将该 Jar 文件从 Java 项目类路径中移除",
"contributes.commands.java.view.package.refresh": "刷新",
"contributes.commands.java.project.build.workspace": "构建工作空间",
Expand Down
2 changes: 0 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export namespace Commands {

export const JAVA_PROJECT_CLEAN_WORKSPACE = "java.project.clean.workspace";

export const JAVA_MAVEN_PROJECT_ADD_DEPENDENCY = "java.project.maven.addDependency";

export const JAVA_MAVEN_CREATE_PROJECT = "maven.archetype.generate";

export const JAVA_PROJECT_LIST = "java.project.list";
Expand Down
11 changes: 9 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
// Licensed under the MIT license.

export namespace Context {
export const MAVEN_ENABLED: string = "mavenEnabled";
export const EXTENSION_ACTIVATED: string = "extensionActivated";
}

export namespace Explorer {
export const DEFAULT_PACKAGE_NAME: string = "default-package";
export const DEFAULT_PACKAGE_NAME: string = "(default package)";
export enum ContextValueType {
WorkspaceFolder = "workspaceFolder",
Project = "project",
Container = "container",
PackageRoot = "packageRoot",
Package = "package",
Jar = "jar",
}
}
11 changes: 0 additions & 11 deletions src/controllers/libraryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,13 @@ export class LibraryController implements Disposable {
this.removeLibrary(Uri.parse(node.uri).fsPath)),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REFRESH_LIBRARIES, () =>
this.refreshLibraries()),
instrumentOperationAsVsCodeCommand(Commands.JAVA_MAVEN_PROJECT_ADD_DEPENDENCY, (node: ContainerNode) =>
this.addMavenDependency(node)),
);
}

public dispose() {
this.disposable.dispose();
}

public async addMavenDependency(node: ContainerNode) {
const pomPath: string = path.join(node.projectBasePath, "pom.xml");
if (await fse.pathExists(pomPath)) {
commands.executeCommand("maven.project.addDependency", { pomPath });
} else {
commands.executeCommand("maven.project.addDependency");
}
}

public async addLibraries(libraryGlobs?: string[]) {
if (!libraryGlobs) {
libraryGlobs = [];
Expand Down
14 changes: 0 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ async function activateExtension(_operationId: string, context: ExtensionContext

Settings.initialize(context);
contextManager.initialize(context);
setMavenExtensionState();

context.subscriptions.push(new ProjectController(context));
context.subscriptions.push(new LibraryController(context));
Expand All @@ -65,19 +64,6 @@ async function activateExtension(_operationId: string, context: ExtensionContext
initExpService(context);
}

// determine if the add dependency shortcut will show or not
function setMavenExtensionState() {
setMavenEnabledContext();
extensions.onDidChange(() => {
setMavenEnabledContext();
});

function setMavenEnabledContext() {
const mavenExt: Extension<any> | undefined = extensions.getExtension("vscjava.vscode-maven");
contextManager.setContextValue(Context.MAVEN_ENABLED, !!mavenExt);
}
}

// this method is called when your extension is deactivated
export async function deactivate() {
await disposeTelemetryWrapper();
Expand Down
2 changes: 1 addition & 1 deletion src/java/nodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export interface INodeData {
uri?: string;
kind: NodeKind;
children?: any[];
metaData?: Map<string, any>;
metaData?: { [id: string]: any };
}
29 changes: 28 additions & 1 deletion src/views/containerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { ThemeIcon, Uri } from "vscode";
import { Explorer } from "../constants";
import { Jdtls } from "../java/jdtls";
import { INodeData, NodeKind } from "../java/nodeData";
import { DataNode } from "./dataNode";
Expand Down Expand Up @@ -33,10 +34,36 @@ export class ContainerNode extends DataNode {
}

protected get contextValue(): string {
return `container/${this.name}`;
let contextValue: string = Explorer.ContextValueType.Container;
const containerType: string = getContainerType(this._nodeData.path);
if (containerType) {
contextValue += `+${containerType}`;
}
return contextValue;
}

protected get iconPath(): ThemeIcon {
return new ThemeIcon("library");
}
}

function getContainerType(containerPath: string | undefined): string {
if (!containerPath) {
return "";
} else if (containerPath.startsWith(ContainerPath.JRE)) {
return "jre";
} else if (containerPath.startsWith(ContainerPath.Maven)) {
return "maven";
} else if (containerPath.startsWith(ContainerPath.Gradle)) {
return "gradle";
} else if (containerPath.startsWith(ContainerPath.ReferencedLibrary)) {
return "referencedLibrary";
}
}

const enum ContainerPath {
JRE = "org.eclipse.jdt.launching.JRE_CONTAINER",
Maven = "org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER",
Gradle = "org.eclipse.buildship.core.gradleclasspathcontainer",
ReferencedLibrary = "REFERENCED_LIBRARIES_PATH",
}
5 changes: 2 additions & 3 deletions src/views/dataNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as _ from "lodash";
import { ProviderResult, ThemeIcon, TreeItem, TreeItemCollapsibleState, Uri } from "vscode";
import { INodeData } from "../java/nodeData";
import { ExplorerNode } from "./explorerNode";
Expand Down Expand Up @@ -41,8 +40,8 @@ export abstract class DataNode extends ExplorerNode {
return this._nodeData.handlerIdentifier;
}

public get name() { // return name like `referenced-library`
return _.kebabCase(this._nodeData.name);
public get name() {
return this._nodeData.name;
}

public async revealPaths(paths: INodeData[]): Promise<DataNode> {
Expand Down
5 changes: 4 additions & 1 deletion src/views/packageNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { ThemeIcon } from "vscode";
import { Explorer } from "../constants";
import { Jdtls } from "../java/jdtls";
import { INodeData, NodeKind } from "../java/nodeData";
import { IPackageRootNodeData, PackageRootKind } from "../java/packageRootNodeData";
Expand Down Expand Up @@ -49,7 +50,9 @@ export class PackageNode extends DataNode {
protected get contextValue(): string {
const parentData = <IPackageRootNodeData> this._rootNode.nodeData;
if (parentData.entryKind === PackageRootKind.K_SOURCE) {
return `package/${this.name}`;
return `${Explorer.ContextValueType.Package}+source`;
} else if (parentData.entryKind === PackageRootKind.K_BINARY) {
return `${Explorer.ContextValueType.Package}+binary`;
}
}
}
13 changes: 10 additions & 3 deletions src/views/packageRootNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { ThemeIcon } from "vscode";
import { Explorer } from "../constants";
import { Jdtls } from "../java/jdtls";
import { INodeData, NodeKind } from "../java/nodeData";
import { IPackageRootNodeData, PackageRootKind } from "../java/packageRootNodeData";
Expand Down Expand Up @@ -62,13 +63,19 @@ export class PackageRootNode extends DataNode {
protected get contextValue(): string {
const data = <IPackageRootNodeData>this.nodeData;
if (data.entryKind === PackageRootKind.K_BINARY) {
let contextValue: string = Explorer.ContextValueType.Jar;
const parent = <ContainerNode>this.getParent();
return `jar/${parent.name}`;
} else if (!resourceRoots.includes(this._nodeData.name)) {
if (parent.path.startsWith("REFERENCED_LIBRARIES_PATH")) {
contextValue += "+referencedLibrary";
}
return contextValue;
} else if (resourceRoots.includes(this._nodeData.name)) {
// APIs in JDT does not have a consistent result telling whether a package root
// is a source root or resource root, so we hard code some common resources root
// here as a workaround.
return `packageRoot/${this.name}`;
return `${Explorer.ContextValueType.PackageRoot}+resource`;
} else {
return `${Explorer.ContextValueType.PackageRoot}+source`;
}
}

Expand Down
48 changes: 47 additions & 1 deletion src/views/projectNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { ThemeIcon } from "vscode";
import { Explorer } from "../constants";
import { ContainerEntryKind, IContainerNodeData } from "../java/containerNodeData";
import { Jdtls } from "../java/jdtls";
import { INodeData, NodeKind } from "../java/nodeData";
Expand Down Expand Up @@ -69,7 +70,52 @@ export class ProjectNode extends DataNode {
protected get iconPath(): ThemeIcon {
return new ThemeIcon("project");
}

protected get contextValue(): string {
return `project/${this.name}`;
let contextValue: string = Explorer.ContextValueType.Project;
const natureIds: string[] | undefined = this.nodeData.metaData[NATURE_ID];
if (natureIds) {
const attributeString: string = getProjectTypeAttributes(natureIds);
contextValue += attributeString;
}
return contextValue;
}
}

function getProjectTypeAttributes(natureIds: string []): string {
let attributeString: string = "";
for (const natureId of natureIds) {
const readableNature: string = getProjectType(natureId);
if (readableNature) {
attributeString += `+${readableNature}`;
}
}
return attributeString;
}

function getProjectType(natureId: string): string {
switch (natureId) {
case NatureId.Java:
return ReadableNature.Java;
case NatureId.Maven:
return ReadableNature.Maven;
case NatureId.Gradle:
return ReadableNature.Gradle;
default:
return "";
}
}

enum NatureId {
Maven = "org.eclipse.m2e.core.maven2Nature",
Gradle = "org.eclipse.buildship.core.gradleprojectnature",
Java = "org.eclipse.jdt.core.javanature",
}

enum ReadableNature {
Maven = "maven",
Gradle = "gradle",
Java = "java",
}

const NATURE_ID = "NatureId";
Loading