<commit_before>"use strict";

import * as vscode from "vscode";
import { VimState } from "../mode/modeHandler";
import { Position } from './../common/motion/position';
import { TextEditor } from "../textEditor";
import { Configuration } from '../configuration/configuration';
import { spawn } from "child_process";
import { attach } from "promised-neovim-client";
import { Register } from "../register/register";

export class Neovim {

  static async initNvim(vimState: VimState) {
    const proc = spawn(Configuration.neovimPath, ['-u', 'NONE', '-N', '--embed'], {cwd: __dirname });
    vimState.nvim = await attach(proc.stdin, proc.stdout);
    const nvim = vimState.nvim;
    await nvim.setOption("clipboard", await nvim.getOption("clipboard") + "unnamed");
  }

  // Data flows from VS to Vim
  static async syncVSToVim(vimState: VimState) {
    const nvim = vimState.nvim;
    const buf = await nvim.getCurrentBuf();
    await buf.setLines(0, -1, true, TextEditor.getText().split('\n'));
    const [rangeStart, rangeEnd] = [Position.EarlierOf(vimState.cursorPosition, vimState.cursorStartPosition),
                                     Position.LaterOf(vimState.cursorPosition, vimState.cursorStartPosition)];
    await nvim.callFunction("setpos", [".", [0, vimState.cursorPosition.line + 1, vimState.cursorPosition.character, false]]);
    await nvim.callFunction("setpos", ["'<", [0, rangeStart.line + 1, rangeEnd.character, false]]);
    await nvim.callFunction("setpos", ["'>", [0, rangeEnd.line + 1, rangeEnd.character, false]]);
    for (const mark of vimState.historyTracker.getMarks()){
      await nvim.callFunction("setpos", [`'${mark.name}`, [0, mark.position.line + 1, mark.position.character, false]]);
    }
  }

  // Data flows from Vim to VS
  static async syncVimToVs(vimState: VimState) {
    const nvim = vimState.nvim;
    const buf = await nvim.getCurrentBuf();

    await TextEditor.replace(
    new vscode.Range(0, 0, TextEditor.getLineCount() - 1,
    TextEditor.getLineMaxColumn(TextEditor.getLineCount() - 1)),
    (await buf.getLines(0, -1, false)).join('\n')
    );

    let [row, character]  = (await nvim.callFunction("getpos", ["."]) as Array<number>).slice(1, 3);
    vimState.editor.selection = new vscode.Selection(new Position(row - 1, character), new Position(row - 1, character));

    if (Configuration.expandtab) {
      await vscode.commands.executeCommand("editor.action.indentationToSpaces");
    }
    // We're only syncing back the default register for now, due to the way we could
    // be storing macros in registers.
    // I also can't figure out how to properly set register variables right
    // now, so this is a temporary solution.
    if (await nvim.eval('@"') !== "") {
      Register.put(await nvim.eval('@"') as string, vimState);
    }
  }

  static async command(vimState: VimState, command: string) {
    const nvim = vimState.nvim;
    await this.syncVSToVim(vimState);
    command = ":" + command + "\n";
    command.replace('<', '<lt>');

    await nvim.input(command);
    if ((await nvim.getMode()).blocking) {
      await nvim.input('<esc>');
    }
    await this.syncVimToVs(vimState);

    return;
  }

  static async input(vimState: VimState, keys: string) {
    const nvim = vimState.nvim;
    await this.syncVSToVim(vimState);
    await nvim.input(keys);
    await this.syncVimToVs(vimState);

    return;
  }

}<commit_msg>added proper handling of registers/register types<commit_after>"use strict";

import * as vscode from "vscode";
import { VimState } from "../mode/modeHandler";
import { Position } from './../common/motion/position';
import { TextEditor } from "../textEditor";
import { Configuration } from '../configuration/configuration';
import { spawn } from "child_process";
import { attach } from "promised-neovim-client";
import { Register, RegisterMode } from "../register/register";
import { ModeName } from "../mode/mode";

export class Neovim {

  static async initNvim(vimState: VimState) {
    const proc = spawn(Configuration.neovimPath, ['-u', 'NONE', '-N', '--embed'], {cwd: __dirname });
    vimState.nvim = await attach(proc.stdin, proc.stdout);
    const nvim = vimState.nvim;
  }

  // Data flows from VS to Vim
  static async syncVSToVim(vimState: VimState) {
    const nvim = vimState.nvim;
    const buf = await nvim.getCurrentBuf();
    await buf.setLines(0, -1, true, TextEditor.getText().split('\n'));
    const [rangeStart, rangeEnd] = [Position.EarlierOf(vimState.cursorPosition, vimState.cursorStartPosition),
                                     Position.LaterOf(vimState.cursorPosition, vimState.cursorStartPosition)];
    await nvim.callFunction("setpos", [".", [0, vimState.cursorPosition.line + 1, vimState.cursorPosition.character, false]]);
    await nvim.callFunction("setpos", ["'<", [0, rangeStart.line + 1, rangeEnd.character, false]]);
    await nvim.callFunction("setpos", ["'>", [0, rangeEnd.line + 1, rangeEnd.character, false]]);
    for (const mark of vimState.historyTracker.getMarks()){
      await nvim.callFunction("setpos", [`'${mark.name}`, [0, mark.position.line + 1, mark.position.character, false]]);
    }

    const effectiveRegisterMode = (register: RegisterMode) => {
      if (register === RegisterMode.FigureItOutFromCurrentMode) {
        if (vimState.currentMode === ModeName.VisualLine) {
          return RegisterMode.LineWise;
        } else if (vimState.currentMode === ModeName.VisualBlock) {
          return RegisterMode.BlockWise;
        } else {
          return RegisterMode.CharacterWise;
        }
      } else {
        return register;
      }
    }

    // We only copy over " register for now, due to our weird handling of macros.
    let reg = await Register.get(vimState);
    let vsRegTovimReg = [undefined, "c", "l", "b"];
    console.log(reg.registerMode);
    console.log(effectiveRegisterMode(reg.registerMode));
    await nvim.callFunction("setreg", [reg.text as string, vsRegTovimReg[effectiveRegisterMode(reg.registerMode)] as string] );
  }

  // Data flows from Vim to VS
  static async syncVimToVs(vimState: VimState) {
    const nvim = vimState.nvim;
    const buf = await nvim.getCurrentBuf();

    await TextEditor.replace(
    new vscode.Range(0, 0, TextEditor.getLineCount() - 1,
    TextEditor.getLineMaxColumn(TextEditor.getLineCount() - 1)),
    (await buf.getLines(0, -1, false)).join('\n')
    );

    let [row, character]  = (await nvim.callFunction("getpos", ["."]) as Array<number>).slice(1, 3);
    vimState.editor.selection = new vscode.Selection(new Position(row - 1, character), new Position(row - 1, character));

    if (Configuration.expandtab) {
      await vscode.commands.executeCommand("editor.action.indentationToSpaces");
    }
    // We're only syncing back the default register for now, due to the way we could
    // be storing macros in registers.
    const vimRegToVsReg = {"v": RegisterMode.CharacterWise, "V": RegisterMode.LineWise, "\x16": RegisterMode.BlockWise}
    vimState.currentRegisterMode = vimRegToVsReg[await nvim.callFunction("getregtype", ['"']) as string];
    Register.put(await nvim.callFunction("getreg", ['"']) as string, vimState);
  }

  static async command(vimState: VimState, command: string) {
    const nvim = vimState.nvim;
    await this.syncVSToVim(vimState);
    command = ":" + command + "\n";
    command.replace('<', '<lt>');

    await nvim.input(command);
    if ((await nvim.getMode()).blocking) {
      await nvim.input('<esc>');
    }
    await this.syncVimToVs(vimState);

    return;
  }

  static async input(vimState: VimState, keys: string) {
    const nvim = vimState.nvim;
    await this.syncVSToVim(vimState);
    await nvim.input(keys);
    await this.syncVimToVs(vimState);

    return;
  }

}
{'fraction_non_alphanumeric': 0.1469487808024771, 'fraction_numerical': 0.005418655657334537, 'mean_word_length': 3.6033254156769594, 'pattern_counts': {'":': 5, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7184171', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2471, 'n_words': 588}