Skip to content

Commit 4644b60

Browse files
author
Benjamin Pasero
committed
remove internal API for accessing the value of a model directly
1 parent fb1d219 commit 4644b60

7 files changed

Lines changed: 56 additions & 63 deletions

File tree

src/vs/workbench/common/editor/textEditorModel.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
6767
/**
6868
* Creates the text editor model with the provided value, modeId (can be comma separated for multiple values) and optional resource URL.
6969
*/
70-
protected createTextEditorModel(value: string | ITextBufferFactory, resource?: URI, modeId?: string): TPromise<EditorModel> {
70+
protected createTextEditorModel(value: ITextBufferFactory, resource?: URI, modeId?: string): TPromise<EditorModel> {
7171
const firstLineText = this.getFirstLineText(value);
7272
const mode = this.getOrCreateMode(this.modeService, modeId, firstLineText);
7373
return TPromise.as(this.doCreateTextEditorModel(value, mode, resource));
7474
}
7575

76-
private doCreateTextEditorModel(value: string | ITextBufferFactory, mode: TPromise<IMode>, resource: URI): EditorModel {
76+
private doCreateTextEditorModel(value: ITextBufferFactory, mode: TPromise<IMode>, resource: URI): EditorModel {
7777
let model = resource && this.modelService.getModel(resource);
7878
if (!model) {
7979
model = this.modelService.createModel(value, mode, resource);
@@ -91,24 +91,7 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
9191
return this;
9292
}
9393

94-
protected getFirstLineText(value: string | ITextBufferFactory | ITextSnapshot): string {
95-
96-
// string
97-
if (typeof value === 'string') {
98-
const firstLineText = value.substr(0, 100);
99-
100-
let crIndex = firstLineText.indexOf('\r');
101-
if (crIndex < 0) {
102-
crIndex = firstLineText.length;
103-
}
104-
105-
let lfIndex = firstLineText.indexOf('\n');
106-
if (lfIndex < 0) {
107-
lfIndex = firstLineText.length;
108-
}
109-
110-
return firstLineText.substr(0, Math.min(crIndex, lfIndex));
111-
}
94+
protected getFirstLineText(value: ITextBufferFactory | ITextSnapshot): string {
11295

11396
// text buffer factory
11497
const textBufferFactory = value as ITextBufferFactory;
@@ -118,7 +101,19 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
118101

119102
// text snapshot
120103
const textSnapshot = value as ITextSnapshot;
121-
return this.getFirstLineText(textSnapshot.read() || '');
104+
const firstLineText = textSnapshot.read().substr(0, 100);
105+
106+
let crIndex = firstLineText.indexOf('\r');
107+
if (crIndex < 0) {
108+
crIndex = firstLineText.length;
109+
}
110+
111+
let lfIndex = firstLineText.indexOf('\n');
112+
if (lfIndex < 0) {
113+
lfIndex = firstLineText.length;
114+
}
115+
116+
return firstLineText.substr(0, Math.min(crIndex, lfIndex));
122117
}
123118

124119
/**
@@ -133,7 +128,7 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
133128
/**
134129
* Updates the text editor model with the provided value. If the value is the same as the model has, this is a no-op.
135130
*/
136-
protected updateTextEditorModel(newValue: string | ITextBufferFactory): void {
131+
protected updateTextEditorModel(newValue: ITextBufferFactory): void {
137132
if (!this.textEditorModel) {
138133
return;
139134
}

src/vs/workbench/services/backup/common/backup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ export interface IBackupFileService {
5252
* Backs up a resource.
5353
*
5454
* @param resource The resource to back up.
55-
* @param content The content of the resource as value or snapshot.
55+
* @param content The content of the resource as snapshot.
5656
* @param versionId The version id of the resource to backup.
5757
*/
58-
backupResource(resource: Uri, content: string | ITextSnapshot, versionId?: number): TPromise<void>;
58+
backupResource(resource: Uri, content: ITextSnapshot, versionId?: number): TPromise<void>;
5959

6060
/**
6161
* Gets a list of file backups for the current workspace.

src/vs/workbench/services/backup/node/backupFileService.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as pfs from 'vs/base/node/pfs';
1111
import Uri from 'vs/base/common/uri';
1212
import { ResourceQueue } from 'vs/base/common/async';
1313
import { IBackupFileService, BACKUP_FILE_UPDATE_OPTIONS, BACKUP_FILE_RESOLVE_OPTIONS } from 'vs/workbench/services/backup/common/backup';
14-
import { IFileService, ITextSnapshot, IFileStat } from 'vs/platform/files/common/files';
14+
import { IFileService, ITextSnapshot } from 'vs/platform/files/common/files';
1515
import { TPromise } from 'vs/base/common/winjs.base';
1616
import { readToMatchingString } from 'vs/base/node/stream';
1717
import { ITextBufferFactory } from 'vs/editor/common/model';
@@ -171,7 +171,7 @@ export class BackupFileService implements IBackupFileService {
171171
});
172172
}
173173

174-
public backupResource(resource: Uri, content: string | ITextSnapshot, versionId?: number): TPromise<void> {
174+
public backupResource(resource: Uri, content: ITextSnapshot, versionId?: number): TPromise<void> {
175175
if (this.isShuttingDown) {
176176
return TPromise.as(void 0);
177177
}
@@ -190,17 +190,7 @@ export class BackupFileService implements IBackupFileService {
190190
const preamble = `${resource.toString()}${BackupFileService.META_MARKER}`;
191191

192192
// Update content with value
193-
let updateContentPromise: TPromise<IFileStat>;
194-
if (typeof content === 'string') {
195-
updateContentPromise = this.fileService.updateContent(backupResource, `${preamble}${content}`, BACKUP_FILE_UPDATE_OPTIONS);
196-
}
197-
198-
// Update content with snapshot
199-
else {
200-
updateContentPromise = this.fileService.updateContent(backupResource, new BackupSnapshot(content, preamble), BACKUP_FILE_UPDATE_OPTIONS);
201-
}
202-
203-
return updateContentPromise.then(() => model.add(backupResource, versionId));
193+
return this.fileService.updateContent(backupResource, new BackupSnapshot(content, preamble), BACKUP_FILE_UPDATE_OPTIONS).then(() => model.add(backupResource, versionId));
204194
});
205195
});
206196
}

src/vs/workbench/services/backup/test/node/backupFileService.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import pfs = require('vs/base/node/pfs');
1616
import Uri from 'vs/base/common/uri';
1717
import { BackupFileService, BackupFilesModel } from 'vs/workbench/services/backup/node/backupFileService';
1818
import { FileService } from 'vs/workbench/services/files/node/fileService';
19-
import { TextModel } from 'vs/editor/common/model/textModel';
19+
import { TextModel, createTextBufferFactory } from 'vs/editor/common/model/textModel';
2020
import { TestContextService, TestTextResourceConfigurationService, getRandomTestPath, TestLifecycleService } from 'vs/workbench/test/workbenchTestServices';
2121
import { Workspace, toWorkspaceFolders } from 'vs/platform/workspace/common/workspace';
2222
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
@@ -107,7 +107,7 @@ suite('BackupFileService', () => {
107107

108108
suite('backupResource', () => {
109109
test('text file', function (done: () => void) {
110-
service.backupResource(fooFile, 'test').then(() => {
110+
service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
111111
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
112112
assert.equal(fs.existsSync(fooBackupPath), true);
113113
assert.equal(fs.readFileSync(fooBackupPath), `${fooFile.toString()}\ntest`);
@@ -116,7 +116,7 @@ suite('BackupFileService', () => {
116116
});
117117

118118
test('untitled file', function (done: () => void) {
119-
service.backupResource(untitledFile, 'test').then(() => {
119+
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
120120
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
121121
assert.equal(fs.existsSync(untitledBackupPath), true);
122122
assert.equal(fs.readFileSync(untitledBackupPath), `${untitledFile.toString()}\ntest`);
@@ -177,7 +177,7 @@ suite('BackupFileService', () => {
177177

178178
suite('discardResourceBackup', () => {
179179
test('text file', function (done: () => void) {
180-
service.backupResource(fooFile, 'test').then(() => {
180+
service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
181181
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
182182
service.discardResourceBackup(fooFile).then(() => {
183183
assert.equal(fs.existsSync(fooBackupPath), false);
@@ -188,7 +188,7 @@ suite('BackupFileService', () => {
188188
});
189189

190190
test('untitled file', function (done: () => void) {
191-
service.backupResource(untitledFile, 'test').then(() => {
191+
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
192192
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
193193
service.discardResourceBackup(untitledFile).then(() => {
194194
assert.equal(fs.existsSync(untitledBackupPath), false);
@@ -201,9 +201,9 @@ suite('BackupFileService', () => {
201201

202202
suite('discardAllWorkspaceBackups', () => {
203203
test('text file', function (done: () => void) {
204-
service.backupResource(fooFile, 'test').then(() => {
204+
service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
205205
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
206-
service.backupResource(barFile, 'test').then(() => {
206+
service.backupResource(barFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
207207
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 2);
208208
service.discardAllWorkspaceBackups().then(() => {
209209
assert.equal(fs.existsSync(fooBackupPath), false);
@@ -216,7 +216,7 @@ suite('BackupFileService', () => {
216216
});
217217

218218
test('untitled file', function (done: () => void) {
219-
service.backupResource(untitledFile, 'test').then(() => {
219+
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
220220
assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
221221
service.discardAllWorkspaceBackups().then(() => {
222222
assert.equal(fs.existsSync(untitledBackupPath), false);
@@ -228,7 +228,7 @@ suite('BackupFileService', () => {
228228

229229
test('should disable further backups', function (done: () => void) {
230230
service.discardAllWorkspaceBackups().then(() => {
231-
service.backupResource(untitledFile, 'test').then(() => {
231+
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
232232
assert.equal(fs.existsSync(workspaceBackupPath), false);
233233
done();
234234
});
@@ -238,10 +238,10 @@ suite('BackupFileService', () => {
238238

239239
suite('getWorkspaceFileBackups', () => {
240240
test('("file") - text file', done => {
241-
service.backupResource(fooFile, `test`).then(() => {
241+
service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
242242
service.getWorkspaceFileBackups().then(textFiles => {
243243
assert.deepEqual(textFiles.map(f => f.fsPath), [fooFile.fsPath]);
244-
service.backupResource(barFile, `test`).then(() => {
244+
service.backupResource(barFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
245245
service.getWorkspaceFileBackups().then(textFiles => {
246246
assert.deepEqual(textFiles.map(f => f.fsPath), [fooFile.fsPath, barFile.fsPath]);
247247
done();
@@ -252,7 +252,7 @@ suite('BackupFileService', () => {
252252
});
253253

254254
test('("file") - untitled file', done => {
255-
service.backupResource(untitledFile, `test`).then(() => {
255+
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
256256
service.getWorkspaceFileBackups().then(textFiles => {
257257
assert.deepEqual(textFiles.map(f => f.fsPath), [untitledFile.fsPath]);
258258
done();
@@ -261,7 +261,7 @@ suite('BackupFileService', () => {
261261
});
262262

263263
test('("untitled") - untitled file', done => {
264-
service.backupResource(untitledFile, `test`).then(() => {
264+
service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
265265
service.getWorkspaceFileBackups().then(textFiles => {
266266
assert.deepEqual(textFiles.map(f => f.fsPath), ['Untitled-1']);
267267
done();
@@ -273,7 +273,7 @@ suite('BackupFileService', () => {
273273
test('resolveBackupContent', () => {
274274
test('should restore the original contents (untitled file)', () => {
275275
const contents = 'test\nand more stuff';
276-
service.backupResource(untitledFile, contents).then(() => {
276+
service.backupResource(untitledFile, createTextBufferFactory(contents).create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
277277
service.resolveBackupContent(service.toBackupResource(untitledFile)).then(factory => {
278278
assert.equal(contents, snapshotToString(factory.create(platform.isWindows ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF).createSnapshot(true)));
279279
});
@@ -288,7 +288,7 @@ suite('BackupFileService', () => {
288288
'adipiscing ßß elit',
289289
].join('');
290290

291-
service.backupResource(fooFile, contents).then(() => {
291+
service.backupResource(fooFile, createTextBufferFactory(contents).create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
292292
service.resolveBackupContent(service.toBackupResource(untitledFile)).then(factory => {
293293
assert.equal(contents, snapshotToString(factory.create(platform.isWindows ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF).createSnapshot(true)));
294294
});

src/vs/workbench/services/textfile/common/textFileEditorModel.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { ITextFileService, IAutoSaveConfiguration, ModelState, ITextFileEditorMo
2323
import { EncodingMode } from 'vs/workbench/common/editor';
2424
import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel';
2525
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
26-
import { IFileService, IFileStat, FileOperationError, FileOperationResult, IContent, CONTENT_CHANGE_EVENT_BUFFER_DELAY, FileChangesEvent, FileChangeType } from 'vs/platform/files/common/files';
26+
import { IFileService, IFileStat, FileOperationError, FileOperationResult, CONTENT_CHANGE_EVENT_BUFFER_DELAY, FileChangesEvent, FileChangeType } from 'vs/platform/files/common/files';
2727
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
2828
import { IMessageService, Severity } from 'vs/platform/message/common/message';
2929
import { IModeService } from 'vs/editor/common/services/modeService';
@@ -32,6 +32,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
3232
import { RunOnceScheduler } from 'vs/base/common/async';
3333
import { ITextBufferFactory } from 'vs/editor/common/model';
3434
import { IHashService } from 'vs/workbench/services/hash/common/hashService';
35+
import { createTextBufferFactory } from 'vs/editor/common/model/textModel';
3536

3637
/**
3738
* 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
290291

291292
// If we have a backup, continue loading with it
292293
if (!!backup) {
293-
const content: IContent = {
294+
const content: IRawTextContent = {
294295
resource: this.resource,
295296
name: paths.basename(this.resource.fsPath),
296297
mtime: Date.now(),
297298
etag: void 0,
298-
value: '', /* will be filled later from backup */
299+
value: createTextBufferFactory(''), /* will be filled later from backup */
299300
encoding: this.fileService.getEncoding(this.resource, this.preferredEncoding)
300301
};
301302

@@ -355,7 +356,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
355356
return TPromise.wrapError<TextFileEditorModel>(error);
356357
}
357358

358-
private loadWithContent(content: IRawTextContent | IContent, backup?: URI): TPromise<TextFileEditorModel> {
359+
private loadWithContent(content: IRawTextContent, backup?: URI): TPromise<TextFileEditorModel> {
359360
return this.doLoadWithContent(content, backup).then(model => {
360361

361362
// 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
379380
});
380381
}
381382

382-
private doLoadWithContent(content: IRawTextContent | IContent, backup?: URI): TPromise<TextFileEditorModel> {
383+
private doLoadWithContent(content: IRawTextContent, backup?: URI): TPromise<TextFileEditorModel> {
383384
diag('load() - resolved content', this.resource, new Date());
384385

385386
// Update our resolved disk stat model
@@ -420,7 +421,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
420421
return this.doCreateTextModel(content.resource, content.value, backup);
421422
}
422423

423-
private doUpdateTextModel(value: string | ITextBufferFactory): TPromise<TextFileEditorModel> {
424+
private doUpdateTextModel(value: ITextBufferFactory): TPromise<TextFileEditorModel> {
424425
diag('load() - updated text editor model', this.resource, new Date());
425426

426427
// Ensure we are not tracking a stale state
@@ -440,7 +441,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
440441
return TPromise.as<TextFileEditorModel>(this);
441442
}
442443

443-
private doCreateTextModel(resource: URI, value: string | ITextBufferFactory, backup: URI): TPromise<TextFileEditorModel> {
444+
private doCreateTextModel(resource: URI, value: ITextBufferFactory, backup: URI): TPromise<TextFileEditorModel> {
444445
diag('load() - created text editor model', this.resource, new Date());
445446

446447
this.createTextEditorModelPromise = this.doLoadBackup(backup).then(backupContent => {

src/vs/workbench/test/common/editor/editorModel.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ import { ModeServiceImpl } from 'vs/editor/common/services/modeServiceImpl';
1515
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
1616
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
1717
import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl';
18+
import { ITextBufferFactory } from 'vs/editor/common/model';
19+
import URI from 'vs/base/common/uri';
20+
import { createTextBufferFactory } from 'vs/editor/common/model/textModel';
1821

1922
class MyEditorModel extends EditorModel { }
20-
class MyTextEditorModel extends BaseTextEditorModel { }
23+
class MyTextEditorModel extends BaseTextEditorModel {
24+
public createTextEditorModel(value: ITextBufferFactory, resource?: URI, modeId?: string) {
25+
return super.createTextEditorModel(value, resource, modeId);
26+
}
27+
}
2128

2229
suite('Workbench - EditorModel', () => {
2330

@@ -51,9 +58,9 @@ suite('Workbench - EditorModel', () => {
5158
let modelService = stubModelService(instantiationService);
5259

5360
let m = new MyTextEditorModel(modelService, modeService);
54-
m.load().then((model: any) => {
61+
m.load().then((model: MyTextEditorModel) => {
5562
assert(model === m);
56-
return model.createTextEditorModel('foo', null, 'text/plain').then(() => {
63+
return model.createTextEditorModel(createTextBufferFactory('foo'), null, 'text/plain').then(() => {
5764
assert.strictEqual(m.isResolved(), true);
5865
});
5966
}).done(() => {

src/vs/workbench/test/workbenchTestServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ export class TestBackupFileService implements IBackupFileService {
857857
return null;
858858
}
859859

860-
public backupResource(resource: URI, content: string | ITextSnapshot): TPromise<void> {
860+
public backupResource(resource: URI, content: ITextSnapshot): TPromise<void> {
861861
return TPromise.as(void 0);
862862
}
863863

0 commit comments

Comments
 (0)