Skip to content

Commit 4f23b6d

Browse files
authored
Get project type by its natures (#1282)
Signed-off-by: Sheng Chen <[email protected]>
1 parent 7c9fc75 commit 4f23b6d

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

src/classpath/classpathConfigurationView.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import compareVersions from "compare-versions";
1616
let classpathConfigurationPanel: vscode.WebviewPanel | undefined;
1717
let lsApi: LanguageServerAPI | undefined;
1818
let currentProjectRoot: vscode.Uri;
19+
const Nature_IDS: string = "org.eclipse.jdt.ls.core.natureIds"
1920
const SOURCE_PATH_KEY: string = "org.eclipse.jdt.ls.core.sourcePaths";
2021
const OUTPUT_PATH_KEY: string = "org.eclipse.jdt.ls.core.outputPath";
2122
const VM_LOCATION_KEY: string = "org.eclipse.jdt.ls.core.vm.location";
@@ -490,6 +491,7 @@ async function getVmInstallsFromLS(): Promise<VmInstall[]> {
490491

491492
async function getProjectClasspathFromLS(uri: vscode.Uri): Promise<ClasspathComponent> {
492493
const queryKeys: string[] = [
494+
Nature_IDS,
493495
SOURCE_PATH_KEY,
494496
OUTPUT_PATH_KEY,
495497
VM_LOCATION_KEY,
@@ -501,7 +503,7 @@ async function getProjectClasspathFromLS(uri: vscode.Uri): Promise<ClasspathComp
501503
queryKeys
502504
);
503505
const classpath: ClasspathComponent = {
504-
projectType: await getProjectType(uri.fsPath),
506+
projectType: getProjectType(uri.fsPath, response[Nature_IDS] as string[]),
505507
sourcePaths: response[SOURCE_PATH_KEY] as string[],
506508
defaultOutputPath: response[OUTPUT_PATH_KEY] as string,
507509
jdkPath: response[VM_LOCATION_KEY] as string,

src/java-runtime/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ async function getProjectRuntimesFromPM(): Promise<ProjectRuntimeEntry[]> {
267267

268268
for (const project of projects) {
269269
const runtimeSpec = await getRuntimeSpec(project.uri);
270-
const projectType: ProjectType = await getProjectType(vscode.Uri.parse(project.uri).fsPath);
270+
const projectType: ProjectType = getProjectType(vscode.Uri.parse(project.uri).fsPath, runtimeSpec.natureIds);
271271
ret.push({
272272
name: project.displayName || project.name,
273273
rootPath: project.uri,
@@ -293,7 +293,7 @@ async function getProjectRuntimesFromLS(): Promise<ProjectRuntimeEntry[]> {
293293

294294
for (const projectRoot of projects) {
295295
const runtimeSpec = await getRuntimeSpec(projectRoot);
296-
const projectType: ProjectType = await getProjectType(vscode.Uri.parse(projectRoot).fsPath);
296+
const projectType: ProjectType = await getProjectType(vscode.Uri.parse(projectRoot).fsPath, runtimeSpec.natureIds);
297297
ret.push({
298298
name: getProjectNameFromUri(projectRoot),
299299
rootPath: projectRoot,
@@ -306,14 +306,17 @@ async function getProjectRuntimesFromLS(): Promise<ProjectRuntimeEntry[]> {
306306
}
307307

308308
async function getRuntimeSpec(projectRootUri: string) {
309+
let natureIds;
309310
let runtimePath;
310311
let sourceLevel;
311312
const javaExt = vscode.extensions.getExtension("redhat.java");
312313
if (javaExt && javaExt.isActive) {
314+
const NATURE_IDS = "org.eclipse.jdt.ls.core.natureIds";
313315
const SOURCE_LEVEL_KEY = "org.eclipse.jdt.core.compiler.source";
314316
const VM_INSTALL_PATH = "org.eclipse.jdt.ls.core.vm.location";
315317
try {
316-
const settings: any = await javaExt.exports.getProjectSettings(projectRootUri, [SOURCE_LEVEL_KEY, VM_INSTALL_PATH]);
318+
const settings: any = await javaExt.exports.getProjectSettings(projectRootUri, [NATURE_IDS, SOURCE_LEVEL_KEY, VM_INSTALL_PATH]);
319+
natureIds = settings[NATURE_IDS];
317320
runtimePath = settings[VM_INSTALL_PATH];
318321
sourceLevel = settings[SOURCE_LEVEL_KEY];
319322
} catch (error) {
@@ -322,6 +325,7 @@ async function getRuntimeSpec(projectRootUri: string) {
322325
}
323326

324327
return {
328+
natureIds,
325329
runtimePath,
326330
sourceLevel
327331
};

src/utils/jdt.ts

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

4-
import * as fse from "fs-extra";
54
import * as path from "path";
6-
import { ProjectType } from "./webview";
5+
import { NatureId, ProjectType } from "./webview";
76
import * as vscode from "vscode";
87

9-
export async function getProjectType(fsPath: string): Promise<ProjectType> {
8+
export function getProjectType(fsPath: string, natureIds: string[]): ProjectType {
109
if (isDefaultProject(fsPath)) {
1110
return ProjectType.Default;
1211
}
13-
const buildDotGradleFile = path.join(fsPath, "build.gradle");
14-
if (await fse.pathExists(buildDotGradleFile)) {
12+
13+
if (natureIds.includes(NatureId.Gradle) || natureIds.includes(NatureId.GradleBs)) {
1514
return ProjectType.Gradle;
1615
}
17-
const pomDotXmlFile = path.join(fsPath, "pom.xml");
18-
if (await fse.pathExists(pomDotXmlFile)) {
16+
if (natureIds.includes(NatureId.Maven)){
1917
return ProjectType.Maven;
2018
}
21-
const dotProjectFile = path.join(fsPath, ".project");
22-
if (!await fse.pathExists(dotProjectFile)) { // for invisible projects, .project file is located in workspace storage.
19+
if (natureIds.includes(NatureId.UnmanagedFolder)) {
2320
return ProjectType.UnmanagedFolder;
2421
}
2522
return ProjectType.Others;

src/utils/webview.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ export enum ProjectType {
5757
export enum NatureId {
5858
Maven = "org.eclipse.m2e.core.maven2Nature",
5959
Gradle = "org.eclipse.buildship.core.gradleprojectnature",
60-
Java = "org.eclipse.jdt.core.javanature",
60+
GradleBs = "com.microsoft.gradle.bs.importer.GradleBuildServerProjectNature",
61+
UnmanagedFolder = "org.eclipse.jdt.ls.unmanagedFolderNature",
6162
}

0 commit comments

Comments
 (0)