<commit_before>import { ModeName } from '../../mode/mode';
import { existsSync } from 'fs';
import { configuration } from '../../configuration/configuration';
import * as util from '../../util/util';
import { Message } from '../../util/message';

// InputMethodSwitcher change input method automatically when mode changed
export class InputMethodSwitcher {
  private savedIMKey = '';

  public async switchInputMethod(prevMode: ModeName, newMode: ModeName) {
    if (configuration.autoSwitchInputMethod.enable !== true) {
      return;
    }
    if (!this.validateConfiguration()) {
      this.disableIMSwitch();
      return;
    }
    // when you exit from insert-like mode, save origin input method and set it to default
    if (
      prevMode === ModeName.Insert ||
      prevMode === ModeName.SurroundInputMode ||
      prevMode === ModeName.Replace
    ) {
      if (
        newMode !== ModeName.Insert &&
        newMode !== ModeName.SurroundInputMode &&
        newMode !== ModeName.Replace
      ) {
        this.switchToDefaultIM();
        return;
      }
    }
    // when you enter insert-like mode, resume origin input method
    if (
      newMode === ModeName.Insert ||
      newMode === ModeName.SurroundInputMode ||
      newMode === ModeName.Replace
    ) {
      if (
        prevMode !== ModeName.Insert &&
        prevMode !== ModeName.SurroundInputMode &&
        prevMode !== ModeName.Replace
      ) {
        this.resumeIM();
      }
    }
  }

  // save origin input method and set input method to default
  private async switchToDefaultIM() {
    const obtainIMCmd = configuration.autoSwitchInputMethod.obtainIMCmd;
    const rawObtainIMCmd = this.getRawCmd(obtainIMCmd);
    if (existsSync(rawObtainIMCmd)) {
      try {
        const insertIMKey = await util.executeShell(obtainIMCmd);
        if (insertIMKey !== undefined) {
          this.savedIMKey = insertIMKey.trim();
        }
      } catch (error) {
        console.log(error);
      }
    } else {
      this.showCmdNotFoundErrorMessage(rawObtainIMCmd, 'vim.autoSwitchInputMethod.obtainIMCmd');
    }

    const defaultIMKey = configuration.autoSwitchInputMethod.defaultIM;
    if (defaultIMKey !== this.savedIMKey) {
      this.switchToIM(defaultIMKey);
    }
  }

  // resume origin inputmethod
  private async resumeIM() {
    if (this.savedIMKey !== configuration.autoSwitchInputMethod.defaultIM) {
      this.switchToIM(this.savedIMKey);
    }
  }

  private async switchToIM(imKey: string) {
    let switchIMCmd = configuration.autoSwitchInputMethod.switchIMCmd;
    const rawSwitchIMCmd = this.getRawCmd(switchIMCmd);
    if (existsSync(rawSwitchIMCmd)) {
      if (imKey !== '' && imKey !== undefined) {
        switchIMCmd = switchIMCmd.replace('{im}', imKey);
        try {
          await util.executeShell(switchIMCmd);
        } catch (error) {
          console.log(error);
        }
      }
    } else {
      this.showCmdNotFoundErrorMessage(rawSwitchIMCmd, 'vim.autoSwitchInputMethod.switchIMCmd');
    }
  }

  private getRawCmd(cmd: string): string {
    return cmd.split(' ')[0];
  }

  private showCmdNotFoundErrorMessage(cmd: string, config: string) {
    Message.ShowError('Unable to find ' + cmd + '. check your ' + config + ' in VSCode setting.');<commit_msg>Update: use logger to log<commit_after>import { ModeName } from '../../mode/mode';
import { existsSync } from 'fs';
import { configuration } from '../../configuration/configuration';
import * as util from '../../util/util';
import { logger } from '../../util/logger';
import { Message } from '../../util/message';

// InputMethodSwitcher change input method automatically when mode changed
export class InputMethodSwitcher {
  private savedIMKey = '';

  public async switchInputMethod(prevMode: ModeName, newMode: ModeName) {
    if (configuration.autoSwitchInputMethod.enable !== true) {
      return;
    }
    if (!this.validateConfiguration()) {
      this.disableIMSwitch();
      return;
    }
    // when you exit from insert-like mode, save origin input method and set it to default
    if (
      prevMode === ModeName.Insert ||
      prevMode === ModeName.SurroundInputMode ||
      prevMode === ModeName.Replace
    ) {
      if (
        newMode !== ModeName.Insert &&
        newMode !== ModeName.SurroundInputMode &&
        newMode !== ModeName.Replace
      ) {
        this.switchToDefaultIM();
        return;
      }
    }
    // when you enter insert-like mode, resume origin input method
    if (
      newMode === ModeName.Insert ||
      newMode === ModeName.SurroundInputMode ||
      newMode === ModeName.Replace
    ) {
      if (
        prevMode !== ModeName.Insert &&
        prevMode !== ModeName.SurroundInputMode &&
        prevMode !== ModeName.Replace
      ) {
        this.resumeIM();
      }
    }
  }

  // save origin input method and set input method to default
  private async switchToDefaultIM() {
    const obtainIMCmd = configuration.autoSwitchInputMethod.obtainIMCmd;
    const rawObtainIMCmd = this.getRawCmd(obtainIMCmd);
    if (existsSync(rawObtainIMCmd)) {
      try {
        const insertIMKey = await util.executeShell(obtainIMCmd);
        if (insertIMKey !== undefined) {
          this.savedIMKey = insertIMKey.trim();
        }
      } catch (e) {
        logger.error('IMSwitcher: ${e}.');
      }
    } else {
      this.showCmdNotFoundErrorMessage(rawObtainIMCmd, 'vim.autoSwitchInputMethod.obtainIMCmd');
    }

    const defaultIMKey = configuration.autoSwitchInputMethod.defaultIM;
    if (defaultIMKey !== this.savedIMKey) {
      this.switchToIM(defaultIMKey);
    }
  }

  // resume origin inputmethod
  private async resumeIM() {
    if (this.savedIMKey !== configuration.autoSwitchInputMethod.defaultIM) {
      this.switchToIM(this.savedIMKey);
    }
  }

  private async switchToIM(imKey: string) {
    let switchIMCmd = configuration.autoSwitchInputMethod.switchIMCmd;
    const rawSwitchIMCmd = this.getRawCmd(switchIMCmd);
    if (existsSync(rawSwitchIMCmd)) {
      if (imKey !== '' && imKey !== undefined) {
        switchIMCmd = switchIMCmd.replace('{im}', imKey);
        try {
          await util.executeShell('lss');
        } catch (e) {
          logger.error('IMSwitcher: ${e}');
        }
      }
    } else {
      this.showCmdNotFoundErrorMessage(rawSwitchIMCmd, 'vim.autoSwitchInputMethod.switchIMCmd');
    }
  }

  private getRawCmd(cmd: string): string {
    return cmd.split(' ')[0];
  }

  private showCmdNotFoundErrorMessage(cmd: string, config: string) {
    Message.ShowError('Unable to find ' + cmd + '. check your ' + config + ' in VSCode setting.');
{'fraction_non_alphanumeric': 0.12230107202174241, 'fraction_numerical': 0.00030197795560924054, 'mean_word_length': 2.744488411531939, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1863241', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1907, 'n_words': 423}