@@ -286,7 +286,7 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
286286 // can align if possible.
287287 // (needsLinebreak is set if we started a new line before)
288288 p .expr (pair .Key )
289- p .print (pair .Colon , token .COLON , vtab )
289+ p .print (pair .Colon , token .ASSIGN , vtab )
290290 p .expr (pair .Value )
291291 } else {
292292 p .expr0 (x , depth )
@@ -369,7 +369,7 @@ func (p *printer) parameters(fields *ast.FieldList) {
369369 // by a linebreak call after a type, or in the next multi-line identList
370370 // will do the right thing.
371371 p .identList (par .Names , ws == indent )
372- p .print (blank )
372+ // p.print(blank)
373373 }
374374 // parameter type
375375 // p.expr(stripParensAlways(par.Type))
@@ -785,7 +785,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
785785
786786 case * ast.KeyValueExpr :
787787 p .expr (x .Key )
788- p .print (x .Colon , token .COLON , blank )
788+ p .print (x .Colon , token .ASSIGN , blank )
789789 p .expr (x .Value )
790790
791791 case * ast.StarExpr :
@@ -943,7 +943,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
943943 p .expr1 (x .Type , token .HighestPrec , depth )
944944 }
945945 p .level ++
946- p .print (x .Lbrace , token .LBRACE )
946+ p .print (x .Lbrace , token .LPAREN )
947947 p .exprList (x .Lbrace , x .Elts , 1 , commaTerm , x .Rbrace , x .Incomplete )
948948 // do not insert extra line break following a /*-style comment
949949 // 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) {
956956 }
957957 // need the initial indent to print lone comments with
958958 // 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 )
960960 p .level --
961961
962962 case * ast.Ellipsis :
@@ -1148,7 +1148,7 @@ func (p *printer) stmtList(list []ast.Stmt, nindent int, nextIsRBrace bool) {
11481148func (p * printer ) block (b * ast.BlockStmt , nindent int ) {
11491149 p .print (b .Lbrace , token .COLON )
11501150 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)
11521152 p .print (b .Rbrace ) // , token.RBRACE)
11531153}
11541154
@@ -1224,9 +1224,10 @@ func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, po
12241224 }
12251225 }
12261226 }
1227- if needsBlank {
1228- p .print (blank )
1229- }
1227+ _ = needsBlank
1228+ // if needsBlank {
1229+ // p.print(blank)
1230+ // }
12301231}
12311232
12321233// indentList reports whether an expression list would look better if it
@@ -1365,9 +1366,12 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
13651366 p .controlClause (false , s .Init , s .Cond , nil )
13661367 p .block (s .Body , 1 )
13671368 if s .Else != nil {
1368- p .print (token .ELSE , blank )
1369+ p .print (newline , token .ELSE )
13691370 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 )
13711375 p .stmt (s .Else , nextIsRBrace )
13721376 default :
13731377 // This can only happen with an incorrectly
@@ -1390,8 +1394,8 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
13901394 p .stmtList (s .Body , 1 , nextIsRBrace )
13911395
13921396 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 )
13951399 p .block (s .Body , 0 )
13961400
13971401 case * ast.TypeSwitchStmt :
@@ -1428,7 +1432,20 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
14281432
14291433 case * ast.ForStmt :
14301434 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+ }
14321449 p .block (s .Body , 1 )
14331450
14341451 case * ast.RangeStmt :
@@ -1441,9 +1458,10 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
14411458 p .print (s .Value .Pos (), token .COMMA , blank )
14421459 p .expr (s .Value )
14431460 }
1444- p .print (blank , s .TokPos , s .Tok , blank )
1461+ // p.print(blank, s.TokPos, s.Tok, blank)
14451462 }
1446- p .print (token .RANGE , blank )
1463+ // p.print(token.RANGE, blank)
1464+ p .print (blank , "in" , blank )
14471465 p .expr (stripParens (s .X ))
14481466 p .print (blank )
14491467 p .block (s .Body , 1 )
@@ -1621,22 +1639,28 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
16211639 p .setComment (s .Comment )
16221640
16231641 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
16251648 p .print ("class" , blank )
16261649 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+ // }
16321655 if s .Assign .IsValid () {
16331656 p .print (token .ASSIGN , blank )
16341657 }
1658+ p .pyFuncComments (s .Doc ) // neither s.Doc nor s.Comment work here
16351659 p .expr (s .Type )
16361660 p .print ("<<<<EndClass: " )
16371661 p .expr (s .Name )
16381662 p .print (">>>>" , newline )
1639- p .setComment (s .Comment )
1663+ // p.setComment(s.Comment)
16401664
16411665 default :
16421666 panic ("unreachable" )
@@ -1842,19 +1866,19 @@ func (p *printer) funcDecl(d *ast.FuncDecl) {
18421866 // We have to save startCol only after emitting FUNC; otherwise it can be on a
18431867 // different line (all whitespace preceding the FUNC is emitted only when the
18441868 // 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+ }
18471873 if d .Recv != nil {
18481874 p .print ("<<<<Method: " )
18491875 p .printMethRecvType (d .Recv .List [0 ].Type )
18501876 p .print (">>>>" , newline )
18511877 // p.parameters(d.Recv) // method: print receiver
18521878 // p.print(blank)
18531879 p .print (indent )
1854- } else {
1855- p .commentOffset = 0
18561880 }
1857- p .flush (p .pos , p . lastTok )
1881+ p .flush (p .pos , token . FUNC ) // get rid of any comments
18581882 p .print ("def" , blank )
18591883 startCol := p .out .Column - len ("def " )
18601884 p .expr (d .Name )
@@ -1873,8 +1897,7 @@ func (p *printer) funcDecl(d *ast.FuncDecl) {
18731897 p .funcBody (p .distanceFrom (d .Pos (), startCol ), vtab , d .Body )
18741898 if d .Recv != nil {
18751899 p .print (unindent )
1876- p .flush (p .pos , p .lastTok )
1877- p .print ("<<<<EndMethod>>>>" , newline )
1900+ p .print (newline , "<<<<EndMethod>>>>" , newline )
18781901 }
18791902}
18801903
0 commit comments