Skip to content

Commit 21fcce9

Browse files
0dinDjdneo
andauthored
fix: Add libraries dialog not working on Linux (#434)
Co-authored-by: Sheng Chen <[email protected]>
1 parent 26e51db commit 21fcce9

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
"category": "Java",
4747
"icon": "$(add)"
4848
},
49+
{
50+
"command": "java.project.addLibraryFolders",
51+
"title": "%contributes.commands.java.project.addLibraryFolders%",
52+
"category": "Java",
53+
"icon": "$(new-folder)"
54+
},
4955
{
5056
"command": "java.project.removeLibrary",
5157
"title": "%contributes.commands.java.project.removeLibrary%",
@@ -286,6 +292,10 @@
286292
"command": "java.project.addLibraries",
287293
"when": "false"
288294
},
295+
{
296+
"command": "java.project.addLibraryFolders",
297+
"when": "false"
298+
},
289299
{
290300
"command": "java.project.removeLibrary",
291301
"when": "false"
@@ -466,6 +476,7 @@
466476
},
467477
{
468478
"command": "java.project.addLibraries",
479+
"alt": "java.project.addLibraryFolders",
469480
"when": "view == javaProjectExplorer && viewItem =~ /java:container(?=.*?\\b\\+referencedLibrary\\b)/",
470481
"group": "inline@0"
471482
},

package.nls.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"description": "Manage Java projects in Visual Studio Code",
33
"contributes.commands.java.project.create": "Create Java Project...",
4-
"contributes.commands.java.project.addLibraries": "Add a jar file or a folder to project classpath",
5-
"contributes.commands.java.project.removeLibrary": "Remove jar file from project classpath",
4+
"contributes.commands.java.project.addLibraries": "Add Jar Libraries to Project Classpath...",
5+
"contributes.commands.java.project.addLibraryFolders": "Add Library Folders to Project Classpath...",
6+
"contributes.commands.java.project.removeLibrary": "Remove from Project Classpath",
67
"contributes.commands.java.view.package.refresh": "Refresh",
78
"contributes.commands.java.project.build.workspace": "Build Workspace",
89
"contributes.commands.java.project.clean.workspace": "Clean Workspace",

package.nls.zh.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"description": "在 Visual Studio Code 中管理 Java 项目",
33
"contributes.commands.java.project.create": "创建 Java 项目...",
4-
"contributes.commands.java.project.addLibraries": "将一个 Jar 文件或一个目录添加到 Java 项目类路径中",
5-
"contributes.commands.java.project.removeLibrary": "将该 Jar 文件从 Java 项目类路径中移除",
4+
"contributes.commands.java.project.addLibraries": "添加 Jar 文件至项目 Classpath...",
5+
"contributes.commands.java.project.addLibraryFolders": "添加文件夹至项目 Classpath...",
6+
"contributes.commands.java.project.removeLibrary": "从项目 Classpath 中移除",
67
"contributes.commands.java.view.package.refresh": "刷新",
78
"contributes.commands.java.project.build.workspace": "构建工作空间",
89
"contributes.commands.java.project.clean.workspace": "清理工作空间",

src/commands.ts

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

4747
export const JAVA_PROJECT_ADD_LIBRARIES = "java.project.addLibraries";
4848

49+
export const JAVA_PROJECT_ADD_LIBRARY_FOLDERS = "java.project.addLibraryFolders";
50+
4951
export const JAVA_PROJECT_REMOVE_LIBRARY = "java.project.removeLibrary";
5052

5153
export const JAVA_PROJECT_REFRESH_LIBRARIES = "java.project.refreshLibraries";

src/controllers/libraryController.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import * as fse from "fs-extra";
55
import * as _ from "lodash";
66
import * as minimatch from "minimatch";
7+
import { platform } from "os";
78
import * as path from "path";
89
import { Disposable, ExtensionContext, Uri, window, workspace, WorkspaceFolder } from "vscode";
910
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
@@ -20,6 +21,7 @@ export class LibraryController implements Disposable {
2021
public constructor(public readonly context: ExtensionContext) {
2122
this.disposable = Disposable.from(
2223
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_ADD_LIBRARIES, () => this.addLibraries()),
24+
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_ADD_LIBRARY_FOLDERS, () => this.addLibraries(true)),
2325
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REMOVE_LIBRARY, (node: DataNode) =>
2426
node.uri && this.removeLibrary(Uri.parse(node.uri).fsPath)),
2527
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REFRESH_LIBRARIES, () =>
@@ -31,36 +33,36 @@ export class LibraryController implements Disposable {
3133
this.disposable.dispose();
3234
}
3335

34-
public async addLibraries(libraryGlobs?: string[]) {
35-
if (!libraryGlobs) {
36-
libraryGlobs = [];
37-
const workspaceFolder: WorkspaceFolder | undefined = Utility.getDefaultWorkspaceFolder();
38-
const isWindows = process.platform.indexOf("win") === 0;
39-
const results: Uri[] | undefined = await window.showOpenDialog({
40-
defaultUri: workspaceFolder && workspaceFolder.uri,
41-
canSelectFiles: true,
42-
canSelectFolders: isWindows ? false : true,
43-
canSelectMany: true,
44-
openLabel: isWindows ? "Select jar files" : "Select jar files or directories",
45-
filters: { Library: ["jar"] },
46-
});
47-
if (!results) {
48-
return;
49-
}
50-
libraryGlobs = await Promise.all(results.map(async (uri: Uri) => {
51-
// keep the param: `includeWorkspaceFolder` to false here
52-
// since the multi-root is not supported well for invisible projects
53-
const uriPath = workspace.asRelativePath(uri, false);
54-
return (await fse.stat(uri.fsPath)).isDirectory() ? `${uriPath}/**/*.jar` : uriPath;
55-
}));
56-
}
57-
36+
public async addLibraryGlobs(libraryGlobs: string[]) {
5837
const setting = Settings.referencedLibraries();
5938
setting.exclude = this.dedupAlreadyCoveredPattern(libraryGlobs, ...setting.exclude);
6039
setting.include = this.updatePatternArray(setting.include, ...libraryGlobs);
6140
Settings.updateReferencedLibraries(setting);
6241
}
6342

43+
public async addLibraries(canSelectFolders?: boolean) {
44+
const workspaceFolder: WorkspaceFolder | undefined = Utility.getDefaultWorkspaceFolder();
45+
const isMac = platform() === "darwin";
46+
const results: Uri[] | undefined = await window.showOpenDialog({
47+
defaultUri: workspaceFolder && workspaceFolder.uri,
48+
canSelectFiles: !canSelectFolders,
49+
canSelectFolders: canSelectFolders || isMac,
50+
canSelectMany: true,
51+
openLabel: canSelectFolders ? "Select Library Folders" : "Select Jar Libraries",
52+
filters: canSelectFolders ? { Folders: ["*"] } : { "Jar Files": ["jar"] },
53+
});
54+
if (!results) {
55+
return;
56+
}
57+
this.addLibraryGlobs(await Promise.all(results.map(async (uri: Uri) => {
58+
// keep the param: `includeWorkspaceFolder` to false here
59+
// since the multi-root is not supported well for invisible projects
60+
const uriPath = workspace.asRelativePath(uri, false);
61+
const isLibraryFolder = canSelectFolders || isMac && (await fse.stat(uri.fsPath)).isDirectory();
62+
return isLibraryFolder ? uriPath + "/**/*.jar" : uriPath;
63+
})));
64+
}
65+
6466
public async removeLibrary(removalFsPath: string) {
6567
const setting = Settings.referencedLibraries();
6668
const removedPaths = _.remove(setting.include, (include) => {

0 commit comments

Comments
 (0)