@@ -697,7 +697,7 @@ class Std {
697697
698698 private object ManifestJson extends Val .Builtin1 (" manifestJson" , " v" ) {
699699 def evalRhs (v : Val , ev : EvalScope , pos : Position ): Val =
700- Val .Str (pos, Materializer .apply0(v, new MaterializeJsonRenderer ())(ev).toString)
700+ Val .Str (pos, Materializer .apply0(v, MaterializeJsonRenderer ())(ev).toString)
701701 }
702702
703703 private object ManifestJsonMinified extends Val .Builtin1 (" manifestJsonMinified" , " v" ) {
@@ -727,6 +727,65 @@ class Std {
727727 }
728728 }
729729
730+ private object ManifestTomlEx extends Val .Builtin2 (" manifestTomlEx" , " value" , " indent" ) {
731+ private def isTableArray (v : Val ) = v match {
732+ case s : Val .Arr => s.length > 0 && s.asLazyArray.forall(_.isInstanceOf [Val .Obj ])
733+ case _ => false
734+ }
735+
736+ private def isSection (v : Val ) = v.isInstanceOf [Val .Obj ] || isTableArray(v)
737+
738+ private def renderTableInternal (out : StringWriter , v : Val .Obj , cumulatedIndent : String , indent : String , path : Seq [String ], indexedPath : Seq [String ])(implicit ev : EvalScope ): StringWriter = {
739+ val (sections, nonSections) = v.visibleKeyNames.partition(k => isSection(v.value(k, v.pos)(ev)))
740+ for (k <- nonSections.sorted) {
741+ out.write(cumulatedIndent)
742+ out.write(TomlRenderer .escapeKey(k))
743+ out.write(" = " )
744+ Materializer .apply0(v.value(k, v.pos)(ev), new TomlRenderer (out, cumulatedIndent, indent))(ev)
745+ }
746+ out.write('\n ' )
747+
748+ for (k <- sections.sorted) {
749+ val v0 = v.value(k, v.pos, v)(ev)
750+ if (isTableArray(v0)) {
751+ for (i <- 0 until v0.asArr.length) {
752+ out.write(cumulatedIndent)
753+ renderTableArrayHeader(out, path :+ k)
754+ out.write('\n ' )
755+ renderTableInternal(out, v0.asArr.force(i).asObj, cumulatedIndent + indent, indent, path :+ k,
756+ indexedPath ++ Seq (k, i.toString))
757+ }
758+ } else {
759+ out.write(cumulatedIndent)
760+ renderTableHeader(out, path :+ k)
761+ out.write('\n ' )
762+ renderTableInternal(out, v0.asObj, cumulatedIndent + indent, indent, path :+ k, indexedPath :+ k)
763+ }
764+ }
765+ out
766+ }
767+
768+ private def renderTableHeader (out : StringWriter , path : Seq [String ]) = {
769+ out.write('[' )
770+ out.write(path.map(TomlRenderer .escapeKey).mkString(" ." ))
771+ out.write(']' )
772+ out
773+ }
774+
775+ private def renderTableArrayHeader (out : StringWriter , path : Seq [String ]) = {
776+ out.write('[' )
777+ renderTableHeader(out, path)
778+ out.write(']' )
779+ out
780+ }
781+
782+ def evalRhs (v : Val , indent : Val , ev : EvalScope , pos : Position ): Val = {
783+ val out = new StringWriter
784+ renderTableInternal(out, v.force.asObj, " " , indent.asString, Seq .empty[String ], Seq .empty[String ])(ev)
785+ Val .Str (pos, out.toString.strip)
786+ }
787+ }
788+
730789 private object Set_ extends Val .Builtin2 (" set" , " arr" , " keyF" , Array (null , Val .False (dummyPos))) {
731790 def evalRhs (arr : Val , keyF : Val , ev : EvalScope , pos : Position ): Val = {
732791 uniqArr(pos, ev, sortArr(pos, ev, arr, keyF), keyF)
@@ -1056,6 +1115,10 @@ class Std {
10561115 builtin(ManifestJson ),
10571116 builtin(ManifestJsonMinified ),
10581117 builtin(ManifestJsonEx ),
1118+ builtin(" manifestToml" , " value" ){ (pos, ev, value : Val ) =>
1119+ ManifestTomlEx .evalRhs(value, Val .Str (pos, " " ), ev, pos)
1120+ },
1121+ builtin(ManifestTomlEx ),
10591122 builtinWithDefaults(" manifestYamlDoc" ,
10601123 " v" -> null ,
10611124 " indent_array_in_object" -> Val .False (dummyPos)){ (args, pos, ev) =>
0 commit comments