Skip to content

Commit

Permalink
julefmt: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 27, 2024
1 parent 1a72627 commit 85c2ac1
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/format.jule
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ impl Formatter {
ret self.write_comments_except(row + 1)
}

fn pop_row_comments_by_f(&self, row: int, f: fn(_: &Comment)) {
for i, c in self.cm.map {
if c.row == row {
f(c)
self.cm.map = append(self.cm.map[:i], self.cm.map[i + 1:]...)
continue
} else if c.row > row {
break
}
}
}

fn pop_row_comments_by(&self, row: int) {
self.pop_row_comments_by_f(row, fn(c: &Comment) {
self.write(" ")
self.write_comment(c)
})
}

fn pop_row_comments(&self, row: int) {
for {
let c = self.cm.pop(row)
Expand Down Expand Up @@ -523,12 +542,10 @@ impl Formatter {
let row = rows[k]
self.write_comments_except(row)
self.write(line)
let c = self.cm.first(row)
if c != nil && c.row == row {
self.cm.drop_first()
self.pop_row_comments_by_f(row, fn (c: &Comment) {
self.write(strings::repeat(" ", padding_abs(max - line.len) + 1))
self.write_comment(c)
}
})
self.write("\n")
start++
}
Expand All @@ -544,12 +561,6 @@ impl Formatter {
i = 0
}

self.ef.ignore_comments = true
let ignore_comments = self.ef.ignore_comments
defer {
self.ef.ignore_comments = ignore_comments
}

self.ef.row = -1
let mut max = 0
let n = self.buf.len
Expand Down Expand Up @@ -596,12 +607,10 @@ impl Formatter {
let row = rows[j]
self.write_comments_except(row)
self.write(line)
let c = self.cm.first(row)
if c != nil && c.row == row {
self.cm.drop_first()
self.pop_row_comments_by_f(row, fn(c: &Comment) {
self.write(strings::repeat(" ", padding_abs(max - line.len) + 1))
self.write_comment(c)
}
})
self.write("\n")
}
}
Expand Down Expand Up @@ -1149,7 +1158,6 @@ impl ScopeFormatter {
struct ExprFormatter {
mut fmt: &Formatter
mut row: int // Last seen row.
mut ignore_comments: bool
}

impl ExprFormatter {
Expand Down Expand Up @@ -1232,9 +1240,7 @@ impl ExprFormatter {
ret
}
self.fmt.add_indent()
if !self.ignore_comments {
self.fmt.pop_row_comments(bin.op.row)
}
self.fmt.pop_row_comments_by(bin.op.row)
self.write("\n")
self.write(self.fmt.indent)
match type bin.right.kind {
Expand Down Expand Up @@ -1360,12 +1366,6 @@ impl ExprFormatter {
let mut lined = false

self.fmt.add_indent()
self.fmt.ef.ignore_comments = true
let ignore_comments = self.fmt.ef.ignore_comments
defer {
self.fmt.ef.ignore_comments = ignore_comments
}

for (i, mut expr) in lit.exprs {
if row != expr.token.row {
lined = true
Expand Down

0 comments on commit 85c2ac1

Please sign in to comment.