File tree Expand file tree Collapse file tree 6 files changed +23
-1
lines changed
Expand file tree Collapse file tree 6 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -205,6 +205,12 @@ func (p *Parser) objectItem() (*ast.ObjectItem, error) {
205205 }
206206 }
207207
208+ // key=#comment
209+ // val
210+ if p .lineComment != nil {
211+ o .LineComment , p .lineComment = p .lineComment , nil
212+ }
213+
208214 // do a look-ahead for line comment
209215 p .scan ()
210216 if len (keys ) > 0 && o .Val .Pos ().Line == keys [0 ].Pos ().Line && p .lineComment != nil {
Original file line number Diff line number Diff line change @@ -252,6 +252,14 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte {
252252 }
253253 }
254254
255+ // If key and val are on different lines, treat line comments like lead comments.
256+ if o .LineComment != nil && o .Val .Pos ().Line != o .Keys [0 ].Pos ().Line {
257+ for _ , comment := range o .LineComment .List {
258+ buf .WriteString (comment .Text )
259+ buf .WriteByte (newline )
260+ }
261+ }
262+
255263 for i , k := range o .Keys {
256264 buf .WriteString (k .Token .Text )
257265 buf .WriteByte (blank )
@@ -265,7 +273,7 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte {
265273
266274 buf .Write (p .output (o .Val ))
267275
268- if o .Val .Pos ().Line == o .Keys [0 ].Pos ().Line && o . LineComment != nil {
276+ if o .LineComment != nil && o . Val .Pos ().Line == o .Keys [0 ].Pos ().Line {
269277 buf .WriteByte (blank )
270278 for _ , comment := range o .LineComment .List {
271279 buf .WriteString (comment .Text )
Original file line number Diff line number Diff line change @@ -153,6 +153,7 @@ func TestFormatValidOutput(t *testing.T) {
153153 cases := []string {
154154 "#\x00 " ,
155155 "#\ue123 t" ,
156+ "x=//\n 0y=<<_\n _\n " ,
156157 "Y=<<4\n 4/\n \n \n /4/@=4/\n \n \n /4000000004\r \r \n 00004\n " ,
157158 "x=<<_\n _\r \r \n _\n " ,
158159 "X=<<-\n \r \r \n " ,
Original file line number Diff line number Diff line change @@ -34,3 +34,6 @@ variable = {
3434foo {
3535 bar = "fatih" // line comment 2
3636} // line comment 3
37+
38+ // comment
39+ multiline = "assignment"
Original file line number Diff line number Diff line change 3535 bar = "fatih" // line comment 2
3636} // line comment 3
3737
38+ multiline = // comment
39+ "assignment"
Original file line number Diff line number Diff line change 3535 bar = "fatih" // line comment 2
3636} // line comment 3
3737
38+ multiline = // comment
39+ "assignment"
You can’t perform that action at this time.
0 commit comments