提交 3517c023 编写于 作者: B Benjamin Pasero

files - stop checking for BOM before writing

This would always result in a file read which can be slow in remote connections.
上级 7bfd7fb6
......@@ -102,7 +102,7 @@ class PartsSplash {
layoutInfo,
baseTheme
}),
{ encoding: 'utf8', overwriteEncoding: true }
{ encoding: 'utf8' }
);
if (baseTheme !== this._lastBaseTheme || colorInfo.editorBackground !== this._lastBackground) {
......
......@@ -34,7 +34,7 @@ import { IWorkingCopyFileService } from 'vs/workbench/services/workingCopy/commo
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { WORKSPACE_EXTENSION } from 'vs/platform/workspaces/common/workspaces';
import { UTF8, UTF8_with_bom, UTF16be, UTF16le, encodingExists, UTF8_BOM, detectEncodingByBOMFromBuffer, toEncodeReadable, toDecodeStream, IDecodeStreamResult } from 'vs/workbench/services/textfile/common/encoding';
import { UTF8, UTF8_with_bom, UTF16be, UTF16le, encodingExists, toEncodeReadable, toDecodeStream, IDecodeStreamResult } from 'vs/workbench/services/textfile/common/encoding';
import { consumeStream } from 'vs/base/common/stream';
import { IModeService } from 'vs/editor/common/services/modeService';
import { ILogService } from 'vs/platform/log/common/log';
......@@ -532,7 +532,6 @@ export class EncodingOracle extends Disposable implements IResourceEncodings {
@ITextResourceConfigurationService private textResourceConfigurationService: ITextResourceConfigurationService,
@IWorkbenchEnvironmentService private environmentService: IWorkbenchEnvironmentService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@IFileService private fileService: IFileService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService
) {
super();
......@@ -569,26 +568,7 @@ export class EncodingOracle extends Disposable implements IResourceEncodings {
async getWriteEncoding(resource: URI, options?: IWriteTextFileOptions): Promise<{ encoding: string, addBOM: boolean }> {
const { encoding, hasBOM } = await this.getPreferredWriteEncoding(resource, options ? options.encoding : undefined);
// Some encodings come with a BOM automatically
if (hasBOM) {
return { encoding, addBOM: true };
}
// Ensure that we preserve an existing BOM if found for UTF8
// unless we are instructed to overwrite the encoding
const overwriteEncoding = options?.overwriteEncoding;
if (!overwriteEncoding && encoding === UTF8) {
try {
const buffer = (await this.fileService.readFile(resource, { length: UTF8_BOM.length })).value;
if (detectEncodingByBOMFromBuffer(buffer, buffer.byteLength) === UTF8_with_bom) {
return { encoding, addBOM: true };
}
} catch (error) {
// ignore - file might not exist
}
}
return { encoding, addBOM: false };
return { encoding, addBOM: hasBOM };
}
async getPreferredWriteEncoding(resource: URI, preferredEncoding?: string): Promise<IResourceEncoding> {
......
......@@ -810,7 +810,6 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
try {
const stat = await this.textFileService.write(lastResolvedFileStat.resource, textFileEditorModel.createSnapshot(), {
overwriteReadonly: options.overwriteReadonly,
overwriteEncoding: options.overwriteEncoding,
mtime: lastResolvedFileStat.mtime,
encoding: this.getEncoding(),
etag: (options.ignoreModifiedSince || !this.filesConfigurationService.preventSaveConflicts(lastResolvedFileStat.resource, textFileEditorModel.getMode())) ? ETAG_DISABLED : lastResolvedFileStat.etag,
......@@ -978,7 +977,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
}
if (!this.inConflictMode) {
this.save({ overwriteEncoding: true });
this.save();
}
}
......
......@@ -124,11 +124,6 @@ export interface IWriteTextFileOptions extends IWriteFileOptions {
*/
encoding?: string;
/**
* If set to true, will enforce the selected encoding and not perform any detection using BOMs.
*/
overwriteEncoding?: boolean;
/**
* Whether to overwrite a file even if it is readonly.
*/
......@@ -370,11 +365,6 @@ export interface ITextFileSaveOptions extends ISaveOptions {
*/
overwriteReadonly?: boolean;
/**
* Overwrite the encoding of the file on disk as configured.
*/
overwriteEncoding?: boolean;
/**
* Save the file with elevated privileges.
*
......
......@@ -310,13 +310,13 @@ export default function createSuite(params: Params) {
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
assert.equal(detectedEncoding, UTF8_with_bom);
// ensure BOM preserved
await service.write(resource, content, { encoding: UTF8 });
// ensure BOM preserved if enforced
await service.write(resource, content, { encoding: UTF8_with_bom });
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
assert.equal(detectedEncoding, UTF8_with_bom);
// allow to remove BOM
await service.write(resource, content, { encoding: UTF8, overwriteEncoding: true });
await service.write(resource, content, { encoding: UTF8 });
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
assert.equal(detectedEncoding, null);
......@@ -338,13 +338,13 @@ export default function createSuite(params: Params) {
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
assert.equal(detectedEncoding, UTF8_with_bom);
// ensure BOM preserved
await service.write(resource, model.createSnapshot(), { encoding: UTF8 });
// ensure BOM preserved if enforced
await service.write(resource, model.createSnapshot(), { encoding: UTF8_with_bom });
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
assert.equal(detectedEncoding, UTF8_with_bom);
// allow to remove BOM
await service.write(resource, model.createSnapshot(), { encoding: UTF8, overwriteEncoding: true });
await service.write(resource, model.createSnapshot(), { encoding: UTF8 });
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
assert.equal(detectedEncoding, null);
......@@ -360,7 +360,7 @@ export default function createSuite(params: Params) {
let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
assert.equal(detectedEncoding, UTF8_with_bom);
await service.write(resource, 'Hello World');
await service.write(resource, 'Hello World', { encoding: detectedEncoding! });
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
assert.equal(detectedEncoding, UTF8_with_bom);
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册