<commit_before>/*
 * sbt
 * Copyright 2011 - 2017, Lightbend, Inc.
 * Copyright 2008 - 2010, <NAME>
 * Licensed under BSD-3-Clause license (see LICENSE)
 */

package sbt
package internal
package server

import sjsonnew.JsonFormat
import sjsonnew.support.scalajson.unsafe.Converter
import sbt.protocol.Serialization
import sbt.protocol.{ SettingQuery => Q }
import sbt.internal.protocol._
import sbt.internal.protocol.codec._
import sbt.internal.langserver._
import sbt.internal.util.ObjectEvent
import sbt.util.Logger

private[sbt] case class LangServerError(code: Long, message: String) extends Throwable(message)

/**
 * Implements Language Server Protocol <https://github.com/Microsoft/language-server-protocol>.
 */
private[sbt] trait LanguageServerProtocol extends CommandChannel {

  lazy val internalJsonProtocol = new InitializeOptionFormats with sjsonnew.BasicJsonProtocol {}

  protected def authenticate(token: String): Boolean
  protected def authOptions: Set[ServerAuthentication]
  protected def setInitialized(value: Boolean): Unit
  protected def log: Logger
  protected def onSettingQuery(execId: Option[String], req: Q): Unit

  protected def onRequestMessage(request: JsonRpcRequestMessage): Unit = {
    import sbt.internal.langserver.codec.JsonProtocol._
    import internalJsonProtocol._
    def json =
      request.params.getOrElse(
        throw LangServerError(ErrorCodes.InvalidParams,
                              s"param is expected on '${request.method}' method."))
    log.debug(s"onRequestMessage: $request")
    request.method match {
      case "initialize" =>
        if (authOptions(ServerAuthentication.Token)) {
          val param = Converter.fromJson[InitializeParams](json).get
          val optionJson = param.initializationOptions.getOrElse(
            throw LangServerError(ErrorCodes.InvalidParams,
                                  "initializationOptions is expected on 'initialize' param."))
          val opt = Converter.fromJson[InitializeOption](optionJson).get
          val token = opt.token.getOrElse(sys.error("'token' is missing."))
          if (authenticate(token)) ()
          else throw LangServerError(ErrorCodes.InvalidRequest, "invalid token")
        } else ()
        setInitialized(true)
        append(Exec(s"collectAnalyses", Some(request.id), Some(CommandSource(name))))
        langRespond(InitializeResult(serverCapabilities), Option(request.id))
      case "textDocument/didSave" =>
        append(Exec(";compile; collectAnalyses", Some(request.id), Some(CommandSource(name))))
      case "textDocument/definition" =>
        import scala.concurrent.ExecutionContext.Implicits.global
        Definition.lspDefinition(json, request.id, CommandSource(name), log)
      case "sbt/exec" =>
        val param = Converter.fromJson[SbtExecParams](json).get
        append(Exec(param.commandLine, Some(request.id), Some(CommandSource(name))))
      case "sbt/setting" => {
        import sbt.protocol.codec.JsonProtocol._
        val param = Converter.fromJson[Q](json).get
        onSettingQuery(Option(request.id), param)
      }
      case unhandledRequest => log.debug(s"Unhandled request received: $unhandledRequest")<commit_msg>Added onNotification handler for the LSP CommandChannel<commit_after>/*
 * sbt
 * Copyright 2011 - 2017, Lightbend, Inc.
 * Copyright 2008 - 2010, <NAME>
 * Licensed under BSD-3-Clause license (see LICENSE)
 */

package sbt
package internal
package server

import sjsonnew.JsonFormat
import sjsonnew.support.scalajson.unsafe.Converter
import sbt.protocol.Serialization
import sbt.protocol.{ SettingQuery => Q }
import sbt.internal.protocol._
import sbt.internal.protocol.codec._
import sbt.internal.langserver._
import sbt.internal.util.ObjectEvent
import sbt.util.Logger

private[sbt] case class LangServerError(code: Long, message: String) extends Throwable(message)

/**
 * Implements Language Server Protocol <https://github.com/Microsoft/language-server-protocol>.
 */
private[sbt] trait LanguageServerProtocol extends CommandChannel {

  lazy val internalJsonProtocol = new InitializeOptionFormats with sjsonnew.BasicJsonProtocol {}

  protected def authenticate(token: String): Boolean
  protected def authOptions: Set[ServerAuthentication]
  protected def setInitialized(value: Boolean): Unit
  protected def log: Logger
  protected def onSettingQuery(execId: Option[String], req: Q): Unit

  protected def onNotification(notification: JsonRpcNotificationMessage): Unit = {
    log.debug(s"onNotification: $notification")
    notification.method match {
      case "textDocument/didSave" =>
        append(Exec(";compile; collectAnalyses", None, Some(CommandSource(name))))
      case _ => ()
    }
  }

  protected def onRequestMessage(request: JsonRpcRequestMessage): Unit = {
    import sbt.internal.langserver.codec.JsonProtocol._
    import internalJsonProtocol._
    def json =
      request.params.getOrElse(
        throw LangServerError(ErrorCodes.InvalidParams,
                              s"param is expected on '${request.method}' method."))
    log.debug(s"onRequestMessage: $request")
    request.method match {
      case "initialize" =>
        if (authOptions(ServerAuthentication.Token)) {
          val param = Converter.fromJson[InitializeParams](json).get
          val optionJson = param.initializationOptions.getOrElse(
            throw LangServerError(ErrorCodes.InvalidParams,
                                  "initializationOptions is expected on 'initialize' param."))
          val opt = Converter.fromJson[InitializeOption](optionJson).get
          val token = opt.token.getOrElse(sys.error("'token' is missing."))
          if (authenticate(token)) ()
          else throw LangServerError(ErrorCodes.InvalidRequest, "invalid token")
        } else ()
        setInitialized(true)
        append(Exec(s"collectAnalyses", Some(request.id), Some(CommandSource(name))))
        langRespond(InitializeResult(serverCapabilities), Option(request.id))
      case "textDocument/definition" =>
        import scala.concurrent.ExecutionContext.Implicits.global
        Definition.lspDefinition(json, request.id, CommandSource(name), log)
      case "sbt/exec" =>
        val param = Converter.fromJson[SbtExecParams](json).get
        append(Exec(param.commandLine, Some(request.id), Some(CommandSource(name))))
      case "sbt/setting" => {
        import sbt.protocol.codec.JsonProtocol._
        val param = Converter.fromJson[Q](json).get
        onSettingQuery(Option(request.id), param)
      }
      case unhandledRequest => log.debug(s"Unhandled request received: $unhandledRequest")
{'fraction_non_alphanumeric': 0.10327646081836026, 'fraction_numerical': 0.005133625245357089, 'mean_word_length': 3.77577505407354, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, 'https://': 2, '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': '5218080', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1772, 'n_words': 405}