Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mill._, scalalib._, publish._, scalajslib.ScalaJSModule
val sjsonnetVersion = "0.2.7"
val sjsonnetVersion = "0.2.9"

object sjsonnet extends Cross[SjsonnetModule]("2.12.12", "2.13.3")
class SjsonnetModule(val crossScalaVersion: String) extends Module {
Expand Down
4 changes: 4 additions & 0 deletions sjsonnet/src-js/sjsonnet/SjsonnetMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ case class JsVirtualPath(path: String) extends Path{
def last: String = path.split('/').last

def /(s: String): Path = JsVirtualPath(path + "/" + s)

def renderOffsetStr(offset: Int, loadedFileContents: mutable.Map[Path, Array[Int]]): String = {
path + ":" + offset
}
}
8 changes: 7 additions & 1 deletion sjsonnet/src-jvm/sjsonnet/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ object Cli{
indent: Int = 3,
preserveOrder: Boolean = false,
strict: Boolean = false,
yamlOut: Boolean = false)
yamlOut: Boolean = false,
yamlDebug: Boolean = false)


def genericSignature(wd: os.Path) = Seq(
Expand Down Expand Up @@ -146,6 +147,11 @@ object Cli{
"Write output as a YAML document",
(c, v) => c.copy(yamlOut = true)
),
Arg[Config, Unit](
"yaml-debug", None,
"Generate source line comments in the output YAML doc to make it easier to figure out where values come from.",
(c, v) => c.copy(yamlDebug = true, yamlOut = true)
),

)
def showArg(arg: Arg[_, _]) =
Expand Down
20 changes: 19 additions & 1 deletion sjsonnet/src-jvm/sjsonnet/OsPath.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
package sjsonnet

import scala.collection.mutable

case class OsPath(p: os.Path) extends Path{
def relativeToString(other: Path): String = p.relativeTo(other.asInstanceOf[OsPath].p).toString
def parent(): OsPath = OsPath(p / os.up)
def segmentCount() = p.segmentCount
def debugRead(): Option[String] = try Some(os.read(p)) catch{case e: Throwable => None}
def last: String = p.last
def /(s: String): Path = OsPath(p / s)
}


def renderOffsetStr(offset: Int, loadedFileContents: mutable.Map[Path, Array[Int]]): String = {
val offsetStr =
if (p.toString.contains("(materialize)")) ""
else {
val lineStarts = loadedFileContents.getOrElseUpdate(
this,
fastparse.internal.Util.lineNumberLookup(os.read(p))
)

":" + Util.prettyIndex(lineStarts, offset)
}

p.relativeTo(os.pwd) + offsetStr
}
}
25 changes: 16 additions & 9 deletions sjsonnet/src-jvm/sjsonnet/SjsonnetMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ object SjsonnetMain {
}
}

def rendererForConfig(wr: Writer, config: Config) =
if (config.yamlOut) new PrettyYamlRenderer(wr, indent = config.indent)
def rendererForConfig(wr: Writer, config: Config, getCurrentPosition: () => Position) =
if (config.yamlOut) new PrettyYamlRenderer(
wr,
indent = config.indent,
getCurrentPosition = getCurrentPosition
)
else new Renderer(wr, indent = config.indent)
def handleWriteFile[T](f: => T): Either[String, T] =
Try(f).toEither.left.map{
Expand Down Expand Up @@ -113,9 +117,10 @@ object SjsonnetMain {
}
}

def renderNormal(config: Config, interp: Interpreter, path: os.Path, wd: os.Path) = {
def renderNormal(config: Config, interp: Interpreter, path: os.Path, wd: os.Path,
getCurrentPosition: () => Position) = {
writeToFile(config, wd){ writer =>
val renderer = rendererForConfig(writer, config)
val renderer = rendererForConfig(writer, config, getCurrentPosition)
val res = interp.interpret0(os.read(path), OsPath(path), renderer)
if (config.yamlOut) writer.write('\n')
res
Expand All @@ -131,6 +136,7 @@ object SjsonnetMain {
allowedInputs: Option[Set[os.Path]] = None,
importer: Option[(Path, String) => Option[os.Path]] = None): Either[String, String] = {
val path = os.Path(file, wd)
var currentPos: Position = null
val interp = new Interpreter(
parseCache,
config.varBinding,
Expand All @@ -141,7 +147,8 @@ object SjsonnetMain {
case None => resolveImport(config.jpaths.map(os.Path(_, wd)).map(OsPath(_)), allowedInputs)
},
preserveOrder = config.preserveOrder,
strict = config.strict
strict = config.strict,
storePos = if (config.yamlDebug) currentPos = _ else _ => ()
)

(config.multi, config.yamlStream) match {
Expand Down Expand Up @@ -183,15 +190,15 @@ object SjsonnetMain {
arr.value.toSeq match {
case Nil => //donothing
case Seq(single) =>
val renderer = rendererForConfig(writer, config)
val renderer = rendererForConfig(writer, config, () => currentPos)
single.transform(renderer)
writer.write(if (isScalar(single)) "\n..." else "")
case multiple =>
for((v, i) <- multiple.zipWithIndex){
if (i > 0) writer.write('\n')
if (isScalar(v)) writer.write("--- ")
else if (i != 0) writer.write("---\n")
val renderer = rendererForConfig(writer, config)
val renderer = rendererForConfig(writer, config, () => currentPos)
v.transform(renderer)

}
Expand All @@ -200,9 +207,9 @@ object SjsonnetMain {
Right("")
}

case _ => renderNormal(config, interp, path, wd)
case _ => renderNormal(config, interp, path, wd, () => currentPos)
}
case _ => renderNormal(config, interp, path, wd)
case _ => renderNormal(config, interp, path, wd, () => currentPos)

}
}
Expand Down
Loading