Skip to content

Commit

Permalink
Simplify conditions for AddToRecommendations action
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Jun 24, 2018
1 parent 1b8ff83 commit 6923709
Showing 1 changed file with 45 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { ToggleViewletAction } from 'vs/workbench/browser/viewlet';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { Query } from 'vs/workbench/parts/extensions/common/extensionQuery';
import { IFileService, IContent } from 'vs/platform/files/common/files';
import { IWorkspaceContextService, WorkbenchState, IWorkspaceFolder, IWorkspaceFoldersChangeEvent } from 'vs/platform/workspace/common/workspace';
import { IWorkspaceContextService, WorkbenchState, IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { IWindowService, IWindowsService } from 'vs/platform/windows/common/windows';
import { IExtensionService, IExtensionDescription } from 'vs/workbench/services/extensions/common/extensions';
import URI from 'vs/base/common/uri';
Expand All @@ -38,7 +38,7 @@ import { ITextEditorSelection } from 'vs/platform/editor/common/editor';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { PagedModel } from 'vs/base/common/paging';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { IContextKeyService, RawContextKey, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
import { PICK_WORKSPACE_FOLDER_COMMAND_ID } from 'vs/workbench/browser/actions/workspaceCommands';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
Expand Down Expand Up @@ -1672,13 +1672,10 @@ export class ConfigureRecommendedExtensionsCommandsContributor extends Disposabl
private workspaceFolderContextKey = new RawContextKey<boolean>('workspaceFolderRecommendations', true);
private addToRecommendationsContextKey = new RawContextKey<boolean>('addToRecommendations', false);

private boundAddToRecommendationsContextKey: IContextKey<boolean>;

constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
@IEditorService private editorService: IEditorService,
@IFileService private fileService: IFileService,
@IWorkspaceContextService workspaceContextService: IWorkspaceContextService,
@IEditorService editorService: IEditorService
) {
super();
const boundWorkspaceContextKey = this.workspaceContextKey.bindTo(contextKeyService);
Expand All @@ -1689,17 +1686,9 @@ export class ConfigureRecommendedExtensionsCommandsContributor extends Disposabl
boundWorkspaceFolderContextKey.set(workspaceContextService.getWorkspace().folders.length > 0);
this._register(workspaceContextService.onDidChangeWorkspaceFolders(() => boundWorkspaceFolderContextKey.set(workspaceContextService.getWorkspace().folders.length > 0)));

this.boundAddToRecommendationsContextKey = this.addToRecommendationsContextKey.bindTo(contextKeyService);
this.updateAddToRecommendationsCommandContextKey();
this._register(editorService.onDidActiveEditorChange(() => {
this.updateAddToRecommendationsCommandContextKey();
}));
this._register(workspaceContextService.onDidChangeWorkspaceFolders((event: IWorkspaceFoldersChangeEvent) => {
const enabled = this.boundAddToRecommendationsContextKey.get();
if ((event.added.length && enabled) || (event.removed.length && !enabled)) {
this.updateAddToRecommendationsCommandContextKey();
}
}));
const boundAddToRecommendationsContextKey = this.addToRecommendationsContextKey.bindTo(contextKeyService);
boundAddToRecommendationsContextKey.set(editorService.activeEditor instanceof ExtensionsInput);
this._register(editorService.onDidActiveEditorChange(() => boundAddToRecommendationsContextKey.set(editorService.activeEditor instanceof ExtensionsInput)));

this.registerCommands();
}
Expand Down Expand Up @@ -1728,20 +1717,7 @@ export class ConfigureRecommendedExtensionsCommandsContributor extends Disposabl
});

CommandsRegistry.registerCommand(AddToWorkspaceRecommendationsAction.ID, serviceAccesor => {
this.boundAddToRecommendationsContextKey.set(false);
const extensionId = (this.editorService.activeEditor as ExtensionsInput).extension.id;
serviceAccesor
.get(IInstantiationService)
.createInstance(
AddToWorkspaceRecommendationsAction,
AddToWorkspaceRecommendationsAction.ID,
AddToWorkspaceRecommendationsAction.LABEL
)
.run(extensionId)
.then(
(quickPickCancelled) => this.boundAddToRecommendationsContextKey.set(quickPickCancelled),
() => this.boundAddToRecommendationsContextKey.set(this.editorService.activeEditor instanceof ExtensionsInput)
);
serviceAccesor.get(IInstantiationService).createInstance(AddToWorkspaceRecommendationsAction, AddToWorkspaceRecommendationsAction.ID, AddToWorkspaceRecommendationsAction.LABEL).run();
});
MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
command: {
Expand All @@ -1752,39 +1728,6 @@ export class ConfigureRecommendedExtensionsCommandsContributor extends Disposabl
});
}

private updateAddToRecommendationsCommandContextKey(): void {
if (!(this.editorService.activeEditor instanceof ExtensionsInput)) {
return this.boundAddToRecommendationsContextKey.set(false);
}

const folders = this.workspaceContextService.getWorkspace().folders;
const configurationFiles = folders
.map(workspaceFolder => workspaceFolder.toResource(paths.join('.vscode', 'extensions.json')));
const getFoldersRecommendedExtensionsPromises = configurationFiles.map(configurationFile => {
return this.getFolderRecommendedExtensions(configurationFile);
});
Promise.all(getFoldersRecommendedExtensionsPromises).then(foldersRecommendationsArray => {
if (this.editorService.activeEditor instanceof ExtensionsInput) {
// Don't show the command if the selected extension is
// recommended in ANY the folders of the current workspace
const extensionId = this.editorService.activeEditor.extension.id.toLowerCase();
const enabled = !foldersRecommendationsArray.some((recommendations: string[]) => {
return recommendations.some(r => r.toLowerCase() === extensionId);
});
this.boundAddToRecommendationsContextKey.set(enabled);
} else {
this.boundAddToRecommendationsContextKey.set(false);
}
});
}

private getFolderRecommendedExtensions(extensionsFileResource: URI): TPromise<string[]> {
return this.fileService.resolveContent(extensionsFileResource)
.then(content => {
const folderRecommendations = (<IExtensionsContent>json.parse(content.value));
return folderRecommendations.recommendations || [];
}, err => []);
}
}

interface IExtensionsContent {
Expand All @@ -1798,7 +1741,7 @@ export abstract class AbstractConfigureRecommendedExtensionsAction extends Actio
label: string,
@IWorkspaceContextService protected contextService: IWorkspaceContextService,
@IFileService private fileService: IFileService,
@IEditorService private editorService: IEditorService,
@IEditorService protected editorService: IEditorService,
@IJSONEditingService private jsonEditingService: IJSONEditingService,
@ITextModelService private textModelResolverService: ITextModelService
) {
Expand Down Expand Up @@ -1855,6 +1798,14 @@ export abstract class AbstractConfigureRecommendedExtensionsAction extends Actio
});
}

protected getFolderRecommendedExtensions(extensionsFileResource: URI): TPromise<string[]> {
return this.fileService.resolveContent(extensionsFileResource)
.then(content => {
const folderRecommendations = (<IExtensionsContent>json.parse(content.value));
return folderRecommendations.recommendations || [];
}, err => []);
}

private getOrUpdateWorkspaceConfigurationFile(workspaceConfigurationFile: URI): TPromise<IContent> {
return this.fileService.resolveContent(workspaceConfigurationFile)
.then(content => {
Expand Down Expand Up @@ -1999,7 +1950,7 @@ export class AddToWorkspaceRecommendationsAction extends AbstractConfigureRecomm
@IJSONEditingService jsonEditingService: IJSONEditingService,
@ITextModelService textModelResolverService: ITextModelService,
@ICommandService private commandService: ICommandService,
@INotificationService private notificationService: INotificationService,
@INotificationService private notificationService: INotificationService
) {
super(
id,
Expand All @@ -2012,28 +1963,39 @@ export class AddToWorkspaceRecommendationsAction extends AbstractConfigureRecomm
);
}

run(extensionId: string): TPromise<boolean> {
run(): TPromise<void> {
if (!(this.editorService.activeEditor instanceof ExtensionsInput) || !this.editorService.activeEditor.extension) {
return TPromise.as(null);
}
const folders = this.contextService.getWorkspace().folders;
if (!folders || !folders.length) {
this.notificationService.info(localize('AddToWorkspaceRecommendations.noWorkspace', 'There is no workspace open to add recommendations.'));
return TPromise.as(null);
}

const extensionId = this.editorService.activeEditor.extension.id;
const pickFolderPromise = folders.length === 1
? TPromise.as(folders[0])
: this.commandService.executeCommand<IWorkspaceFolder>(PICK_WORKSPACE_FOLDER_COMMAND_ID);
return pickFolderPromise
.then(async workspaceFolder => {
if (workspaceFolder) {
const configurationFile = workspaceFolder.toResource(paths.join('.vscode', 'extensions.json'));
await this.addRecommendedExtensionToFolder(configurationFile, extensionId);
return false;
}
return true;
})
.then((pickCancelled: boolean) => {
if (!pickCancelled) {
this.notificationService.info(localize('AddToWorkspaceRecommendations.success', 'The extension was recommended in the selected folder.'));
.then(workspaceFolder => {
if (!workspaceFolder) {
return TPromise.as(null);
}
return pickCancelled;
}, err => {
this.notificationService.error(err);
throw err;
const configurationFile = workspaceFolder.toResource(paths.join('.vscode', 'extensions.json'));
return this.getFolderRecommendedExtensions(configurationFile).then(recommendations => {
if (recommendations.map(e => e.toLowerCase()).indexOf(extensionId.toLowerCase()) > -1) {
this.notificationService.info(localize('AddToWorkspaceRecommendations.alreadyExists', 'This extension is already present in workspace recommendations.'));
return TPromise.as(null);
}

return this.addRecommendedExtensionToFolder(configurationFile, extensionId).then(() => {
this.notificationService.info(localize('AddToWorkspaceRecommendations.success', 'The extension was successfully added to workspace recommendations.'));
}, err => {
this.notificationService.error(err);
throw err;
});
});
});
}
}
Expand Down

0 comments on commit 6923709

Please sign in to comment.