Skip to content

Commit ddf844b

Browse files
committed
fix: Project Settings UI update the absolute path
1 parent 8de1414 commit ddf844b

3 files changed

Lines changed: 35 additions & 14 deletions

File tree

src/project-settings/assets/classpath/features/components/Libraries.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { removeReferencedLibrary, addLibraries } from "../classpathConfiguration
1111
import { ClasspathRequest } from "../../../vscode/utils";
1212

1313
import { ClasspathEntry, ClasspathEntryKind } from "../../../../types";
14+
import { ProjectType } from "../../../../../utils/webview";
1415

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

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

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

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

3436
const handleAdd = () => {
35-
ClasspathRequest.onWillSelectLibraries();
37+
ClasspathRequest.onWillSelectLibraries(projectType);
3638
};
3739

3840
const onDidAddLibraries = (event: OnDidAddLibrariesEvent) => {

src/project-settings/assets/vscode/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ export namespace ClasspathRequest {
8181
});
8282
}
8383

84-
export function onWillSelectLibraries() {
84+
export function onWillSelectLibraries(projectType: ProjectType) {
8585
vscode.postMessage({
86-
command: "classpath.onWillSelectLibraries"
86+
command: "classpath.onWillSelectLibraries",
87+
projectType,
8788
});
8889
}
8990

src/project-settings/handlers/ClasspathRequestHandler.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
5858
await this.addNewJdk(this.currentProjectRoot);
5959
break;
6060
case "classpath.onWillSelectLibraries":
61-
await this.selectLibraries(this.currentProjectRoot);
61+
await this.selectLibraries(this.currentProjectRoot, message.projectType);
6262
break;
6363
case "classpath.onClickGotoProjectConfiguration":
6464
this.gotoProjectConfigurationFile(message.rootUri, message.projectType);
@@ -197,7 +197,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
197197
path: `org.eclipse.jdt.launching.JRE_CONTAINER/${vmInstallPath}`,
198198
});
199199
}
200-
classpathEntries.push(...libraries);
200+
classpathEntries.push(...libraries.map(library => this.toClasspathEntryForUpdate(library, currentProjectRoot)));
201201
if (classpathEntries.length > 0) {
202202
await vscode.commands.executeCommand(
203203
"java.execute.workspaceCommand",
@@ -362,7 +362,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
362362
}
363363
});
364364

365-
private selectLibraries = instrumentOperation("projectSettings.classpath.selectLibraries", async (_operationId: string, currentProjectRoot: vscode.Uri) => {
365+
private selectLibraries = instrumentOperation("projectSettings.classpath.selectLibraries", async (_operationId: string, currentProjectRoot: vscode.Uri, projectType: ProjectType) => {
366366
const jarFiles: vscode.Uri[] | undefined = await vscode.window.showOpenDialog({
367367
defaultUri: vscode.workspace.workspaceFolders?.[0].uri,
368368
openLabel: "Select Jar File",
@@ -374,24 +374,42 @@ export class ClasspathRequestHandler implements vscode.Disposable {
374374
},
375375
});
376376
if (jarFiles) {
377-
const jarPaths: string[] = jarFiles.map(uri => {
378-
if (uri.fsPath.startsWith(currentProjectRoot.fsPath)) {
379-
return path.relative(currentProjectRoot.fsPath, uri.fsPath);
380-
}
381-
return uri.fsPath;
382-
});
383377
this.webview.postMessage({
384378
command: "classpath.onDidAddLibraries",
385-
jars: jarPaths.map(jarPath => {
379+
jars: jarFiles.map(uri => {
386380
return {
387381
kind: ClasspathEntryKind.Library,
388-
path: jarPath,
382+
path: this.toLibraryPathForProject(uri, currentProjectRoot, projectType),
389383
};
390384
}),
391385
});
392386
}
393387
});
394388

389+
private toLibraryPathForProject(uri: vscode.Uri, currentProjectRoot: vscode.Uri, projectType: ProjectType): string {
390+
if (projectType !== ProjectType.UnmanagedFolder) {
391+
return uri.fsPath;
392+
}
393+
394+
const relativePath = path.relative(currentProjectRoot.fsPath, uri.fsPath);
395+
if (relativePath && relativePath !== ".." && !relativePath.startsWith(".." + path.sep) && !path.isAbsolute(relativePath)) {
396+
return relativePath;
397+
}
398+
399+
return uri.fsPath;
400+
}
401+
402+
private toClasspathEntryForUpdate(entry: ClasspathEntry, currentProjectRoot: vscode.Uri): ClasspathEntry {
403+
if (entry.kind === ClasspathEntryKind.Library && !path.isAbsolute(entry.path)) {
404+
return {
405+
...entry,
406+
path: path.join(currentProjectRoot.fsPath, entry.path),
407+
};
408+
}
409+
410+
return entry;
411+
}
412+
395413
private updateUnmanagedFolderLibraries = instrumentOperation("projectSettings.classpath.updateUnmanagedFolderLibraries", async (_operationId: string, jarFilePaths: string[]) => {
396414
const setting = this.getReferencedLibrariesSetting();
397415
setting.include = jarFilePaths;

0 commit comments

Comments
 (0)