Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
serializeJson returns JValue
Browse files Browse the repository at this point in the history
  • Loading branch information
jchapuis committed Sep 19, 2017
1 parent f31670f commit e3abfe2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name := "scala-parser-combinators-completion"
organization := "com.nexthink"
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
version := "1.0.4"
version := "1.0.5"
scalaVersion := "2.12.2"
bintrayRepository := "maven"
bintrayVcsUrl := Some("jchapuis@github.com:jchapuis/scala-parser-combinators-completion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
package com.nexthink.utils.parsing.combinator.completion

import org.json4s
import org.json4s.{JArray, JValue}

import scala.util.parsing.input.{NoPosition, Position}
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods._

import scala.collection.immutable

/** Collection of data types allowing definition of structured parser completions.
Expand All @@ -33,6 +35,7 @@ import scala.collection.immutable
*/
trait CompletionTypes {
type Elem
type Elems = Seq[Elem]

val DefaultCompletionTag = ""
val DefaultCompletionScore = 0
Expand All @@ -57,7 +60,7 @@ trait CompletionTypes {
}

override def toString: String = pretty(render(serializeJson))
def toJson: String = compact(render(serializeJson))
def toJson: JValue = serializeJson
}

case object CompletionTag {
Expand Down Expand Up @@ -86,7 +89,7 @@ trait CompletionTypes {
("tag" -> tag.serializeJson) ~ ("completions" -> entries.map(_.serializeJson).toList)

override def toString: String = pretty(render(serializeJson))
def toJson: String = compact(render(serializeJson))
def toJson: JValue = serializeJson
}

case object CompletionSet {
Expand Down Expand Up @@ -119,9 +122,9 @@ trait CompletionTypes {

def apply(completions: Traversable[Elems]): CompletionSet =
CompletionSet(CompletionTag.Default, completions.map(c => c -> Completion(c)).toSeq)
}

type Elems = Seq[Elem]
implicit def orderingByScoreAndThenAlphabetical: Ordering[CompletionSet] = Ordering.by(s => (-s.score, s.label))
}

/** Completion entry
* @param value entry value (e.g. string literal)
Expand Down Expand Up @@ -153,15 +156,15 @@ trait CompletionTypes {
def isEmpty: Boolean = sets.isEmpty
def nonEmpty: Boolean = !isEmpty
def setWithTag(tag: String): Option[CompletionSet] = sets.get(tag)
def allSets: Iterable[CompletionSet] = sets.values
def allSets: Iterable[CompletionSet] = sets.values.toSeq.sorted
def allCompletions: Iterable[Completion] = allSets.flatMap(_.sortedEntries)
def defaultSet: Option[CompletionSet] = sets.get("")

private def serializeJson = ("position" -> (("line" -> position.line) ~ ("column" -> position.column))) ~ ("sets" -> allSets.map(_.serializeJson))

override def toString: String = pretty(render(serializeJson))
def toJson: String = compact(render(serializeJson))

def toJson: JValue = serializeJson
def setsToJson: JArray = allSets.map(_.serializeJson)

private def mergeMetaData(left: Option[String], right: Option[String]) = (left, right) match {
case (Some(l), Some(r)) =>
Expand Down

0 comments on commit e3abfe2

Please sign in to comment.