textfiles - simplify getAll()

This commit is contained in:
Benjamin Pasero 2020-01-24 12:26:38 +01:00
parent 7b315c80be
commit e57ba0c870
4 changed files with 32 additions and 49 deletions

View file

@ -6,7 +6,7 @@
import * as nls from 'vs/nls';
import { URI } from 'vs/base/common/uri';
import { Emitter, AsyncEmitter } from 'vs/base/common/event';
import { IResult, ITextFileOperationResult, ITextFileService, ITextFileStreamContent, ITextFileEditorModel, ITextFileContent, IResourceEncodings, IReadTextFileOptions, IWriteTextFileOptions, toBufferOrReadable, TextFileOperationError, TextFileOperationResult, FileOperationWillRunEvent, FileOperationDidRunEvent, ITextFileSaveOptions } from 'vs/workbench/services/textfile/common/textfiles';
import { IResult, ITextFileOperationResult, ITextFileService, ITextFileStreamContent, ITextFileEditorModel, ITextFileContent, IResourceEncodings, IReadTextFileOptions, IWriteTextFileOptions, toBufferOrReadable, TextFileOperationError, TextFileOperationResult, FileOperationWillRunEvent, FileOperationDidRunEvent, ITextFileSaveOptions, ITextFileEditorModelManager } from 'vs/workbench/services/textfile/common/textfiles';
import { IRevertOptions, IEncodingSupport } from 'vs/workbench/common/editor';
import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
import { IFileService, FileOperationError, FileOperationResult, IFileStatWithMetadata, ICreateFileOptions, FileOperation } from 'vs/platform/files/common/files';
@ -33,6 +33,7 @@ import { CancellationToken } from 'vs/base/common/cancellation';
import { ITextModelService, IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService';
import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { coalesce } from 'vs/base/common/arrays';
/**
* The workbench file service implementation implements the raw file service spec and adds additional methods on top.
@ -51,16 +52,15 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
//#endregion
readonly files = this._register(this.instantiationService.createInstance(TextFileEditorModelManager));
readonly files: ITextFileEditorModelManager = this._register(this.instantiationService.createInstance(TextFileEditorModelManager));
private _untitled: IUntitledTextEditorModelManager;
get untitled(): IUntitledTextEditorModelManager { return this._untitled; }
readonly untitled: IUntitledTextEditorModelManager = this.untitledTextEditorService;
abstract get encoding(): IResourceEncodings;
constructor(
@IFileService protected readonly fileService: IFileService,
@IUntitledTextEditorService untitledTextEditorService: IUntitledTextEditorService,
@IUntitledTextEditorService private untitledTextEditorService: IUntitledTextEditorService,
@ILifecycleService protected readonly lifecycleService: ILifecycleService,
@IInstantiationService protected readonly instantiationService: IInstantiationService,
@IModelService private readonly modelService: IModelService,
@ -76,8 +76,6 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
) {
super();
this._untitled = untitledTextEditorService;
this.registerListeners();
}
@ -350,15 +348,12 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
return this.fileDialogService.pickFileToSave(defaultUri, availableFileSystems);
}
private getFileModels(resources?: URI | URI[]): ITextFileEditorModel[] {
private getFileModels(resources?: URI[]): ITextFileEditorModel[] {
if (Array.isArray(resources)) {
const models: ITextFileEditorModel[] = [];
resources.forEach(resource => models.push(...this.getFileModels(resource)));
return models;
return coalesce(resources.map(resource => this.files.get(resource)));
}
return this.files.getAll(resources);
return this.files.getAll();
}
private getDirtyFileModels(resources?: URI[]): ITextFileEditorModel[] {
@ -637,19 +632,12 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
//#region dirty
isDirty(resource: URI): boolean {
// Check for dirty untitled
if (resource.scheme === Schemas.untitled) {
const model = this.untitled.get(resource);
if (model) {
return model.isDirty();
}
return false;
const model = resource.scheme === Schemas.untitled ? this.untitled.get(resource) : this.files.get(resource);
if (model) {
return model.isDirty();
}
// Check for dirty file
return this.files.getAll(resource).some(model => model.isDirty());
return false;
}
//#endregion

View file

@ -177,13 +177,7 @@ export class TextFileEditorModelManager extends Disposable implements ITextFileE
}
}
getAll(resource?: URI, filter?: (model: ITextFileEditorModel) => boolean): ITextFileEditorModel[] {
if (resource) {
const res = this.mapResourceToModel.get(resource);
return res ? [res] : [];
}
getAll(filter?: (model: ITextFileEditorModel) => boolean): ITextFileEditorModel[] {
const res: ITextFileEditorModel[] = [];
this.mapResourceToModel.forEach(model => {
if (!filter || filter(model)) {

View file

@ -356,6 +356,7 @@ export interface ITextFileEditorModelManager {
readonly onDidChangeOrphaned: Event<ITextFileEditorModel>;
get(resource: URI): ITextFileEditorModel | undefined;
getAll(): ITextFileEditorModel[];
resolve(resource: URI, options?: IModelLoadOrCreateOptions): Promise<ITextFileEditorModel>;

View file

@ -50,37 +50,37 @@ suite('Files - TextFileEditorModelManager', () => {
assert.ok(!manager.get(fileUpper));
let result = manager.getAll();
assert.strictEqual(3, result.length);
let results = manager.getAll();
assert.strictEqual(3, results.length);
result = manager.getAll(URI.file('/yes'));
assert.strictEqual(0, result.length);
let result = manager.get(URI.file('/yes'));
assert.ok(!result);
result = manager.getAll(URI.file('/some/other.txt'));
assert.strictEqual(0, result.length);
result = manager.get(URI.file('/some/other.txt'));
assert.ok(!result);
result = manager.getAll(URI.file('/some/other.html'));
assert.strictEqual(1, result.length);
result = manager.get(URI.file('/some/other.html'));
assert.ok(result);
result = manager.getAll(fileUpper);
assert.strictEqual(0, result.length);
result = manager.get(fileUpper);
assert.ok(!result);
manager.remove(URI.file(''));
result = manager.getAll();
assert.strictEqual(3, result.length);
results = manager.getAll();
assert.strictEqual(3, results.length);
manager.remove(URI.file('/some/other.html'));
result = manager.getAll();
assert.strictEqual(2, result.length);
results = manager.getAll();
assert.strictEqual(2, results.length);
manager.remove(fileUpper);
result = manager.getAll();
assert.strictEqual(2, result.length);
results = manager.getAll();
assert.strictEqual(2, results.length);
manager.clear();
result = manager.getAll();
assert.strictEqual(0, result.length);
results = manager.getAll();
assert.strictEqual(0, results.length);
model1.dispose();
model2.dispose();