@@ -240,7 +240,7 @@ async function addFileClient(uri: Uri) {
240
240
const file = uri . toString ( ) ;
241
241
if ( ! lspClients . has ( file ) ) {
242
242
// Start the client. This will also launch the server
243
- const client = new Client ( uri ) ;
243
+ const client = new Client ( uri , undefined , file ) ;
244
244
lspClients . set ( file , client ) ;
245
245
await client . start ( ) ;
246
246
}
@@ -290,8 +290,13 @@ async function runNargoExpand() {
290
290
if ( ! editor ) return 'Not available' ;
291
291
292
292
const document = editor . document ;
293
- const workspaceFolder = workspace . getWorkspaceFolder ( document . uri ) . uri . toString ( ) ;
294
- const client = lspClients . get ( workspaceFolder ) ;
293
+ const workspaceFolder = workspace . getWorkspaceFolder ( document . uri ) ;
294
+ let client : Client ;
295
+ if ( workspaceFolder ) {
296
+ client = lspClients . get ( workspaceFolder . uri . toString ( ) ) ;
297
+ } else {
298
+ client = lspClients . get ( document . uri . toString ( ) ) ;
299
+ }
295
300
if ( ! client ) return 'Not available' ;
296
301
297
302
const position = editor . selection . active ;
@@ -365,12 +370,17 @@ async function didOpenTextDocument(document: TextDocument): Promise<Disposable>
365
370
} else {
366
371
// We only want to handle `file:` and `untitled:` schemes because
367
372
// vscode sends `output:` schemes for markdown responses from our LSP
368
- if ( uri . scheme !== 'file' && uri . scheme !== 'untitled' ) {
373
+ if ( uri . scheme !== 'file' && uri . scheme !== 'untitled' && uri . scheme !== 'noir-std' ) {
369
374
return Disposable . from ( ) ;
370
375
}
371
376
372
377
// Each file outside of a workspace gets it's own client
373
378
await addFileClient ( uri ) ;
379
+
380
+ if ( uri . scheme === 'noir-std' ) {
381
+ return Disposable . from ( ) ;
382
+ }
383
+
374
384
registerFileCommands ( uri ) ;
375
385
376
386
configHandler = mutex ( uri . toString ( ) , async ( e : ConfigurationChangeEvent ) => {
@@ -401,6 +411,18 @@ async function didOpenTextDocument(document: TextDocument): Promise<Disposable>
401
411
}
402
412
}
403
413
414
+ async function didCloseTextDocument ( document : TextDocument ) : Promise < Disposable > {
415
+ // We are only interested in language mode text
416
+ if ( document . languageId !== languageId ) {
417
+ return Disposable . from ( ) ;
418
+ }
419
+
420
+ const uri = document . uri ;
421
+ if ( uri . scheme === 'noir-std' ) {
422
+ await removeFileClient ( uri ) ;
423
+ }
424
+ }
425
+
404
426
async function didChangeWorkspaceFolders ( event : WorkspaceFoldersChangeEvent ) {
405
427
// Reset the workspace folders so it'll sort them again
406
428
workspaceFolders = [ ] ;
@@ -415,12 +437,21 @@ async function didChangeWorkspaceFolders(event: WorkspaceFoldersChangeEvent) {
415
437
}
416
438
417
439
export async function activate ( context : ExtensionContext ) : Promise < void > {
440
+ registerNoirStdContentProvider ( ) ;
441
+
418
442
const didOpenTextDocument$ = workspace . onDidOpenTextDocument ( didOpenTextDocument ) ;
443
+ const didCloseTextDocument$ = workspace . onDidCloseTextDocument ( didCloseTextDocument ) ;
419
444
const didChangeWorkspaceFolders$ = workspace . onDidChangeWorkspaceFolders ( didChangeWorkspaceFolders ) ;
420
445
const restart$ = commands . registerCommand ( 'noir.restart' , restartAllClients ) ;
421
446
const expand$ = commands . registerCommand ( 'nargo.expand' , runNargoExpand ) ;
422
447
423
- context . subscriptions . push ( didOpenTextDocument$ , didChangeWorkspaceFolders$ , restart$ , expand$ ) ;
448
+ context . subscriptions . push (
449
+ didOpenTextDocument$ ,
450
+ didCloseTextDocument$ ,
451
+ didChangeWorkspaceFolders$ ,
452
+ restart$ ,
453
+ expand$ ,
454
+ ) ;
424
455
425
456
for ( const doc of workspace . textDocuments ) {
426
457
const disposable = await didOpenTextDocument ( doc ) ;
@@ -439,3 +470,17 @@ export async function deactivate(): Promise<void> {
439
470
440
471
await commands . executeCommand ( 'setContext' , NOIR_PROJECT_CONTEXT_NAME , undefined ) ;
441
472
}
473
+
474
+ function registerNoirStdContentProvider ( ) {
475
+ const noir_std_provider = new ( class implements TextDocumentContentProvider {
476
+ async provideTextDocumentContent ( uri : Uri ) : Promise < string > {
477
+ if ( lspClients . size == 0 ) {
478
+ return 'Not available' ;
479
+ }
480
+ // Any client can answer this request
481
+ const client : Client = lspClients . values ( ) . next ( ) . value ;
482
+ return await client . sendRequest < string > ( 'nargo/std-source-code' , { uri : uri . toString ( ) } ) ;
483
+ }
484
+ } ) ( ) ;
485
+ workspace . registerTextDocumentContentProvider ( 'noir-std' , noir_std_provider ) ;
486
+ }
0 commit comments