Skip to content

Commit

Permalink
julefmt: minor fix for empty scope formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Sep 8, 2024
1 parent 460d09a commit b1afd32
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/format.jule
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ impl formatter {
self.write(c.txt)
}

fn seekCommentExcept(&self, row: int): &comment {
for (_, mut c) in self.cm.buf {
if row != -1 && c.row >= row {
break
}
ret c
}
ret nil
}

fn writeCommentsExceptL(&self, start: int, row: int): int {
mut lrow := row
mut i := 0
Expand Down Expand Up @@ -1478,14 +1488,36 @@ impl scopeFormatter {
}
}

fn emptyScope(&self, mut &scope: &ast::ScopeTree) {
// Lookup for first comment.
c := self.fmt.seekCommentExcept(scope.End.Row)

// Range comment exist and scope is not indented.
// Indent scope if indent is true.
indent := unsafe { self.fmt.buf.Buf()[self.fmt.buf.Len()-1] } != '\n' &&
c != nil && c.txt[1] == '*'

if indent {
self.write("\n")
self.fmt.addIndent()
}
self.fmt.writeCommentsExcept(scope.End.Row)
if indent {
self.fmt.doneIndent()
self.fmt.buf.Write(self.fmt.indent)
}
}

fn formatStmts(&self, mut &scope: &ast::ScopeTree, inline: bool) {
defer {
self.fmt.row = scope.End.Row
}

if len(scope.Stmts) == 0 {
self.fmt.writeCommentsExcept(scope.End.Row)
self.emptyScope(scope)
ret
}

mut i := 0
self.fmt.row = scope.Stmts[0].Token.Row
mut z := i
Expand Down

0 comments on commit b1afd32

Please sign in to comment.