Skip to content

Commit

Permalink
#289 Adding a context option to Open in Solution Explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandoescolar committed Dec 11, 2023
1 parent 3220e90 commit 4c59f38
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 9 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
}
],
"commands": [
{
"command": "solutionExplorer.openSelectedSolution",
"title": "Open in Solution Explorer",
"icon": "$(folder-opened)"
},
{
"command": "solutionExplorer.openSolution",
"title": "Open Solution",
Expand Down Expand Up @@ -328,6 +333,13 @@
}
],
"menus": {
"explorer/context": [
{
"command": "solutionExplorer.openSelectedSolution",
"when": "resourceExtname == .sln",
"group": "navigation"
}
],
"view/title": [
{
"command": "solutionExplorer.openSolution",
Expand Down
6 changes: 5 additions & 1 deletion src/SolutionExplorerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ export class SolutionExplorerCommands {
this.commands['openSolution'] = [new cmds.OpenSolutionCommand(eventAggregator),
undefined];

this.commands['openSelectedSolution'] = [new cmds.OpenSolutionFromDefaultExplorerCommand(eventAggregator),
undefined];

this.commands['deleteMultiple'] = [new cmds.DeleteUnifiedCommand(),
[ContextValues.multipleSelection]];
}
Expand All @@ -177,7 +180,8 @@ export class SolutionExplorerCommands {
vscode.commands.registerCommand(name, async (arg) => {
const clickedItem = arg instanceof TreeItem ? arg : undefined;
const selectedItems = this.provider.getSelectedItems();
const actions = await command.getActionsBase(clickedItem, selectedItems);
const ctx = new cmds.ActionCommandContext(clickedItem, selectedItems, arg);
const actions = await command.getActionsBase(ctx);
if (actions.length > 0) {
await this.actionsRunner.run(actions, { isCancellationRequested: false });
}
Expand Down
7 changes: 6 additions & 1 deletion src/commands/ActionsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { TreeItem } from "@tree";
import { Action } from "@actions";

export class ActionCommandContext {
constructor(public readonly clickedItem: TreeItem | undefined, public readonly selectedItems: readonly TreeItem[] | undefined, public readonly args: any) {
}
}

export abstract class ActionsCommand {
constructor(protected title: string) {
}

public abstract getActionsBase(clickedItem: TreeItem | undefined, selectedItems: readonly TreeItem[] | undefined):
public abstract getActionsBase(ctx: ActionCommandContext):
Promise<Action[]>;
}

Expand Down
7 changes: 3 additions & 4 deletions src/commands/DeleteUnifiedCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TreeItem, ContextValues } from "@tree";
import { ActionsCommand, prepareContextActionGetters } from "@commands";
import { ActionCommandContext, ActionsCommand, prepareContextActionGetters } from "@commands";
import {
Action,
DeleteProjectFile,
Expand All @@ -17,10 +17,9 @@ export class DeleteUnifiedCommand extends ActionsCommand {
super('Delete');
}

public async getActionsBase(clickedItem: TreeItem | undefined, selectedItems: readonly TreeItem[] | undefined):
Promise<Action[]> {
public async getActionsBase(ctx: ActionCommandContext): Promise<Action[]> {

const clickedItems = this.getClickedItems(clickedItem, selectedItems);
const clickedItems = this.getClickedItems(ctx.clickedItem, ctx.selectedItems);

const topClickedItems = clickedItems.filter(item => !this.includedInFolder(clickedItems, item));

Expand Down
2 changes: 2 additions & 0 deletions src/commands/OpenSolutionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export class OpenSolutionCommand extends SingleItemActionsCommand {
return [new OpenSolution(solutionPath, this.eventAggregator)];
}
}


18 changes: 18 additions & 0 deletions src/commands/OpenSolutionFromDefaultExplorerCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IEventAggregator } from "@events";
import { Action, OpenSolution } from "@actions";
import { SingleItemActionsFromDefaultExplorerCommand } from "./SingleItemActionsFromDefaultExplorerCommand";


export class OpenSolutionFromDefaultExplorerCommand extends SingleItemActionsFromDefaultExplorerCommand {
constructor(private readonly eventAggregator: IEventAggregator) {
super('Open Solution');
}

public shouldRun(item: string): boolean {
return item.toLocaleLowerCase().endsWith('.sln');
}

public async getActions(item: string): Promise<Action[]> {
return [new OpenSolution(item, this.eventAggregator)];
}
}
8 changes: 5 additions & 3 deletions src/commands/SingleItemActionsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { TreeItem } from "@tree";
import { Action } from "@actions";
import { ActionsCommand } from "./ActionsCommand";
import { ActionCommandContext, ActionsCommand } from "./ActionsCommand";

export abstract class SingleItemActionsCommand extends ActionsCommand {
constructor(title: string) {
super(title);
}

public async getActionsBase(clickedItem: TreeItem | undefined, selectedItems: readonly TreeItem[] | undefined): Promise<Action[]> {
const item = clickedItem ?? (selectedItems?.length === 1 ? selectedItems[0] : undefined);
public async getActionsBase(ctx: ActionCommandContext): Promise<Action[]> {
const item = ctx.clickedItem ?? (ctx.selectedItems?.length === 1 ? ctx.selectedItems[0] : undefined);
return this.shouldRun(item) ? this.getActions(item) : [];
}

public abstract shouldRun(item: TreeItem | undefined): boolean;

public abstract getActions(item: TreeItem | undefined): Promise<Action[]>;
}


22 changes: 22 additions & 0 deletions src/commands/SingleItemActionsFromDefaultExplorerCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Action } from "@actions";
import { ActionCommandContext, ActionsCommand } from "./ActionsCommand";



export abstract class SingleItemActionsFromDefaultExplorerCommand extends ActionsCommand {
constructor(title: string) {
super(title);
}

public async getActionsBase(ctx: ActionCommandContext): Promise<Action[]> {
if (!!ctx && !!ctx.args && !!ctx.args.path) {
return this.shouldRun(ctx.args.path) ? this.getActions(ctx.args.path) : [];
}

return [];
}

public abstract shouldRun(item: string): boolean;

public abstract getActions(item: string): Promise<Action[]>;
}
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from "./MoveFileDownCommand";
export * from "./MoveToSolutionFolderCommand";
export * from "./OpenFileCommand";
export * from "./OpenSolutionCommand";
export * from "./OpenSolutionFromDefaultExplorerCommand";
export * from "./PackCommand";
export * from "./PasteCommand";
export * from "./PublishCommand";
Expand Down

0 comments on commit 4c59f38

Please sign in to comment.