Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -11,6 +11,7 @@ import { removeReferencedLibrary, addLibraries } from "../classpathConfiguration
import { ClasspathRequest } from "../../../vscode/utils";

import { ClasspathEntry, ClasspathEntryKind } from "../../../../types";
import { ProjectType } from "../../../../../utils/webview";

const Libraries = (): JSX.Element => {

Expand All @@ -22,6 +23,7 @@ const Libraries = (): JSX.Element => {
}, [activeProjectIndex]);

const libraries: ClasspathEntry[] = useSelector((state: any) => state.classpathConfig.data.libraries[activeProjectIndex]);
const projectType: ProjectType = useSelector((state: any) => state.commonConfig.data.projectType[activeProjectIndex]);
const dispatch: Dispatch<any> = useDispatch();

const handleRemove = (index: number) => {
Expand All @@ -32,7 +34,7 @@ const Libraries = (): JSX.Element => {
};

const handleAdd = () => {
ClasspathRequest.onWillSelectLibraries();
ClasspathRequest.onWillSelectLibraries(projectType);
};
Comment thread
wenytang-ms marked this conversation as resolved.

const onDidAddLibraries = (event: OnDidAddLibrariesEvent) => {
Expand Down
5 changes: 3 additions & 2 deletions src/project-settings/assets/vscode/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ export namespace ClasspathRequest {
});
}

export function onWillSelectLibraries() {
export function onWillSelectLibraries(projectType: ProjectType) {
vscode.postMessage({
command: "classpath.onWillSelectLibraries"
command: "classpath.onWillSelectLibraries",
projectType,
});
}

Expand Down
40 changes: 29 additions & 11 deletions src/project-settings/handlers/ClasspathRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
await this.addNewJdk(this.currentProjectRoot);
break;
case "classpath.onWillSelectLibraries":
await this.selectLibraries(this.currentProjectRoot);
await this.selectLibraries(this.currentProjectRoot, message.projectType);
break;
case "classpath.onClickGotoProjectConfiguration":
this.gotoProjectConfigurationFile(message.rootUri, message.projectType);
Expand Down Expand Up @@ -197,7 +197,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
path: `org.eclipse.jdt.launching.JRE_CONTAINER/${vmInstallPath}`,
});
}
classpathEntries.push(...libraries);
classpathEntries.push(...libraries.map(library => this.toClasspathEntryForUpdate(library, currentProjectRoot)));
if (classpathEntries.length > 0) {
await vscode.commands.executeCommand(
"java.execute.workspaceCommand",
Expand Down Expand Up @@ -362,7 +362,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
}
});

private selectLibraries = instrumentOperation("projectSettings.classpath.selectLibraries", async (_operationId: string, currentProjectRoot: vscode.Uri) => {
private selectLibraries = instrumentOperation("projectSettings.classpath.selectLibraries", async (_operationId: string, currentProjectRoot: vscode.Uri, projectType: ProjectType) => {
const jarFiles: vscode.Uri[] | undefined = await vscode.window.showOpenDialog({
defaultUri: vscode.workspace.workspaceFolders?.[0].uri,
openLabel: "Select Jar File",
Expand All @@ -374,24 +374,42 @@ export class ClasspathRequestHandler implements vscode.Disposable {
},
});
if (jarFiles) {
const jarPaths: string[] = jarFiles.map(uri => {
if (uri.fsPath.startsWith(currentProjectRoot.fsPath)) {
return path.relative(currentProjectRoot.fsPath, uri.fsPath);
}
return uri.fsPath;
});
this.webview.postMessage({
command: "classpath.onDidAddLibraries",
jars: jarPaths.map(jarPath => {
jars: jarFiles.map(uri => {
return {
kind: ClasspathEntryKind.Library,
path: jarPath,
path: this.toLibraryPathForProject(uri, currentProjectRoot, projectType),
};
}),
});
}
});

private toLibraryPathForProject(uri: vscode.Uri, currentProjectRoot: vscode.Uri, projectType: ProjectType): string {
if (projectType !== ProjectType.UnmanagedFolder) {
return uri.fsPath;
}

const relativePath = path.relative(currentProjectRoot.fsPath, uri.fsPath);
if (relativePath && relativePath !== ".." && !relativePath.startsWith(".." + path.sep) && !path.isAbsolute(relativePath)) {
return relativePath;
}

return uri.fsPath;
}

private toClasspathEntryForUpdate(entry: ClasspathEntry, currentProjectRoot: vscode.Uri): ClasspathEntry {
if (entry.kind === ClasspathEntryKind.Library && !path.isAbsolute(entry.path)) {
return {
...entry,
path: path.join(currentProjectRoot.fsPath, entry.path),
};
}

return entry;
}

private updateUnmanagedFolderLibraries = instrumentOperation("projectSettings.classpath.updateUnmanagedFolderLibraries", async (_operationId: string, jarFilePaths: string[]) => {
const setting = this.getReferencedLibrariesSetting();
setting.include = jarFilePaths;
Expand Down
Loading