@@ -49,7 +49,7 @@ import { Script, createContext, runInContext } from 'vm';
49
49
import { installPasteSupport } from './repl-paste-support' ;
50
50
import util from 'util' ;
51
51
52
- import { MongoDBAutocompleter } from '@mongodb-js/mongodb-ts-autocomplete' ;
52
+ import type { MongoDBAutocompleter } from '@mongodb-js/mongodb-ts-autocomplete' ;
53
53
54
54
declare const __non_webpack_require__ : any ;
55
55
@@ -439,21 +439,21 @@ class MongoshNodeRepl implements EvaluationListener {
439
439
this . outputFinishString += installPasteSupport ( repl ) ;
440
440
441
441
const origReplCompleter = promisify ( repl . completer . bind ( repl ) ) ; // repl.completer is callback-style
442
- let newMongoshCompleter : MongoDBAutocompleter ;
442
+
443
+ let newMongoshCompleter : MongoDBAutocompleter | undefined ;
443
444
let oldMongoshCompleter : (
444
445
line : string
445
446
) => Promise < [ string [ ] , string , 'exclusive' ] | [ string [ ] , string ] > ;
447
+
446
448
if ( process . env . USE_NEW_AUTOCOMPLETE ) {
447
- const autocompletionContext = instanceState . getAutocompletionContext ( ) ;
448
- newMongoshCompleter = new MongoDBAutocompleter ( {
449
- context : autocompletionContext ,
450
- } ) ;
449
+ // we will lazily instantiate the new autocompleter on first use
451
450
} else {
452
451
oldMongoshCompleter = completer . bind (
453
452
null ,
454
453
instanceState . getAutocompleteParameters ( )
455
454
) ;
456
455
}
456
+
457
457
const innerCompleter = async (
458
458
text : string
459
459
) : Promise < [ string [ ] , string ] > => {
@@ -468,6 +468,20 @@ class MongoshNodeRepl implements EvaluationListener {
468
468
} ) ( ) ,
469
469
( async ( ) => {
470
470
if ( process . env . USE_NEW_AUTOCOMPLETE ) {
471
+ if ( ! newMongoshCompleter ) {
472
+ // only import the autocompleter code the first time we need it to
473
+ // hide the time it takes from the initial startup time
474
+ const { MongoDBAutocompleter } = await import (
475
+ '@mongodb-js/mongodb-ts-autocomplete'
476
+ ) ;
477
+
478
+ const autocompletionContext =
479
+ instanceState . getAutocompletionContext ( ) ;
480
+ newMongoshCompleter = new MongoDBAutocompleter ( {
481
+ context : autocompletionContext ,
482
+ } ) ;
483
+ }
484
+
471
485
const results = await newMongoshCompleter . autocomplete ( text ) ;
472
486
const transformed = transformAutocompleteResults ( text , results ) ;
473
487
return transformed ;
0 commit comments