@@ -15,6 +15,19 @@ import { ExtHostTextEditor } from 'vs/workbench/api/common/extHostTextEditor';
1515import * as typeConverters from 'vs/workbench/api/common/extHostTypeConverters' ;
1616import { ILogService } from 'vs/platform/log/common/log' ;
1717import { ResourceMap } from 'vs/base/common/map' ;
18+ import { Schemas } from 'vs/base/common/network' ;
19+ import { Iterable } from 'vs/base/common/iterator' ;
20+
21+ class Reference < T > {
22+ private _count = 0 ;
23+ constructor ( readonly value : T ) { }
24+ ref ( ) {
25+ this . _count ++ ;
26+ }
27+ unref ( ) {
28+ return -- this . _count === 0 ;
29+ }
30+ }
1831
1932export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsShape {
2033
@@ -23,7 +36,7 @@ export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsSha
2336 private _activeEditorId : string | null = null ;
2437
2538 private readonly _editors = new Map < string , ExtHostTextEditor > ( ) ;
26- private readonly _documents = new ResourceMap < ExtHostDocumentData > ( ) ;
39+ private readonly _documents = new ResourceMap < Reference < ExtHostDocumentData > > ( ) ;
2740
2841 private readonly _onDidAddDocuments = new Emitter < ExtHostDocumentData [ ] > ( ) ;
2942 private readonly _onDidRemoveDocuments = new Emitter < ExtHostDocumentData [ ] > ( ) ;
@@ -50,29 +63,40 @@ export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsSha
5063 for ( const uriComponent of delta . removedDocuments ) {
5164 const uri = URI . revive ( uriComponent ) ;
5265 const data = this . _documents . get ( uri ) ;
53- this . _documents . delete ( uri ) ;
54- if ( data ) {
55- removedDocuments . push ( data ) ;
66+ if ( data ?. unref ( ) ) {
67+ this . _documents . delete ( uri ) ;
68+ removedDocuments . push ( data . value ) ;
5669 }
5770 }
5871 }
5972
6073 if ( delta . addedDocuments ) {
6174 for ( const data of delta . addedDocuments ) {
6275 const resource = URI . revive ( data . uri ) ;
63- assert . ok ( ! this . _documents . has ( resource ) , `document '${ resource } already exists!'` ) ;
64-
65- const documentData = new ExtHostDocumentData (
66- this . _extHostRpc . getProxy ( MainContext . MainThreadDocuments ) ,
67- resource ,
68- data . lines ,
69- data . EOL ,
70- data . modeId ,
71- data . versionId ,
72- data . isDirty
73- ) ;
74- this . _documents . set ( resource , documentData ) ;
75- addedDocuments . push ( documentData ) ;
76+ let ref = this . _documents . get ( resource ) ;
77+
78+ // double check -> only notebook cell documents should be
79+ // referenced/opened more than once...
80+ if ( ref ) {
81+ if ( resource . scheme !== Schemas . vscodeNotebookCell ) {
82+ throw new Error ( `document '${ resource } already exists!'` ) ;
83+ }
84+ }
85+ if ( ! ref ) {
86+ ref = new Reference ( new ExtHostDocumentData (
87+ this . _extHostRpc . getProxy ( MainContext . MainThreadDocuments ) ,
88+ resource ,
89+ data . lines ,
90+ data . EOL ,
91+ data . modeId ,
92+ data . versionId ,
93+ data . isDirty
94+ ) ) ;
95+ this . _documents . set ( resource , ref ) ;
96+ addedDocuments . push ( ref . value ) ;
97+ }
98+
99+ ref . ref ( ) ;
76100 }
77101 }
78102
@@ -92,7 +116,7 @@ export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsSha
92116 assert . ok ( this . _documents . has ( resource ) , `document '${ resource } ' does not exist` ) ;
93117 assert . ok ( ! this . _editors . has ( data . id ) , `editor '${ data . id } ' already exists!` ) ;
94118
95- const documentData = this . _documents . get ( resource ) ! ;
119+ const documentData = this . _documents . get ( resource ) ! . value ;
96120 const editor = new ExtHostTextEditor (
97121 data . id ,
98122 this . _extHostRpc . getProxy ( MainContext . MainThreadTextEditors ) ,
@@ -132,11 +156,11 @@ export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsSha
132156 }
133157
134158 getDocument ( uri : URI ) : ExtHostDocumentData | undefined {
135- return this . _documents . get ( uri ) ;
159+ return this . _documents . get ( uri ) ?. value ;
136160 }
137161
138- allDocuments ( ) : ExtHostDocumentData [ ] {
139- return [ ... this . _documents . values ( ) ] ;
162+ allDocuments ( ) : Iterable < ExtHostDocumentData > {
163+ return Iterable . map ( this . _documents . values ( ) , ref => ref . value ) ;
140164 }
141165
142166 getEditor ( id : string ) : ExtHostTextEditor | undefined {
0 commit comments