Skip to content

Commit 1e2c8d6

Browse files
reid-spencerclaude
andcommitted
Fix three prettify formatting regressions from 1.12.0
Remove commas between aggregate fields (RIDDL grammar makes them optional and original models don't use them), keep "} with {" on same line for types with metadata by trimming trailing newline before emitMetaData, and preserve relative include paths by using url.path instead of url.toExternalForm. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 933518b commit 1e2c8d6

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

passes/jvm-native/src/test/scala/com/ossuminc/riddl/passes/RiddlTest.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,44 @@ class RiddlTest extends AbstractTestingBasis {
170170
indexIndent must be > ofIndent
171171
}
172172

173+
"not insert commas between aggregate fields" in {
174+
val input =
175+
"""domain foo is {
176+
| context bar is {
177+
| record Baz is {
178+
| x: String
179+
| y: Integer
180+
| }
181+
| }
182+
|}""".stripMargin
183+
val rpi = RiddlParserInput(input, "")
184+
Riddl.parse(rpi) match
185+
case Left(messages) => fail(messages.format)
186+
case Right(root) =>
187+
val output = Riddl.toRiddlText(root)
188+
output must not include ","
189+
}
190+
191+
"keep } with { on same line for types" in {
192+
val input =
193+
"""domain foo is {
194+
| context bar is {
195+
| record Baz is {
196+
| x: String
197+
| } with {
198+
| briefly "A record"
199+
| }
200+
| }
201+
|}""".stripMargin
202+
val rpi = RiddlParserInput(input, "")
203+
Riddl.parse(rpi) match
204+
case Left(messages) => fail(messages.format)
205+
case Right(root) =>
206+
val output = Riddl.toRiddlText(root)
207+
output must include ("} with {")
208+
output must not include ("}\n with {")
209+
}
210+
173211
"preserve include structure in multi-file mode" in {
174212
val includePath = "language/input/includes/domainIncludes.riddl"
175213
val includeUrl = PathUtils.urlFromCwdPath(Path.of(includePath))
@@ -191,6 +229,7 @@ class RiddlTest extends AbstractTestingBasis {
191229
// The main file should contain an include directive
192230
val mainContent = state.filesAsString
193231
mainContent must include("include")
232+
mainContent must not include "file://"
194233
}
195234
Await.result(future, 10.seconds)
196235
}

passes/shared/src/main/scala/com/ossuminc/riddl/passes/prettify/PrettifyVisitor.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class PrettifyVisitor(options: PrettifyPass.Options)(using PlatformContext) exte
3737
def closeType(typ: Type, parents: Parents): Unit =
3838
state.withCurrent { rfe =>
3939
if typ.metadata.isEmpty then rfe.nl
40-
else rfe.emitMetaData(typ.metadata)
40+
else
41+
rfe.trimTrailingNewline()
42+
rfe.emitMetaData(typ.metadata)
4143
}
4244

4345
def openDomain(domain: Domain, parents: Parents): Unit = open(domain)
@@ -414,7 +416,7 @@ class PrettifyVisitor(options: PrettifyPass.Options)(using PlatformContext) exte
414416
state.withCurrent { (rfe: RiddlFileEmitter) =>
415417
if !state.flatten then
416418
val url = include.origin
417-
rfe.addLine(s"""include "${url.toExternalForm}"""")
419+
rfe.addLine(s"""include "${url.path}"""")
418420
val outputURL = state.toDestination(url)
419421
val newRFE = RiddlFileEmitter(outputURL)
420422
state.pushFile(newRFE)

passes/shared/src/main/scala/com/ossuminc/riddl/passes/prettify/RiddlFileEmitter.scala

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ case class RiddlFileEmitter(url: URL)(using PlatformContext) extends FileBuilder
134134
private def emitULIDAttachment(a: ULIDAttachment): this.type =
135135
addIndent("attachment " + a.id.format).add(s" is \"${a.ulid.toString}\"")
136136

137+
def trimTrailingNewline(): this.type =
138+
if sb.length >= new_line.length &&
139+
sb.substring(sb.length - new_line.length) == new_line
140+
then sb.setLength(sb.length - new_line.length)
141+
this
142+
end trimTrailingNewline
143+
137144
def emitString(s: String_): this.type = {
138145
(s.min, s.max) match {
139146
case (Some(n), Some(x)) => this.add(s"String($n,$x)")
@@ -200,18 +207,9 @@ case class RiddlFileEmitter(url: URL)(using PlatformContext) extends FileBuilder
200207
.nl
201208
case Some(_) =>
202209
this.add("{").nl.incr
203-
val lastIdx = of.size - 1
204-
of.zipWithIndex.foreach { case (f, idx) =>
210+
of.foreach { f =>
205211
add(spc).emitField(f)
206-
if idx < lastIdx then
207-
if f.metadata.nonEmpty then
208-
// metadata ended with }\n; insert comma before trailing newline
209-
sb.insert(sb.length - new_line.length, ",")
210-
else
211-
add(",").nl
212-
else
213-
// last field — no comma
214-
if f.metadata.isEmpty then nl
212+
if f.metadata.isEmpty then nl
215213
}
216214
decr.addIndent("}").nl
217215
}

0 commit comments

Comments
 (0)