diff --git a/src/vs/workbench/browser/parts/editor/stringEditor.ts b/src/vs/workbench/browser/parts/editor/stringEditor.ts index 1812f99ac2555eafb5da05f16d53319497b6ebdd..f530f11c46f58419453fcd009022f1fb0bdc3e26 100644 --- a/src/vs/workbench/browser/parts/editor/stringEditor.ts +++ b/src/vs/workbench/browser/parts/editor/stringEditor.ts @@ -51,10 +51,10 @@ export class StringEditor extends BaseTextEditor { this.mapResourceToEditorViewState = Object.create(null); - this.toUnbind.push(this.eventService.addListener2(EventType.UNTITLED_FILE_DELETED, (e: UntitledEditorEvent) => this.onUntitledDeletedEvent(e))); + this.toUnbind.push(this.eventService.addListener2(EventType.UNTITLED_FILE_SAVED, (e: UntitledEditorEvent) => this.onUntitledSavedEvent(e))); } - private onUntitledDeletedEvent(e: UntitledEditorEvent): void { + private onUntitledSavedEvent(e: UntitledEditorEvent): void { delete this.mapResourceToEditorViewState[e.resource.toString()]; } diff --git a/src/vs/workbench/common/editor/untitledEditorInput.ts b/src/vs/workbench/common/editor/untitledEditorInput.ts index 9e1306354e0eaa5a823278a9e2b4bdc2ac894769..63b169ce2d9b35c4b575753497afac0acc5fc62f 100644 --- a/src/vs/workbench/common/editor/untitledEditorInput.ts +++ b/src/vs/workbench/common/editor/untitledEditorInput.ts @@ -58,7 +58,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { } private registerListeners(): void { - this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DELETED, (e: UntitledEditorEvent) => this.onDirtyStateChange(e))); + this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_SAVED, (e: UntitledEditorEvent) => this.onDirtyStateChange(e))); this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DIRTY, (e: UntitledEditorEvent) => this.onDirtyStateChange(e))); } @@ -97,7 +97,9 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { } public revert(): TPromise { - return this.textFileService.revert(this.resource); + this.cachedModel.revert(); + + return TPromise.as(true); } public suggestFileName(): string { @@ -179,15 +181,15 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { public dispose(): void { + // Listeners + dispose(this.toUnbind); + // Model if (this.cachedModel) { this.cachedModel.dispose(); this.cachedModel = null; } - // Listeners - dispose(this.toUnbind); - super.dispose(); } } \ No newline at end of file diff --git a/src/vs/workbench/common/editor/untitledEditorModel.ts b/src/vs/workbench/common/editor/untitledEditorModel.ts index 2e56ef52901832fb9f3a87892c39d8102e6f6bee..fae42740555dc9a36a40b3362a19d87d7788ab4e 100644 --- a/src/vs/workbench/common/editor/untitledEditorModel.ts +++ b/src/vs/workbench/common/editor/untitledEditorModel.ts @@ -86,6 +86,12 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS return this.dirty; } + public revert(): void { + this.dirty = false; + + this.eventService.emit(WorkbenchEventType.UNTITLED_FILE_SAVED, new UntitledEditorEvent(this.resource)); + } + public load(): TPromise { return super.load().then((model) => { const configuration = this.configurationService.getConfiguration(); @@ -117,8 +123,6 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS public dispose(): void { super.dispose(); - this.dirty = false; // we can no longer be dirty - if (this.textModelChangeListener) { this.textModelChangeListener.dispose(); this.textModelChangeListener = null; @@ -128,7 +132,5 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS this.configurationChangeListener.dispose(); this.configurationChangeListener = null; } - - this.eventService.emit(WorkbenchEventType.UNTITLED_FILE_DELETED, new UntitledEditorEvent(this.resource)); } } \ No newline at end of file diff --git a/src/vs/workbench/common/events.ts b/src/vs/workbench/common/events.ts index 5247264ee05a85465e30aea100dbb6f9c4ced661..85d595b824f88e9ddf1a9e435b3f05606fb481f0 100644 --- a/src/vs/workbench/common/events.ts +++ b/src/vs/workbench/common/events.ts @@ -33,9 +33,9 @@ export class EventType { static UNTITLED_FILE_DIRTY = 'untitledFileDirty'; /** - * Event type for when an untitled file is deleted. + * Event type for when an untitled file is saved. */ - static UNTITLED_FILE_DELETED = 'untitledFileDeleted'; + static UNTITLED_FILE_SAVED = 'untitledFileSaved'; /** * Event type for when a resources encoding changes. diff --git a/src/vs/workbench/parts/files/browser/fileActions.ts b/src/vs/workbench/parts/files/browser/fileActions.ts index 8fd49f80646a804b57e6d609ad6fab533ad9abaf..4f73160ccc19473aa7789c3cdff88bd6b2b9f9df 100644 --- a/src/vs/workbench/parts/files/browser/fileActions.ts +++ b/src/vs/workbench/parts/files/browser/fileActions.ts @@ -1535,7 +1535,7 @@ export abstract class BaseSaveAllAction extends BaseActionWithErrorReporting { if (this.includeUntitled()) { this.toDispose.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DIRTY, () => this.updateEnablement(true))); - this.toDispose.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DELETED, () => this.updateEnablement(false))); + this.toDispose.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_SAVED, () => this.updateEnablement(false))); } } diff --git a/src/vs/workbench/parts/files/browser/fileTracker.ts b/src/vs/workbench/parts/files/browser/fileTracker.ts index 661b1e376caaa020c805dd6c9d8f0299c9f77130..77116530f8e48d0ba0f9b2453b2e644cb48c015f 100644 --- a/src/vs/workbench/parts/files/browser/fileTracker.ts +++ b/src/vs/workbench/parts/files/browser/fileTracker.ts @@ -65,7 +65,7 @@ export class FileTracker implements IWorkbenchContribution { // Update editors and inputs from local changes and saves this.toUnbind.push(this.editorGroupService.onEditorsChanged(() => this.onEditorsChanged())); - this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DELETED, (e: UntitledEditorEvent) => this.onUntitledEditorDeleted(e))); + this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_SAVED, (e: UntitledEditorEvent) => this.onUntitledEditorSaved(e))); this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DIRTY, (e: UntitledEditorEvent) => this.onUntitledEditorDirty(e))); this.toUnbind.push(this.eventService.addListener2(FileEventType.FILE_DIRTY, (e: TextFileChangeEvent) => this.onTextFileDirty(e))); this.toUnbind.push(this.eventService.addListener2(FileEventType.FILE_SAVE_ERROR, (e: TextFileChangeEvent) => this.onTextFileSaveError(e))); @@ -107,7 +107,7 @@ export class FileTracker implements IWorkbenchContribution { this.updateActivityBadge(); } - private onUntitledEditorDeleted(e: UntitledEditorEvent): void { + private onUntitledEditorSaved(e: UntitledEditorEvent): void { if (this.lastDirtyCount > 0) { this.updateActivityBadge(); } diff --git a/src/vs/workbench/parts/files/electron-browser/electronFileTracker.ts b/src/vs/workbench/parts/files/electron-browser/electronFileTracker.ts index 487a6e1da1d4dab01af5dd7d210659e1c4e7c7d3..b1a414925df2ed6c4bbfd38b6551edafae513c20 100644 --- a/src/vs/workbench/parts/files/electron-browser/electronFileTracker.ts +++ b/src/vs/workbench/parts/files/electron-browser/electronFileTracker.ts @@ -77,7 +77,7 @@ export class FileTracker implements IWorkbenchContribution { private registerListeners(): void { // Local text file changes - this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DELETED, () => this.onUntitledDeletedEvent())); + this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_SAVED, () => this.onUntitledSavedEvent())); this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DIRTY, () => this.onUntitledDirtyEvent())); this.toUnbind.push(this.eventService.addListener2(FileEventType.FILE_DIRTY, (e: TextFileChangeEvent) => this.onTextFileDirty(e))); this.toUnbind.push(this.eventService.addListener2(FileEventType.FILE_SAVED, (e: TextFileChangeEvent) => this.onTextFileSaved(e))); @@ -199,7 +199,7 @@ export class FileTracker implements IWorkbenchContribution { } } - private onUntitledDeletedEvent(): void { + private onUntitledSavedEvent(): void { if (this.isDocumentedEdited) { this.updateDocumentEdited(); } diff --git a/src/vs/workbench/parts/files/electron-browser/textFileServices.ts b/src/vs/workbench/parts/files/electron-browser/textFileServices.ts index 7d5302f9c8a1cb861e3ad7e78916dcb09598a97e..33ced42460f82acd03c544a324449d81f5d92f0d 100644 --- a/src/vs/workbench/parts/files/electron-browser/textFileServices.ts +++ b/src/vs/workbench/parts/files/electron-browser/textFileServices.ts @@ -117,17 +117,8 @@ export class TextFileService extends AbstractTextFileService { return super.revertAll(resources, force).then(r => { // Revert untitled - let untitledInputs = this.untitledEditorService.getAll(resources); - untitledInputs.forEach(input => { - if (input) { - input.dispose(); - - r.results.push({ - source: input.getResource(), - success: true - }); - } - }); + const reverted = this.untitledEditorService.revertAll(resources); + reverted.forEach(res => r.results.push({ source: res, success: true })); return r; }); diff --git a/src/vs/workbench/parts/search/browser/searchViewlet.ts b/src/vs/workbench/parts/search/browser/searchViewlet.ts index 56e3e857fec8e607d7bebac2b50f8653d3daf406..0c01d0a10ecb6fe8106cfbff31ee6181fe7ee1a7 100644 --- a/src/vs/workbench/parts/search/browser/searchViewlet.ts +++ b/src/vs/workbench/parts/search/browser/searchViewlet.ts @@ -692,7 +692,7 @@ export class SearchViewlet extends Viewlet { this.viewletSettings = this.getMemento(storageService, Scope.WORKSPACE); this.toUnbind.push(this.eventService.addListener2(FileEventType.FILE_CHANGES, (e) => this.onFilesChanged(e))); - this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DELETED, (e) => this.onUntitledFileDeleted(e))); + this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_SAVED, (e) => this.onUntitledFileSaved(e))); this.toUnbind.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationUpdated(e.config))); } @@ -1456,7 +1456,7 @@ export class SearchViewlet extends Viewlet { }, sideBySide); } - private onUntitledFileDeleted(e: UntitledEditorEvent): void { + private onUntitledFileSaved(e: UntitledEditorEvent): void { if (!this.viewModel) { return; } diff --git a/src/vs/workbench/services/untitled/common/untitledEditorService.ts b/src/vs/workbench/services/untitled/common/untitledEditorService.ts index 2107f71c8892facb8cc1019e6a970f19226bd797..dcf869e4d93d88d9ba6db2dac6a0a3fce8abf9fa 100644 --- a/src/vs/workbench/services/untitled/common/untitledEditorService.ts +++ b/src/vs/workbench/services/untitled/common/untitledEditorService.ts @@ -36,6 +36,11 @@ export interface IUntitledEditorService { */ isDirty(resource: URI): boolean; + /** + * Reverts the untitled resources if found. + */ + revertAll(resources?: URI[]): URI[]; + /** * Creates a new untitled input with the optional resource URI or returns an existing one * if the provided resource exists already as untitled input. @@ -72,6 +77,21 @@ export class UntitledEditorService implements IUntitledEditorService { return Object.keys(UntitledEditorService.CACHE).map((key) => UntitledEditorService.CACHE[key]); } + public revertAll(resources?: URI[], force?: boolean): URI[] { + const reverted: URI[] = []; + + const untitledInputs = this.getAll(resources); + untitledInputs.forEach(input => { + if (input) { + input.revert(); + + reverted.push(input.getResource()); + } + }); + + return reverted; + } + public isDirty(resource: URI): boolean { let input = this.get(resource); diff --git a/src/vs/workbench/test/common/servicesTestUtils.ts b/src/vs/workbench/test/common/servicesTestUtils.ts index 9cb30e539386747b2b0b2f606123dc75da522aa7..4c773cef9cb4855108a4394de63d6ca5c42a3efc 100644 --- a/src/vs/workbench/test/common/servicesTestUtils.ts +++ b/src/vs/workbench/test/common/servicesTestUtils.ts @@ -295,6 +295,10 @@ export class TestUntitledEditorService implements IUntitledEditorService { return []; } + public revertAll(resources?: URI[]): URI[] { + return []; + } + public isDirty() { return false; }