editors dnd - fix untitled transfer

This commit is contained in:
Benjamin Pasero 2021-05-27 10:37:53 +02:00
parent bba59424f5
commit 70418f0ace
No known key found for this signature in database
GPG key ID: E6380CC4C8219E65
4 changed files with 25 additions and 10 deletions

View file

@ -300,27 +300,27 @@ export function fillEditorsDragData(accessor: ServicesAccessor, resourcesOrEdito
}
// Fill in some properties if they are not there already by accessing
// some well known things, such as text files or untitled contents.
// some well known things from the text file universe.
// This is not ideal for custom editors, but those have a chance to
// provide everything from the `asResourceEditorInput` method.
{
const resource = editor.resource;
const textModel = resource ? resource.scheme === Schemas.untitled ? textFileService.untitled.get(resource) : textFileService.files.get(resource) : undefined;
if (textModel) {
const textFileModel = resource ? textFileService.files.get(resource) : undefined;
if (textFileModel) {
// mode
if (typeof editor.mode !== 'string') {
editor.mode = textModel.getMode();
editor.mode = textFileModel.getMode();
}
// encoding
if (typeof editor.encoding !== 'string') {
editor.encoding = textModel.getEncoding();
editor.encoding = textFileModel.getEncoding();
}
// contents (only if dirty)
if (typeof editor.contents !== 'string' && textModel.isDirty()) {
editor.contents = textModel.textEditorModel.getValue();
if (typeof editor.contents !== 'string' && textFileModel.isDirty()) {
editor.contents = textFileModel.textEditorModel.getValue();
}
}

View file

@ -57,6 +57,9 @@ suite('Files - FileEditorInput', () => {
assert.ok(input.getDescription());
assert.ok(input.getTitle(Verbosity.SHORT));
const untypedInput = input.asResourceEditorInput(0);
assert.strictEqual(untypedInput.resource.toString(), input.resource.toString());
assert.strictEqual('file.js', input.getName());
assert.strictEqual(toResource.call(this, '/foo/bar/file.js').fsPath, input.resource.fsPath);

View file

@ -10,7 +10,9 @@ import { EncodingMode, IEncodingSupport, IModeSupport, ITextFileService } from '
import { ILabelService } from 'vs/platform/label/common/label';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IFileService } from 'vs/platform/files/common/files';
import { isEqual } from 'vs/base/common/resources';
import { isEqual, toLocalResource } from 'vs/base/common/resources';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IPathService } from 'vs/workbench/services/path/common/pathService';
/**
* An editor input to be used for untitled text buffers.
@ -30,7 +32,9 @@ export class UntitledTextEditorInput extends AbstractTextResourceEditorInput imp
@ITextFileService textFileService: ITextFileService,
@ILabelService labelService: ILabelService,
@IEditorService editorService: IEditorService,
@IFileService fileService: IFileService
@IFileService fileService: IFileService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
@IPathService private readonly pathService: IPathService
) {
super(model.resource, undefined, editorService, textFileService, labelService, fileService);
@ -117,10 +121,11 @@ export class UntitledTextEditorInput extends AbstractTextResourceEditorInput imp
override asResourceEditorInput(group: GroupIdentifier): IUntitledTextResourceEditorInput {
return {
resource: this.model.hasAssociatedFilePath ? this.model.resource : undefined,
resource: this.model.hasAssociatedFilePath ? toLocalResource(this.model.resource, this.environmentService.remoteAuthority, this.pathService.defaultUriScheme) : undefined,
forceUntitled: true,
encoding: this.getEncoding(),
mode: this.getMode(),
contents: this.model.isDirty() ? this.model.textEditorModel?.getValue() : undefined,
options: {
viewState: this.getViewStateFor(group)
}

View file

@ -45,6 +45,10 @@ suite('Untitled text editors', () => {
const input2 = instantiationService.createInstance(UntitledTextEditorInput, service.create());
assert.strictEqual(service.get(input2.resource), input2.model);
// asResourceEditorInput()
const untypedInput = input1.asResourceEditorInput(0);
assert.strictEqual(untypedInput.forceUntitled, true);
// get()
assert.strictEqual(service.get(input1.resource), input1.model);
assert.strictEqual(service.get(input2.resource), input2.model);
@ -71,6 +75,9 @@ suite('Untitled text editors', () => {
assert.ok(input2.isDirty());
const dirtyUntypedInput = input2.asResourceEditorInput(0);
assert.strictEqual(dirtyUntypedInput.contents, 'foo bar');
assert.ok(workingCopyService.isDirty(input2.resource));
assert.strictEqual(workingCopyService.dirtyCount, 1);