From e20bed14ec0dc4cf1da6f9a6b3ef2530095a2688 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 20 May 2016 07:41:02 +0200 Subject: [PATCH] make editor dirty event emit for all inputs open --- .../browser/parts/editor/editorActions.ts | 1 + .../browser/parts/editor/editorPart.ts | 4 +-- .../parts/editor/sideBySideEditorControl.ts | 2 -- src/vs/workbench/common/events.ts | 4 +-- .../parts/files/browser/fileTracker.ts | 31 +++++++++---------- .../services/editor/browser/editorService.ts | 29 ----------------- .../services/history/browser/history.ts | 4 +-- 7 files changed, 21 insertions(+), 54 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/editorActions.ts b/src/vs/workbench/browser/parts/editor/editorActions.ts index ef0729c5a90..e88b9823e65 100644 --- a/src/vs/workbench/browser/parts/editor/editorActions.ts +++ b/src/vs/workbench/browser/parts/editor/editorActions.ts @@ -419,6 +419,7 @@ export class CloseEditorAction extends Action { let input = editorIdentifier ? editorIdentifier.editor : null; if (!input) { + // Get Top Editor at Position let visibleEditors = this.editorService.getVisibleEditors(); if (visibleEditors[position]) { diff --git a/src/vs/workbench/browser/parts/editor/editorPart.ts b/src/vs/workbench/browser/parts/editor/editorPart.ts index a5fd03d015f..e70f8f5e995 100644 --- a/src/vs/workbench/browser/parts/editor/editorPart.ts +++ b/src/vs/workbench/browser/parts/editor/editorPart.ts @@ -120,13 +120,13 @@ export class EditorPart extends Part implements IEditorPart { } private registerListeners(): void { - this.toUnbind.push(this.eventService.addListener(WorkbenchEventType.EDITOR_INPUT_STATE_CHANGED, (event: EditorInputEvent) => this.onEditorInputStateChanged(event))); + this.toUnbind.push(this.eventService.addListener(WorkbenchEventType.EDITOR_INPUT_DIRTY_STATE_CHANGED, (event: EditorInputEvent) => this.onEditorInputDirtyStateChanged(event))); const unbind = this.stacks.onEditorDisposed(editor => this.onEditorDisposed(editor)); this.toUnbind.push(() => unbind.dispose()); } - private onEditorInputStateChanged(event: EditorInputEvent): void { + private onEditorInputDirtyStateChanged(event: EditorInputEvent): void { if (this.sideBySideControl) { this.sideBySideControl.updateTitleArea(event.editorInput); } diff --git a/src/vs/workbench/browser/parts/editor/sideBySideEditorControl.ts b/src/vs/workbench/browser/parts/editor/sideBySideEditorControl.ts index bb0fab75949..b6b7bd02688 100644 --- a/src/vs/workbench/browser/parts/editor/sideBySideEditorControl.ts +++ b/src/vs/workbench/browser/parts/editor/sideBySideEditorControl.ts @@ -115,7 +115,6 @@ export class SideBySideEditorControl implements ISideBySideEditorControl, IVerti private titleContainer: Builder[]; private titleLabel: Builder[]; private titleDescription: Builder[]; - private editorInputStateDescription: Builder[]; private progressBar: ProgressBar[]; private editorTitleToolbar: ToolBar[]; @@ -168,7 +167,6 @@ export class SideBySideEditorControl implements ISideBySideEditorControl, IVerti this.titleContainer = []; this.titleLabel = []; this.titleDescription = []; - this.editorInputStateDescription = []; this.editorTitleToolbar = []; this.editorActionsToolbar = []; this.progressBar = []; diff --git a/src/vs/workbench/common/events.ts b/src/vs/workbench/common/events.ts index b7d4f22c598..f399f75b958 100644 --- a/src/vs/workbench/common/events.ts +++ b/src/vs/workbench/common/events.ts @@ -58,9 +58,9 @@ export class EventType { static EDITOR_INPUT_CHANGED = 'editorInputChanged'; /** - * Event type for when the editor input state changed. + * Event type for when the editor input dirty state changed. */ - static EDITOR_INPUT_STATE_CHANGED = 'editorInputStateChanged'; + static EDITOR_INPUT_DIRTY_STATE_CHANGED = 'editorInputDirtyStateChanged'; /** * Event type for when the editor input failed to be set to the editor. diff --git a/src/vs/workbench/parts/files/browser/fileTracker.ts b/src/vs/workbench/parts/files/browser/fileTracker.ts index 39af523b950..87a38c32f0e 100644 --- a/src/vs/workbench/parts/files/browser/fileTracker.ts +++ b/src/vs/workbench/parts/files/browser/fileTracker.ts @@ -10,6 +10,7 @@ import nls = require('vs/nls'); import {MIME_UNKNOWN} from 'vs/base/common/mime'; import URI from 'vs/base/common/uri'; import paths = require('vs/base/common/paths'); +import arrays = require('vs/base/common/arrays'); import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput'; import {EditorInput, EditorOptions} from 'vs/workbench/common/editor'; import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor'; @@ -64,7 +65,6 @@ export class FileTracker implements IWorkbenchContribution { this.toUnbind.push(this.eventService.addListener(WorkbenchEventType.UNTITLED_FILE_DELETED, (e: UntitledEditorEvent) => this.onUntitledEditorDeleted(e))); this.toUnbind.push(this.eventService.addListener(WorkbenchEventType.UNTITLED_FILE_DIRTY, (e: UntitledEditorEvent) => this.onUntitledEditorDirty(e))); this.toUnbind.push(this.eventService.addListener(FileEventType.FILE_DIRTY, (e: TextFileChangeEvent) => this.onTextFileDirty(e))); - this.toUnbind.push(this.eventService.addListener(FileEventType.FILE_SAVING, (e: TextFileChangeEvent) => this.onTextFileSaving(e))); this.toUnbind.push(this.eventService.addListener(FileEventType.FILE_SAVE_ERROR, (e: TextFileChangeEvent) => this.onTextFileSaveError(e))); this.toUnbind.push(this.eventService.addListener(FileEventType.FILE_SAVED, (e: TextFileChangeEvent) => this.onTextFileSaved(e))); this.toUnbind.push(this.eventService.addListener(FileEventType.FILE_REVERTED, (e: TextFileChangeEvent) => this.onTextFileReverted(e))); @@ -75,7 +75,7 @@ export class FileTracker implements IWorkbenchContribution { } private onTextFileDirty(e: TextFileChangeEvent): void { - this.emitInputStateChangeEvent(e.resource); + this.emitInputDirtyStateChangeEvent(e.resource, true); if (this.textFileService.getAutoSaveMode() !== AutoSaveMode.AFTER_SHORT_DELAY) { this.updateActivityBadge(); // no indication needed when auto save is enabled for short delay @@ -84,17 +84,13 @@ export class FileTracker implements IWorkbenchContribution { this.updateActiveEditor(e.resource); } - private onTextFileSaving(e: TextFileChangeEvent): void { - this.emitInputStateChangeEvent(e.resource); - } - private onTextFileSaveError(e: TextFileChangeEvent): void { - this.emitInputStateChangeEvent(e.resource); + this.emitInputDirtyStateChangeEvent(e.resource, true); this.updateActivityBadge(); } private onTextFileSaved(e: TextFileChangeEvent): void { - this.emitInputStateChangeEvent(e.resource); + this.emitInputDirtyStateChangeEvent(e.resource, false); if (this.lastDirtyCount > 0) { this.updateActivityBadge(); @@ -102,7 +98,7 @@ export class FileTracker implements IWorkbenchContribution { } private onTextFileReverted(e: TextFileChangeEvent): void { - this.emitInputStateChangeEvent(e.resource); + this.emitInputDirtyStateChangeEvent(e.resource, false); if (this.lastDirtyCount > 0) { this.updateActivityBadge(); @@ -112,7 +108,7 @@ export class FileTracker implements IWorkbenchContribution { private onUntitledEditorDirty(e: UntitledEditorEvent): void { let input = this.untitledEditorService.get(e.resource); if (input) { - this.eventService.emit(WorkbenchEventType.EDITOR_INPUT_STATE_CHANGED, new EditorInputEvent(input)); + this.eventService.emit(WorkbenchEventType.EDITOR_INPUT_DIRTY_STATE_CHANGED, new EditorInputEvent(input)); } this.updateActivityBadge(); @@ -122,7 +118,7 @@ export class FileTracker implements IWorkbenchContribution { private onUntitledEditorDeleted(e: UntitledEditorEvent): void { let input = this.untitledEditorService.get(e.resource); if (input) { - this.eventService.emit(WorkbenchEventType.EDITOR_INPUT_STATE_CHANGED, new EditorInputEvent(input)); + this.eventService.emit(WorkbenchEventType.EDITOR_INPUT_DIRTY_STATE_CHANGED, new EditorInputEvent(input)); } if (this.lastDirtyCount > 0) { @@ -156,14 +152,15 @@ export class FileTracker implements IWorkbenchContribution { } } - private emitInputStateChangeEvent(resource: URI): void { + private emitInputDirtyStateChangeEvent(resource: URI, gotDirty: boolean): void { // Find all file editor inputs that are open from the given file resource and emit a editor input state change event. // We could do all of this within the file editor input but having all the file change listeners in // one place is more elegant and keeps the logic together at once place. - let editors = this.editorService.getVisibleEditors(); - editors.forEach((editor) => { - let input = editor.input; + let editors = arrays.flatten(this.editorService.getStacksModel().groups.map(g => g.getEditors())); + editors.forEach(input => { + + // Diff Editor Input if (input instanceof DiffEditorInput) { input = (input).getModifiedInput(); } @@ -172,9 +169,9 @@ export class FileTracker implements IWorkbenchContribution { if (input instanceof FileEditorInput) { let fileInput = input; if (fileInput.getResource().toString() === resource.toString()) { - let inputEvent = editor.input instanceof DiffEditorInput ? editor.input : fileInput; // make sure to still send around the input from the diff editor if given + let inputEvent = input instanceof DiffEditorInput ? input : fileInput; // make sure to still send around the input from the diff editor if given - this.eventService.emit(WorkbenchEventType.EDITOR_INPUT_STATE_CHANGED, new EditorInputEvent(inputEvent)); + this.eventService.emit(WorkbenchEventType.EDITOR_INPUT_DIRTY_STATE_CHANGED, new EditorInputEvent(inputEvent)); } } }); diff --git a/src/vs/workbench/services/editor/browser/editorService.ts b/src/vs/workbench/services/editor/browser/editorService.ts index bcb79c6a3bf..6ba1158303e 100644 --- a/src/vs/workbench/services/editor/browser/editorService.ts +++ b/src/vs/workbench/services/editor/browser/editorService.ts @@ -202,35 +202,6 @@ export class WorkbenchEditorService implements IWorkbenchEditorService { this.editorPart.unpinEditor(position, input); } - private findEditor(editor?: IEditor): BaseEditor; - private findEditor(position?: Position): BaseEditor; - private findEditor(arg?: any): BaseEditor { - - // Editor provided - if (arg instanceof BaseEditor) { - return arg; - } - - // Find active editor - if (types.isUndefinedOrNull(arg)) { - return this.editorPart.getActiveEditor(); - } - - // Target position provided - if (types.isNumber(arg)) { - let position = arg; - let visibleEditors = this.editorPart.getVisibleEditors(); - for (let i = 0; i < visibleEditors.length; i++) { - let editor = visibleEditors[i]; - if (editor.position === position) { - return editor; - } - } - } - - return null; - } - public resolveEditorModel(input: IEditorInput, refresh?: boolean): TPromise; public resolveEditorModel(input: IResourceInput, refresh?: boolean): TPromise; public resolveEditorModel(input: any, refresh?: boolean): TPromise { diff --git a/src/vs/workbench/services/history/browser/history.ts b/src/vs/workbench/services/history/browser/history.ts index 8b3d8e71c1d..47e3a1e0485 100644 --- a/src/vs/workbench/services/history/browser/history.ts +++ b/src/vs/workbench/services/history/browser/history.ts @@ -84,13 +84,13 @@ export abstract class BaseHistoryService { this.toUnbind.push(this.eventService.addListener(WorkbenchEventType.EDITOR_INPUT_CHANGED, (e: EditorEvent) => this.onEditorInputChanged(e))); // Editor Input State Changes - this.toUnbind.push(this.eventService.addListener(WorkbenchEventType.EDITOR_INPUT_STATE_CHANGED, (e: EditorInputEvent) => this.onEditorInputStateChanged(e.editorInput))); + this.toUnbind.push(this.eventService.addListener(WorkbenchEventType.EDITOR_INPUT_DIRTY_STATE_CHANGED, (e: EditorInputEvent) => this.onEditorInputDirtyStateChanged(e.editorInput))); // Text Editor Selection Changes this.toUnbind.push(this.eventService.addListener(WorkbenchEventType.TEXT_EDITOR_SELECTION_CHANGED, (event: TextEditorSelectionEvent) => this.onTextEditorSelectionChanged(event))); } - private onEditorInputStateChanged(input: IEditorInput): void { + private onEditorInputDirtyStateChanged(input: IEditorInput): void { // If an active editor is set, but is different from the one from the event, prevent update because the editor is not active. let activeEditor = this.editorService.getActiveEditor(); -- GitLab