Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: mojom/mojom_parser/formatter/printer.go

Issue 1750183002: Small formatter fixes. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package formatter 5 package formatter
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt" 9 "fmt"
10 "mojom/mojom_parser/lexer" 10 "mojom/mojom_parser/lexer"
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 p.writeRightComments(enumValue) 260 p.writeRightComments(enumValue)
261 } 261 }
262 262
263 // writeDeclaredObjectsContainer 263 // writeDeclaredObjectsContainer
264 // DeclaredObjectsContainers are MojomEnum, MojomUnion, MojomInterface, MojomStr uct. 264 // DeclaredObjectsContainers are MojomEnum, MojomUnion, MojomInterface, MojomStr uct.
265 // This method writes the name of the object, an opening brace, the contained 265 // This method writes the name of the object, an opening brace, the contained
266 // declarations (fields, enum values, nested declarations and methods) and the 266 // declarations (fields, enum values, nested declarations and methods) and the
267 // closing brace. 267 // closing brace.
268 func (p *printer) writeDeclaredObjectsContainer(container mojom.DeclaredObjectsC ontainer) { 268 func (p *printer) writeDeclaredObjectsContainer(container mojom.DeclaredObjectsC ontainer) {
269 269
270 p.writef("%v {", container.(mojom.DeclaredObject).NameToken().Text)
270 if len(container.GetDeclaredObjects()) == 0 && !containsFinalComments(co ntainer.(mojom.MojomElement)) { 271 if len(container.GetDeclaredObjects()) == 0 && !containsFinalComments(co ntainer.(mojom.MojomElement)) {
271 » » p.writef("%v{};", container.(mojom.DeclaredObject).NameToken().T ext) 272 » » p.writef("};")
272 return 273 return
273 } 274 }
274 p.writef("%v {", container.(mojom.DeclaredObject).NameToken().Text)
275 p.writeRightComments(container.(mojom.MojomElement)) 275 p.writeRightComments(container.(mojom.MojomElement))
276 p.incIndent() 276 p.incIndent()
277 p.nl() 277 p.nl()
278 declaredObjects := container.GetDeclaredObjects() 278 declaredObjects := container.GetDeclaredObjects()
279 for i, declaredObject := range declaredObjects { 279 for i, declaredObject := range declaredObjects {
280 p.writeDeclaredObject(declaredObject) 280 p.writeDeclaredObject(declaredObject)
281 if i < len(declaredObjects)-1 { 281 if i < len(declaredObjects)-1 {
282 p.nl() 282 p.nl()
283 } 283 }
284 } 284 }
(...skipping 30 matching lines...) Expand all
315 315
316 if len(attrs.List) == 0 { 316 if len(attrs.List) == 0 {
317 return 317 return
318 } 318 }
319 319
320 p.writef("[") 320 p.writef("[")
321 for idx, attr := range attrs.List { 321 for idx, attr := range attrs.List {
322 p.writeAttribute(&attr) 322 p.writeAttribute(&attr)
323 if idx < len(attrs.List)-1 { 323 if idx < len(attrs.List)-1 {
324 p.writef(",") 324 p.writef(",")
325 » » » if singleLine { 325 » » » if !singleLine {
326 » » » » p.write(" ")
327 » » » } else {
328 p.nl() 326 p.nl()
329 } 327 }
328 p.write(" ")
330 } 329 }
331 } 330 }
332 p.writef("]") 331 p.writef("]")
333 » if !singleLine { 332 » if singleLine {
333 » » p.write(" ")
334 » } else {
334 p.nl() 335 p.nl()
335 } 336 }
336 } 337 }
337 338
338 func (p *printer) writeAttribute(attr *mojom.MojomAttribute) { 339 func (p *printer) writeAttribute(attr *mojom.MojomAttribute) {
339 p.writeBeforeComments(attr) 340 p.writeBeforeComments(attr)
340 p.writef("%s=", attr.Key) 341 p.writef("%s=", attr.Key)
341 p.writeValueRef(attr.Value) 342 p.writeValueRef(attr.Value)
342 p.writeRightComments(attr) 343 p.writeRightComments(attr)
343 } 344 }
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 p.lineStarted = true 567 p.lineStarted = true
567 } 568 }
568 p.buffer.WriteString(s) 569 p.buffer.WriteString(s)
569 } 570 }
570 571
571 // nl writes a new line. Before writing the new line, nl writes the last 572 // nl writes a new line. Before writing the new line, nl writes the last
572 // comment on the line. 573 // comment on the line.
573 func (p *printer) nl() { 574 func (p *printer) nl() {
574 // Before going to the next line, print the last comment on the line. 575 // Before going to the next line, print the last comment on the line.
575 if p.eolComment != nil { 576 if p.eolComment != nil {
576 » » p.writef(" %s", p.eolComment.Text) 577 » » p.writef(" %s", p.eolComment.Text)
577 p.eolComment = nil 578 p.eolComment = nil
578 } 579 }
579 580
580 p.buffer.WriteString("\n") 581 p.buffer.WriteString("\n")
581 p.lineStarted = false 582 p.lineStarted = false
582 } 583 }
583 584
584 func (p *printer) incIndent() { 585 func (p *printer) incIndent() {
585 p.indentLevel += 1 586 p.indentLevel += 1
586 } 587 }
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 673
673 // See sort.Interface. 674 // See sort.Interface.
674 func (ifs *importedFilesSorter) Less(i, j int) bool { 675 func (ifs *importedFilesSorter) Less(i, j int) bool {
675 return ifs.imports[i].SpecifiedName < ifs.imports[j].SpecifiedName 676 return ifs.imports[i].SpecifiedName < ifs.imports[j].SpecifiedName
676 } 677 }
677 678
678 // See sort.Interface. 679 // See sort.Interface.
679 func (ifs *importedFilesSorter) Swap(i, j int) { 680 func (ifs *importedFilesSorter) Swap(i, j int) {
680 ifs.imports[i], ifs.imports[j] = ifs.imports[j], ifs.imports[i] 681 ifs.imports[i], ifs.imports[j] = ifs.imports[j], ifs.imports[i]
681 } 682 }
OLDNEW
« no previous file with comments | « mojom/mojom_parser/formatter/formatter_test.go ('k') | mojom/mojom_parser/formatter/printer_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698