Skip to content

Commit aabb4e3

Browse files
committed
add TextDocument#notebook api proposal, microsoft#100890
1 parent 1d08d20 commit aabb4e3

7 files changed

Lines changed: 75 additions & 47 deletions

File tree

src/vs/vscode.proposed.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,4 +2024,12 @@ declare module 'vscode' {
20242024
}
20252025

20262026
//#endregion
2027+
2028+
2029+
//#region https://github.com/microsoft/vscode/issues/102091
2030+
2031+
export interface TextDocument {
2032+
notebook: NotebookDocument | undefined;
2033+
}
2034+
//#endregion
20272035
}

src/vs/workbench/api/common/extHostDocumentData.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ export function getWordDefinitionFor(modeId: string): RegExp | undefined {
2929

3030
export class ExtHostDocumentData extends MirrorTextModel {
3131

32-
private _proxy: MainThreadDocumentsShape;
33-
private _languageId: string;
34-
private _isDirty: boolean;
3532
private _document?: vscode.TextDocument;
3633
private _isDisposed: boolean = false;
3734

38-
constructor(proxy: MainThreadDocumentsShape, uri: URI, lines: string[], eol: string,
39-
languageId: string, versionId: number, isDirty: boolean
35+
constructor(
36+
private readonly _proxy: MainThreadDocumentsShape,
37+
uri: URI, lines: string[], eol: string, versionId: number,
38+
private _languageId: string,
39+
private _isDirty: boolean,
40+
private readonly _notebook?: vscode.NotebookDocument | undefined
4041
) {
4142
super(uri, lines, eol, versionId);
42-
this._proxy = proxy;
43-
this._languageId = languageId;
44-
this._isDirty = isDirty;
4543
}
4644

4745
dispose(): void {
@@ -59,25 +57,26 @@ export class ExtHostDocumentData extends MirrorTextModel {
5957

6058
get document(): vscode.TextDocument {
6159
if (!this._document) {
62-
const data = this;
60+
const that = this;
6361
this._document = {
64-
get uri() { return data._uri; },
65-
get fileName() { return data._uri.fsPath; },
66-
get isUntitled() { return data._uri.scheme === Schemas.untitled; },
67-
get languageId() { return data._languageId; },
68-
get version() { return data._versionId; },
69-
get isClosed() { return data._isDisposed; },
70-
get isDirty() { return data._isDirty; },
71-
save() { return data._save(); },
72-
getText(range?) { return range ? data._getTextInRange(range) : data.getText(); },
73-
get eol() { return data._eol === '\n' ? EndOfLine.LF : EndOfLine.CRLF; },
74-
get lineCount() { return data._lines.length; },
75-
lineAt(lineOrPos: number | vscode.Position) { return data._lineAt(lineOrPos); },
76-
offsetAt(pos) { return data._offsetAt(pos); },
77-
positionAt(offset) { return data._positionAt(offset); },
78-
validateRange(ran) { return data._validateRange(ran); },
79-
validatePosition(pos) { return data._validatePosition(pos); },
80-
getWordRangeAtPosition(pos, regexp?) { return data._getWordRangeAtPosition(pos, regexp); }
62+
get uri() { return that._uri; },
63+
get fileName() { return that._uri.fsPath; },
64+
get isUntitled() { return that._uri.scheme === Schemas.untitled; },
65+
get languageId() { return that._languageId; },
66+
get version() { return that._versionId; },
67+
get isClosed() { return that._isDisposed; },
68+
get isDirty() { return that._isDirty; },
69+
get notebook() { return that._notebook; },
70+
save() { return that._save(); },
71+
getText(range?) { return range ? that._getTextInRange(range) : that.getText(); },
72+
get eol() { return that._eol === '\n' ? EndOfLine.LF : EndOfLine.CRLF; },
73+
get lineCount() { return that._lines.length; },
74+
lineAt(lineOrPos: number | vscode.Position) { return that._lineAt(lineOrPos); },
75+
offsetAt(pos) { return that._offsetAt(pos); },
76+
positionAt(offset) { return that._positionAt(offset); },
77+
validateRange(ran) { return that._validateRange(ran); },
78+
validatePosition(pos) { return that._validatePosition(pos); },
79+
getWordRangeAtPosition(pos, regexp?) { return that._getWordRangeAtPosition(pos, regexp); },
8180
};
8281
}
8382
return Object.freeze(this._document);

src/vs/workbench/api/common/extHostDocumentsAndEditors.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as assert from 'vs/base/common/assert';
7+
import * as vscode from 'vscode';
78
import { Emitter, Event } from 'vs/base/common/event';
89
import { dispose } from 'vs/base/common/lifecycle';
910
import { URI } from 'vs/base/common/uri';
1011
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
11-
import { ExtHostDocumentsAndEditorsShape, IDocumentsAndEditorsDelta, MainContext } from 'vs/workbench/api/common/extHost.protocol';
12+
import { ExtHostDocumentsAndEditorsShape, IDocumentsAndEditorsDelta, IModelAddedData, MainContext } from 'vs/workbench/api/common/extHost.protocol';
1213
import { ExtHostDocumentData } from 'vs/workbench/api/common/extHostDocumentData';
1314
import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
1415
import { ExtHostTextEditor } from 'vs/workbench/api/common/extHostTextEditor';
@@ -29,6 +30,14 @@ class Reference<T> {
2930
}
3031
}
3132

33+
export interface IExtHostModelAddedData extends IModelAddedData {
34+
notebook?: vscode.NotebookDocument;
35+
}
36+
37+
export interface IExtHostDocumentsAndEditorsDelta extends IDocumentsAndEditorsDelta {
38+
addedDocuments?: IExtHostModelAddedData[];
39+
}
40+
3241
export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsShape {
3342

3443
readonly _serviceBrand: undefined;
@@ -54,6 +63,10 @@ export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsSha
5463
) { }
5564

5665
$acceptDocumentsAndEditorsDelta(delta: IDocumentsAndEditorsDelta): void {
66+
this.acceptDocumentsAndEditorsDelta(delta);
67+
}
68+
69+
acceptDocumentsAndEditorsDelta(delta: IExtHostDocumentsAndEditorsDelta): void {
5770

5871
const removedDocuments: ExtHostDocumentData[] = [];
5972
const addedDocuments: ExtHostDocumentData[] = [];
@@ -88,9 +101,10 @@ export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsSha
88101
resource,
89102
data.lines,
90103
data.EOL,
91-
data.modeId,
92104
data.versionId,
93-
data.isDirty
105+
data.modeId,
106+
data.isDirty,
107+
data.notebook
94108
));
95109
this._documents.set(resource, ref);
96110
addedDocuments.push(ref.value);

src/vs/workbench/api/common/extHostNotebook.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { IExtensionDescription } from 'vs/platform/extensions/common/extensions'
1717
import { CellKind, ExtHostNotebookShape, IMainContext, IModelAddedData, INotebookDocumentsAndEditorsDelta, INotebookEditorPropertiesChangeData, MainContext, MainThreadNotebookShape, NotebookCellOutputsSplice } from 'vs/workbench/api/common/extHost.protocol';
1818
import { ILogService } from 'vs/platform/log/common/log';
1919
import { ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
20-
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
20+
import { ExtHostDocumentsAndEditors, IExtHostModelAddedData } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
2121
import { IExtensionStoragePaths } from 'vs/workbench/api/common/extHostStoragePaths';
2222
import * as typeConverters from 'vs/workbench/api/common/extHostTypeConverters';
2323
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
@@ -61,14 +61,15 @@ const addIdToOutput = (output: IRawOutput, id = UUID.generateUuid()): IProcessed
6161

6262
export class ExtHostCell extends Disposable implements vscode.NotebookCell {
6363

64-
public static asModelAddData(cell: IMainCellDto): IModelAddedData {
64+
public static asModelAddData(notebook: ExtHostNotebookDocument, cell: IMainCellDto): IExtHostModelAddedData {
6565
return {
6666
EOL: cell.eol,
6767
lines: cell.source,
6868
modeId: cell.language,
6969
uri: cell.uri,
7070
isDirty: false,
71-
versionId: 1
71+
versionId: 1,
72+
notebook
7273
};
7374
}
7475

@@ -363,7 +364,7 @@ export class ExtHostNotebookDocument extends Disposable implements vscode.Notebo
363364
}
364365

365366
const contentChangeEvents: vscode.NotebookCellsChangeData[] = [];
366-
const addedCellDocuments: IModelAddedData[] = [];
367+
const addedCellDocuments: IExtHostModelAddedData[] = [];
367368

368369
splices.reverse().forEach(splice => {
369370
const cellDtos = splice[2];
@@ -372,7 +373,7 @@ export class ExtHostNotebookDocument extends Disposable implements vscode.Notebo
372373
const extCell = new ExtHostCell(this._proxy, this, this._documentsAndEditors, cell);
373374

374375
if (!initialization) {
375-
addedCellDocuments.push(ExtHostCell.asModelAddData(cell));
376+
addedCellDocuments.push(ExtHostCell.asModelAddData(this, cell));
376377
}
377378

378379
if (!this._cellDisposableMapping.has(extCell.handle)) {
@@ -404,7 +405,7 @@ export class ExtHostNotebookDocument extends Disposable implements vscode.Notebo
404405
});
405406

406407
if (addedCellDocuments) {
407-
this._documentsAndEditors.$acceptDocumentsAndEditorsDelta({ addedDocuments: addedCellDocuments });
408+
this._documentsAndEditors.acceptDocumentsAndEditorsDelta({ addedDocuments: addedCellDocuments });
408409
}
409410

410411
if (!initialization) {
@@ -1588,7 +1589,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
15881589
});
15891590

15901591
// add cell document as vscode.TextDocument
1591-
addedCellDocuments.push(...modelData.cells.map(ExtHostCell.asModelAddData));
1592+
addedCellDocuments.push(...modelData.cells.map(cell => ExtHostCell.asModelAddData(document, cell)));
15921593

15931594
this._documents.get(revivedUri)?.dispose();
15941595
this._documents.set(revivedUri, document);
@@ -1600,9 +1601,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
16001601
}
16011602
}
16021603

1603-
this._documentsAndEditors.$acceptDocumentsAndEditorsDelta({
1604-
addedDocuments: addedCellDocuments
1605-
});
1604+
this._documentsAndEditors.$acceptDocumentsAndEditorsDelta({ addedDocuments: addedCellDocuments });
16061605

16071606
const document = this._documents.get(revivedUri)!;
16081607
this._onDidOpenNotebookDocument.fire(document);

src/vs/workbench/test/browser/api/extHostDocumentData.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ suite('ExtHostDocumentData', () => {
3535
'and this is line number two', //27
3636
'it is followed by #3', //20
3737
'and finished with the fourth.', //29
38-
], '\n', 'text', 1, false);
38+
], '\n', 1, 'text', false);
3939
});
4040

4141
test('readonly-ness', () => {
@@ -55,7 +55,7 @@ suite('ExtHostDocumentData', () => {
5555
saved = uri;
5656
return Promise.resolve(true);
5757
}
58-
}, URI.parse('foo:bar'), [], '\n', 'text', 1, true);
58+
}, URI.parse('foo:bar'), [], '\n', 1, 'text', true);
5959

6060
return data.document.save().then(() => {
6161
assert.equal(saved.toString(), 'foo:bar');
@@ -242,7 +242,7 @@ suite('ExtHostDocumentData', () => {
242242
test('getWordRangeAtPosition', () => {
243243
data = new ExtHostDocumentData(undefined!, URI.file(''), [
244244
'aaaa bbbb+cccc abc'
245-
], '\n', 'text', 1, false);
245+
], '\n', 1, 'text', false);
246246

247247
let range = data.document.getWordRangeAtPosition(new Position(0, 2))!;
248248
assert.equal(range.start.line, 0);
@@ -276,7 +276,7 @@ suite('ExtHostDocumentData', () => {
276276
'function() {',
277277
' "far boo"',
278278
'}'
279-
], '\n', 'text', 1, false);
279+
], '\n', 1, 'text', false);
280280

281281
let range = data.document.getWordRangeAtPosition(new Position(0, 0), /\/\*.+\*\//);
282282
assert.equal(range, undefined);
@@ -304,7 +304,7 @@ suite('ExtHostDocumentData', () => {
304304

305305
data = new ExtHostDocumentData(undefined!, URI.file(''), [
306306
perfData._$_$_expensive
307-
], '\n', 'text', 1, false);
307+
], '\n', 1, 'text', false);
308308

309309
let range = data.document.getWordRangeAtPosition(new Position(0, 1_177_170), regex)!;
310310
assert.equal(range, undefined);
@@ -323,7 +323,7 @@ suite('ExtHostDocumentData', () => {
323323

324324
data = new ExtHostDocumentData(undefined!, URI.file(''), [
325325
line
326-
], '\n', 'text', 1, false);
326+
], '\n', 1, 'text', false);
327327

328328
let range = data.document.getWordRangeAtPosition(new Position(0, 27), regex)!;
329329
assert.equal(range.start.line, 0);
@@ -387,7 +387,7 @@ suite('ExtHostDocumentData updates line mapping', () => {
387387
}
388388

389389
function testLineMappingDirectionAfterEvents(lines: string[], eol: string, direction: AssertDocumentLineMappingDirection, e: IModelChangedEvent): void {
390-
let myDocument = new ExtHostDocumentData(undefined!, URI.file(''), lines.slice(0), eol, 'text', 1, false);
390+
let myDocument = new ExtHostDocumentData(undefined!, URI.file(''), lines.slice(0), eol, 1, 'text', false);
391391
assertDocumentLineMapping(myDocument, direction);
392392

393393
myDocument.onEvents(e);

src/vs/workbench/test/browser/api/extHostNotebook.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ suite('NotebookCell#Document', function () {
9797
assert.ok(d1);
9898
assert.equal(d1.languageId, c1.language);
9999
assert.equal(d1.version, 1);
100+
assert.ok(d1.notebook === notebook);
100101

101102
const d2 = extHostDocuments.getDocument(c2.uri);
102103
assert.ok(d2);
103104
assert.equal(d2.languageId, c2.language);
104105
assert.equal(d2.version, 1);
106+
assert.ok(d2.notebook === notebook);
105107
});
106108

107109
test('cell document goes when notebook closes', async function () {
@@ -215,4 +217,10 @@ suite('NotebookCell#Document', function () {
215217
assert.equal(doc.isClosed, true);
216218
}
217219
});
220+
221+
test('cell document knows notebook', function () {
222+
for (let cells of notebook.cells) {
223+
assert.equal(cells.document.notebook === notebook, true);
224+
}
225+
});
218226
});

src/vs/workbench/test/browser/api/extHostTextEditor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ suite('ExtHostTextEditor', () => {
1717
let editor: ExtHostTextEditor;
1818
let doc = new ExtHostDocumentData(undefined!, URI.file(''), [
1919
'aaaa bbbb+cccc abc'
20-
], '\n', 'text', 1, false);
20+
], '\n', 1, 'text', false);
2121

2222
setup(() => {
2323
editor = new ExtHostTextEditor('fake', null!, new NullLogService(), doc, [], { cursorStyle: 0, insertSpaces: true, lineNumbers: 1, tabSize: 4, indentSize: 4 }, [], 1);

0 commit comments

Comments
 (0)