@@ -28,16 +28,22 @@ interface ICodeKeybinding {
28
28
commands ?: { command : string ; args : any [ ] } [ ] ;
29
29
}
30
30
31
- export async function getAndUpdateModeHandler ( forceSyncAndUpdate = false ) : Promise < ModeHandler > {
31
+ export async function getAndUpdateModeHandler (
32
+ forceSyncAndUpdate = false
33
+ ) : Promise < ModeHandler | undefined > {
32
34
const activeTextEditor = vscode . window . activeTextEditor ;
35
+ if ( activeTextEditor === undefined ) {
36
+ return undefined ;
37
+ }
38
+
33
39
const activeEditorId = EditorIdentity . fromEditor ( activeTextEditor ) ;
34
40
35
41
let [ curHandler , isNew ] = await ModeHandlerMap . getOrCreate ( activeEditorId ) ;
36
42
if ( isNew ) {
37
43
extensionContext . subscriptions . push ( curHandler ) ;
38
44
}
39
45
40
- curHandler . vimState . editor = activeTextEditor ! ;
46
+ curHandler . vimState . editor = activeTextEditor ;
41
47
42
48
if (
43
49
forceSyncAndUpdate ||
@@ -250,9 +256,8 @@ export async function activate(
250
256
Register . putByKey ( filepathComponents [ filepathComponents . length - 1 ] , '%' , undefined , true ) ;
251
257
252
258
taskQueue . enqueueTask ( async ( ) => {
253
- if ( vscode . window . activeTextEditor !== undefined ) {
254
- const mh : ModeHandler = await getAndUpdateModeHandler ( true ) ;
255
-
259
+ const mh = await getAndUpdateModeHandler ( true ) ;
260
+ if ( mh ) {
256
261
globalState . jumpTracker . handleFileJump (
257
262
lastClosedModeHandler ? Jump . fromStateNow ( lastClosedModeHandler . vimState ) : null ,
258
263
Jump . fromStateNow ( mh . vimState )
@@ -272,12 +277,15 @@ export async function activate(
272
277
vscode . window . activeTextEditor === undefined ||
273
278
e . textEditor . document !== vscode . window . activeTextEditor . document
274
279
) {
275
- // we don't care if there is no active editor
276
- // or user selection changed in a paneled window (e.g debug console/terminal)
280
+ // We don't care if user selection changed in a paneled window (e.g debug console/terminal)
277
281
return ;
278
282
}
279
283
280
284
const mh = await getAndUpdateModeHandler ( ) ;
285
+ if ( mh === undefined ) {
286
+ // We don't care if there is no active editor
287
+ return ;
288
+ }
281
289
282
290
if ( e . kind !== vscode . TextEditorSelectionChangeKind . Mouse ) {
283
291
const selectionsHash = e . selections . reduce (
@@ -341,32 +349,34 @@ export async function activate(
341
349
overrideCommand ( context , 'type' , async ( args ) => {
342
350
taskQueue . enqueueTask ( async ( ) => {
343
351
const mh = await getAndUpdateModeHandler ( ) ;
344
-
345
- if ( compositionState . isInComposition ) {
346
- compositionState . composingText += args . text ;
347
- } else {
348
- await mh . handleKeyEvent ( args . text ) ;
352
+ if ( mh ) {
353
+ if ( compositionState . isInComposition ) {
354
+ compositionState . composingText += args . text ;
355
+ } else {
356
+ await mh . handleKeyEvent ( args . text ) ;
357
+ }
349
358
}
350
359
} ) ;
351
360
} ) ;
352
361
353
362
overrideCommand ( context , 'replacePreviousChar' , async ( args ) => {
354
363
taskQueue . enqueueTask ( async ( ) => {
355
364
const mh = await getAndUpdateModeHandler ( ) ;
356
-
357
- if ( compositionState . isInComposition ) {
358
- compositionState . composingText =
359
- compositionState . composingText . substr (
360
- 0 ,
361
- compositionState . composingText . length - args . replaceCharCnt
362
- ) + args . text ;
363
- } else {
364
- await vscode . commands . executeCommand ( 'default:replacePreviousChar' , {
365
- text : args . text ,
366
- replaceCharCnt : args . replaceCharCnt ,
367
- } ) ;
368
- mh . vimState . cursorStopPosition = mh . vimState . editor . selection . start ;
369
- mh . vimState . cursorStartPosition = mh . vimState . editor . selection . start ;
365
+ if ( mh ) {
366
+ if ( compositionState . isInComposition ) {
367
+ compositionState . composingText =
368
+ compositionState . composingText . substr (
369
+ 0 ,
370
+ compositionState . composingText . length - args . replaceCharCnt
371
+ ) + args . text ;
372
+ } else {
373
+ await vscode . commands . executeCommand ( 'default:replacePreviousChar' , {
374
+ text : args . text ,
375
+ replaceCharCnt : args . replaceCharCnt ,
376
+ } ) ;
377
+ mh . vimState . cursorStopPosition = mh . vimState . editor . selection . start ;
378
+ mh . vimState . cursorStartPosition = mh . vimState . editor . selection . start ;
379
+ }
370
380
}
371
381
} ) ;
372
382
} ) ;
@@ -380,22 +390,30 @@ export async function activate(
380
390
overrideCommand ( context , 'compositionEnd' , async ( ) => {
381
391
taskQueue . enqueueTask ( async ( ) => {
382
392
const mh = await getAndUpdateModeHandler ( ) ;
383
- let text = compositionState . composingText ;
384
- compositionState . reset ( ) ;
385
- mh . handleMultipleKeyEvents ( text . split ( '' ) ) ;
393
+ if ( mh ) {
394
+ const text = compositionState . composingText ;
395
+ compositionState . reset ( ) ;
396
+ mh . handleMultipleKeyEvents ( text . split ( '' ) ) ;
397
+ }
386
398
} ) ;
387
399
} ) ;
388
400
389
401
// Register extension commands
390
402
registerCommand ( context , 'vim.showQuickpickCmdLine' , async ( ) => {
391
403
const mh = await getAndUpdateModeHandler ( ) ;
392
- await commandLine . PromptAndRun ( '' , mh . vimState ) ;
393
- mh . updateView ( ) ;
404
+ if ( mh ) {
405
+ await commandLine . PromptAndRun ( '' , mh . vimState ) ;
406
+ mh . updateView ( ) ;
407
+ }
394
408
} ) ;
395
409
396
410
registerCommand ( context , 'vim.remap' , async ( args : ICodeKeybinding ) => {
397
411
taskQueue . enqueueTask ( async ( ) => {
398
412
const mh = await getAndUpdateModeHandler ( ) ;
413
+ if ( mh === undefined ) {
414
+ return ;
415
+ }
416
+
399
417
if ( args . after ) {
400
418
for ( const key of args . after ) {
401
419
await mh . handleKeyEvent ( Notation . NormalizeKey ( key , configuration . leader ) ) ;
@@ -443,11 +461,13 @@ export async function activate(
443
461
} ) ;
444
462
}
445
463
446
- // Initialize mode handler for current active Text Editor at startup.
447
- if ( vscode . window . activeTextEditor ) {
448
- let mh = await getAndUpdateModeHandler ( ) ;
449
- // This is called last because getAndUpdateModeHandler() will change cursor
450
- mh . updateView ( { drawSelection : false , revealRange : false } ) ;
464
+ {
465
+ // Initialize mode handler for current active Text Editor at startup.
466
+ const modeHandler = await getAndUpdateModeHandler ( ) ;
467
+ if ( modeHandler ) {
468
+ // This is called last because getAndUpdateModeHandler() will change cursor
469
+ modeHandler . updateView ( { drawSelection : false , revealRange : false } ) ;
470
+ }
451
471
}
452
472
453
473
// Disable automatic keyboard navigation in lists, so it doesn't interfere
@@ -472,13 +492,15 @@ async function toggleExtension(isDisabled: boolean, compositionState: Compositio
472
492
// If activate was called and no editor window is open, we can't properly initialize.
473
493
return ;
474
494
}
475
- let mh = await getAndUpdateModeHandler ( ) ;
476
- if ( isDisabled ) {
477
- await mh . handleKeyEvent ( SpecialKeys . ExtensionDisable ) ;
478
- compositionState . reset ( ) ;
479
- ModeHandlerMap . clear ( ) ;
480
- } else {
481
- await mh . handleKeyEvent ( SpecialKeys . ExtensionEnable ) ;
495
+ const mh = await getAndUpdateModeHandler ( ) ;
496
+ if ( mh ) {
497
+ if ( isDisabled ) {
498
+ await mh . handleKeyEvent ( SpecialKeys . ExtensionDisable ) ;
499
+ compositionState . reset ( ) ;
500
+ ModeHandlerMap . clear ( ) ;
501
+ } else {
502
+ await mh . handleKeyEvent ( SpecialKeys . ExtensionEnable ) ;
503
+ }
482
504
}
483
505
}
484
506
@@ -547,18 +569,21 @@ function registerEventListener<T>(
547
569
548
570
async function handleKeyEvent ( key : string ) : Promise < void > {
549
571
const mh = await getAndUpdateModeHandler ( ) ;
550
-
551
- taskQueue . enqueueTask ( async ( ) => {
552
- await mh . handleKeyEvent ( key ) ;
553
- } ) ;
572
+ if ( mh ) {
573
+ taskQueue . enqueueTask ( async ( ) => {
574
+ await mh . handleKeyEvent ( key ) ;
575
+ } ) ;
576
+ }
554
577
}
555
578
556
579
async function checkIfRecursiveRemapping ( key : string ) : Promise < void > {
557
580
const mh = await getAndUpdateModeHandler ( ) ;
558
- if ( mh . vimState . isCurrentlyPerformingRecursiveRemapping ) {
559
- mh . vimState . forceStopRecursiveRemapping = true ;
560
- } else {
561
- handleKeyEvent ( key ) ;
581
+ if ( mh ) {
582
+ if ( mh . vimState . isCurrentlyPerformingRecursiveRemapping ) {
583
+ mh . vimState . forceStopRecursiveRemapping = true ;
584
+ } else {
585
+ handleKeyEvent ( key ) ;
586
+ }
562
587
}
563
588
}
564
589
0 commit comments