@@ -32,6 +32,51 @@ import TelemetryService from '../telemetry/telemetryService';
32
32
33
33
const log = createLogger ( 'editors controller' ) ;
34
34
35
+ export function getFileDisplayNameForDocument (
36
+ documentId : EJSON . SerializableTypes ,
37
+ namespace : string
38
+ ) {
39
+ let displayName = `${ namespace } :${ EJSON . stringify ( documentId ) } ` ;
40
+
41
+ // Encode special file uri characters to ensure VSCode handles
42
+ // it correctly in a uri while avoiding collisions.
43
+ displayName = displayName . replace ( / [ \\ / % ] / gi, function ( c ) {
44
+ return `%${ c . charCodeAt ( 0 ) . toString ( 16 ) } ` ;
45
+ } ) ;
46
+
47
+ displayName = displayName . length > 200
48
+ ? displayName . substring ( 0 , 200 )
49
+ : displayName ;
50
+
51
+ return displayName ;
52
+ }
53
+
54
+ export function getViewCollectionDocumentsUri (
55
+ operationId : string ,
56
+ namespace : string ,
57
+ connectionId : string
58
+ ) : vscode . Uri {
59
+ // We attach a unique id to the query so that it creates a new file in
60
+ // the editor and so that we can virtually manage the amount of docs shown.
61
+ const operationIdUriQuery = `${ OPERATION_ID_URI_IDENTIFIER } =${ operationId } ` ;
62
+ const connectionIdUriQuery = `${ CONNECTION_ID_URI_IDENTIFIER } =${ connectionId } ` ;
63
+ const namespaceUriQuery = `${ NAMESPACE_URI_IDENTIFIER } =${ namespace } ` ;
64
+ const uriQuery = `?${ namespaceUriQuery } &${ connectionIdUriQuery } &${ operationIdUriQuery } ` ;
65
+
66
+ // Encode special file uri characters to ensure VSCode handles
67
+ // it correctly in a uri while avoiding collisions.
68
+ const namespaceDisplayName = encodeURIComponent (
69
+ namespace . replace ( / [ \\ / % ] / gi, function ( c ) {
70
+ return `%${ c . charCodeAt ( 0 ) . toString ( 16 ) } ` ;
71
+ } )
72
+ ) ;
73
+
74
+ // The part of the URI after the scheme and before the query is the file name.
75
+ return vscode . Uri . parse (
76
+ `${ VIEW_COLLECTION_SCHEME } :Results: ${ namespaceDisplayName } .json${ uriQuery } `
77
+ ) ;
78
+ }
79
+
35
80
/**
36
81
* This controller manages when our extension needs to open
37
82
* new editors and the data they need. It also manages active editors.
@@ -108,14 +153,6 @@ export default class EditorsController {
108
153
109
154
async openMongoDBDocument ( data : EditDocumentInfo ) : Promise < boolean > {
110
155
try {
111
- let fileDocumentId = EJSON . stringify ( data . documentId ) ;
112
-
113
- fileDocumentId =
114
- fileDocumentId . length > 50
115
- ? fileDocumentId . substring ( 0 , 50 )
116
- : fileDocumentId ;
117
-
118
- const fileName = `${ VIEW_DOCUMENT_SCHEME } :/${ data . namespace } :${ fileDocumentId } .json` ;
119
156
const mdbDocument = ( await this . _mongoDBDocumentService . fetchDocument (
120
157
data
121
158
) ) as EJSON . SerializableTypes ;
@@ -128,19 +165,27 @@ export default class EditorsController {
128
165
return false ;
129
166
}
130
167
131
- this . _saveDocumentToMemoryFileSystem ( fileName , mdbDocument ) ;
132
-
133
168
const activeConnectionId =
134
169
this . _connectionController . getActiveConnectionId ( ) || '' ;
135
170
const namespaceUriQuery = `${ NAMESPACE_URI_IDENTIFIER } =${ data . namespace } ` ;
136
171
const connectionIdUriQuery = `${ CONNECTION_ID_URI_IDENTIFIER } =${ activeConnectionId } ` ;
137
172
const documentIdReference = this . _documentIdStore . add ( data . documentId ) ;
138
173
const documentIdUriQuery = `${ DOCUMENT_ID_URI_IDENTIFIER } =${ documentIdReference } ` ;
139
174
const documentSourceUriQuery = `${ DOCUMENT_SOURCE_URI_IDENTIFIER } =${ data . source } ` ;
140
- const uri : vscode . Uri = vscode . Uri . parse ( fileName ) . with ( {
175
+
176
+ const fileTitle = encodeURIComponent ( getFileDisplayNameForDocument (
177
+ data . documentId ,
178
+ data . namespace
179
+ ) ) ;
180
+ const fileName = `${ VIEW_DOCUMENT_SCHEME } :/${ fileTitle } .json` ;
181
+
182
+ const fileUri = vscode . Uri . parse ( fileName , true ) . with ( {
141
183
query : `?${ namespaceUriQuery } &${ connectionIdUriQuery } &${ documentIdUriQuery } &${ documentSourceUriQuery } `
142
184
} ) ;
143
- const document = await vscode . workspace . openTextDocument ( uri ) ;
185
+
186
+ this . _saveDocumentToMemoryFileSystem ( fileUri , mdbDocument ) ;
187
+
188
+ const document = await vscode . workspace . openTextDocument ( fileUri ) ;
144
189
145
190
await vscode . window . showTextDocument ( document , { preview : false } ) ;
146
191
@@ -212,31 +257,13 @@ export default class EditorsController {
212
257
}
213
258
}
214
259
215
- static getViewCollectionDocumentsUri (
216
- operationId : string ,
217
- namespace : string ,
218
- connectionId : string
219
- ) : vscode . Uri {
220
- // We attach a unique id to the query so that it creates a new file in
221
- // the editor and so that we can virtually manage the amount of docs shown.
222
- const operationIdUriQuery = `${ OPERATION_ID_URI_IDENTIFIER } =${ operationId } ` ;
223
- const connectionIdUriQuery = `${ CONNECTION_ID_URI_IDENTIFIER } =${ connectionId } ` ;
224
- const namespaceUriQuery = `${ NAMESPACE_URI_IDENTIFIER } =${ namespace } ` ;
225
- const uriQuery = `?${ namespaceUriQuery } &${ connectionIdUriQuery } &${ operationIdUriQuery } ` ;
226
-
227
- // The part of the URI after the scheme and before the query is the file name.
228
- return vscode . Uri . parse (
229
- `${ VIEW_COLLECTION_SCHEME } :Results: ${ namespace } .json${ uriQuery } `
230
- ) ;
231
- }
232
-
233
260
async onViewCollectionDocuments ( namespace : string ) : Promise < boolean > {
234
261
log . info ( 'view collection documents' , namespace ) ;
235
262
236
263
const operationId = this . _collectionDocumentsOperationsStore . createNewOperation ( ) ;
237
264
const activeConnectionId =
238
265
this . _connectionController . getActiveConnectionId ( ) || '' ;
239
- const uri = EditorsController . getViewCollectionDocumentsUri (
266
+ const uri = getViewCollectionDocumentsUri (
240
267
operationId ,
241
268
namespace ,
242
269
activeConnectionId
@@ -294,7 +321,7 @@ export default class EditorsController {
294
321
) ;
295
322
}
296
323
297
- const uri = EditorsController . getViewCollectionDocumentsUri (
324
+ const uri = getViewCollectionDocumentsUri (
298
325
operationId ,
299
326
namespace ,
300
327
connectionId
@@ -311,11 +338,11 @@ export default class EditorsController {
311
338
}
312
339
313
340
_saveDocumentToMemoryFileSystem (
314
- fileName : string ,
341
+ fileUri : vscode . Uri ,
315
342
document : EJSON . SerializableTypes
316
343
) : void {
317
344
this . _memoryFileSystemProvider . writeFile (
318
- vscode . Uri . parse ( fileName ) ,
345
+ fileUri ,
319
346
Buffer . from ( JSON . stringify ( document , null , 2 ) ) ,
320
347
{ create : true , overwrite : true }
321
348
) ;
0 commit comments