mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 18:46:40 -05:00
Merge from vscode 3a6dcb42008d509900b3a3b2d695564eeb4dbdac (#5098)
This commit is contained in:
@@ -154,7 +154,6 @@ export class TextFileEditorModelManager extends Disposable implements ITextFileE
|
||||
// Model does not exist
|
||||
else {
|
||||
const newModel = model = this.instantiationService.createInstance(TextFileEditorModel, resource, options ? options.encoding : undefined);
|
||||
model = newModel;
|
||||
modelPromise = model.load(options);
|
||||
|
||||
// Install state change listener
|
||||
@@ -192,24 +191,24 @@ export class TextFileEditorModelManager extends Disposable implements ITextFileE
|
||||
this.mapResourceToPendingModelLoaders.set(resource, modelPromise);
|
||||
|
||||
try {
|
||||
const model = await modelPromise;
|
||||
const resolvedModel = await modelPromise;
|
||||
|
||||
// Make known to manager (if not already known)
|
||||
this.add(resource, model);
|
||||
this.add(resource, resolvedModel);
|
||||
|
||||
// Model can be dirty if a backup was restored, so we make sure to have this event delivered
|
||||
if (model.isDirty()) {
|
||||
this._onModelDirty.fire(new TextFileModelChangeEvent(model, StateChange.DIRTY));
|
||||
if (resolvedModel.isDirty()) {
|
||||
this._onModelDirty.fire(new TextFileModelChangeEvent(resolvedModel, StateChange.DIRTY));
|
||||
}
|
||||
|
||||
// Remove from pending loads
|
||||
this.mapResourceToPendingModelLoaders.delete(resource);
|
||||
|
||||
return model;
|
||||
return resolvedModel;
|
||||
} catch (error) {
|
||||
|
||||
// Free resources of this invalid model
|
||||
if (model && typeof model.dispose === 'function') { // workaround for https://github.com/Microsoft/vscode/issues/72404
|
||||
if (model) {
|
||||
model.dispose();
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ import { coalesce } from 'vs/base/common/arrays';
|
||||
import { trim } from 'vs/base/common/strings';
|
||||
import { VSBuffer } from 'vs/base/common/buffer';
|
||||
import { ITextSnapshot } from 'vs/editor/common/model';
|
||||
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
|
||||
|
||||
/**
|
||||
* The workbench file service implementation implements the raw file service spec and adds additional methods on top.
|
||||
@@ -85,7 +86,8 @@ export abstract class TextFileService extends Disposable implements ITextFileSer
|
||||
@IContextKeyService contextKeyService: IContextKeyService,
|
||||
@IDialogService private readonly dialogService: IDialogService,
|
||||
@IFileDialogService private readonly fileDialogService: IFileDialogService,
|
||||
@IEditorService private readonly editorService: IEditorService
|
||||
@IEditorService private readonly editorService: IEditorService,
|
||||
@ITextResourceConfigurationService protected readonly textResourceConfigurationService: ITextResourceConfigurationService
|
||||
) {
|
||||
super();
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import { isMacintosh, isLinux } from 'vs/base/common/platform';
|
||||
import product from 'vs/platform/product/node/product';
|
||||
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
|
||||
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
|
||||
import { UTF8, UTF8_with_bom, UTF16be, UTF16le, encodingExists, IDetectedEncodingResult, encodeStream, UTF8_BOM, UTF16be_BOM, UTF16le_BOM, toDecodeStream, IDecodeStreamResult, detectEncodingByBOMFromBuffer } from 'vs/base/node/encoding';
|
||||
import { UTF8, UTF8_with_bom, UTF16be, UTF16le, encodingExists, encodeStream, UTF8_BOM, UTF16be_BOM, UTF16le_BOM, toDecodeStream, IDecodeStreamResult, detectEncodingByBOMFromBuffer } from 'vs/base/node/encoding';
|
||||
import { WORKSPACE_EXTENSION } from 'vs/platform/workspaces/common/workspaces';
|
||||
import { joinPath, extname, isEqualOrParent } from 'vs/base/common/resources';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
@@ -70,8 +70,8 @@ export class NodeTextFileService extends TextFileService {
|
||||
|
||||
// read through encoding library
|
||||
const decoder = await toDecodeStream(this.streamToNodeReadable(bufferStream.value), {
|
||||
guessEncoding: options && options.autoGuessEncoding,
|
||||
overwriteEncoding: detected => this.encoding.getReadEncoding(resource, options, { encoding: detected, seemsBinary: false })
|
||||
guessEncoding: (options && options.autoGuessEncoding) || this.textResourceConfigurationService.getValue(resource, 'files.autoGuessEncoding'),
|
||||
overwriteEncoding: detectedEncoding => this.encoding.getReadEncoding(resource, options, detectedEncoding)
|
||||
});
|
||||
|
||||
// validate binary
|
||||
@@ -417,7 +417,7 @@ export class EncodingOracle extends Disposable implements IResourceEncodings {
|
||||
const overwriteEncoding = options && options.overwriteEncoding;
|
||||
if (!overwriteEncoding && encoding === UTF8) {
|
||||
try {
|
||||
const buffer = (await this.fileService.readFile(resource, { length: 3 })).value;
|
||||
const buffer = (await this.fileService.readFile(resource, { length: UTF8_BOM.length })).value;
|
||||
if (detectEncodingByBOMFromBuffer(buffer, buffer.byteLength) === UTF8) {
|
||||
return { encoding, addBOM: true };
|
||||
}
|
||||
@@ -438,12 +438,12 @@ export class EncodingOracle extends Disposable implements IResourceEncodings {
|
||||
};
|
||||
}
|
||||
|
||||
getReadEncoding(resource: URI, options: IReadTextFileOptions | undefined, detected: IDetectedEncodingResult): string {
|
||||
getReadEncoding(resource: URI, options: IReadTextFileOptions | undefined, detectedEncoding: string | null): string {
|
||||
let preferredEncoding: string | undefined;
|
||||
|
||||
// Encoding passed in as option
|
||||
if (options && options.encoding) {
|
||||
if (detected.encoding === UTF8 && options.encoding === UTF8) {
|
||||
if (detectedEncoding === UTF8 && options.encoding === UTF8) {
|
||||
preferredEncoding = UTF8_with_bom; // indicate the file has BOM if we are to resolve with UTF 8
|
||||
} else {
|
||||
preferredEncoding = options.encoding; // give passed in encoding highest priority
|
||||
@@ -451,11 +451,11 @@ export class EncodingOracle extends Disposable implements IResourceEncodings {
|
||||
}
|
||||
|
||||
// Encoding detected
|
||||
else if (detected.encoding) {
|
||||
if (detected.encoding === UTF8) {
|
||||
else if (detectedEncoding) {
|
||||
if (detectedEncoding === UTF8) {
|
||||
preferredEncoding = UTF8_with_bom; // if we detected UTF-8, it can only be because of a BOM
|
||||
} else {
|
||||
preferredEncoding = detected.encoding;
|
||||
preferredEncoding = detectedEncoding;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -570,6 +570,13 @@ suite('Files - TextFileService i/o', () => {
|
||||
assert.equal(result.encoding, 'utf16be');
|
||||
});
|
||||
|
||||
test('readStream - autoguessEncoding', async () => {
|
||||
const resource = URI.file(join(testDir, 'some_cp1252.txt'));
|
||||
|
||||
const result = await service.readStream(resource, { autoGuessEncoding: true });
|
||||
assert.equal(result.encoding, 'windows1252');
|
||||
});
|
||||
|
||||
test('readStream - FILE_IS_BINARY', async () => {
|
||||
const resource = URI.file(join(testDir, 'binary.txt'));
|
||||
|
||||
@@ -586,4 +593,21 @@ suite('Files - TextFileService i/o', () => {
|
||||
const result = await service.readStream(URI.file(join(testDir, 'small.txt')), { acceptTextOnly: true });
|
||||
assert.equal(result.name, 'small.txt');
|
||||
});
|
||||
|
||||
test('read - FILE_IS_BINARY', async () => {
|
||||
const resource = URI.file(join(testDir, 'binary.txt'));
|
||||
|
||||
let error: TextFileOperationError | undefined = undefined;
|
||||
try {
|
||||
await service.read(resource, { acceptTextOnly: true });
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
|
||||
assert.ok(error);
|
||||
assert.equal(error!.textFileOperationResult, TextFileOperationResult.FILE_IS_BINARY);
|
||||
|
||||
const result = await service.read(URI.file(join(testDir, 'small.txt')), { acceptTextOnly: true });
|
||||
assert.equal(result.name, 'small.txt');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user