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

Created new Favorites Menu as described in the issue #884 #900

Merged
merged 6 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
22 changes: 21 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-maven",
"displayName": "Maven for Java",
"description": "%description%",
"version": "0.39.2",
"version": "0.40.0",
"icon": "resources/logo.png",
"publisher": "vscjava",
"preview": true,
Expand Down Expand Up @@ -171,6 +171,12 @@
"category": "Maven",
"icon": "$(play)"
},
{
"command": "maven.goal.execute.fromFavoritesMenu",
"title": "%contributes.commands.maven.plugin.execute%",
"category": "Maven",
"icon": "$(play)"
},
{
"command": "maven.plugin.execute",
"title": "%contributes.commands.maven.plugin.execute%",
Expand Down Expand Up @@ -322,6 +328,10 @@
"command": "maven.goal.execute.fromLifecycleMenu",
"when": "never"
},
{
"command": "maven.goal.execute.fromFavoritesMenu",
"when": "never"
},
{
"command": "maven.plugin.execute",
"when": "never"
Expand Down Expand Up @@ -560,6 +570,16 @@
"command": "maven.goal.execute.fromLifecycleMenu",
"when": "view == mavenProjects && viewItem == maven:lifecycle",
"group": "1@1"
},
{
"command": "maven.goal.execute.fromFavoritesMenu",
"when": "view == mavenProjects && viewItem == maven:favorites",
"group": "inline"
},
{
"command": "maven.goal.execute.fromFavoritesMenu",
"when": "view == mavenProjects && viewItem == maven:favorites",
"group": "1@1"
}
],
"javaProject.maven": [
Expand Down
8 changes: 6 additions & 2 deletions src/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as vscode from "vscode";
import { Uri, workspace } from "vscode";
import { FavoriteCommand } from "./explorer/model/FavoriteCommand";
import { MavenProject } from "./explorer/model/MavenProject";

export namespace Settings {
export function excludedFolders(resource: Uri): string[] {
Expand Down Expand Up @@ -59,8 +62,9 @@ export namespace Settings {
return _getMavenSection("terminal.customEnv", resourceOrFilepath);
}

export function favorites(resource: Uri): { alias: string; command: string }[] | undefined {
return _getMavenSection("terminal.favorites", resource);
export function favorites(project: MavenProject): FavoriteCommand[] | undefined {
const favorites: {alias: string, command: string, debug?: boolean}[] | undefined = _getMavenSection("terminal.favorites", vscode.Uri.file(project.pomPath));
return favorites?.map(favorite => new FavoriteCommand(project, favorite.command, favorite.alias, favorite.debug));
}
}
export namespace Executable {
Expand Down
23 changes: 23 additions & 0 deletions src/explorer/model/FavoriteCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as vscode from "vscode";
import { TreeItem } from "vscode";
import { ITreeItem } from "./ITreeItem";
import { MavenProject } from "./MavenProject";

export class FavoriteCommand implements ITreeItem {

constructor(public project: MavenProject, public command: string, public alias: string, public debug?: boolean) {}

getContextValue(): string {
return "maven:favorites";
}

getTreeItem(): TreeItem | Thenable<TreeItem> {
const treeItem: vscode.TreeItem = new vscode.TreeItem(this.alias, vscode.TreeItemCollapsibleState.None);
gustavosimon marked this conversation as resolved.
Show resolved Hide resolved
treeItem.iconPath = new vscode.ThemeIcon("gear");
return treeItem;
}

}
27 changes: 27 additions & 0 deletions src/explorer/model/FavoritesMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as vscode from "vscode";
import { Menu } from "./Menu";
import { ITreeItem } from "./ITreeItem";
import { MavenProject } from "./MavenProject";
import { FavoriteCommand } from "./FavoriteCommand";
import { Settings } from "../../Settings";

export class FavoritesMenu extends Menu implements ITreeItem {

constructor(project: MavenProject) {
super(project);
this.name = "Favorites";
}

public async getChildren(): Promise<FavoriteCommand[] | undefined> {
return Settings.Terminal.favorites(this.project);
}

public getTreeItem(): vscode.TreeItem | Thenable<vscode.TreeItem> {
const treeItem: vscode.TreeItem = new vscode.TreeItem(this.name, vscode.TreeItemCollapsibleState.Collapsed);
treeItem.iconPath = new vscode.ThemeIcon("star-empty");
return treeItem;
}
}
1 change: 1 addition & 0 deletions src/explorer/model/ITreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface ITreeItem {
* If implemented, it will be triggered to get children items.
*/
getChildren?(): ITreeItem[] | undefined | Promise<ITreeItem[] | undefined>;

/**
* If implemented, it will be triggered to refresh tree item.
*/
Expand Down
1 change: 1 addition & 0 deletions src/explorer/model/LifecycleMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MavenProject } from "./MavenProject";
import { Menu } from "./Menu";

export class LifecycleMenu extends Menu implements ITreeItem {

constructor(project: MavenProject) {
super(project);
this.name = "Lifecycle";
Expand Down
6 changes: 3 additions & 3 deletions src/explorer/model/LifecyclePhase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

Expand All @@ -7,12 +6,13 @@ import { ITreeItem } from "./ITreeItem";
import { MavenProject } from "./MavenProject";

export class LifecyclePhase implements ITreeItem {
constructor(public project: MavenProject, public phase: string) {
}

constructor(public project: MavenProject, public phase: string) {}

public getContextValue(): string {
return "maven:lifecycle";
}

public getTreeItem(): vscode.TreeItem | Thenable<vscode.TreeItem> {
const treeItem: vscode.TreeItem = new vscode.TreeItem(this.phase, vscode.TreeItemCollapsibleState.None);
treeItem.iconPath = new vscode.ThemeIcon("gear");
Expand Down
2 changes: 2 additions & 0 deletions src/explorer/model/MavenProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ITreeItem } from "./ITreeItem";
import { LifecycleMenu } from "./LifecycleMenu";
import { MavenPlugin } from "./MavenPlugin";
import { PluginsMenu } from "./PluginsMenu";
import { FavoritesMenu } from "./FavoritesMenu";

const CONTEXT_VALUE: string = "maven:project";

Expand Down Expand Up @@ -165,6 +166,7 @@ export class MavenProject implements ITreeItem {
ret.push(new LifecycleMenu(this));
ret.push(new PluginsMenu(this));
ret.push(new DependenciesMenu(this));
ret.push(new FavoritesMenu(this));
if (this.moduleNames.length > 0 && Settings.viewType() === "hierarchical") {
const projects: MavenProject[] = this.modules.map(m => MavenProjectManager.get(m)).filter(Boolean) as MavenProject[];
ret.push(...projects);
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async function doActivate(_operationId: string, context: vscode.ExtensionContext
registerCommandRequiringTrust(context, "maven.goal.execute", Utils.executeMavenCommand);
registerCommandRequiringTrust(context, "maven.goal.execute.fromProjectManager", Utils.executeMavenCommand);
registerCommandRequiringTrust(context, "maven.goal.execute.fromLifecycleMenu", Utils.executeMavenCommand);
registerCommandRequiringTrust(context, "maven.goal.execute.fromFavoritesMenu", Utils.executeMavenCommand);
registerCommandRequiringTrust(context, "maven.plugin.execute", async (pluginGoal: PluginGoal) => await executeInTerminal({ command: pluginGoal.name, pomfile: pluginGoal.plugin.project.pomPath }));
registerCommand(context, "maven.view.flat", () => Settings.changeToFlatView());
registerCommand(context, "maven.view.hierarchical", () => Settings.changeToHierarchicalView());
Expand Down
31 changes: 17 additions & 14 deletions src/handlers/runFavoriteCommandsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ 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";

type FavoriteCommand = { command: string, alias: string, debug?: boolean };
export async function runFavoriteCommandsHandler(project: MavenProject | undefined): Promise<void> {
export async function runFavoriteCommandsHandler(project: MavenProject | undefined, command?: FavoriteCommand): Promise<void> {
let selectedProject: MavenProject | undefined = project;
if (!selectedProject) {
selectedProject = await selectProjectIfNecessary();
}
if (!selectedProject) {
return;
}
const favorites: FavoriteCommand[] | undefined = Settings.Terminal.favorites(vscode.Uri.file(selectedProject.pomPath));
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);
Expand All @@ -28,17 +28,20 @@ export async function runFavoriteCommandsHandler(project: MavenProject | undefin
return;
}

const selectedCommand: FavoriteCommand | undefined = 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);
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;
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { commands, Progress, ProgressLocation, Uri, window } from "vscode";
import { createUuid, setUserError } from "vscode-extension-telemetry-wrapper";
import * as xml2js from "xml2js";
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 { MavenProjectManager } from "../project/MavenProjectManager";
import { Settings } from "../Settings";
import { getExtensionVersion, getPathToTempFolder, getPathToWorkspaceStorage } from "./contextUtils";
Expand Down Expand Up @@ -211,6 +213,9 @@ export namespace Utils {
if (node instanceof LifecyclePhase) {
selectedProject = node.project;
selectedCommand = node.phase;
} else if (node instanceof FavoriteCommand) {
selectedProject = node.project;
selectedCommand = node.command;
} else if (node && node.uri) {
// for nodes from Project Manager
const pomPath: string = path.join(Uri.parse(node.uri).fsPath, "pom.xml");
Expand Down Expand Up @@ -250,6 +255,11 @@ export namespace Utils {
}
}

if (node instanceof FavoriteCommand){
await runFavoriteCommandsHandler(selectedProject, node);
return;
}

await commands.executeCommand(`maven.goal.${selectedCommand}`, selectedProject);
}

Expand Down