Skip to content

Commit bd633d3

Browse files
committed
juledoc: fix handling of grouped variable declarations and comments
1 parent 66857aa commit bd633d3

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

builder/builder.jule

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,16 @@ impl Builder {
7575
}
7676

7777
fn formatMainNode(self, mut ast: &ast::AST, mut &i: int): str {
78-
match type ast.Nodes[i].Data {
79-
| &ast::Var:
80-
// No-op:
81-
|:
78+
mut data, ok := ast.Nodes[i].Data.(&ast::Var)
79+
if !ok {
8280
ret self.formatNode(ast.Nodes[i])
8381
}
8482
// For variables, do not use plain node formatting.
8583
// Variables may be grouped with a common documentation.
8684
// Do not waste it.
8785
self.fmt.buf.Clear()
8886
{
89-
data := ast.Nodes[i].Data.(&ast::Var)
90-
// Format directly if data is grouped explicitly.
91-
if data.Group != nil {
92-
ret self.formatNode(ast.Nodes[i])
93-
}
94-
mut row := data.Token.Row
87+
mut row := ast.Nodes[i].Token.Row
9588
if len(data.Directives) > 0 {
9689
row = data.Directives[0].Tag.Row
9790
}
@@ -107,7 +100,11 @@ impl Builder {
107100
}
108101
}
109102
})
110-
i-- // Build function will advance the offset, avoid false offset.
103+
// If variable is not grouped, the build function will advance the offset,
104+
// avoid false offset.
105+
if len(data.Group) == 0 {
106+
i--
107+
}
111108
ret strings::TrimSpace(self.fmt.buf.Str())
112109
}
113110

builder/formatter.jule

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,30 @@ impl formatter {
809809
ret
810810
}
811811

812+
// If group declaration used, fall-back to special group handling.
813+
const match type T {
814+
| &ast::Var:
815+
const match type Node {
816+
| &ast::Var:
817+
if nodes[i].Group != nil {
818+
self.varGroup(nodes[i])
819+
ret
820+
}
821+
| ast::Node | ast::Stmt:
822+
mut v, ok := nodes[i].Data.(T)
823+
if ok && v.Group != nil {
824+
self.varGroup(v)
825+
ret
826+
}
827+
}
828+
| ast::Node | ast::Stmt:
829+
mut v, ok := nodes[i].Data.(&ast::Var)
830+
if ok && v.Group != nil {
831+
self.varGroup(v)
832+
ret
833+
}
834+
}
835+
812836
const Cap = 1 << 4
813837
mut lines := make([][]byte, 0, Cap)
814838
mut rows := make([]int, 0, Cap)

0 commit comments

Comments
 (0)