Change grouping for emitting Method signatures

This changes the grouping of words in method signatures so that method
signatures wrap correctly.

Bug: 137553653
Test: Visual
Test: hidl-gen -Lformat (all interfaces) && hidl-gen -Lcheck (all
interfaces)
Change-Id: I321eca2b3d24f2c2cebaf202397b08c8df56dc97
diff --git a/Method.cpp b/Method.cpp
index 797e503..9717beb 100644
--- a/Method.cpp
+++ b/Method.cpp
@@ -19,6 +19,7 @@
 #include "Annotation.h"
 #include "ConstantExpression.h"
 #include "FormattingConstants.h"
+#include "Reference.h"
 #include "ScalarType.h"
 #include "Type.h"
 
@@ -268,19 +269,19 @@
 }
 
 static void fillHidlArgResultTokens(const std::vector<NamedReference<Type>*>& args,
-                                    WrappedOutput* wrappedOutput) {
-    for (auto iter = args.begin(); iter != args.end(); ++iter) {
-        auto arg = *iter;
+                                    WrappedOutput* wrappedOutput, const std::string& attachToLast) {
+    for (size_t i = 0; i < args.size(); i++) {
+        const NamedReference<Type>* arg = args[i];
         std::string out = arg->localName() + " " + arg->name();
-        if (iter != args.begin()) {
-            *wrappedOutput << ",";
-            wrappedOutput->group([&] {
-                wrappedOutput->printUnlessWrapped(" ");
-                *wrappedOutput << out;
-            });
-        } else {
-            wrappedOutput->group([&] { *wrappedOutput << out; });
-        }
+        wrappedOutput->group([&] {
+            if (i != 0) wrappedOutput->printUnlessWrapped(" ");
+            *wrappedOutput << out;
+            if (i == args.size() - 1) {
+                if (!attachToLast.empty()) *wrappedOutput << attachToLast;
+            } else {
+                *wrappedOutput << ",";
+            }
+        });
     }
 }
 
@@ -296,21 +297,20 @@
     if (isOneway()) wrappedOutput << "oneway ";
     wrappedOutput << name() << "(";
 
-    wrappedOutput.group([&] { fillHidlArgResultTokens(args(), &wrappedOutput); });
-
-    wrappedOutput << ")";
+    if (!args().empty()) {
+        fillHidlArgResultTokens(args(), &wrappedOutput, results().empty() ? ");\n" : ")");
+    } else {
+        wrappedOutput << (results().empty() ? ");\n" : ")");
+    }
 
     if (!results().empty()) {
         wrappedOutput.group([&] {
             wrappedOutput.printUnlessWrapped(" ");
             wrappedOutput << "generates (";
-            fillHidlArgResultTokens(results(), &wrappedOutput);
-            wrappedOutput << ")";
+            fillHidlArgResultTokens(results(), &wrappedOutput, ");\n");
         });
     }
 
-    wrappedOutput << ";\n";
-
     out << wrappedOutput;
 }