Break method lines based on line length.

R=rudominer@chromium.org, rudominer
BUG= #592

Review URL: https://codereview.chromium.org/1762473002 .
diff --git a/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1 b/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
index bcbbf03..8edd04e 100644
--- a/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
+++ b/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
@@ -1 +1 @@
-e73fcb08409e0e0e0472ea1b205ea34215b5f3ce
\ No newline at end of file
+6984a9b7464791a7d23de48a184b7cb5a68640fe
\ No newline at end of file
diff --git a/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1 b/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
index 09652a7..4ce3fbd 100644
--- a/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
+++ b/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
@@ -1 +1 @@
-599aebd8d0aec502a96ab446cca21e7ab34d674e
\ No newline at end of file
+d955666dec2c8edb9691bf50d8e9dbb56c03f677
\ No newline at end of file
diff --git a/mojom/mojom_parser/formatter/formatter_test.go b/mojom/mojom_parser/formatter/formatter_test.go
index 3ccd06e..df89871 100644
--- a/mojom/mojom_parser/formatter/formatter_test.go
+++ b/mojom/mojom_parser/formatter/formatter_test.go
@@ -67,12 +67,9 @@
 	method3();
 	method4() => (Foo bar);
 	method5(int8 p1 /* p1 comment */, int16 p2);  // method comment
-	method6(int8 p1, int16 p2)
-	    => (int8 p1, int16 p2);
-	method7(int8 p1,
-          int16 p2,
-          int16 p3)
-	    => (int8 p1, int16 p2);
+	method6(WayTooLongAndReallyLongFactoryVisitoryFactory field1,
+	        WayTooLongAndReallyLongFactoryVisitoryFactory field2)
+	    => (int8 alpha);
 };
 
 // Final Comments.
diff --git a/mojom/mojom_parser/formatter/printer.go b/mojom/mojom_parser/formatter/printer.go
index ea5b70f..16c58fb8b 100644
--- a/mojom/mojom_parser/formatter/printer.go
+++ b/mojom/mojom_parser/formatter/printer.go
@@ -39,13 +39,19 @@
 	// eolComment is the comment to be printed at the end of the current line.
 	eolComment *lexer.Token
 
-	// lineLength is the number of runes that have been written to the current line.
-	lineLength int
+	// linePos is the number of runes that have been written to the current line.
+	linePos int
+
+	// maxLineLength is the maximum number of runes that should be printed on a line.
+	// A negative maxLineLength indicates no maximum line length should be enforced.
+	// This is currently only used to decide when to break up a method on different lines.
+	maxLineLength int
 }
 
 // newPrinter is a constructor for printer.
 func newPrinter() (p *printer) {
 	p = new(printer)
+	p.maxLineLength = 100
 	return
 }
 
@@ -174,20 +180,24 @@
 }
 
 func (p *printer) writeMojomMethod(mojomMethod *mojom.MojomMethod) {
+	splitResponse := false
+	if p.maxLineLength > 0 {
+		scratch := newPrinter()
+		scratch.maxLineLength = -1
+		scratch.writeMojomMethod(mojomMethod)
+		if len(scratch.result())+p.lineLength() > p.maxLineLength {
+			splitResponse = true
+		}
+	}
+
 	p.write(mojomMethod.NameToken().Text)
 	if mojomMethod.DeclaredOrdinal() >= 0 {
 		p.writef("@%v", mojomMethod.DeclaredOrdinal())
 	}
 
-	totalParams := len(mojomMethod.Parameters.Fields)
-	if mojomMethod.ResponseParameters != nil {
-		totalParams += len(mojomMethod.ResponseParameters.Fields)
-	}
-
 	p.writeMethodParams(mojomMethod.Parameters)
 	if mojomMethod.ResponseParameters != nil {
-		// TODO(azani): Actually enforce a line length limit instead.
-		if totalParams > 2 {
+		if splitResponse {
 			p.nl()
 			p.write("   ")
 		}
@@ -201,12 +211,19 @@
 // writeMethodParams writes the pretty-printed method parameters represented by
 // a MojomStruct.
 func (p *printer) writeMethodParams(params *mojom.MojomStruct) {
+	ownLine := false
+	if p.maxLineLength > 0 {
+		scratch := newPrinter()
+		scratch.maxLineLength = -1
+		scratch.writeMethodParams(params)
+		if len(scratch.result())+p.lineLength() > p.maxLineLength {
+			ownLine = true
+		}
+	}
 	p.write("(")
 	declaredObjects := params.GetDeclaredObjects()
 
-	// TODO(azani): Actually enforce a line length limit instead.
-	ownLine := len(declaredObjects) > 2
-	extraIndent := p.lineLength - p.indentSize
+	extraIndent := p.lineLength() - p.indentSize
 	if ownLine {
 		p.indentSize += extraIndent
 	}
@@ -617,11 +634,11 @@
 	}
 
 	// We only print the indentation if the line is not empty.
-	if p.lineLength == 0 {
+	if p.linePos == 0 {
 		p.buffer.WriteString(strings.Repeat(" ", p.indentSize))
-		p.lineLength += p.indentSize
+		p.linePos += p.indentSize
 	}
-	p.lineLength += len(s)
+	p.linePos += len(s)
 	p.buffer.WriteString(s)
 }
 
@@ -635,7 +652,7 @@
 	}
 
 	p.buffer.WriteString("\n")
-	p.lineLength = 0
+	p.linePos = 0
 }
 
 func (p *printer) incIndent() {
@@ -665,6 +682,17 @@
 	p.eolComment = &comment
 }
 
+// lineLength returns the length of the line so far. If nothing has been written
+// to the line yet, it returns the indent size.
+// The purpose of lineLenght is to answer the question: If I write something now
+// how far on the current line will it be written?
+func (p *printer) lineLength() int {
+	if p.linePos == 0 {
+		return p.indentSize
+	}
+	return p.linePos
+}
+
 // Standalone utilities that do not operate on the buffer.
 
 // isNewBlock determines if an empty line should be printed above the element
diff --git a/mojom/mojom_parser/formatter/printer_test.go b/mojom/mojom_parser/formatter/printer_test.go
index a96309b..77877cf 100644
--- a/mojom/mojom_parser/formatter/printer_test.go
+++ b/mojom/mojom_parser/formatter/printer_test.go
@@ -397,5 +397,5 @@
 
 	p := getNewPrinter()
 	p.writeDeclaredObject(mojomMethod)
-	checkEq(t, "method_foo(int8 param1, int16 param2)\n    => (int32 rparam1, int64 rparam2);", p.result())
+	checkEq(t, "method_foo(int8 param1, int16 param2) => (int32 rparam1, int64 rparam2);", p.result())
 }