Skip to content

Commit

Permalink
#264 allow undefined selected item commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandoescolar committed Feb 21, 2023
1 parent fd5d57a commit 33bef6a
Show file tree
Hide file tree
Showing 34 changed files with 100 additions and 91 deletions.
8 changes: 4 additions & 4 deletions src/commands/AddExistingFileToSolutionFolderCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export class AddExistingFileToSolutionFolderCommand extends SingleItemActionsCom
super('Add existing file to solution folder');
}

public shouldRun(item: TreeItem): boolean {
return item && !!item.projectInSolution && (item.contextValue === ContextValues.solutionFolder);
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.projectInSolution && (item.contextValue === ContextValues.solutionFolder);
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
const filePath = await dialogs.openFile('Select a file to add');
if (!item.solution || !item.projectInSolution || !filePath) {
if (!item || !item.solution || !item.projectInSolution || !filePath) {
return [];
}

Expand Down
6 changes: 4 additions & 2 deletions src/commands/AddExistingProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export class AddExistingProjectCommand extends SingleItemActionsCommand {
super('Add existing project');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return !item || (item && !!item.path && (item.contextValue === ContextValues.solution || item.contextValue === ContextValues.solution + '-cps'));
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item) { return []; }

const solution = await dialogs.selectOption('Select solution', this.getSolutions(item));
const projectPath = await dialogs.openProjectFile('Select a project file to add');
if (!solution || !projectPath) {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/AddNewProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export class AddNewProjectCommand extends SingleItemActionsCommand {
super('Add new project');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return !item || (item && !!item.path && (item.contextValue === ContextValues.solution || item.contextValue === ContextValues.solution + '-cps'));
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
this.loadProjectTemplates();
this.wizard = dialogs.wizard('Add new project')
.selectOption('Select solution', this.getSolutions(item))
Expand Down Expand Up @@ -75,7 +75,7 @@ export class AddNewProjectCommand extends SingleItemActionsCommand {
];
}

private getSolutions(item: TreeItem): dialogs.ItemsOrItemsResolver {
private getSolutions(item: TreeItem | undefined): dialogs.ItemsOrItemsResolver {
if (item && item.path) {
const result: { [id: string]: string } = {};
result[item.label] = item.path;
Expand Down
6 changes: 3 additions & 3 deletions src/commands/AddPackageCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export class AddPackageCommand extends SingleItemActionsCommand {
super('Add package');
}

public shouldRun(item: TreeItem): boolean {
return item && !!item.project && item.project.type === 'cps';
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.project && item.project.type === 'cps';
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.project) { return []; }

const projectFullPath = item.project.fullPath;
Expand Down
6 changes: 3 additions & 3 deletions src/commands/AddProjectReferenceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export class AddProjectReferenceCommand extends SingleItemActionsCommand {
super('Add project reference');
}

public shouldRun(item: TreeItem): boolean {
return item && !!item.project && item.project.type === 'cps';
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.project && item.project.type === 'cps';
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.project) { return []; }

const projectPath = await dialogs.selectOption('Select project...', () => this.getCPSProjects(item));
Expand Down
6 changes: 3 additions & 3 deletions src/commands/BuildCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class BuildCommand extends SingleItemActionsCommand {
super('Build');
}

public shouldRun(item: TreeItem): boolean {
return item && (item.contextValue === ContextValues.project + '-cps' || item.contextValue === ContextValues.solution + '-cps');
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && (item.contextValue === ContextValues.project + '-cps' || item.contextValue === ContextValues.solution + '-cps');
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.path) { return []; }

return [ new Build(item.path) ];
Expand Down
6 changes: 3 additions & 3 deletions src/commands/CleanCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class CleanCommand extends SingleItemActionsCommand {
super('Clean');
}

public shouldRun(item: TreeItem): boolean {
return item && (item.contextValue === ContextValues.project + '-cps' || item.contextValue === ContextValues.solution + '-cps');
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && (item.contextValue === ContextValues.project + '-cps' || item.contextValue === ContextValues.solution + '-cps');
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.path) { return []; }

return [ new Clean(item.path) ];
Expand Down
4 changes: 2 additions & 2 deletions src/commands/CollapseAllCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export class CollapseAllCommand extends SingleItemActionsCommand {
super('Collapse All');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return true;
}

public getActions(item: TreeItem): Promise<Action[]> {
public getActions(item: TreeItem | undefined): Promise<Action[]> {
return Promise.resolve([
new CollapseAll(this.provider)
]);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/CopyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export class CopyCommand extends SingleItemActionsCommand {
super('Copy');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.path;
}

public getActions(item: TreeItem): Promise<Action[]> {
if(!item.path) { return Promise.resolve([]); }
public getActions(item: TreeItem | undefined): Promise<Action[]> {
if(!item || !item.path) { return Promise.resolve([]); }

return Promise.resolve([new Copy(item.path)]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/CreateFileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export class CreateFileCommand extends SingleItemActionsCommand {
super('Create file');
}

public shouldRun(item: TreeItem): boolean {
return item && !!item.project;
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.project;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.project || !item.path) { return []; }

this.workspaceRoot = item.workspaceRoot;
Expand Down
6 changes: 3 additions & 3 deletions src/commands/CreateFolderCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export class CreateFolderCommand extends SingleItemActionsCommand {
super('Create folder');
}

public shouldRun(item: TreeItem): boolean {
return !!item.project;
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.project;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.project || !item.path) { return []; }

const folderName = await dialogs.getText('New folder name');
Expand Down
4 changes: 2 additions & 2 deletions src/commands/CreateNewSolutionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export class CreateNewSolutionCommand extends SingleItemActionsCommand {
super('Create solution');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return true;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
const solutionName = await dialogs.getText('Solution name');
const workingFolder = vscode.workspace.rootPath;
if (!solutionName || !workingFolder) {
Expand Down
9 changes: 6 additions & 3 deletions src/commands/CreateSolutionFolderCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export class CreateSolutionFolderCommand extends SingleItemActionsCommand {
super('Create solution folder');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.solution;
}

public async getActions(item: TreeItem): Promise<Action[]> {
const folderName = await dialogs.getText('New folder name');
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.solution) {
return [];
}

const folderName = await dialogs.getText('New folder name');
if (!folderName) {
return [];
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/DuplicateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export class DuplicateCommand extends SingleItemActionsCommand {
super('Duplicate');
}

public shouldRun(item: TreeItem): boolean {
return item && !!item.path && item.contextValue.startsWith(ContextValues.projectFile);
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.path && item.contextValue.startsWith(ContextValues.projectFile);
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.project || !item.path) { return []; }

const filepath = await createCopyName(item.path);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/InstallWorkspaceTemplateFilesCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export class InstallWorkspaceTemplateFilesCommand extends SingleItemActionsComma
super('Install Workspace Template Files');
}

public shouldRun(item: TreeItem): boolean {
return item && !!item.workspaceRoot;
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.workspaceRoot;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.workspaceRoot) { return []; }

const workspace = item.workspaceRoot;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/MoveCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export class MoveCommand extends SingleItemActionsCommand {
super('Move');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.project && !!item.path && ( item.contextValue.startsWith(ContextValues.projectFile) || item.contextValue.startsWith(ContextValues.projectFolder) );
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.project || !item.path) { return []; }

const folders = await item.project?.getFolderList() ?? [];
Expand Down
6 changes: 4 additions & 2 deletions src/commands/MoveToSolutionFolderCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export class MoveToSolutionFolderCommand extends SingleItemActionsCommand {
super('Move to solution folder');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.solution && !!item.projectInSolution;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.solution) { return []; }

const folder = await dialogs.selectOption('Select folder...', () => this.getFolders(item.solution));
if (!folder) { return []; }

Expand Down
4 changes: 2 additions & 2 deletions src/commands/OpenFileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export class OpenFileCommand extends SingleItemActionsCommand {
super('Open file');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.path;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.path) { return []; }
if (!(await fs.exists(item.path))) {
return [];
Expand Down
4 changes: 2 additions & 2 deletions src/commands/OpenSolutionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export class OpenSolutionCommand extends SingleItemActionsCommand {
super('Open Solution');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return true;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
const solutionPath = await dialogs.openSolutionFile('Open solution');
if (!solutionPath) { return []; }

Expand Down
6 changes: 3 additions & 3 deletions src/commands/PackCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class PackCommand extends SingleItemActionsCommand {
super('Pack');
}

public shouldRun(item: TreeItem): boolean {
return item && (item.contextValue === ContextValues.project + '-cps' || item.contextValue === ContextValues.solution + '-cps');
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && (item.contextValue === ContextValues.project + '-cps' || item.contextValue === ContextValues.solution + '-cps');
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.path) { return []; }

return [ new Pack(item.path) ];
Expand Down
4 changes: 2 additions & 2 deletions src/commands/PasteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export class PasteCommand extends SingleItemActionsCommand {
super('Paste');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
if (item && item.path) { return true; }
return !!item && !!item.path && !!item.project;
}

public getActions(item: TreeItem): Promise<Action[]> {
public getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.path || !item.project) { return Promise.resolve([]); }

return Promise.resolve([new Paste(item.project, item.path)]);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/PublishCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class PublishCommand extends SingleItemActionsCommand {
super('Publish');
}

public shouldRun(item: TreeItem): boolean {
return item && (item.contextValue === ContextValues.project + '-cps' || item.contextValue === ContextValues.solution + '-cps');
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && (item.contextValue === ContextValues.project + '-cps' || item.contextValue === ContextValues.solution + '-cps');
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.path) { return []; }

return [ new Publish(item.path) ];
Expand Down
4 changes: 2 additions & 2 deletions src/commands/RefreshCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export class RefreshCommand extends SingleItemActionsCommand {
super('Refresh');
}

public shouldRun(item: TreeItem): boolean {
public shouldRun(item: TreeItem | undefined): boolean {
return true;
}

public getActions(item: TreeItem): Promise<Action[]> {
public getActions(item: TreeItem | undefined): Promise<Action[]> {
const result: Action[] = [];
if (item) {
result.push(new RefreshTreeItem(item));
Expand Down
6 changes: 3 additions & 3 deletions src/commands/RenameCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export class RenameCommand extends SingleItemActionsCommand {
super('Rename');
}

public shouldRun(item: TreeItem): boolean {
return !!item.project && !!item.path;
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.project && !!item.path;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item || !item.project || !item.path) { return []; }

const oldname = item.label;
Expand Down
8 changes: 5 additions & 3 deletions src/commands/RenameSolutionItemCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export class RenameSolutionItemCommand extends SingleItemActionsCommand {
super('Rename');
}

public shouldRun(item: TreeItem): boolean {
return !!item.solution;
public shouldRun(item: TreeItem | undefined): boolean {
return !!item && !!item.solution;
}

public async getActions(item: TreeItem): Promise<Action[]> {
public async getActions(item: TreeItem | undefined): Promise<Action[]> {
if (!item) { return []; }

const newname = await dialogs.getText('New name', 'New name', item.label);
if (!newname) { return []; }

Expand Down
Loading

0 comments on commit 33bef6a

Please sign in to comment.