提交 4644b60d 编写于 作者: B Benjamin Pasero

remove internal API for accessing the value of a model directly

上级 fb1d2193
......@@ -67,13 +67,13 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
/**
* Creates the text editor model with the provided value, modeId (can be comma separated for multiple values) and optional resource URL.
*/
protected createTextEditorModel(value: string | ITextBufferFactory, resource?: URI, modeId?: string): TPromise<EditorModel> {
protected createTextEditorModel(value: ITextBufferFactory, resource?: URI, modeId?: string): TPromise<EditorModel> {
const firstLineText = this.getFirstLineText(value);
const mode = this.getOrCreateMode(this.modeService, modeId, firstLineText);
return TPromise.as(this.doCreateTextEditorModel(value, mode, resource));
}
private doCreateTextEditorModel(value: string | ITextBufferFactory, mode: TPromise<IMode>, resource: URI): EditorModel {
private doCreateTextEditorModel(value: ITextBufferFactory, mode: TPromise<IMode>, resource: URI): EditorModel {
let model = resource && this.modelService.getModel(resource);
if (!model) {
model = this.modelService.createModel(value, mode, resource);
......@@ -91,24 +91,7 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
return this;
}
protected getFirstLineText(value: string | ITextBufferFactory | ITextSnapshot): string {
// string
if (typeof value === 'string') {
const firstLineText = value.substr(0, 100);
let crIndex = firstLineText.indexOf('\r');
if (crIndex < 0) {
crIndex = firstLineText.length;
}
let lfIndex = firstLineText.indexOf('\n');
if (lfIndex < 0) {
lfIndex = firstLineText.length;
}
return firstLineText.substr(0, Math.min(crIndex, lfIndex));
}
protected getFirstLineText(value: ITextBufferFactory | ITextSnapshot): string {
// text buffer factory
const textBufferFactory = value as ITextBufferFactory;
......@@ -118,7 +101,19 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
// text snapshot
const textSnapshot = value as ITextSnapshot;
return this.getFirstLineText(textSnapshot.read() || '');
const firstLineText = textSnapshot.read().substr(0, 100);
let crIndex = firstLineText.indexOf('\r');
if (crIndex < 0) {
crIndex = firstLineText.length;
}
let lfIndex = firstLineText.indexOf('\n');
if (lfIndex < 0) {
lfIndex = firstLineText.length;
}
return firstLineText.substr(0, Math.min(crIndex, lfIndex));
}
/**
......@@ -133,7 +128,7 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
/**
* Updates the text editor model with the provided value. If the value is the same as the model has, this is a no-op.
*/
protected updateTextEditorModel(newValue: string | ITextBufferFactory): void {
protected updateTextEditorModel(newValue: ITextBufferFactory): void {
if (!this.textEditorModel) {
return;
}
......
......@@ -52,10 +52,10 @@ export interface IBackupFileService {
* Backs up a resource.
*
* @param resource The resource to back up.
* @param content The content of the resource as value or snapshot.
* @param content The content of the resource as snapshot.
* @param versionId The version id of the resource to backup.
*/
backupResource(resource: Uri, content: string | ITextSnapshot, versionId?: number): TPromise<void>;
backupResource(resource: Uri, content: ITextSnapshot, versionId?: number): TPromise<void>;
/**
* Gets a list of file backups for the current workspace.
......
......@@ -11,7 +11,7 @@ import * as pfs from 'vs/base/node/pfs';
import Uri from 'vs/base/common/uri';
import { ResourceQueue } from 'vs/base/common/async';
import { IBackupFileService, BACKUP_FILE_UPDATE_OPTIONS, BACKUP_FILE_RESOLVE_OPTIONS } from 'vs/workbench/services/backup/common/backup';
import { IFileService, ITextSnapshot, IFileStat } from 'vs/platform/files/common/files';
import { IFileService, ITextSnapshot } from 'vs/platform/files/common/files';
import { TPromise } from 'vs/base/common/winjs.base';
import { readToMatchingString } from 'vs/base/node/stream';
import { ITextBufferFactory } from 'vs/editor/common/model';
......@@ -171,7 +171,7 @@ export class BackupFileService implements IBackupFileService {
});
}
public backupResource(resource: Uri, content: string | ITextSnapshot, versionId?: number): TPromise<void> {
public backupResource(resource: Uri, content: ITextSnapshot, versionId?: number): TPromise<void> {
if (this.isShuttingDown) {
return TPromise.as(void 0);
}
......@@ -190,17 +190,7 @@ export class BackupFileService implements IBackupFileService {
const preamble = `${resource.toString()}${BackupFileService.META_MARKER}`;
// Update content with value
let updateContentPromise: TPromise<IFileStat>;
if (typeof content === 'string') {
updateContentPromise = this.fileService.updateContent(backupResource, `${preamble}${content}`, BACKUP_FILE_UPDATE_OPTIONS);
}
// Update content with snapshot
else {
updateContentPromise = this.fileService.updateContent(backupResource, new BackupSnapshot(content, preamble), BACKUP_FILE_UPDATE_OPTIONS);
}
return updateContentPromise.then(() => model.add(backupResource, versionId));
return this.fileService.updateContent(backupResource, new BackupSnapshot(content, preamble), BACKUP_FILE_UPDATE_OPTIONS).then(() => model.add(backupResource, versionId));
});
});
}
......
......@@ -16,7 +16,7 @@ import pfs = require('vs/base/node/pfs');
import Uri from 'vs/base/common/uri';
import { BackupFileService, BackupFilesModel } from 'vs/workbench/services/backup/node/backupFileService';
import { FileService } from 'vs/workbench/services/files/node/fileService';
import { TextModel } from 'vs/editor/common/model/textModel';
import { TextModel, createTextBufferFactory } from 'vs/editor/common/model/textModel';
import { TestContextService, TestTextResourceConfigurationService, getRandomTestPath, TestLifecycleService } from 'vs/workbench/test/workbenchTestServices';
import { Workspace, toWorkspaceFolders } from 'vs/platform/workspace/common/workspace';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
......@@ -107,7 +107,7 @@ suite('BackupFileService', () => {
suite('backupResource', () => {
test('text file', function (done: () => void) {
service.backupResource(fooFile, 'test').then(() => {
service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
assert.equal(fs.existsSync(fooBackupPath), true);
assert.equal(fs.readFileSync(fooBackupPath), `${fooFile.toString()}\ntest`);
......@@ -116,7 +116,7 @@ suite('BackupFileService', () => {
});
test('untitled file', function (done: () => void) {
service.backupResource(untitledFile, 'test').then(() => {
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
assert.equal(fs.existsSync(untitledBackupPath), true);
assert.equal(fs.readFileSync(untitledBackupPath), `${untitledFile.toString()}\ntest`);
......@@ -177,7 +177,7 @@ suite('BackupFileService', () => {
suite('discardResourceBackup', () => {
test('text file', function (done: () => void) {
service.backupResource(fooFile, 'test').then(() => {
service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
service.discardResourceBackup(fooFile).then(() => {
assert.equal(fs.existsSync(fooBackupPath), false);
......@@ -188,7 +188,7 @@ suite('BackupFileService', () => {
});
test('untitled file', function (done: () => void) {
service.backupResource(untitledFile, 'test').then(() => {
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
service.discardResourceBackup(untitledFile).then(() => {
assert.equal(fs.existsSync(untitledBackupPath), false);
......@@ -201,9 +201,9 @@ suite('BackupFileService', () => {
suite('discardAllWorkspaceBackups', () => {
test('text file', function (done: () => void) {
service.backupResource(fooFile, 'test').then(() => {
service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
service.backupResource(barFile, 'test').then(() => {
service.backupResource(barFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 2);
service.discardAllWorkspaceBackups().then(() => {
assert.equal(fs.existsSync(fooBackupPath), false);
......@@ -216,7 +216,7 @@ suite('BackupFileService', () => {
});
test('untitled file', function (done: () => void) {
service.backupResource(untitledFile, 'test').then(() => {
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
service.discardAllWorkspaceBackups().then(() => {
assert.equal(fs.existsSync(untitledBackupPath), false);
......@@ -228,7 +228,7 @@ suite('BackupFileService', () => {
test('should disable further backups', function (done: () => void) {
service.discardAllWorkspaceBackups().then(() => {
service.backupResource(untitledFile, 'test').then(() => {
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
assert.equal(fs.existsSync(workspaceBackupPath), false);
done();
});
......@@ -238,10 +238,10 @@ suite('BackupFileService', () => {
suite('getWorkspaceFileBackups', () => {
test('("file") - text file', done => {
service.backupResource(fooFile, `test`).then(() => {
service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
service.getWorkspaceFileBackups().then(textFiles => {
assert.deepEqual(textFiles.map(f => f.fsPath), [fooFile.fsPath]);
service.backupResource(barFile, `test`).then(() => {
service.backupResource(barFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
service.getWorkspaceFileBackups().then(textFiles => {
assert.deepEqual(textFiles.map(f => f.fsPath), [fooFile.fsPath, barFile.fsPath]);
done();
......@@ -252,7 +252,7 @@ suite('BackupFileService', () => {
});
test('("file") - untitled file', done => {
service.backupResource(untitledFile, `test`).then(() => {
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
service.getWorkspaceFileBackups().then(textFiles => {
assert.deepEqual(textFiles.map(f => f.fsPath), [untitledFile.fsPath]);
done();
......@@ -261,7 +261,7 @@ suite('BackupFileService', () => {
});
test('("untitled") - untitled file', done => {
service.backupResource(untitledFile, `test`).then(() => {
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
service.getWorkspaceFileBackups().then(textFiles => {
assert.deepEqual(textFiles.map(f => f.fsPath), ['Untitled-1']);
done();
......@@ -273,7 +273,7 @@ suite('BackupFileService', () => {
test('resolveBackupContent', () => {
test('should restore the original contents (untitled file)', () => {
const contents = 'test\nand more stuff';
service.backupResource(untitledFile, contents).then(() => {
service.backupResource(untitledFile, createTextBufferFactory(contents).create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
service.resolveBackupContent(service.toBackupResource(untitledFile)).then(factory => {
assert.equal(contents, snapshotToString(factory.create(platform.isWindows ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF).createSnapshot(true)));
});
......@@ -288,7 +288,7 @@ suite('BackupFileService', () => {
'adipiscing ßß elit',
].join('');
service.backupResource(fooFile, contents).then(() => {
service.backupResource(fooFile, createTextBufferFactory(contents).create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
service.resolveBackupContent(service.toBackupResource(untitledFile)).then(factory => {
assert.equal(contents, snapshotToString(factory.create(platform.isWindows ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF).createSnapshot(true)));
});
......
......@@ -23,7 +23,7 @@ import { ITextFileService, IAutoSaveConfiguration, ModelState, ITextFileEditorMo
import { EncodingMode } from 'vs/workbench/common/editor';
import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel';
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
import { IFileService, IFileStat, FileOperationError, FileOperationResult, IContent, CONTENT_CHANGE_EVENT_BUFFER_DELAY, FileChangesEvent, FileChangeType } from 'vs/platform/files/common/files';
import { IFileService, IFileStat, FileOperationError, FileOperationResult, CONTENT_CHANGE_EVENT_BUFFER_DELAY, FileChangesEvent, FileChangeType } from 'vs/platform/files/common/files';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IMessageService, Severity } from 'vs/platform/message/common/message';
import { IModeService } from 'vs/editor/common/services/modeService';
......@@ -32,6 +32,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { RunOnceScheduler } from 'vs/base/common/async';
import { ITextBufferFactory } from 'vs/editor/common/model';
import { IHashService } from 'vs/workbench/services/hash/common/hashService';
import { createTextBufferFactory } from 'vs/editor/common/model/textModel';
/**
* The text file editor model listens to changes to its underlying code editor model and saves these changes through the file service back to the disk.
......@@ -290,12 +291,12 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
// If we have a backup, continue loading with it
if (!!backup) {
const content: IContent = {
const content: IRawTextContent = {
resource: this.resource,
name: paths.basename(this.resource.fsPath),
mtime: Date.now(),
etag: void 0,
value: '', /* will be filled later from backup */
value: createTextBufferFactory(''), /* will be filled later from backup */
encoding: this.fileService.getEncoding(this.resource, this.preferredEncoding)
};
......@@ -355,7 +356,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
return TPromise.wrapError<TextFileEditorModel>(error);
}
private loadWithContent(content: IRawTextContent | IContent, backup?: URI): TPromise<TextFileEditorModel> {
private loadWithContent(content: IRawTextContent, backup?: URI): TPromise<TextFileEditorModel> {
return this.doLoadWithContent(content, backup).then(model => {
// Telemetry: We log the fileGet telemetry event after the model has been loaded to ensure a good mimetype
......@@ -379,7 +380,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
});
}
private doLoadWithContent(content: IRawTextContent | IContent, backup?: URI): TPromise<TextFileEditorModel> {
private doLoadWithContent(content: IRawTextContent, backup?: URI): TPromise<TextFileEditorModel> {
diag('load() - resolved content', this.resource, new Date());
// Update our resolved disk stat model
......@@ -420,7 +421,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
return this.doCreateTextModel(content.resource, content.value, backup);
}
private doUpdateTextModel(value: string | ITextBufferFactory): TPromise<TextFileEditorModel> {
private doUpdateTextModel(value: ITextBufferFactory): TPromise<TextFileEditorModel> {
diag('load() - updated text editor model', this.resource, new Date());
// Ensure we are not tracking a stale state
......@@ -440,7 +441,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
return TPromise.as<TextFileEditorModel>(this);
}
private doCreateTextModel(resource: URI, value: string | ITextBufferFactory, backup: URI): TPromise<TextFileEditorModel> {
private doCreateTextModel(resource: URI, value: ITextBufferFactory, backup: URI): TPromise<TextFileEditorModel> {
diag('load() - created text editor model', this.resource, new Date());
this.createTextEditorModelPromise = this.doLoadBackup(backup).then(backupContent => {
......
......@@ -15,9 +15,16 @@ import { ModeServiceImpl } from 'vs/editor/common/services/modeServiceImpl';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl';
import { ITextBufferFactory } from 'vs/editor/common/model';
import URI from 'vs/base/common/uri';
import { createTextBufferFactory } from 'vs/editor/common/model/textModel';
class MyEditorModel extends EditorModel { }
class MyTextEditorModel extends BaseTextEditorModel { }
class MyTextEditorModel extends BaseTextEditorModel {
public createTextEditorModel(value: ITextBufferFactory, resource?: URI, modeId?: string) {
return super.createTextEditorModel(value, resource, modeId);
}
}
suite('Workbench - EditorModel', () => {
......@@ -51,9 +58,9 @@ suite('Workbench - EditorModel', () => {
let modelService = stubModelService(instantiationService);
let m = new MyTextEditorModel(modelService, modeService);
m.load().then((model: any) => {
m.load().then((model: MyTextEditorModel) => {
assert(model === m);
return model.createTextEditorModel('foo', null, 'text/plain').then(() => {
return model.createTextEditorModel(createTextBufferFactory('foo'), null, 'text/plain').then(() => {
assert.strictEqual(m.isResolved(), true);
});
}).done(() => {
......
......@@ -857,7 +857,7 @@ export class TestBackupFileService implements IBackupFileService {
return null;
}
public backupResource(resource: URI, content: string | ITextSnapshot): TPromise<void> {
public backupResource(resource: URI, content: ITextSnapshot): TPromise<void> {
return TPromise.as(void 0);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册