提交 ea79f0a9 编写于 作者: B Benjamin Pasero

untitled - reduce service methods

上级 c5fb0adc
......@@ -11,7 +11,6 @@ import { toResource } from 'vs/base/test/common/utils';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
import { HotExitConfiguration, IFileService } from 'vs/platform/files/common/files';
import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/common/textFileEditorModelManager';
import { IWorkspaceContextService, Workspace } from 'vs/platform/workspace/common/workspace';
......@@ -29,7 +28,6 @@ class ServiceAccessor {
@ILifecycleService public lifecycleService: TestLifecycleService,
@ITextFileService public textFileService: TestTextFileService,
@IFilesConfigurationService public filesConfigurationService: TestFilesConfigurationService,
@IUntitledTextEditorService public untitledTextEditorService: IUntitledTextEditorService,
@IWorkspaceContextService public contextService: TestContextService,
@IModelService public modelService: ModelServiceImpl,
@IFileService public fileService: TestFileService,
......@@ -69,7 +67,6 @@ suite('BackupOnShutdown', () => {
model.dispose();
}
(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
accessor.untitledTextEditorService.revertAll();
backupOnShutdown.dispose();
});
......
......@@ -87,7 +87,6 @@ suite('BackupRestorer', () => {
disposables = [];
(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
accessor.untitledTextEditorService.revertAll();
return pfs.rimraf(backupHome, pfs.RimRafMode.MOVE);
});
......@@ -132,10 +131,12 @@ suite('BackupRestorer', () => {
if (isEqual(resource, untitledFile1)) {
const model = await accessor.untitledTextEditorService.createOrGet(resource).resolve();
assert.equal(model.textEditorModel.getValue(), 'untitled-1');
model.dispose();
counter++;
} else if (isEqual(resource, untitledFile2)) {
const model = await accessor.untitledTextEditorService.createOrGet(resource).resolve();
assert.equal(model.textEditorModel.getValue(), 'untitled-2');
model.dispose();
counter++;
} else if (isEqual(resource, fooFile)) {
const model = await accessor.textFileService.models.get(resource!)?.load();
......
......@@ -93,7 +93,6 @@ suite('BackupTracker', () => {
disposables = [];
(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
accessor.untitledTextEditorService.revertAll();
return pfs.rimraf(backupHome, pfs.RimRafMode.MOVE);
});
......
......@@ -592,7 +592,7 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
}
private suggestFileName(untitledResource: URI): URI {
const untitledFileName = this.untitledTextEditorService.suggestFileName(untitledResource);
const untitledFileName = this.untitledTextEditorService.exists(untitledResource) ? this.untitledTextEditorService.createOrGet(untitledResource).suggestFileName() : basename(untitledResource);
const remoteAuthority = this.environmentService.configuration.remoteAuthority;
const schemeFilter = remoteAuthority ? Schemas.vscodeRemote : Schemas.file;
......@@ -618,19 +618,23 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
return !(await this.doRevertAll([resource], options)).results.some(result => result.error);
}
private async doRevertAll(resources?: URI[], options?: IRevertOptions): Promise<ITextFileOperationResult> {
private async doRevertAll(resources: URI[], options?: IRevertOptions): Promise<ITextFileOperationResult> {
// Revert files first
const revertOperationResult = await this.doRevertAllFiles(resources, options);
const revertFileOperationResult = await this.doRevertAllFiles(resources, options);
// Revert untitled
const untitledReverted = this.untitledTextEditorService.revertAll(resources);
untitledReverted.forEach(untitled => revertOperationResult.results.push({ source: untitled }));
const revertUntitledOperationResult = await this.doRevertAllUntitled(resources, options);
return revertOperationResult;
return {
results: [
...revertFileOperationResult.results,
...revertUntitledOperationResult.results
]
};
}
private async doRevertAllFiles(resources?: URI[], options?: IRevertOptions): Promise<ITextFileOperationResult> {
private async doRevertAllFiles(resources: URI[], options?: IRevertOptions): Promise<ITextFileOperationResult> {
const fileModels = options?.force ? this.getFileModels(resources) : this.getDirtyFileModels(resources);
const mapResourceToResult = new ResourceMap<IResult>();
......@@ -668,6 +672,22 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
return { results: mapResourceToResult.values() };
}
private async doRevertAllUntitled(resources: URI[], options?: IRevertOptions): Promise<ITextFileOperationResult> {
const result: ITextFileOperationResult = { results: [] };
await Promise.all(resources.map(resource => {
if (this.untitledTextEditorService.exists(resource)) {
result.results.push({ source: resource });
return this.untitledTextEditorService.createOrGet(resource).revert(options);
}
return Promise.resolve(undefined);
}));
return result;
}
//#endregion
//#region dirty
......
......@@ -6,7 +6,6 @@ import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { workbenchInstantiationService, TestTextFileService } from 'vs/workbench/test/workbenchTestServices';
import { ITextFileService, snapshotToString, TextFileOperationResult, TextFileOperationError } from 'vs/workbench/services/textfile/common/textfiles';
import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
import { IFileService } from 'vs/platform/files/common/files';
import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/common/textFileEditorModelManager';
import { Schemas } from 'vs/base/common/network';
......@@ -31,8 +30,7 @@ import { detectEncodingByBOM } from 'vs/base/test/node/encoding/encoding.test';
class ServiceAccessor {
constructor(
@ITextFileService public textFileService: TestTextFileService,
@IUntitledTextEditorService public untitledTextEditorService: IUntitledTextEditorService
@ITextFileService public textFileService: TestTextFileService
) {
}
}
......@@ -95,7 +93,6 @@ suite('Files - TextFileService i/o', () => {
teardown(async () => {
(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
accessor.untitledTextEditorService.revertAll();
disposables.clear();
......
......@@ -53,7 +53,6 @@ suite('Files - TextFileService', () => {
model.dispose();
}
(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
accessor.untitledTextEditorService.revertAll();
});
test('isDirty/getDirty - files and untitled', async function () {
......@@ -79,6 +78,8 @@ suite('Files - TextFileService', () => {
assert.ok(accessor.textFileService.isDirty(untitled.getResource()));
assert.equal(accessor.textFileService.getDirty().length, 2);
assert.equal(accessor.textFileService.getDirty([untitled.getResource()])[0].toString(), untitled.getResource().toString());
untitledModel.dispose();
});
test('save - file', async function () {
......
......@@ -49,7 +49,6 @@ suite('Workbench - TextModelResolverService', () => {
model = (undefined)!;
}
(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
accessor.untitledTextEditorService.revertAll();
});
test('resolve resource', async () => {
......@@ -120,6 +119,7 @@ suite('Workbench - TextModelResolverService', () => {
assert.ok(editorModel);
ref.dispose();
input.dispose();
model.dispose();
});
test('even loading documents should be refcounted', async () => {
......
......@@ -14,7 +14,6 @@ import { ResourceMap } from 'vs/base/common/map';
import { Schemas } from 'vs/base/common/network';
import { Disposable } from 'vs/base/common/lifecycle';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { basename } from 'vs/base/common/resources';
export const IUntitledTextEditorService = createDecorator<IUntitledTextEditorService>('untitledTextEditorService');
......@@ -60,16 +59,6 @@ export interface IUntitledTextEditorService {
*/
isDirty(resource: URI): boolean;
/**
* Find out if a backup with the provided resource exists and has a backup on disk.
*/
hasBackup(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.
......@@ -84,16 +73,6 @@ export interface IUntitledTextEditorService {
* A check to find out if a untitled resource has a file path associated or not.
*/
hasAssociatedFilePath(resource: URI): boolean;
/**
* Suggests a filename for the given untitled resource if it is known.
*/
suggestFileName(resource: URI): string;
/**
* Get the configured encoding for the given untitled resource if any.
*/
getEncoding(resource: URI): string | undefined;
}
export class UntitledTextEditorService extends Disposable implements IUntitledTextEditorService {
......@@ -136,33 +115,12 @@ export class UntitledTextEditorService extends Disposable implements IUntitledTe
return this.mapResourceToInput.has(resource);
}
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;
}
isDirty(resource: URI): boolean {
const input = this.get(resource);
return input ? input.isDirty() : false;
}
hasBackup(resource: URI): boolean {
const input = this.get(resource);
return input ? input.hasBackup() : false;
}
getDirty(resources?: URI[]): URI[] {
let inputs: UntitledTextEditorInput[];
if (resources) {
......@@ -253,18 +211,6 @@ export class UntitledTextEditorService extends Disposable implements IUntitledTe
hasAssociatedFilePath(resource: URI): boolean {
return this.mapResourceToAssociatedFilePath.has(resource);
}
suggestFileName(resource: URI): string {
const input = this.get(resource);
return input ? input.suggestFileName() : basename(resource);
}
getEncoding(resource: URI): string | undefined {
const input = this.get(resource);
return input ? input.getEncoding() : undefined;
}
}
registerSingleton(IUntitledTextEditorService, UntitledTextEditorService, true);
......@@ -14,8 +14,7 @@ import { workbenchInstantiationService } from 'vs/workbench/test/workbenchTestSe
import { Schemas } from 'vs/base/common/network';
class ServiceAccessor {
constructor(@IUntitledTextEditorService public untitledTextEditorService: UntitledTextEditorService) {
}
constructor(@IUntitledTextEditorService public untitledTextEditorService: UntitledTextEditorService) { }
}
class FileEditorInput extends EditorInput {
......@@ -48,7 +47,6 @@ suite('Workbench editor', () => {
});
teardown(() => {
accessor.untitledTextEditorService.revertAll();
accessor.untitledTextEditorService.dispose();
});
......
......@@ -44,7 +44,6 @@ suite('Workbench untitled text editors', () => {
});
teardown(() => {
accessor.untitledTextEditorService.revertAll();
accessor.untitledTextEditorService.dispose();
});
......@@ -67,8 +66,8 @@ suite('Workbench untitled text editors', () => {
assert.equal(service.getAll().length, 2);
assert.equal(service.getAll([input1.getResource(), input2.getResource()]).length, 2);
// revertAll()
service.revertAll([input1.getResource()]);
// revert()
input1.revert();
assert.ok(input1.isDisposed());
assert.equal(service.getAll().length, 1);
......@@ -90,7 +89,8 @@ suite('Workbench untitled text editors', () => {
assert.ok(workingCopyService.isDirty(input2.getResource()));
assert.equal(workingCopyService.dirtyCount, 1);
service.revertAll();
input1.revert();
input2.revert();
assert.equal(service.getAll().length, 0);
assert.ok(!input2.isDirty());
assert.ok(!model.isDirty());
......@@ -109,6 +109,9 @@ suite('Workbench untitled text editors', () => {
});
model.textEditorModel.setValue('foo bar');
model.dispose();
input1.dispose();
input2.dispose();
});
test('Untitled with associated resource is dirty', () => {
......@@ -136,6 +139,7 @@ suite('Workbench untitled text editors', () => {
assert.ok(!model.isDirty());
assert.ok(!workingCopyService.isDirty(model.resource));
input.dispose();
model.dispose();
});
test('Untitled via createOrGet options', async () => {
......@@ -174,7 +178,8 @@ suite('Workbench untitled text editors', () => {
const service = accessor.untitledTextEditorService;
const input = service.createOrGet();
assert.ok(service.suggestFileName(input.getResource()));
assert.ok(input.suggestFileName().length > 0);
input.dispose();
});
test('Untitled with associated path remains dirty when content gets empty', async () => {
......@@ -189,6 +194,7 @@ suite('Workbench untitled text editors', () => {
model.textEditorModel.setValue('');
assert.ok(model.isDirty());
input.dispose();
model.dispose();
});
test('Untitled with initial content is dirty', async () => {
......@@ -211,6 +217,7 @@ suite('Workbench untitled text editors', () => {
untitled.dispose();
listener.dispose();
model.dispose();
});
test('Untitled created with files.defaultLanguage setting', () => {
......@@ -281,6 +288,7 @@ suite('Workbench untitled text editors', () => {
assert.equal(input.getMode(), PLAINTEXT_MODE_ID);
input.dispose();
model.dispose();
});
test('encoding change event', async () => {
......@@ -299,6 +307,7 @@ suite('Workbench untitled text editors', () => {
model.setEncoding('utf16');
assert.equal(counter, 1);
input.dispose();
model.dispose();
});
test('onDidChangeContent event', async function () {
......@@ -324,6 +333,7 @@ suite('Workbench untitled text editors', () => {
assert.equal(counter, 4, 'Dirty model should trigger event');
input.dispose();
model.dispose();
});
test('onDidChangeDirty event', async function () {
......@@ -343,6 +353,7 @@ suite('Workbench untitled text editors', () => {
assert.equal(counter, 1, 'Another change does not fire event');
input.dispose();
model.dispose();
});
test('onDidDisposeModel event', async () => {
......@@ -356,9 +367,10 @@ suite('Workbench untitled text editors', () => {
assert.equal(r.toString(), input.getResource().toString());
});
await input.resolve();
const model = await input.resolve();
assert.equal(counter, 0);
input.dispose();
assert.equal(counter, 1);
model.dispose();
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册