diff --git a/CHANGELOG.md b/CHANGELOG.md index df3e26c..70b02a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to the "vscode-maven" extension will be documented in this f ## 0.40.0 ### Added - Created a new FavoritesMenu. This menu allows shortcuts to execute the favorite commands. [#884](https://github.com/microsoft/vscode-maven/issues/884) +- Created a UI to add favorites into the user workspace scope. [#901](https://github.com/microsoft/vscode-maven/issues/901) ## 0.39.2 ### Fixed diff --git a/package.json b/package.json index 89b236c..f67b8d9 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "onCommand:maven.plugin.execute", "onCommand:maven.project.addDependency", "onCommand:maven.project.excludeDependency", + "onCommand:maven.project.addFavorite", "onView:mavenProjects" ], "main": "./dist/extension", @@ -222,6 +223,12 @@ "category": "Maven", "icon": "$(wrench)" }, + { + "command": "maven.project.addFavorite", + "title": "%contributes.commands.maven.project.addFavorite%", + "category": "Maven", + "icon": "$(add)" + }, { "command": "maven.dependency.goToEffective", "title": "%contributes.commands.maven.dependency.goToEffective%", @@ -556,6 +563,11 @@ "when": "view == mavenProjects && viewItem == maven:dependenciesMenu", "group": "inline@0" }, + { + "command": "maven.project.addFavorite", + "when": "view == mavenProjects && viewItem == maven:favoritesMenu", + "group": "inline@0" + }, { "command": "maven.explorer.refresh", "when": "view == mavenProjects && viewItem == maven:dependenciesMenu", diff --git a/package.nls.json b/package.nls.json index 9838201..653b677 100644 --- a/package.nls.json +++ b/package.nls.json @@ -15,10 +15,11 @@ "contributes.commands.maven.view.flat": "Switch to flat view", "contributes.commands.maven.project.addDependency": "Add a dependency...", "contributes.commands.maven.project.showDependencies": "Show Dependencies", + "contributes.commands.maven.project.goToDefinition": "Go to Definition", "contributes.commands.maven.project.excludeDependency": "Exclude Dependency", "contributes.commands.maven.project.setDependencyVersion": "Resolve Conflict...", + "contributes.commands.maven.project.addFavorite": "Add a favorite...", "contributes.commands.maven.dependency.goToEffective": "Go to Effective Dependency", - "contributes.commands.maven.project.goToDefinition": "Go to Definition", "contributes.views.explorer.mavenProjects": "Maven", "contributes.viewsWelcome.mavenProjects.untrustedWorkspaces": "Advanced features (e.g. executing lifecycle phases and plugin goals) are disabled in Restricted Mode for security concern.\nPOM editing assistance (e.g. [add a dependency](command:maven.project.addDependency)) is still available.\nLearn more about [Workspace Trust](https://aka.ms/vscode-workspace-trust).\n[Manage Workspace Trust](command:workbench.trust.manage)", "configuration.maven.excludedFolders": "Specifies file path pattern of folders to exclude while searching for Maven projects.", diff --git a/package.nls.zh-cn.json b/package.nls.zh-cn.json index 0159ad0..0fab04f 100644 --- a/package.nls.zh-cn.json +++ b/package.nls.zh-cn.json @@ -18,6 +18,7 @@ "contributes.commands.maven.project.goToDefinition": "转到定义", "contributes.commands.maven.project.excludeDependency": "删除依赖", "contributes.commands.maven.project.setDependencyVersion": "指定依赖版本为...", + "contributes.commands.maven.project.addFavorite": "添加常用命令...", "contributes.commands.maven.dependency.goToEffective": "转到有效的依赖项", "contributes.views.explorer.mavenProjects": "Maven", "contributes.viewsWelcome.mavenProjects.untrustedWorkspaces": "高级功能(例如:执行生命周期阶段与插件的目标命令)将会因为安全考量在限制模式中被停用。\nPOM编辑辅助功能(例如:[添加依赖](command:maven.project.addDependency))仍可以使用。\n学习更多关于[工作区信任](https://aka.ms/vscode-workspace-trust)\n[管理工作区信任](command:workbench.trust.manage)", diff --git a/package.nls.zh-tw.json b/package.nls.zh-tw.json index acde3e4..1e714fd 100644 --- a/package.nls.zh-tw.json +++ b/package.nls.zh-tw.json @@ -15,10 +15,11 @@ "contributes.commands.maven.view.flat": "切換到扁平檢視", "contributes.commands.maven.project.addDependency": "新增相依套件…", "contributes.commands.maven.project.showDependencies": "顯示所有相依套件", + "contributes.commands.maven.project.goToDefinition": "移至定義", "contributes.commands.maven.project.excludeDependency": "刪除相依套件", "contributes.commands.maven.project.setDependencyVersion": "指定相依套件版本為...", + "contributes.commands.maven.project.addFavorite": "添加常用命令...", "contributes.commands.maven.dependency.goToEffective": "移至有效的相依套件項", - "contributes.commands.maven.project.goToDefinition": "移至定義", "contributes.views.explorer.mavenProjects": "Maven", "contributes.viewsWelcome.mavenProjects.untrustedWorkspaces": "進階功能 (例如: 執行生命週期階段與 Plugin 的目標命令) 將會因為安全考量在限制模式中被停用。\nPOM 編輯輔助功能 (例如: [新增相依套件](command:maven.project.addDependency)) 還是可以使用。\n學習更多關於 [工作區信任](https://aka.ms/vscode-workspace-trust)\n[管理工作區信任](command:workbench.trust.manage)", "configuration.maven.excludedFolders": "指定搜尋 Maven 專案時要排除的文件夾。", diff --git a/src/Settings.ts b/src/Settings.ts index d397da4..c8897cc 100644 --- a/src/Settings.ts +++ b/src/Settings.ts @@ -6,6 +6,7 @@ import { Uri, workspace } from "vscode"; import { FavoriteCommand } from "./explorer/model/FavoriteCommand"; import { MavenProject } from "./explorer/model/MavenProject"; +type FavoriteFormat = {alias: string; command: string; debug?: boolean} export namespace Settings { export function excludedFolders(resource: Uri): string[] { const ret: string[] | undefined = _getMavenSection("excludedFolders", resource); @@ -32,6 +33,12 @@ export namespace Settings { workspace.getConfiguration().update("maven.view", "hierarchical", false); } + export function storeFavorite(favorite: FavoriteFormat): void { + const favorites: FavoriteFormat[] = workspace.getConfiguration().get("maven.terminal.favorites") ?? []; + favorites.push(favorite); + workspace.getConfiguration().update("maven.terminal.favorites", favorites); + } + export function setMavenExecutablePath(mvnPath: string): void { workspace.getConfiguration().update("maven.executable.path", mvnPath, true); } diff --git a/src/explorer/model/FavoritesMenu.ts b/src/explorer/model/FavoritesMenu.ts index 2cbf0fb..9e94cb4 100644 --- a/src/explorer/model/FavoritesMenu.ts +++ b/src/explorer/model/FavoritesMenu.ts @@ -15,6 +15,10 @@ export class FavoritesMenu extends Menu implements ITreeItem { this.name = "Favorites"; } + public getContextValue(): string { + return "maven:favoritesMenu"; + } + public async getChildren(): Promise { return Settings.Terminal.favorites(this.project); } diff --git a/src/extension.ts b/src/extension.ts index 6f36145..8e0c544 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -27,11 +27,12 @@ import { pluginInfoProvider } from "./explorer/pluginInfoProvider"; import { debugHandler } from "./handlers/debugHandler"; import { addDependencyHandler } from "./handlers/dependency/addDependencyHandler"; import { excludeDependencyHandler } from "./handlers/dependency/excludeDependencyHandler"; +import { addFavoriteHandler } from "./handlers/favorites/addFavoriteHandler"; import { goToEffectiveHandler } from "./handlers/dependency/goToEffectiveHandler"; import { jumpToDefinitionHandler } from "./handlers/dependency/jumpToDefinitionHandler"; import { setDependencyVersionHandler } from "./handlers/dependency/setDependencyVersionHandler"; import { showDependenciesHandler } from "./handlers/dependency/showDependenciesHandler"; -import { runFavoriteCommandsHandler } from "./handlers/runFavoriteCommandsHandler"; +import { runFavoriteCommandsHandler } from "./handlers/favorites/runFavoriteCommandsHandler"; import { hoverProvider } from "./hover/hoverProvider"; import { registerArtifactSearcher } from "./jdtls/artifactSearcher"; import { isJavaExtEnabled } from "./jdtls/commands"; @@ -134,6 +135,9 @@ async function doActivate(_operationId: string, context: vscode.ExtensionContext registerCommand(context, "maven.project.setDependencyVersion", setDependencyVersionHandler); registerCommand(context, "maven.project.goToDefinition", jumpToDefinitionHandler); + // favorites + registerCommand(context, "maven.project.addFavorite", addFavoriteHandler); + // debug registerCommand(context, "maven.plugin.debug", debugHandler); vscode.debug.onDidTerminateDebugSession((session) => { @@ -202,6 +206,7 @@ function registerConfigChangeListener(context: vscode.ExtensionContext): void { if (e.affectsConfiguration("maven.view") || e.affectsConfiguration("maven.pomfile.globPattern") || e.affectsConfiguration("maven.explorer.projectName") + || e.affectsConfiguration("maven.terminal.favorites") ) { MavenExplorerProvider.getInstance().refresh(); } diff --git a/src/handlers/favorites/addFavoriteHandler.ts b/src/handlers/favorites/addFavoriteHandler.ts new file mode 100644 index 0000000..7c714a5 --- /dev/null +++ b/src/handlers/favorites/addFavoriteHandler.ts @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. +import { Settings } from "../../Settings"; +import * as vscode from "vscode"; + +export async function addFavoriteHandler() { + + const command = await vscode.window.showInputBox({ + title: "Add favorite", + ignoreFocusOut: true, + prompt: "Input a command for your favorite execute.", + placeHolder: "e.g. clean install", + validateInput: (text: string) => { + if (text.trim().length < 2) { + return "Command is too short."; + } + return undefined; + } + }); + + if (!command) { + return; + } + + const alias = await vscode.window.showInputBox({ + title: "Add favorite", + ignoreFocusOut: true, + prompt: "Input an alias for your favorite.", + placeHolder: "e.g. Clean and Build Project", + value: command, + validateInput: (text: string) => { + if (text.trim().length < 3) { + return "Favorite is too short."; + } + return undefined; + } + }); + + if (!alias) { + return; + } + + const executionMode = await vscode.window.showQuickPick( + ["Run", "Debug"], + { + title: "Add favorite", + placeHolder: "Select the command execution mode...", + } + ); + + if (!executionMode) { + return; + } + + // store favorite into workspace settings + const debug: boolean = executionMode === 'Debug'; + Settings.storeFavorite({alias, command, debug}); +} \ No newline at end of file diff --git a/src/handlers/runFavoriteCommandsHandler.ts b/src/handlers/favorites/runFavoriteCommandsHandler.ts similarity index 82% rename from src/handlers/runFavoriteCommandsHandler.ts rename to src/handlers/favorites/runFavoriteCommandsHandler.ts index 78889f6..ef0aa9e 100644 --- a/src/handlers/runFavoriteCommandsHandler.ts +++ b/src/handlers/favorites/runFavoriteCommandsHandler.ts @@ -1,59 +1,59 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT license. - -import * as _ from "lodash"; -import * as vscode from "vscode"; -import { MavenProject } from "../explorer/model/MavenProject"; -import { Settings } from "../Settings"; -import { executeInTerminal } from "../utils/mavenUtils"; -import { selectProjectIfNecessary } from "../utils/uiUtils"; -import { debugCommand, IDebugOptions } from "./debugHandler"; -import { FavoriteCommand } from "../explorer/model/FavoriteCommand"; - -export async function runFavoriteCommandsHandler(project: MavenProject | undefined, command?: FavoriteCommand): Promise { - let selectedProject: MavenProject | undefined = project; - if (!selectedProject) { - selectedProject = await selectProjectIfNecessary(); - } - if (!selectedProject) { - return; - } - const favorites: FavoriteCommand[] | undefined = Settings.Terminal.favorites(selectedProject); - if (!favorites || _.isEmpty(favorites)) { - const BUTTON_OPEN_SETTINGS: string = "Open Settings"; - const choice: string | undefined = await vscode.window.showInformationMessage("Found no favorite commands. You can specify `maven.terminal.favorites` in Settings.", BUTTON_OPEN_SETTINGS); - if (choice === BUTTON_OPEN_SETTINGS) { - await vscode.commands.executeCommand("workbench.action.openSettings", "maven.terminal.favorites"); - } - return; - } - - let selectedCommand: FavoriteCommand | undefined = command; - if (!selectedCommand) { - selectedCommand = await vscode.window.showQuickPick( - favorites.map(item => ({ - value: item, - label: item.alias, - description: item.command - })), { - ignoreFocusOut: true, - placeHolder: "Select a favorite command ...", - matchOnDescription: true - } - ).then(item => item ? item.value : undefined); - } - if (!selectedCommand) { - return; - } - - const config: IDebugOptions = { - command: selectedCommand.command, - pomfile: selectedProject.pomPath, - projectName: selectedProject.artifactId - }; - if (selectedCommand.debug) { - await debugCommand(config); - } else { - await executeInTerminal(config); - } -} +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +import * as _ from "lodash"; +import * as vscode from "vscode"; +import { MavenProject } from "../../explorer/model/MavenProject"; +import { Settings } from "../../Settings"; +import { executeInTerminal } from "../../utils/mavenUtils"; +import { selectProjectIfNecessary } from "../../utils/uiUtils"; +import { debugCommand, IDebugOptions } from "../debugHandler"; +import { FavoriteCommand } from "../../explorer/model/FavoriteCommand"; + +export async function runFavoriteCommandsHandler(project: MavenProject | undefined, command?: FavoriteCommand): Promise { + let selectedProject: MavenProject | undefined = project; + if (!selectedProject) { + selectedProject = await selectProjectIfNecessary(); + } + if (!selectedProject) { + return; + } + const favorites: FavoriteCommand[] | undefined = Settings.Terminal.favorites(selectedProject); + if (!favorites || _.isEmpty(favorites)) { + const BUTTON_OPEN_SETTINGS: string = "Open Settings"; + const choice: string | undefined = await vscode.window.showInformationMessage("Found no favorite commands. You can specify `maven.terminal.favorites` in Settings.", BUTTON_OPEN_SETTINGS); + if (choice === BUTTON_OPEN_SETTINGS) { + await vscode.commands.executeCommand("workbench.action.openSettings", "maven.terminal.favorites"); + } + return; + } + + let selectedCommand: FavoriteCommand | undefined = command; + if (!selectedCommand) { + selectedCommand = await vscode.window.showQuickPick( + favorites.map(item => ({ + value: item, + label: item.alias, + description: item.command + })), { + ignoreFocusOut: true, + placeHolder: "Select a favorite command ...", + matchOnDescription: true + } + ).then(item => item ? item.value : undefined); + } + if (!selectedCommand) { + return; + } + + const config: IDebugOptions = { + command: selectedCommand.command, + pomfile: selectedProject.pomPath, + projectName: selectedProject.artifactId + }; + if (selectedCommand.debug) { + await debugCommand(config); + } else { + await executeInTerminal(config); + } +} diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index e27e294..947c451 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -14,7 +14,7 @@ import { DEFAULT_MAVEN_LIFECYCLES } from "../completion/constants"; import { FavoriteCommand } from "../explorer/model/FavoriteCommand"; import { LifecyclePhase } from "../explorer/model/LifecyclePhase"; import { MavenProject } from "../explorer/model/MavenProject"; -import { runFavoriteCommandsHandler } from "../handlers/runFavoriteCommandsHandler"; +import { runFavoriteCommandsHandler } from "../handlers/favorites/runFavoriteCommandsHandler"; import { MavenProjectManager } from "../project/MavenProjectManager"; import { Settings } from "../Settings"; import { getExtensionVersion, getPathToTempFolder, getPathToWorkspaceStorage } from "./contextUtils";