<commit_before>package tutorial.webapp

import java.nio.ByteBuffer

import scala.concurrent.Future
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => jsg, newInstance => jsnew}
import scala.scalajs.js.JSApp

import org.denigma.codemirror.CodeMirror
import org.denigma.codemirror.Editor
import org.denigma.codemirror.extensions.EditorConfig
import org.scalajs.dom
import org.scalajs.dom.raw.ErrorEvent
import org.scalajs.dom.raw.Event
import org.scalajs.dom.raw.FocusEvent
import org.scalajs.dom.raw.HTMLTextAreaElement
import org.scalajs.dom.raw.KeyboardEvent
import org.scalajs.dom.raw.MessageEvent
import org.scalajs.dom.raw.MouseEvent
import org.scalajs.dom.raw.WebSocket
import org.scalajs.jquery.jQuery

import shared.test._

object TutorialApp extends JSApp {
  private val $ = jQuery

  private val ui = new Ui

  implicit class AsDynamic[A](private val a: A) extends AnyVal {
    def jsg: js.Dynamic = a.asInstanceOf[js.Dynamic]
  }

  private object divs {
    val parent = "parent"
    val editorLeft = "editor_left"
    val editorRight = "editor_right"
    val render = "render"
    val right = "right"
    val left = "left"
  }

  override def main(): Unit = {
    authenticate()
    setupUI2()
  }

  private val bm = new BufferManager
  private var ws: WebSocket = _
  private var clientName: String = _
  private var keyMap = Set[Int]()

  // used to measure running time of code
  private var startTime: js.Dynamic = _

  def setupUI2() = {
    import scalatags.JsDom.all._
    val par = div(id := divs.parent, `class` := "fullscreen").render
    val buf = bm.mkEditorBuf("text/x-scala")

    val b = ui.bufferDiv2(buf)
    par.appendChild(b)

    def handleKeyUpDown(e: KeyboardEvent): Boolean = {
      val isDown = e.`type` == "keydown"
      keyMap = if (isDown) keyMap + e.keyCode else keyMap - e.keyCode

      if (isDown) {
        val controlSeq = if (e.keyCode == 8) "<bs>" else ""
        if (controlSeq.nonEmpty) {
          // TODO don't create BufferRef manually here
          val input = Control(BufferRef(b.id), 0, 0, controlSeq)
          send(input)
        }
      }

      println(keyMap)

      true
    }

    def send(req: Request): Unit = {
      import boopickle.Default._
      val msg = Pickle.intoBytes(req)
      ws.send(toArrayBuffer(msg))
      println(s"> sent: $input")
    }
<commit_msg>Handle more key bindings through client->neovim connection<commit_after>package tutorial.webapp

import java.nio.ByteBuffer

import scala.concurrent.Future
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => jsg, newInstance => jsnew}
import scala.scalajs.js.JSApp

import org.denigma.codemirror.CodeMirror
import org.denigma.codemirror.Editor
import org.denigma.codemirror.extensions.EditorConfig
import org.scalajs.dom
import org.scalajs.dom.raw.ErrorEvent
import org.scalajs.dom.raw.Event
import org.scalajs.dom.raw.FocusEvent
import org.scalajs.dom.raw.HTMLTextAreaElement
import org.scalajs.dom.raw.KeyboardEvent
import org.scalajs.dom.raw.MessageEvent
import org.scalajs.dom.raw.MouseEvent
import org.scalajs.dom.raw.WebSocket
import org.scalajs.jquery.jQuery

import shared.test._

object TutorialApp extends JSApp {
  private val $ = jQuery

  private val ui = new Ui

  implicit class AsDynamic[A](private val a: A) extends AnyVal {
    def jsg: js.Dynamic = a.asInstanceOf[js.Dynamic]
  }

  private object divs {
    val parent = "parent"
    val editorLeft = "editor_left"
    val editorRight = "editor_right"
    val render = "render"
    val right = "right"
    val left = "left"
  }

  override def main(): Unit = {
    authenticate()
    setupUI2()
  }

  private val bm = new BufferManager
  private var ws: WebSocket = _
  private var clientName: String = _
  private var keyMap = Set[Int]()

  // used to measure running time of code
  private var startTime: js.Dynamic = _

  private val vimMap = Map(
    8  → "<bs>",
    9  → "<tab>",
    13 → "<enter>",
    16 → "<shift>",
    17 → "<ctrl>",
    18 → "<alt>",
    27 → "<esc>",
    32 → "<space>",
    33 → "<pageup>",
    34 → "<pagedown>",
    35 → "<end>",
    36 → "<home>",
    37 → "<left>",
    38 → "<up>",
    39 → "<right>",
    40 → "<down>",
    45 → "<ins>",
    46 → "<del>"
  )

  def setupUI2() = {
    import scalatags.JsDom.all._
    val par = div(id := divs.parent, `class` := "fullscreen").render
    val buf = bm.mkEditorBuf("text/x-scala")

    val b = ui.bufferDiv2(buf)
    par.appendChild(b)

    def handleKeyUpDown(e: KeyboardEvent): Boolean = {
      val isDown = e.`type` == "keydown"
      keyMap = if (isDown) keyMap + e.keyCode else keyMap - e.keyCode

      if (isDown) {
        val controlSeq = vimMap.getOrElse(e.keyCode, "")
        if (controlSeq.nonEmpty) {
          // TODO don't create BufferRef manually here
          val input = Control(BufferRef(b.id), 0, 0, controlSeq)
          send(input)
        }
      }

      println(keyMap)

      true
    }

    def send(req: Request): Unit = {
      import boopickle.Default._
      val msg = Pickle.intoBytes(req)
      ws.send(toArrayBuffer(msg))
      println(s"> sent: $input")
    }

{'fraction_non_alphanumeric': 0.1210464662241312, 'fraction_numerical': 0.008785630613041781, 'mean_word_length': 2.9498843484965303, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 29, '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': '7482029', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1720, 'n_words': 448}