@@ -286,7 +286,7 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
286
286
// can align if possible.
287
287
// (needsLinebreak is set if we started a new line before)
288
288
p .expr (pair .Key )
289
- p .print (pair .Colon , token .COLON , vtab )
289
+ p .print (pair .Colon , token .ASSIGN , vtab )
290
290
p .expr (pair .Value )
291
291
} else {
292
292
p .expr0 (x , depth )
@@ -369,7 +369,7 @@ func (p *printer) parameters(fields *ast.FieldList) {
369
369
// by a linebreak call after a type, or in the next multi-line identList
370
370
// will do the right thing.
371
371
p .identList (par .Names , ws == indent )
372
- p .print (blank )
372
+ // p.print(blank)
373
373
}
374
374
// parameter type
375
375
// p.expr(stripParensAlways(par.Type))
@@ -785,7 +785,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
785
785
786
786
case * ast.KeyValueExpr :
787
787
p .expr (x .Key )
788
- p .print (x .Colon , token .COLON , blank )
788
+ p .print (x .Colon , token .ASSIGN , blank )
789
789
p .expr (x .Value )
790
790
791
791
case * ast.StarExpr :
@@ -943,7 +943,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
943
943
p .expr1 (x .Type , token .HighestPrec , depth )
944
944
}
945
945
p .level ++
946
- p .print (x .Lbrace , token .LBRACE )
946
+ p .print (x .Lbrace , token .LPAREN )
947
947
p .exprList (x .Lbrace , x .Elts , 1 , commaTerm , x .Rbrace , x .Incomplete )
948
948
// do not insert extra line break following a /*-style comment
949
949
// before the closing '}' as it might break the code if there
@@ -956,7 +956,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
956
956
}
957
957
// need the initial indent to print lone comments with
958
958
// the proper level of indentation
959
- p .print (indent , unindent , mode , x .Rbrace , token .RBRACE , mode )
959
+ p .print (indent , unindent , mode , x .Rbrace , token .RPAREN , mode )
960
960
p .level --
961
961
962
962
case * ast.Ellipsis :
@@ -1148,7 +1148,7 @@ func (p *printer) stmtList(list []ast.Stmt, nindent int, nextIsRBrace bool) {
1148
1148
func (p * printer ) block (b * ast.BlockStmt , nindent int ) {
1149
1149
p .print (b .Lbrace , token .COLON )
1150
1150
p .stmtList (b .List , nindent , true )
1151
- p .linebreak (p .lineFor (b .Rbrace ), 1 , ignore , true )
1151
+ // p.linebreak(p.lineFor(b.Rbrace), 1, ignore, true)
1152
1152
p .print (b .Rbrace ) // , token.RBRACE)
1153
1153
}
1154
1154
@@ -1224,9 +1224,10 @@ func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, po
1224
1224
}
1225
1225
}
1226
1226
}
1227
- if needsBlank {
1228
- p .print (blank )
1229
- }
1227
+ _ = needsBlank
1228
+ // if needsBlank {
1229
+ // p.print(blank)
1230
+ // }
1230
1231
}
1231
1232
1232
1233
// indentList reports whether an expression list would look better if it
@@ -1365,9 +1366,12 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
1365
1366
p .controlClause (false , s .Init , s .Cond , nil )
1366
1367
p .block (s .Body , 1 )
1367
1368
if s .Else != nil {
1368
- p .print (token .ELSE , blank )
1369
+ p .print (newline , token .ELSE )
1369
1370
switch s .Else .(type ) {
1370
- case * ast.BlockStmt , * ast.IfStmt :
1371
+ case * ast.BlockStmt :
1372
+ p .stmt (s .Else , nextIsRBrace )
1373
+ case * ast.IfStmt :
1374
+ p .print (blank )
1371
1375
p .stmt (s .Else , nextIsRBrace )
1372
1376
default :
1373
1377
// This can only happen with an incorrectly
@@ -1390,8 +1394,8 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
1390
1394
p .stmtList (s .Body , 1 , nextIsRBrace )
1391
1395
1392
1396
case * ast.SwitchStmt :
1393
- // p.print(token.SWITCH)
1394
- // p.controlClause(false, s.Init, s.Tag, nil)
1397
+ p .print (token .SWITCH )
1398
+ p .controlClause (false , s .Init , s .Tag , nil )
1395
1399
p .block (s .Body , 0 )
1396
1400
1397
1401
case * ast.TypeSwitchStmt :
@@ -1428,7 +1432,20 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
1428
1432
1429
1433
case * ast.ForStmt :
1430
1434
p .print (token .FOR )
1431
- p .controlClause (true , s .Init , s .Cond , s .Post )
1435
+ didRange := false
1436
+ if x , ok := s .Cond .(* ast.BinaryExpr ); ok {
1437
+ if x .Op == token .LSS {
1438
+ p .print (blank )
1439
+ p .expr (x .X )
1440
+ p .print (" in range" , token .LPAREN )
1441
+ p .expr (x .Y )
1442
+ p .print (token .RPAREN )
1443
+ didRange = true
1444
+ }
1445
+ }
1446
+ if ! didRange {
1447
+ p .controlClause (true , s .Init , s .Cond , s .Post )
1448
+ }
1432
1449
p .block (s .Body , 1 )
1433
1450
1434
1451
case * ast.RangeStmt :
@@ -1441,9 +1458,10 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
1441
1458
p .print (s .Value .Pos (), token .COMMA , blank )
1442
1459
p .expr (s .Value )
1443
1460
}
1444
- p .print (blank , s .TokPos , s .Tok , blank )
1461
+ // p.print(blank, s.TokPos, s.Tok, blank)
1445
1462
}
1446
- p .print (token .RANGE , blank )
1463
+ // p.print(token.RANGE, blank)
1464
+ p .print (blank , "in" , blank )
1447
1465
p .expr (stripParens (s .X ))
1448
1466
p .print (blank )
1449
1467
p .block (s .Body , 1 )
@@ -1621,22 +1639,28 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
1621
1639
p .setComment (s .Comment )
1622
1640
1623
1641
case * ast.TypeSpec :
1624
- p .setComment (s .Doc )
1642
+ // p.setComment(s.Doc)
1643
+ if s .Doc != nil {
1644
+ p .cindex ++ // skip current comments
1645
+ p .commentOffset = 0
1646
+ }
1647
+ p .flush (p .pos , token .TYPE ) // get rid of any comments
1625
1648
p .print ("class" , blank )
1626
1649
p .expr (s .Name )
1627
- if n == 1 {
1628
- p .print (blank )
1629
- } else {
1630
- p .print (vtab )
1631
- }
1650
+ // if n == 1 {
1651
+ // p.print(blank)
1652
+ // } else {
1653
+ // p.print(vtab)
1654
+ // }
1632
1655
if s .Assign .IsValid () {
1633
1656
p .print (token .ASSIGN , blank )
1634
1657
}
1658
+ p .pyFuncComments (s .Doc ) // neither s.Doc nor s.Comment work here
1635
1659
p .expr (s .Type )
1636
1660
p .print ("<<<<EndClass: " )
1637
1661
p .expr (s .Name )
1638
1662
p .print (">>>>" , newline )
1639
- p .setComment (s .Comment )
1663
+ // p.setComment(s.Comment)
1640
1664
1641
1665
default :
1642
1666
panic ("unreachable" )
@@ -1842,19 +1866,19 @@ func (p *printer) funcDecl(d *ast.FuncDecl) {
1842
1866
// We have to save startCol only after emitting FUNC; otherwise it can be on a
1843
1867
// different line (all whitespace preceding the FUNC is emitted only when the
1844
1868
// FUNC is emitted).
1845
- // p.flush(p.pos, p.lastTok)
1846
- // p.comments = nil
1869
+ if d .Doc != nil {
1870
+ p .cindex ++ // skip current comments
1871
+ p .commentOffset = 0
1872
+ }
1847
1873
if d .Recv != nil {
1848
1874
p .print ("<<<<Method: " )
1849
1875
p .printMethRecvType (d .Recv .List [0 ].Type )
1850
1876
p .print (">>>>" , newline )
1851
1877
// p.parameters(d.Recv) // method: print receiver
1852
1878
// p.print(blank)
1853
1879
p .print (indent )
1854
- } else {
1855
- p .commentOffset = 0
1856
1880
}
1857
- p .flush (p .pos , p . lastTok )
1881
+ p .flush (p .pos , token . FUNC ) // get rid of any comments
1858
1882
p .print ("def" , blank )
1859
1883
startCol := p .out .Column - len ("def " )
1860
1884
p .expr (d .Name )
@@ -1873,8 +1897,7 @@ func (p *printer) funcDecl(d *ast.FuncDecl) {
1873
1897
p .funcBody (p .distanceFrom (d .Pos (), startCol ), vtab , d .Body )
1874
1898
if d .Recv != nil {
1875
1899
p .print (unindent )
1876
- p .flush (p .pos , p .lastTok )
1877
- p .print ("<<<<EndMethod>>>>" , newline )
1900
+ p .print (newline , "<<<<EndMethod>>>>" , newline )
1878
1901
}
1879
1902
}
1880
1903
0 commit comments