GMLFormatter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonObject.h>
  27. #include <AK/JsonValue.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibGUI/GMLFormatter.h>
  30. #include <LibGUI/GMLParser.h>
  31. namespace GUI {
  32. static String format_gml_object(const JsonObject& node, size_t indentation = 0, bool is_inline = false)
  33. {
  34. StringBuilder builder;
  35. auto indent = [&builder](size_t indentation) {
  36. for (size_t i = 0; i < indentation; ++i)
  37. builder.append(" ");
  38. };
  39. struct Property {
  40. String key;
  41. JsonValue value;
  42. };
  43. Vector<Property> properties;
  44. node.for_each_member([&](auto& key, auto& value) {
  45. if (key != "class" && key != "layout" && key != "children")
  46. properties.append({ key, value });
  47. return IterationDecision::Continue;
  48. });
  49. if (!is_inline)
  50. indent(indentation);
  51. builder.append('@');
  52. builder.append(node.get("class").as_string());
  53. builder.append(" {\n");
  54. for (auto& property : properties) {
  55. indent(indentation + 1);
  56. builder.append(property.key);
  57. builder.append(": ");
  58. if (property.value.is_array()) {
  59. // custom array serialization as AK's doesn't pretty-print
  60. // objects and arrays (we only care about arrays (for now))
  61. builder.append("[");
  62. auto first = true;
  63. property.value.as_array().for_each([&](auto& value) {
  64. if (!first)
  65. builder.append(", ");
  66. first = false;
  67. value.serialize(builder);
  68. });
  69. builder.append("]");
  70. } else {
  71. property.value.serialize(builder);
  72. }
  73. builder.append("\n");
  74. }
  75. if (node.has("layout")) {
  76. auto layout = node.get("layout").as_object();
  77. if (!properties.is_empty())
  78. builder.append("\n");
  79. indent(indentation + 1);
  80. builder.append("layout: ");
  81. builder.append(format_gml_object(move(layout), indentation + 1, true));
  82. }
  83. if (node.has("children")) {
  84. auto children = node.get("children").as_array();
  85. auto first = properties.is_empty() && !node.has("layout");
  86. children.for_each([&](auto& value) {
  87. if (!first)
  88. builder.append("\n");
  89. first = false;
  90. builder.append(format_gml_object(value.as_object(), indentation + 1));
  91. });
  92. }
  93. indent(indentation);
  94. builder.append("}\n");
  95. return builder.to_string();
  96. }
  97. String format_gml(const StringView& string)
  98. {
  99. // FIXME: Preserve comments somehow, they're not contained
  100. // in the JSON object returned by parse_gml()
  101. auto ast = parse_gml(string);
  102. if (ast.is_null())
  103. return {};
  104. VERIFY(ast.is_object());
  105. return format_gml_object(ast.as_object());
  106. }
  107. }