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 25, 2024
1 parent 45b1d79 commit 51166f6
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions src/format.jule
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@ impl Formatter {
self.indent = self.indent[:self.indent.len - 4]
}

fn write_comment(&self, &c: &Comment) {
if self.indent.len > 0 && c.txt[1] == '*' {
// Range comment should be checked.
// Endind is should be padded by indentation.
let mut i = c.txt.len - 3
lookup:
for i > 1; i-- {
let b = c.txt[i]
match b {
| ' ' | '\t' | '\v' | '\r':
continue
| '\n':
let cmt = c.txt[:i + 1]
i = strings::find_last(self.buf, "\n")
let n = self.buf.len - i - 1
self.write(cmt)
self.write(strings::repeat(" ", n))
self.write("*/")
ret
|:
break lookup
}
}
}
self.write(c.txt)
}

fn write_comments_except(&self, row: int): int {
let mut lrow = row
for {
Expand All @@ -58,28 +85,7 @@ impl Formatter {
lrow = c.row
self.cm.drop_first()
self.write(self.indent)
if self.indent.len > 0 && c.txt[1] == '*' {
// Range comment should be checked.
// Endind is should be padded by indentation.
let mut i = c.txt.len - 3
lookup:
for i > 1; i-- {
let b = c.txt[i]
match b {
| ' ' | '\t' | '\v' | '\r':
continue
| '\n':
self.write(c.txt[:i + 1])
self.write(self.indent)
self.write("*/")
goto end
|:
break lookup
}
}
}
self.write(c.txt)
end:
self.write_comment(c)
self.write("\n")
}
ret lrow
Expand Down Expand Up @@ -399,7 +405,7 @@ impl Formatter {
let c = self.cm.pop(m.token.row)
if c != nil {
self.write(" ")
self.write(c.txt)
self.write_comment(c)
}
self.write("\n")
}
Expand Down Expand Up @@ -507,7 +513,7 @@ impl Formatter {
if c != nil && c.row == row {
self.cm.drop_first()
self.write(strings::repeat(" ", max - line.len + 1))
self.write(c.txt)
self.write_comment(c)
}
self.write("\n")
start++
Expand Down Expand Up @@ -572,7 +578,7 @@ impl Formatter {
if c != nil && c.row == row {
self.cm.drop_first()
self.write(strings::repeat(" ", max - line.len + 1))
self.write(c.txt)
self.write_comment(c)
}
self.write("\n")
start++
Expand Down Expand Up @@ -938,7 +944,7 @@ impl ScopeFormatter {
let c = self.fmt.cm.pop(row)
if c != nil {
self.write(" ")
self.write(c.txt)
self.fmt.write_comment(c)
}
}
self.fmt.write_comments_except(expr.token.row)
Expand All @@ -958,7 +964,7 @@ impl ScopeFormatter {
let cmt = self.fmt.cm.pop(row)
if cmt != nil {
self.write(" ")
self.write(cmt.txt)
self.fmt.write_comment(cmt)
}
self.write("\n")
self.fmt.add_indent()
Expand Down Expand Up @@ -1092,7 +1098,7 @@ impl ScopeFormatter {
let c = self.fmt.cm.pop(stmt.token.row)
if c != nil {
self.write(" ")
self.write(c.txt)
self.fmt.write_comment(c)
}
}
if old != i {
Expand Down Expand Up @@ -1230,12 +1236,12 @@ impl ExprFormatter {
let c = self.fmt.cm.pop(row)
if c != nil {
self.write(" ")
self.write(c.txt)
self.fmt.write_comment(c)
}
self.write("\n")
self.write(self.fmt.indent)
row = arg.token.row
} else {
} else if i > 0 {
self.write(" ")
}
self.format(arg)
Expand All @@ -1258,7 +1264,7 @@ impl ExprFormatter {
let c = self.fmt.cm.pop(f.args[f.args.len - 1].token.row)
if c != nil {
self.write(" ")
self.write(c.txt)
self.fmt.write_comment(c)
}
}
if f.ignored() {
Expand Down Expand Up @@ -1363,7 +1369,7 @@ impl ExprFormatter {
let c = self.fmt.cm.pop(expr.token.row)
if c != nil {
self.write(strings::repeat(" ", max - exprs[j].len + 1))
self.write(c.txt)
self.fmt.write_comment(c)
}
j++
}
Expand All @@ -1387,7 +1393,7 @@ impl ExprFormatter {
let c = self.fmt.cm.pop(lit.token.row)
if c != nil {
self.write(" ")
self.write(c.txt)
self.fmt.write_comment(c)
}
}

Expand All @@ -1404,7 +1410,7 @@ impl ExprFormatter {
let c = self.fmt.cm.pop(s.token.row)
if c != nil {
self.write(" ")
self.write(c.txt)
self.fmt.write_comment(c)
}
}

Expand Down

0 comments on commit 51166f6

Please sign in to comment.