Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix add libraries dialog not working on Linux #434

Merged
merged 7 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
"category": "Java",
"icon": "$(add)"
},
{
"command": "java.project.addLibraryFolders",
"title": "%contributes.commands.java.project.addLibraryFolders%",
"category": "Java",
"icon": "$(new-folder)"
},
{
"command": "java.project.removeLibrary",
"title": "%contributes.commands.java.project.removeLibrary%",
Expand Down Expand Up @@ -285,6 +291,10 @@
"command": "java.project.addLibraries",
"when": "false"
},
{
"command": "java.project.addLibraryFolders",
"when": "false"
},
{
"command": "java.project.removeLibrary",
"when": "false"
Expand Down Expand Up @@ -465,6 +475,7 @@
},
{
"command": "java.project.addLibraries",
"alt": "java.project.addLibraryFolders",
"when": "view == javaProjectExplorer && viewItem =~ /java:container(?=.*?\\b\\+referencedLibrary\\b)/",
"group": "inline@0"
},
Expand Down
5 changes: 3 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"description": "Manage Java projects in Visual Studio Code",
"contributes.commands.java.project.create": "Create Java Project...",
"contributes.commands.java.project.addLibraries": "Add a jar file or a folder to project classpath",
"contributes.commands.java.project.removeLibrary": "Remove jar file from project classpath",
"contributes.commands.java.project.addLibraries": "Add Jar Libraries to Project Classpath...",
"contributes.commands.java.project.addLibraryFolders": "Add Library Folders to Project Classpath...",
"contributes.commands.java.project.removeLibrary": "Remove Library from Project Classpath",
"contributes.commands.java.view.package.refresh": "Refresh",
"contributes.commands.java.project.build.workspace": "Build Workspace",
"contributes.commands.java.project.clean.workspace": "Clean Workspace",
Expand Down
5 changes: 3 additions & 2 deletions package.nls.zh.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"description": "在 Visual Studio Code 中管理 Java 项目",
"contributes.commands.java.project.create": "创建 Java 项目...",
"contributes.commands.java.project.addLibraries": "将一个 Jar 文件或一个目录添加到 Java 项目类路径中",
"contributes.commands.java.project.removeLibrary": "将该 Jar 文件从 Java 项目类路径中移除",
"contributes.commands.java.project.addLibraries": "TRANSLATION NEEDED...",
"contributes.commands.java.project.addLibraryFolders": "TRANSLATION NEEDED...",
"contributes.commands.java.project.removeLibrary": "TRANSLATION NEEDED? (English wording changed slightly, see previous commit)",
"contributes.commands.java.view.package.refresh": "刷新",
"contributes.commands.java.project.build.workspace": "构建工作空间",
"contributes.commands.java.project.clean.workspace": "清理工作空间",
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export namespace Commands {

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

export const JAVA_PROJECT_ADD_LIBRARY_FOLDERS = "java.project.addLibraryFolders";

export const JAVA_PROJECT_REMOVE_LIBRARY = "java.project.removeLibrary";

export const JAVA_PROJECT_REFRESH_LIBRARIES = "java.project.refreshLibraries";
Expand Down
48 changes: 23 additions & 25 deletions src/controllers/libraryController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as fse from "fs-extra";
import * as _ from "lodash";
import * as minimatch from "minimatch";
import * as path from "path";
Expand All @@ -20,6 +19,7 @@ export class LibraryController implements Disposable {
public constructor(public readonly context: ExtensionContext) {
this.disposable = Disposable.from(
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_ADD_LIBRARIES, () => this.addLibraries()),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_ADD_LIBRARY_FOLDERS, () => this.addLibraries(true)),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REMOVE_LIBRARY, (node: DataNode) =>
node.uri && this.removeLibrary(Uri.parse(node.uri).fsPath)),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REFRESH_LIBRARIES, () =>
Expand All @@ -31,36 +31,34 @@ export class LibraryController implements Disposable {
this.disposable.dispose();
}

public async addLibraries(libraryGlobs?: string[]) {
if (!libraryGlobs) {
libraryGlobs = [];
const workspaceFolder: WorkspaceFolder | undefined = Utility.getDefaultWorkspaceFolder();
const isWindows = process.platform.indexOf("win") === 0;
const results: Uri[] | undefined = await window.showOpenDialog({
defaultUri: workspaceFolder && workspaceFolder.uri,
canSelectFiles: true,
canSelectFolders: isWindows ? false : true,
canSelectMany: true,
openLabel: isWindows ? "Select jar files" : "Select jar files or directories",
filters: { Library: ["jar"] },
});
if (!results) {
return;
}
libraryGlobs = await Promise.all(results.map(async (uri: Uri) => {
// keep the param: `includeWorkspaceFolder` to false here
// since the multi-root is not supported well for invisible projects
const uriPath = workspace.asRelativePath(uri, false);
return (await fse.stat(uri.fsPath)).isDirectory() ? `${uriPath}/**/*.jar` : uriPath;
}));
}

public async addLibraryGlobs(libraryGlobs: string[]) {
const setting = Settings.referencedLibraries();
setting.exclude = this.dedupAlreadyCoveredPattern(libraryGlobs, ...setting.exclude);
setting.include = this.updatePatternArray(setting.include, ...libraryGlobs);
Settings.updateReferencedLibraries(setting);
}

public async addLibraries(folders?: boolean) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the param name to canSelectFolders to just align with the API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, fixed in a735dc5.

const workspaceFolder: WorkspaceFolder | undefined = Utility.getDefaultWorkspaceFolder();
const results: Uri[] | undefined = await window.showOpenDialog({
defaultUri: workspaceFolder && workspaceFolder.uri,
canSelectFiles: !folders,
canSelectFolders: folders,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possible regression here is: for the Mac users, they actually lose the capability to select both files and folders in one dialog.

So maybe still also check if it is in MacOS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think it's better for the sake of simplicity to keep folder selection specific to the more advanced alt command. Since I don't think we can change the command title based on OS (without creating a third command that only displays on macOS), there is no consistent way to communicate with the user that both Jar files and folders can be selected when running macOS. However, I do agree that removing the capability is a regression (which might bother some users), so I added back folder selection to the addLibraries dialog on macOS in a735dc5.

canSelectMany: true,
openLabel: folders ? "Select Library Folders" : "Select Jar Libraries",
filters: folders ? { Folders: ["*"] } : { "Jar Files": ["jar"] },
});
if (!results) {
return;
}
this.addLibraryGlobs(await Promise.all(results.map(async (uri: Uri) => {
// keep the param: `includeWorkspaceFolder` to false here
// since the multi-root is not supported well for invisible projects
const uriPath = workspace.asRelativePath(uri, false);
return folders ? uriPath + "/**/*.jar" : uriPath;
})));
}

public async removeLibrary(removalFsPath: string) {
const setting = Settings.referencedLibraries();
const removedPaths = _.remove(setting.include, (include) => {
Expand Down