提交 0256f00d 编写于 作者: B Benjamin Pasero

improve revert logic for untitled

上级 385412e8
...@@ -51,10 +51,10 @@ export class StringEditor extends BaseTextEditor { ...@@ -51,10 +51,10 @@ export class StringEditor extends BaseTextEditor {
this.mapResourceToEditorViewState = Object.create(null); 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()]; delete this.mapResourceToEditorViewState[e.resource.toString()];
} }
......
...@@ -58,7 +58,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { ...@@ -58,7 +58,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
} }
private registerListeners(): void { 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))); this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DIRTY, (e: UntitledEditorEvent) => this.onDirtyStateChange(e)));
} }
...@@ -97,7 +97,9 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { ...@@ -97,7 +97,9 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
} }
public revert(): TPromise<boolean> { public revert(): TPromise<boolean> {
return this.textFileService.revert(this.resource); this.cachedModel.revert();
return TPromise.as(true);
} }
public suggestFileName(): string { public suggestFileName(): string {
...@@ -179,15 +181,15 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { ...@@ -179,15 +181,15 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
public dispose(): void { public dispose(): void {
// Listeners
dispose(this.toUnbind);
// Model // Model
if (this.cachedModel) { if (this.cachedModel) {
this.cachedModel.dispose(); this.cachedModel.dispose();
this.cachedModel = null; this.cachedModel = null;
} }
// Listeners
dispose(this.toUnbind);
super.dispose(); super.dispose();
} }
} }
\ No newline at end of file
...@@ -86,6 +86,12 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS ...@@ -86,6 +86,12 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
return this.dirty; return this.dirty;
} }
public revert(): void {
this.dirty = false;
this.eventService.emit(WorkbenchEventType.UNTITLED_FILE_SAVED, new UntitledEditorEvent(this.resource));
}
public load(): TPromise<EditorModel> { public load(): TPromise<EditorModel> {
return super.load().then((model) => { return super.load().then((model) => {
const configuration = this.configurationService.getConfiguration<IFilesConfiguration>(); const configuration = this.configurationService.getConfiguration<IFilesConfiguration>();
...@@ -117,8 +123,6 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS ...@@ -117,8 +123,6 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
public dispose(): void { public dispose(): void {
super.dispose(); super.dispose();
this.dirty = false; // we can no longer be dirty
if (this.textModelChangeListener) { if (this.textModelChangeListener) {
this.textModelChangeListener.dispose(); this.textModelChangeListener.dispose();
this.textModelChangeListener = null; this.textModelChangeListener = null;
...@@ -128,7 +132,5 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS ...@@ -128,7 +132,5 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
this.configurationChangeListener.dispose(); this.configurationChangeListener.dispose();
this.configurationChangeListener = null; this.configurationChangeListener = null;
} }
this.eventService.emit(WorkbenchEventType.UNTITLED_FILE_DELETED, new UntitledEditorEvent(this.resource));
} }
} }
\ No newline at end of file
...@@ -33,9 +33,9 @@ export class EventType { ...@@ -33,9 +33,9 @@ export class EventType {
static UNTITLED_FILE_DIRTY = 'untitledFileDirty'; 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. * Event type for when a resources encoding changes.
......
...@@ -1535,7 +1535,7 @@ export abstract class BaseSaveAllAction extends BaseActionWithErrorReporting { ...@@ -1535,7 +1535,7 @@ export abstract class BaseSaveAllAction extends BaseActionWithErrorReporting {
if (this.includeUntitled()) { 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_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)));
} }
} }
......
...@@ -65,7 +65,7 @@ export class FileTracker implements IWorkbenchContribution { ...@@ -65,7 +65,7 @@ export class FileTracker implements IWorkbenchContribution {
// Update editors and inputs from local changes and saves // Update editors and inputs from local changes and saves
this.toUnbind.push(this.editorGroupService.onEditorsChanged(() => this.onEditorsChanged())); 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(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_DIRTY, (e: TextFileChangeEvent) => this.onTextFileDirty(e)));
this.toUnbind.push(this.eventService.addListener2(FileEventType.FILE_SAVE_ERROR, (e: TextFileChangeEvent) => this.onTextFileSaveError(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 { ...@@ -107,7 +107,7 @@ export class FileTracker implements IWorkbenchContribution {
this.updateActivityBadge(); this.updateActivityBadge();
} }
private onUntitledEditorDeleted(e: UntitledEditorEvent): void { private onUntitledEditorSaved(e: UntitledEditorEvent): void {
if (this.lastDirtyCount > 0) { if (this.lastDirtyCount > 0) {
this.updateActivityBadge(); this.updateActivityBadge();
} }
......
...@@ -77,7 +77,7 @@ export class FileTracker implements IWorkbenchContribution { ...@@ -77,7 +77,7 @@ export class FileTracker implements IWorkbenchContribution {
private registerListeners(): void { private registerListeners(): void {
// Local text file changes // 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(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_DIRTY, (e: TextFileChangeEvent) => this.onTextFileDirty(e)));
this.toUnbind.push(this.eventService.addListener2(FileEventType.FILE_SAVED, (e: TextFileChangeEvent) => this.onTextFileSaved(e))); this.toUnbind.push(this.eventService.addListener2(FileEventType.FILE_SAVED, (e: TextFileChangeEvent) => this.onTextFileSaved(e)));
...@@ -199,7 +199,7 @@ export class FileTracker implements IWorkbenchContribution { ...@@ -199,7 +199,7 @@ export class FileTracker implements IWorkbenchContribution {
} }
} }
private onUntitledDeletedEvent(): void { private onUntitledSavedEvent(): void {
if (this.isDocumentedEdited) { if (this.isDocumentedEdited) {
this.updateDocumentEdited(); this.updateDocumentEdited();
} }
......
...@@ -117,17 +117,8 @@ export class TextFileService extends AbstractTextFileService { ...@@ -117,17 +117,8 @@ export class TextFileService extends AbstractTextFileService {
return super.revertAll(resources, force).then(r => { return super.revertAll(resources, force).then(r => {
// Revert untitled // Revert untitled
let untitledInputs = this.untitledEditorService.getAll(resources); const reverted = this.untitledEditorService.revertAll(resources);
untitledInputs.forEach(input => { reverted.forEach(res => r.results.push({ source: res, success: true }));
if (input) {
input.dispose();
r.results.push({
source: input.getResource(),
success: true
});
}
});
return r; return r;
}); });
......
...@@ -692,7 +692,7 @@ export class SearchViewlet extends Viewlet { ...@@ -692,7 +692,7 @@ export class SearchViewlet extends Viewlet {
this.viewletSettings = this.getMemento(storageService, Scope.WORKSPACE); 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(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))); this.toUnbind.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationUpdated(e.config)));
} }
...@@ -1456,7 +1456,7 @@ export class SearchViewlet extends Viewlet { ...@@ -1456,7 +1456,7 @@ export class SearchViewlet extends Viewlet {
}, sideBySide); }, sideBySide);
} }
private onUntitledFileDeleted(e: UntitledEditorEvent): void { private onUntitledFileSaved(e: UntitledEditorEvent): void {
if (!this.viewModel) { if (!this.viewModel) {
return; return;
} }
......
...@@ -36,6 +36,11 @@ export interface IUntitledEditorService { ...@@ -36,6 +36,11 @@ export interface IUntitledEditorService {
*/ */
isDirty(resource: URI): boolean; 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 * Creates a new untitled input with the optional resource URI or returns an existing one
* if the provided resource exists already as untitled input. * if the provided resource exists already as untitled input.
...@@ -72,6 +77,21 @@ export class UntitledEditorService implements IUntitledEditorService { ...@@ -72,6 +77,21 @@ export class UntitledEditorService implements IUntitledEditorService {
return Object.keys(UntitledEditorService.CACHE).map((key) => UntitledEditorService.CACHE[key]); 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 { public isDirty(resource: URI): boolean {
let input = this.get(resource); let input = this.get(resource);
......
...@@ -295,6 +295,10 @@ export class TestUntitledEditorService implements IUntitledEditorService { ...@@ -295,6 +295,10 @@ export class TestUntitledEditorService implements IUntitledEditorService {
return []; return [];
} }
public revertAll(resources?: URI[]): URI[] {
return [];
}
public isDirty() { public isDirty() {
return false; return false;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册