Skip to content

Commit c38d257

Browse files
committed
openEditorsView: plugin in contributed context menu actions
1 parent f8deb9c commit c38d257

1 file changed

Lines changed: 109 additions & 82 deletions

File tree

src/vs/workbench/parts/files/electron-browser/views/openEditorsView.ts

Lines changed: 109 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
3737
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
3838
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
3939
import { KeyCode } from 'vs/base/common/keyCodes';
40+
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
41+
import { ContributableActionProvider } from 'vs/workbench/browser/actions';
42+
import { memoize } from 'vs/base/common/decorators';
4043

4144
const $ = dom.$;
4245

@@ -191,6 +194,11 @@ export class OpenEditorsView extends ViewsViewletPanel {
191194
}
192195
}
193196

197+
@memoize
198+
private get actionProvider(): ActionProvider {
199+
return new ActionProvider(this.instantiationService, this.textFileService, this.untitledEditorService);
200+
}
201+
194202
private get elements(): (IEditorGroup | OpenEditor)[] {
195203
const result: (IEditorGroup | OpenEditor)[] = [];
196204
this.model.groups.forEach(g => {
@@ -262,89 +270,10 @@ export class OpenEditorsView extends ViewsViewletPanel {
262270
}
263271

264272
private onListContextMenu(e: IListContextMenuEvent<OpenEditor | IEditorGroup>): void {
265-
// TODO@isidor contributable actions
266-
const autoSaveEnabled = this.textFileService.getAutoSaveMode() === AutoSaveMode.AFTER_SHORT_DELAY;
267-
const element = e.element;
268-
const actions: IAction[] = [];
269-
if (element instanceof EditorGroup) {
270-
if (!autoSaveEnabled) {
271-
actions.push(this.instantiationService.createInstance(SaveAllInGroupAction, SaveAllInGroupAction.ID, nls.localize('saveAll', "Save All")));
272-
actions.push(new Separator());
273-
}
274-
275-
actions.push(this.instantiationService.createInstance(CloseUnmodifiedEditorsInGroupAction, CloseUnmodifiedEditorsInGroupAction.ID, nls.localize('closeAllUnmodified', "Close Unmodified")));
276-
actions.push(this.instantiationService.createInstance(CloseEditorsInGroupAction, CloseEditorsInGroupAction.ID, nls.localize('closeAll', "Close All")));
277-
} else {
278-
const openEditor = <OpenEditor>element;
279-
const resource = openEditor.getResource();
280-
if (resource) {
281-
// Open to side
282-
actions.unshift(this.instantiationService.createInstance(OpenToSideAction, undefined, resource, false));
283-
284-
if (!openEditor.isUntitled()) {
285-
286-
// Files: Save / Revert
287-
if (!autoSaveEnabled) {
288-
actions.push(new Separator());
289-
290-
const saveAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL);
291-
saveAction.setResource(resource);
292-
saveAction.enabled = openEditor.isDirty();
293-
actions.push(saveAction);
294-
295-
const revertAction = this.instantiationService.createInstance(RevertFileAction, RevertFileAction.ID, RevertFileAction.LABEL);
296-
revertAction.setResource(resource);
297-
revertAction.enabled = openEditor.isDirty();
298-
actions.push(revertAction);
299-
}
300-
}
301-
302-
// Untitled: Save / Save As
303-
if (openEditor.isUntitled()) {
304-
actions.push(new Separator());
305-
306-
if (this.untitledEditorService.hasAssociatedFilePath(resource)) {
307-
let saveUntitledAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL);
308-
saveUntitledAction.setResource(resource);
309-
actions.push(saveUntitledAction);
310-
}
311-
312-
let saveAsAction = this.instantiationService.createInstance(SaveFileAsAction, SaveFileAsAction.ID, SaveFileAsAction.LABEL);
313-
saveAsAction.setResource(resource);
314-
actions.push(saveAsAction);
315-
}
316-
317-
// Compare Actions
318-
actions.push(new Separator());
319-
320-
if (!openEditor.isUntitled()) {
321-
const compareWithSavedAction = this.instantiationService.createInstance(CompareWithSavedAction, CompareWithSavedAction.ID, nls.localize('compareWithSaved', "Compare with Saved"));
322-
compareWithSavedAction.setResource(resource);
323-
compareWithSavedAction.enabled = openEditor.isDirty();
324-
actions.push(compareWithSavedAction);
325-
}
326-
327-
const runCompareAction = this.instantiationService.createInstance(CompareResourcesAction, resource, undefined);
328-
if (runCompareAction._isEnabled()) {
329-
actions.push(runCompareAction);
330-
}
331-
actions.push(this.instantiationService.createInstance(SelectResourceForCompareAction, resource, undefined));
332-
333-
actions.push(new Separator());
334-
}
335-
336-
actions.push(this.instantiationService.createInstance(CloseEditorAction, CloseEditorAction.ID, nls.localize('close', "Close")));
337-
const closeOtherEditorsInGroupAction = this.instantiationService.createInstance(CloseOtherEditorsInGroupAction, CloseOtherEditorsInGroupAction.ID, nls.localize('closeOthers', "Close Others"));
338-
closeOtherEditorsInGroupAction.enabled = openEditor.editorGroup.count > 1;
339-
actions.push(closeOtherEditorsInGroupAction);
340-
actions.push(this.instantiationService.createInstance(CloseUnmodifiedEditorsInGroupAction, CloseUnmodifiedEditorsInGroupAction.ID, nls.localize('closeAllUnmodified', "Close Unmodified")));
341-
actions.push(this.instantiationService.createInstance(CloseEditorsInGroupAction, CloseEditorsInGroupAction.ID, nls.localize('closeAll', "Close All")));
342-
}
343-
344273
this.contextMenuService.showContextMenu({
345274
getAnchor: () => e.anchor,
346-
getActions: () => TPromise.as(actions),
347-
getActionsContext: () => element
275+
getActions: () => this.actionProvider.getSecondaryActions(e.element),
276+
getActionsContext: () => e.element
348277
});
349278
}
350279

@@ -448,6 +377,7 @@ interface IOpenEditorTemplateData {
448377
container: HTMLElement;
449378
root: EditorLabel;
450379
actionBar: ActionBar;
380+
toDispose: IDisposable[];
451381
}
452382

453383
interface IEditorGroupTemplateData {
@@ -542,6 +472,8 @@ class OpenEditorRenderer implements IRenderer<OpenEditor, IOpenEditorTemplateDat
542472

543473
editorTemplate.root = this.instantiationService.createInstance(EditorLabel, container, void 0);
544474

475+
editorTemplate.toDispose = [];
476+
545477
return editorTemplate;
546478
}
547479

@@ -558,6 +490,101 @@ class OpenEditorRenderer implements IRenderer<OpenEditor, IOpenEditorTemplateDat
558490
disposeTemplate(templateData: IOpenEditorTemplateData): void {
559491
templateData.actionBar.dispose();
560492
templateData.root.dispose();
493+
dispose(templateData.toDispose);
494+
}
495+
}
496+
497+
export class ActionProvider extends ContributableActionProvider {
498+
499+
constructor(
500+
@IInstantiationService private instantiationService: IInstantiationService,
501+
@ITextFileService private textFileService: ITextFileService,
502+
@IUntitledEditorService private untitledEditorService: IUntitledEditorService
503+
) {
504+
super();
505+
}
506+
507+
public getSecondaryActions(element: any): TPromise<IAction[]> {
508+
return super.getSecondaryActions(undefined, element).then(result => {
509+
const autoSaveEnabled = this.textFileService.getAutoSaveMode() === AutoSaveMode.AFTER_SHORT_DELAY;
510+
511+
if (element instanceof EditorGroup) {
512+
if (!autoSaveEnabled) {
513+
result.push(this.instantiationService.createInstance(SaveAllInGroupAction, SaveAllInGroupAction.ID, nls.localize('saveAll', "Save All")));
514+
result.push(new Separator());
515+
}
516+
517+
result.push(this.instantiationService.createInstance(CloseUnmodifiedEditorsInGroupAction, CloseUnmodifiedEditorsInGroupAction.ID, nls.localize('closeAllUnmodified', "Close Unmodified")));
518+
result.push(this.instantiationService.createInstance(CloseEditorsInGroupAction, CloseEditorsInGroupAction.ID, nls.localize('closeAll', "Close All")));
519+
} else {
520+
const openEditor = <OpenEditor>element;
521+
const resource = openEditor.getResource();
522+
if (resource) {
523+
// Open to side
524+
result.unshift(this.instantiationService.createInstance(OpenToSideAction, undefined, resource, false));
525+
526+
if (!openEditor.isUntitled()) {
527+
528+
// Files: Save / Revert
529+
if (!autoSaveEnabled) {
530+
result.push(new Separator());
531+
532+
const saveAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL);
533+
saveAction.setResource(resource);
534+
saveAction.enabled = openEditor.isDirty();
535+
result.push(saveAction);
536+
537+
const revertAction = this.instantiationService.createInstance(RevertFileAction, RevertFileAction.ID, RevertFileAction.LABEL);
538+
revertAction.setResource(resource);
539+
revertAction.enabled = openEditor.isDirty();
540+
result.push(revertAction);
541+
}
542+
}
543+
544+
// Untitled: Save / Save As
545+
if (openEditor.isUntitled()) {
546+
result.push(new Separator());
547+
548+
if (this.untitledEditorService.hasAssociatedFilePath(resource)) {
549+
let saveUntitledAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL);
550+
saveUntitledAction.setResource(resource);
551+
result.push(saveUntitledAction);
552+
}
553+
554+
let saveAsAction = this.instantiationService.createInstance(SaveFileAsAction, SaveFileAsAction.ID, SaveFileAsAction.LABEL);
555+
saveAsAction.setResource(resource);
556+
result.push(saveAsAction);
557+
}
558+
559+
// Compare Actions
560+
result.push(new Separator());
561+
562+
if (!openEditor.isUntitled()) {
563+
const compareWithSavedAction = this.instantiationService.createInstance(CompareWithSavedAction, CompareWithSavedAction.ID, nls.localize('compareWithSaved', "Compare with Saved"));
564+
compareWithSavedAction.setResource(resource);
565+
compareWithSavedAction.enabled = openEditor.isDirty();
566+
result.push(compareWithSavedAction);
567+
}
568+
569+
const runCompareAction = this.instantiationService.createInstance(CompareResourcesAction, resource, undefined);
570+
if (runCompareAction._isEnabled()) {
571+
result.push(runCompareAction);
572+
}
573+
result.push(this.instantiationService.createInstance(SelectResourceForCompareAction, resource, undefined));
574+
575+
result.push(new Separator());
576+
}
577+
578+
result.push(this.instantiationService.createInstance(CloseEditorAction, CloseEditorAction.ID, nls.localize('close', "Close")));
579+
const closeOtherEditorsInGroupAction = this.instantiationService.createInstance(CloseOtherEditorsInGroupAction, CloseOtherEditorsInGroupAction.ID, nls.localize('closeOthers', "Close Others"));
580+
closeOtherEditorsInGroupAction.enabled = openEditor.editorGroup.count > 1;
581+
result.push(closeOtherEditorsInGroupAction);
582+
result.push(this.instantiationService.createInstance(CloseUnmodifiedEditorsInGroupAction, CloseUnmodifiedEditorsInGroupAction.ID, nls.localize('closeAllUnmodified', "Close Unmodified")));
583+
result.push(this.instantiationService.createInstance(CloseEditorsInGroupAction, CloseEditorsInGroupAction.ID, nls.localize('closeAll', "Close All")));
584+
}
585+
586+
return result;
587+
});
561588
}
562589
}
563590

@@ -642,4 +669,4 @@ class OpenEditorRenderer implements IRenderer<OpenEditor, IOpenEditorTemplateDat
642669
// }
643670
// }
644671
// }
645-
// }
672+
// }

0 commit comments

Comments
 (0)