提交 502f5a57 编写于 作者: B Benjamin Pasero

untitled - some 💄

上级 b135faa0
......@@ -610,7 +610,12 @@ export class EditorService extends Disposable implements EditorServiceImpl {
// Untitled file support
const untitledInput = input as IUntitledTextResourceInput;
if (untitledInput.forceUntitled || !untitledInput.resource || (untitledInput.resource && untitledInput.resource.scheme === Schemas.untitled)) {
return this.untitledTextEditorService.create(untitledInput.resource, untitledInput.mode, untitledInput.contents, untitledInput.encoding);
return this.untitledTextEditorService.create({
resource: untitledInput.resource,
mode: untitledInput.mode,
initialValue: untitledInput.contents,
encoding: untitledInput.encoding
});
}
// Resource Editor Support
......
......@@ -53,9 +53,7 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
readonly files = this._register(this.instantiationService.createInstance(TextFileEditorModelManager));
private _untitled: IUntitledTextEditorModelManager;
get untitled(): IUntitledTextEditorModelManager {
return this._untitled;
}
get untitled(): IUntitledTextEditorModelManager { return this._untitled; }
abstract get encoding(): IResourceEncodings;
......@@ -179,7 +177,7 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
await this._onWillRunOperation.fireAsync({ operation: FileOperation.DELETE, target: resource }, CancellationToken.None);
const dirtyFiles = this.getDirtyFileModels().map(dirtyFileModel => dirtyFileModel.resource).filter(dirty => isEqualOrParent(dirty, resource));
await this.doRevertAllFiles(dirtyFiles, { soft: true });
await this.doRevertFiles(dirtyFiles, { soft: true });
await this.fileService.del(resource, options);
......@@ -245,7 +243,7 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
// in order to move and copy, we need to soft revert all dirty models,
// both from the source as well as the target if any
const dirtyModels = [...sourceModels, ...conflictingModels].filter(model => model.isDirty());
await this.doRevertAllFiles(dirtyModels.map(dirtyModel => dirtyModel.resource), { soft: true });
await this.doRevertFiles(dirtyModels.map(dirtyModel => dirtyModel.resource), { soft: true });
// now we can rename the source to target via file operation
let stat: IFileStatWithMetadata;
......@@ -341,20 +339,18 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
return this.fileDialogService.pickFileToSave(defaultUri, availableFileSystems);
}
private getFileModels(arg1?: URI | URI[]): ITextFileEditorModel[] {
if (Array.isArray(arg1)) {
private getFileModels(resources?: URI | URI[]): ITextFileEditorModel[] {
if (Array.isArray(resources)) {
const models: ITextFileEditorModel[] = [];
arg1.forEach(resource => {
models.push(...this.getFileModels(resource));
});
resources.forEach(resource => models.push(...this.getFileModels(resource)));
return models;
}
return this.files.getAll(arg1);
return this.files.getAll(resources);
}
private getDirtyFileModels(resources?: URI | URI[]): ITextFileEditorModel[] {
private getDirtyFileModels(resources?: URI[]): ITextFileEditorModel[] {
return this.getFileModels(resources).filter(model => model.isDirty());
}
......@@ -576,10 +572,10 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
}
// File
return !(await this.doRevertAllFiles([resource], options)).results.some(result => result.error);
return !(await this.doRevertFiles([resource], options)).results.some(result => result.error);
}
private async doRevertAllFiles(resources: URI[], options?: IRevertOptions): Promise<ITextFileOperationResult> {
private async doRevertFiles(resources: URI[], options?: IRevertOptions): Promise<ITextFileOperationResult> {
const fileModels = options?.force ? this.getFileModels(resources) : this.getDirtyFileModels(resources);
const mapResourceToResult = new ResourceMap<IResult>();
......
......@@ -18,7 +18,7 @@ import type { IResolvedTextEditorModel } from 'vs/editor/common/services/resolve
export const IUntitledTextEditorService = createDecorator<IUntitledTextEditorService>('untitledTextEditorService');
export interface IUntitledCreationOptions {
export interface IUntitledTextEditorOptions {
resource?: URI;
mode?: string;
initialValue?: string;
......@@ -54,20 +54,15 @@ export interface IUntitledTextEditorModelManager {
get(resource: URI): UntitledTextEditorInput | undefined;
/**
* Creates a new untitled input with the optional resource URI to
* be used as associated file path when saving.
* Creates a new untitled input with the provided options.
*/
create(options?: IUntitledCreationOptions): UntitledTextEditorInput;
create(resource?: URI, mode?: string, initialValue?: string, encoding?: string, hasAssociatedFilePath?: boolean): UntitledTextEditorInput;
create(options?: IUntitledTextEditorOptions): UntitledTextEditorInput;
/**
* Creates a new untitled model with the optional resource URI or returns an existing one
* if the provided resource exists already as untitled model.
*
* It is valid to pass in a file resource. In that case the path will be used as identifier.
* The use case is to be able to create a new file with a specific path with VSCode.
* Resolves an untitled editor model from the provided options. This can either
* return a new or existing model based on the options provided.
*/
resolve(options?: IUntitledCreationOptions): Promise<IUntitledTextEditorModel & IResolvedTextEditorModel>;
resolve(options?: IUntitledTextEditorOptions): Promise<IUntitledTextEditorModel & IResolvedTextEditorModel>;
/**
* A check to find out if a untitled resource has a file path associated or not.
......@@ -112,14 +107,8 @@ export class UntitledTextEditorService extends Disposable implements IUntitledTe
return this.mapResourceToInput.get(resource);
}
create(options?: IUntitledCreationOptions): UntitledTextEditorInput;
create(resource?: URI, mode?: string, initialValue?: string, encoding?: string, hasAssociatedFilePath?: boolean): UntitledTextEditorInput;
create(resourceOrOptions?: URI | IUntitledCreationOptions, mode?: string, initialValue?: string, encoding?: string, hasAssociatedFilePath: boolean = false): UntitledTextEditorInput {
if (resourceOrOptions && !URI.isUri(resourceOrOptions)) {
return this.doCreateOrGet(resourceOrOptions.resource, resourceOrOptions.mode, resourceOrOptions.initialValue, resourceOrOptions.encoding, resourceOrOptions.useResourcePath);
}
return this.doCreateOrGet(resourceOrOptions, mode, initialValue, encoding, hasAssociatedFilePath);
create(options?: IUntitledTextEditorOptions): UntitledTextEditorInput {
return this.doCreateOrGet(options?.resource, options?.mode, options?.initialValue, options?.encoding, options?.useResourcePath);
}
private doCreateOrGet(resource?: URI, mode?: string, initialValue?: string, encoding?: string, hasAssociatedFilePath: boolean = false): UntitledTextEditorInput {
......@@ -189,7 +178,7 @@ export class UntitledTextEditorService extends Disposable implements IUntitledTe
return input;
}
resolve(options?: IUntitledCreationOptions): Promise<UntitledTextEditorModel & IResolvedTextEditorModel> {
resolve(options?: IUntitledTextEditorOptions): Promise<UntitledTextEditorModel & IResolvedTextEditorModel> {
return this.doCreateOrGet(options?.resource, options?.mode, options?.initialValue, options?.encoding, options?.useResourcePath).resolve();
}
......
......@@ -46,7 +46,7 @@ suite('Workbench untitled text editors', () => {
const workingCopyService = accessor.workingCopyService;
const input1 = service.create();
assert.equal(input1, service.create(input1.getResource()));
assert.equal(input1, service.create({ resource: input1.getResource() }));
assert.equal(service.get(input1.getResource()), input1);
assert.ok(service.exists(input1.getResource()));
......@@ -109,7 +109,7 @@ suite('Workbench untitled text editors', () => {
test('Untitled with associated resource is dirty', () => {
const service = accessor.untitledTextEditorService;
const file = URI.file(join('C:\\', '/foo/file.txt'));
const untitled = service.create(file);
const untitled = service.create({ resource: file });
assert.ok(service.hasAssociatedFilePath(untitled.getResource()));
assert.equal(untitled.isDirty(), true);
......@@ -177,7 +177,7 @@ suite('Workbench untitled text editors', () => {
test('Untitled with associated path remains dirty when content gets empty', async () => {
const service = accessor.untitledTextEditorService;
const file = URI.file(join('C:\\', '/foo/file.txt'));
const input = service.create(file);
const input = service.create({ resource: file });
// dirty
const model = await input.resolve();
......@@ -193,7 +193,7 @@ suite('Workbench untitled text editors', () => {
const service = accessor.untitledTextEditorService;
const workingCopyService = accessor.workingCopyService;
const untitled = service.create(undefined, undefined, 'Hello World');
const untitled = service.create({ initialValue: 'Hello World' });
assert.equal(untitled.isDirty(), true);
let onDidChangeDirty: IWorkingCopy | undefined = undefined;
......@@ -251,7 +251,7 @@ suite('Workbench untitled text editors', () => {
config.setUserConfiguration('files', { 'defaultLanguage': defaultLanguage });
const service = accessor.untitledTextEditorService;
const input = service.create(null!, mode);
const input = service.create({ mode });
assert.equal(input.getMode(), mode);
......@@ -268,7 +268,7 @@ suite('Workbench untitled text editors', () => {
});
const service = accessor.untitledTextEditorService;
const input = service.create(null!, mode);
const input = service.create({ mode });
assert.equal(input.getMode(), mode);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册