Skip to content

Commit 1409594

Browse files
authored
Refine the context value of the nodes in the project explorer (#311)
1 parent 84a04e6 commit 1409594

File tree

16 files changed

+285
-66
lines changed

16 files changed

+285
-66
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
*/
4242
public class PackageNode {
4343

44+
/**
45+
* Nature Id for the IProject
46+
*/
47+
private static final String NATURE_ID = "NatureId";
48+
4449
public final static String K_TYPE_KIND = "TypeKind";
4550

4651
/**
@@ -146,6 +151,11 @@ public static PackageNode createNodeForProject(IJavaElement javaElement) {
146151
IProject proj = javaElement.getJavaProject().getProject();
147152
PackageNode projectNode = new PackageNode(proj.getName(), proj.getFullPath().toPortableString(), NodeKind.PROJECT);
148153
projectNode.setUri(proj.getLocationURI().toString());
154+
try {
155+
projectNode.setMetaDataValue(NATURE_ID, proj.getDescription().getNatureIds());
156+
} catch (CoreException e) {
157+
// do nothing
158+
}
149159
return projectNode;
150160
}
151161

package.json

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@
4646
"category": "Java",
4747
"icon": "$(add)"
4848
},
49-
{
50-
"command": "java.project.maven.addDependency",
51-
"title": "%contributes.commands.java.project.maven.addDependency%",
52-
"category": "Java",
53-
"icon": "$(add)"
54-
},
5549
{
5650
"command": "java.project.removeLibrary",
5751
"title": "%contributes.commands.java.project.removeLibrary%",
@@ -217,10 +211,6 @@
217211
"command": "java.project.addLibraries",
218212
"when": "never"
219213
},
220-
{
221-
"command": "java.project.maven.addDependency",
222-
"when": "never"
223-
},
224214
{
225215
"command": "java.project.removeLibrary",
226216
"when": "never"
@@ -296,52 +286,47 @@
296286
"view/item/context": [
297287
{
298288
"command": "java.view.package.revealFileInOS",
299-
"when": "view == javaProjectExplorer && viewItem =~ /java:.*?\\+uri/",
289+
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
300290
"group": "path@10"
301291
},
302292
{
303293
"command": "java.view.package.copyFilePath",
304-
"when": "view == javaProjectExplorer && viewItem =~ /java:.*?\\+uri/",
294+
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
305295
"group": "path@20"
306296
},
307297
{
308298
"command": "java.view.package.copyRelativeFilePath",
309-
"when": "view == javaProjectExplorer && viewItem =~ /java:.*?\\+uri/",
299+
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
310300
"group": "path@25"
311301
},
312302
{
313303
"command": "java.view.package.newJavaClass",
314-
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot).*\\+uri/",
304+
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
315305
"group": "new@10"
316306
},
317307
{
318308
"command": "java.view.package.newPackage",
319-
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot).*\\+uri/",
309+
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
320310
"group": "new@20"
321311
},
322312
{
323313
"command": "java.project.addLibraries",
324-
"when": "view == javaProjectExplorer && viewItem =~ /java:container\/referenced-libraries$/",
314+
"when": "view == javaProjectExplorer && viewItem =~ /java:container(?=.*?\\b\\+referencedLibrary\\b)/",
325315
"group": "inline@0"
326316
},
327317
{
328318
"command": "java.project.removeLibrary",
329-
"when": "view == javaProjectExplorer && viewItem =~ /java:jar\/referenced-libraries\\+uri$/",
319+
"when": "view == javaProjectExplorer && viewItem =~ /java:jar(?=.*?\\b\\+referencedLibrary\\b)(?=.*?\\b\\+uri\\b)/",
330320
"group": "inline"
331321
},
332322
{
333323
"command": "java.project.refreshLibraries",
334-
"when": "view == javaProjectExplorer && viewItem =~ /java:container\/referenced-libraries$/",
324+
"when": "view == javaProjectExplorer && viewItem =~ /java:container(?=.*?\\b\\+referencedLibrary\\b)/",
335325
"group": "inline@1"
336326
},
337-
{
338-
"command": "java.project.maven.addDependency",
339-
"when": "view == javaProjectExplorer && mavenEnabled && viewItem =~ /container\/maven-dependencies/",
340-
"group": "inline@0"
341-
},
342327
{
343328
"command": "java.view.package.exportJar",
344-
"when": "view == javaProjectExplorer && viewItem =~ /java:workspace.*?\\+uri/ && java:serverMode!= LightWeight",
329+
"when": "view == javaProjectExplorer && viewItem =~ /java:workspace(?=.*?\\b\\+uri\\b)/ && java:serverMode!= LightWeight",
345330
"group": "inline"
346331
}
347332
]

package.nls.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"description": "Manage Java projects in Visual Studio Code",
33
"contributes.commands.java.project.create": "Create Java Project...",
44
"contributes.commands.java.project.addLibraries": "Add a jar file or a folder to project classpath",
5-
"contributes.commands.java.project.maven.addDependency": "Add a new dependency to the Maven project",
65
"contributes.commands.java.project.removeLibrary": "Remove jar file from project classpath",
76
"contributes.commands.java.view.package.refresh": "Refresh",
87
"contributes.commands.java.project.build.workspace": "Build Workspace",

package.nls.zh.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"description": "在 Visual Studio Code 中管理 Java 项目",
33
"contributes.commands.java.project.create": "创建 Java 项目...",
44
"contributes.commands.java.project.addLibraries": "将一个 Jar 文件或一个目录添加到 Java 项目类路径中",
5-
"contributes.commands.java.project.maven.addDependency": "为该 Maven 项目增加依赖库",
65
"contributes.commands.java.project.removeLibrary": "将该 Jar 文件从 Java 项目类路径中移除",
76
"contributes.commands.java.view.package.refresh": "刷新",
87
"contributes.commands.java.project.build.workspace": "构建工作空间",

src/commands.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ export namespace Commands {
4848

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

51-
export const JAVA_MAVEN_PROJECT_ADD_DEPENDENCY = "java.project.maven.addDependency";
52-
5351
export const JAVA_MAVEN_CREATE_PROJECT = "maven.archetype.generate";
5452

5553
export const JAVA_PROJECT_LIST = "java.project.list";

src/constants.ts

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

44
export namespace Context {
5-
export const MAVEN_ENABLED: string = "mavenEnabled";
65
export const EXTENSION_ACTIVATED: string = "extensionActivated";
76
}
87

98
export namespace Explorer {
10-
export const DEFAULT_PACKAGE_NAME: string = "default-package";
9+
export const DEFAULT_PACKAGE_NAME: string = "(default package)";
10+
export enum ContextValueType {
11+
WorkspaceFolder = "workspaceFolder",
12+
Project = "project",
13+
Container = "container",
14+
PackageRoot = "packageRoot",
15+
Package = "package",
16+
Jar = "jar",
17+
}
1118
}

src/controllers/libraryController.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,13 @@ export class LibraryController implements Disposable {
2525
this.removeLibrary(Uri.parse(node.uri).fsPath)),
2626
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REFRESH_LIBRARIES, () =>
2727
this.refreshLibraries()),
28-
instrumentOperationAsVsCodeCommand(Commands.JAVA_MAVEN_PROJECT_ADD_DEPENDENCY, (node: ContainerNode) =>
29-
this.addMavenDependency(node)),
3028
);
3129
}
3230

3331
public dispose() {
3432
this.disposable.dispose();
3533
}
3634

37-
public async addMavenDependency(node: ContainerNode) {
38-
const pomPath: string = path.join(node.projectBasePath, "pom.xml");
39-
if (await fse.pathExists(pomPath)) {
40-
commands.executeCommand("maven.project.addDependency", { pomPath });
41-
} else {
42-
commands.executeCommand("maven.project.addDependency");
43-
}
44-
}
45-
4635
public async addLibraries(libraryGlobs?: string[]) {
4736
if (!libraryGlobs) {
4837
libraryGlobs = [];

src/extension.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ async function activateExtension(_operationId: string, context: ExtensionContext
5353

5454
Settings.initialize(context);
5555
contextManager.initialize(context);
56-
setMavenExtensionState();
5756

5857
context.subscriptions.push(new ProjectController(context));
5958
context.subscriptions.push(new LibraryController(context));
@@ -65,19 +64,6 @@ async function activateExtension(_operationId: string, context: ExtensionContext
6564
initExpService(context);
6665
}
6766

68-
// determine if the add dependency shortcut will show or not
69-
function setMavenExtensionState() {
70-
setMavenEnabledContext();
71-
extensions.onDidChange(() => {
72-
setMavenEnabledContext();
73-
});
74-
75-
function setMavenEnabledContext() {
76-
const mavenExt: Extension<any> | undefined = extensions.getExtension("vscjava.vscode-maven");
77-
contextManager.setContextValue(Context.MAVEN_ENABLED, !!mavenExt);
78-
}
79-
}
80-
8167
// this method is called when your extension is deactivated
8268
export async function deactivate() {
8369
await disposeTelemetryWrapper();

src/java/nodeData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ export interface INodeData {
3030
uri?: string;
3131
kind: NodeKind;
3232
children?: any[];
33-
metaData?: Map<string, any>;
33+
metaData?: { [id: string]: any };
3434
}

src/views/containerNode.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
import { ThemeIcon, Uri } from "vscode";
5+
import { Explorer } from "../constants";
56
import { Jdtls } from "../java/jdtls";
67
import { INodeData, NodeKind } from "../java/nodeData";
78
import { DataNode } from "./dataNode";
@@ -33,10 +34,36 @@ export class ContainerNode extends DataNode {
3334
}
3435

3536
protected get contextValue(): string {
36-
return `container/${this.name}`;
37+
let contextValue: string = Explorer.ContextValueType.Container;
38+
const containerType: string = getContainerType(this._nodeData.path);
39+
if (containerType) {
40+
contextValue += `+${containerType}`;
41+
}
42+
return contextValue;
3743
}
3844

3945
protected get iconPath(): ThemeIcon {
4046
return new ThemeIcon("library");
4147
}
4248
}
49+
50+
function getContainerType(containerPath: string | undefined): string {
51+
if (!containerPath) {
52+
return "";
53+
} else if (containerPath.startsWith(ContainerPath.JRE)) {
54+
return "jre";
55+
} else if (containerPath.startsWith(ContainerPath.Maven)) {
56+
return "maven";
57+
} else if (containerPath.startsWith(ContainerPath.Gradle)) {
58+
return "gradle";
59+
} else if (containerPath.startsWith(ContainerPath.ReferencedLibrary)) {
60+
return "referencedLibrary";
61+
}
62+
}
63+
64+
const enum ContainerPath {
65+
JRE = "org.eclipse.jdt.launching.JRE_CONTAINER",
66+
Maven = "org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER",
67+
Gradle = "org.eclipse.buildship.core.gradleclasspathcontainer",
68+
ReferencedLibrary = "REFERENCED_LIBRARIES_PATH",
69+
}

0 commit comments

Comments
 (0)