Add -Lformat option to format .hal files

Formats .hal files.

Bug: 137553653
Test: hidl-gen -Lformat <IFACE>
Test: aosp/1051701
Test: hidl-gen -Lformat (google-interfaces) & hidl-gen -Lcheck
(google-interfaces)
Change-Id: I5aa554d99fd4977acb7939d8c793081680a3f598
diff --git a/Interface.cpp b/Interface.cpp
index b6d2f38..ff33972 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -822,6 +822,31 @@
     }
 }
 
+void Interface::emitHidlDefinition(Formatter& out) const {
+    if (getDocComment() != nullptr) getDocComment()->emit(out);
+    out << typeName() << " ";
+
+    const Interface* super = superType();
+    if (super != nullptr && !super->isIBase()) {
+        out << "extends " << super->fqName().getRelativeFQName(fqName()) << " ";
+    }
+
+    out << "{\n";
+
+    out.indent([&] {
+        const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
+        out.join(definedTypes.begin(), definedTypes.end(), "\n",
+                 [&](auto t) { t->emitHidlDefinition(out); });
+
+        if (definedTypes.size() > 0 && userDefinedMethods().size() > 0) out << "\n";
+
+        out.join(userDefinedMethods().begin(), userDefinedMethods().end(), "\n",
+                 [&](auto method) { method->emitHidlDefinition(out); });
+    });
+
+    out << "};\n";
+}
+
 void Interface::emitPackageTypeDeclarations(Formatter& out) const {
     Scope::emitPackageTypeDeclarations(out);