@@ -36,12 +36,10 @@ import org.pkl.parser.syntax.generic.NodeType
3636internal  class  Builder (sourceText :  String , private val  grammarVersion :  GrammarVersion ) {
3737  private  var  id:  Int  =  0 
3838  private  val  source:  CharArray  =  sourceText.toCharArray()
39-   private  var  prevNode:  Node ?  =  null 
4039  private  var  noNewlines =  false 
4140
42-   fun  format (node :  Node ): FormatNode  {
43-     prevNode =  node
44-     return  when  (node.type) {
41+   fun  format (node :  Node ): FormatNode  = 
42+     when  (node.type) {
4543      NodeType .MODULE  ->  formatModule(node)
4644      NodeType .DOC_COMMENT  ->  Nodes (formatGeneric(node.children, null ))
4745      NodeType .DOC_COMMENT_LINE  ->  formatDocComment(node)
@@ -164,7 +162,6 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
164162      NodeType .PARENTHESIZED_TYPE_ELEMENTS  ->  formatParenthesizedTypeElements(node)
165163      else  ->  throw  RuntimeException (" Unknown node type: ${node.type} "  )
166164    }
167-   }
168165
169166  private  fun  formatModule (node :  Node ): FormatNode  {
170167    val  nodes = 
@@ -617,6 +614,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
617614      val  trailingNode =  if  (endsWithClosingCurlyBrace(lastParam.last())) Empty  else  line()
618615      val  lastNodes =  formatGenericWithGen(lastParam, sep, null )
619616      if  (normalParams.isEmpty()) {
617+         val  lastNodes =  formatGenericWithGen(lastParam, sep, null )
620618        group(Group (newId(), lastNodes), trailingNode)
621619      } else  {
622620        val  separator =  getSeparator(normalParams.last(), lastParam[0 ], Space )
@@ -1289,7 +1287,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
12891287
12901288  private  fun  getBaseSeparator (prev :  Node , next :  Node ): FormatNode ?  {
12911289    return  when  {
1292-       prevNode?.type  ==   NodeType . LINE_COMMENT  ->  {
1290+       endsInLineComment(prev)  ->  {
12931291        if  (prev.linesBetween(next) >  1 ) {
12941292          TWO_NEWLINES 
12951293        } else  {
@@ -1325,6 +1323,14 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
13251323    }
13261324  }
13271325
1326+   private  tailrec  fun  endsInLineComment (node :  Node ): Boolean  {
1327+     return  when  {
1328+       node.type ==  NodeType .LINE_COMMENT  ->  true 
1329+       node.children.isEmpty() ->  false 
1330+       else  ->  endsInLineComment(node.children.last())
1331+     }
1332+   }
1333+ 
13281334  private  fun  line (): FormatNode  {
13291335    return  if  (noNewlines) Empty  else  Line 
13301336  }
0 commit comments