message: refactor: add wrapper for registerEventListener
diff: @@ -78,16 +78,17 @@ export async function activate(context: vscode.ExtensionContext) {
  logger.debug('Extension: registering event handlers.');
  // workspace events
  - vscode.workspace.onDidChangeConfiguration(() => {
  + registerEventListener(
  + context,
  + vscode.workspace.onDidChangeConfiguration,
  + () => {
  logger.debug('onDidChangeConfiguration: reloading configuration');
  configuration.reload();
  - });
  -
  - vscode.workspace.onDidChangeTextDocument(async event => {
  - if (configuration.disableExtension) {
  - return;
  - }
  + },
  + false
  + );
  + registerEventListener(context, vscode.workspace.onDidChangeTextDocument, async event => {
  const textWasDeleted = changeEvent =>
  changeEvent.contentChanges.length === 1 &&
  changeEvent.contentChanges[0].text === '' &&
  @@ -148,7 +149,10 @@ export async function activate(context: vscode.ExtensionContext) {
  }, 0);
  });
  - vscode.workspace.onDidCloseTextDocument(async () => {
  + registerEventListener(
  + context,
  + vscode.workspace.onDidCloseTextDocument,
  + async () => {
  const documents = vscode.workspace.textDocuments;
  // Delete modehandler once all tabs of this document have been closed
  @@ -163,18 +167,15 @@ export async function activate(context: vscode.ExtensionContext) {
  ModeHandlerMap.delete(editorIdentity);
  }
  }
  - });
  + },
  + false
  + );
  // window events
  - vscode.window.onDidChangeActiveTextEditor(async () => {
  - if (configuration.disableExtension) {
  - return;
  - }
  -
  - if (Globals.isTesting) {
  - return;
  - }
  -
  + registerEventListener(
  + context,
  + vscode.window.onDidChangeActiveTextEditor,
  + async () => {
  const mhPrevious: ModeHandler | null = previousActiveEditorId
  ? ModeHandlerMap.get(previousActiveEditorId.toString())
  : null;
  @@ -205,17 +206,15 @@ export async function activate(context: vscode.ExtensionContext) {
  );
  }
  });
  - });
  -
  - vscode.window.onDidChangeTextEditorSelection(async (e: vscode.TextEditorSelectionChangeEvent) => {
  - if (configuration.disableExtension) {
  - return;
  - }
  -
  - if (Globals.isTesting) {
  - return;
  - }
  + },
  + true,
  + true
  + );
  + registerEventListener(
  + context,
  + vscode.window.onDidChangeTextEditorSelection,
  + async (e: vscode.TextEditorSelectionChangeEvent) => {
  const mh = await getAndUpdateModeHandler(true);
  if (mh.vimState.focusChanged) {
  @@ -236,7 +235,10 @@ export async function activate(context: vscode.ExtensionContext) {
  */
  true
  );
  - });
  + },
  + true,
  + true
  + );
  const compositionState = new CompositionState();
  @@ -417,6 +419,27 @@ function registerCommand(
  context.subscriptions.push(disposable);
  }
  +function registerEventListener<T>(
  + context: vscode.ExtensionContext,
  + event: vscode.Event<T>,
  + listener: (e: T) => any,
  + exitOnExtensionDisable = true,
  + exitOnTests = false
  +) {
  + const disposable = event(async e => {
  + if (exitOnExtensionDisable && configuration.disableExtension) {
  + return;
  + }
  +
  + if (exitOnTests && Globals.isTesting) {
  + return;
  + }
  +
  + listener(e);
  + });
  + context.subscriptions.push(disposable);
  +}
  +
  async function handleKeyEvent(key: string): Promise<void> {
  const mh = await getAndUpdateModeHandler();
  