Format.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "Format.h"
  9. #include <AK/Assertions.h>
  10. #include <AK/DeprecatedString.h>
  11. #include <AK/Stream.h>
  12. #include <AK/StringBuilder.h>
  13. #include <AK/Vector.h>
  14. namespace Diff {
  15. DeprecatedString generate_only_additions(StringView text)
  16. {
  17. auto lines = text.split_view('\n', SplitBehavior::KeepEmpty);
  18. StringBuilder builder;
  19. builder.appendff("@@ -0,0 +1,{} @@\n", lines.size());
  20. for (auto const& line : lines) {
  21. builder.appendff("+{}\n", line);
  22. }
  23. return builder.to_deprecated_string();
  24. }
  25. ErrorOr<void> write_unified_header(StringView old_path, StringView new_path, Stream& stream)
  26. {
  27. TRY(stream.write_formatted("--- {}\n", old_path));
  28. TRY(stream.write_formatted("+++ {}\n", new_path));
  29. return {};
  30. }
  31. ErrorOr<void> write_unified(Hunk const& hunk, Stream& stream, ColorOutput color_output)
  32. {
  33. TRY(stream.write_formatted("{}\n", hunk.location));
  34. if (color_output == ColorOutput::Yes) {
  35. for (auto const& line : hunk.lines) {
  36. if (line.operation == Line::Operation::Addition)
  37. TRY(stream.write_formatted("\033[32;1m{}\033[0m\n", line));
  38. else if (line.operation == Line::Operation::Removal)
  39. TRY(stream.write_formatted("\033[31;1m{}\033[0m\n", line));
  40. else
  41. TRY(stream.write_formatted("{}\n", line));
  42. }
  43. } else {
  44. for (auto const& line : hunk.lines)
  45. TRY(stream.write_formatted("{}\n", line));
  46. }
  47. return {};
  48. }
  49. ErrorOr<void> write_normal(Hunk const& hunk, Stream& stream, ColorOutput color_output)
  50. {
  51. // Source line(s)
  52. TRY(stream.write_formatted("{}", hunk.location.old_range.start_line));
  53. if (hunk.location.old_range.number_of_lines > 1)
  54. TRY(stream.write_formatted(",{}", (hunk.location.old_range.start_line + hunk.location.old_range.number_of_lines - 1)));
  55. // Action
  56. if (hunk.location.old_range.number_of_lines > 0 && hunk.location.new_range.number_of_lines > 0)
  57. TRY(stream.write_formatted("c"));
  58. else if (hunk.location.new_range.number_of_lines > 0)
  59. TRY(stream.write_formatted("a"));
  60. else
  61. TRY(stream.write_formatted("d"));
  62. // Target line(s)
  63. TRY(stream.write_formatted("{}", hunk.location.new_range.start_line));
  64. if (hunk.location.new_range.number_of_lines > 1)
  65. TRY(stream.write_formatted(",{}", (hunk.location.new_range.start_line + hunk.location.new_range.number_of_lines - 1)));
  66. TRY(stream.write_formatted("\n"));
  67. for (auto const& line : hunk.lines) {
  68. VERIFY(line.operation == Line::Operation::Removal || line.operation == Line::Operation::Addition);
  69. if (line.operation == Line::Operation::Addition) {
  70. if (color_output == ColorOutput::Yes)
  71. TRY(stream.write_formatted("\033[32;1m> {}\033[0m\n", line.content));
  72. else
  73. TRY(stream.write_formatted("> {}\n", line.content));
  74. } else {
  75. if (color_output == ColorOutput::Yes)
  76. TRY(stream.write_formatted("\033[31;1m< {}\033[0m\n", line.content));
  77. else
  78. TRY(stream.write_formatted("< {}\n", line.content));
  79. }
  80. }
  81. return {};
  82. }
  83. struct SplitLines {
  84. Vector<Line> old_lines;
  85. Vector<Line> new_lines;
  86. };
  87. static ErrorOr<SplitLines> split_hunk_into_old_and_new_lines(Hunk const& hunk)
  88. {
  89. size_t new_lines_last_context = 0;
  90. size_t old_lines_last_context = 0;
  91. SplitLines lines;
  92. auto operation = Line::Operation::Context;
  93. bool is_all_insertions = true;
  94. bool is_all_deletions = true;
  95. auto check_if_line_is_a_change = [&](Line::Operation op) {
  96. if (operation != op) {
  97. // We've switched from additions to removals or vice-versa.
  98. // All lines starting from the last context line we saw must be changes.
  99. operation = Line::Operation::Change;
  100. for (size_t i = new_lines_last_context; i < lines.new_lines.size(); ++i)
  101. lines.new_lines[i].operation = Line::Operation::Change;
  102. for (size_t i = old_lines_last_context; i < lines.old_lines.size(); ++i)
  103. lines.old_lines[i].operation = Line::Operation::Change;
  104. }
  105. };
  106. for (auto const& line : hunk.lines) {
  107. switch (line.operation) {
  108. case Line::Operation::Context:
  109. VERIFY(lines.old_lines.size() < hunk.location.old_range.number_of_lines);
  110. VERIFY(lines.new_lines.size() < hunk.location.new_range.number_of_lines);
  111. operation = Line::Operation::Context;
  112. TRY(lines.new_lines.try_append(Line { operation, line.content }));
  113. TRY(lines.old_lines.try_append(Line { operation, line.content }));
  114. new_lines_last_context = lines.new_lines.size();
  115. old_lines_last_context = lines.old_lines.size();
  116. break;
  117. case Line::Operation::Addition:
  118. VERIFY(lines.new_lines.size() < hunk.location.new_range.number_of_lines);
  119. if (operation != Line::Operation::Context)
  120. check_if_line_is_a_change(Line::Operation::Addition);
  121. else
  122. operation = Line::Operation::Addition;
  123. TRY(lines.new_lines.try_append(Line { operation, line.content }));
  124. is_all_deletions = false;
  125. break;
  126. case Line::Operation::Removal:
  127. VERIFY(lines.old_lines.size() < hunk.location.old_range.number_of_lines);
  128. if (operation != Line::Operation::Context)
  129. check_if_line_is_a_change(Line::Operation::Removal);
  130. else
  131. operation = Line::Operation::Removal;
  132. TRY(lines.old_lines.try_append(Line { operation, line.content }));
  133. is_all_insertions = false;
  134. break;
  135. default:
  136. VERIFY_NOT_REACHED();
  137. }
  138. }
  139. VERIFY(lines.new_lines.size() == hunk.location.new_range.number_of_lines && lines.old_lines.size() == hunk.location.old_range.number_of_lines);
  140. if (is_all_insertions)
  141. lines.old_lines.clear();
  142. else if (is_all_deletions)
  143. lines.new_lines.clear();
  144. return lines;
  145. }
  146. static ErrorOr<void> write_hunk_as_context(Vector<Line> const& old_lines, Vector<Line> const& new_lines, HunkLocation const& location, Stream& stream, ColorOutput color_output)
  147. {
  148. TRY(stream.write_formatted("*** {}", location.old_range.start_line));
  149. if (location.old_range.number_of_lines > 1)
  150. TRY(stream.write_formatted(",{}", location.old_range.start_line + location.old_range.number_of_lines - 1));
  151. TRY(stream.write_formatted(" ****\n"));
  152. for (auto const& line : old_lines) {
  153. if (color_output == ColorOutput::Yes && (line.operation == Line::Operation::Removal || line.operation == Line::Operation::Change))
  154. TRY(stream.write_formatted("\033[31;1m{} {}\033[0m\n", line.operation, line.content));
  155. else
  156. TRY(stream.write_formatted("{} {}\n", line.operation, line.content));
  157. }
  158. TRY(stream.write_formatted("--- {}", location.new_range.start_line));
  159. if (location.new_range.number_of_lines > 1)
  160. TRY(stream.write_formatted(",{}", location.new_range.start_line + location.new_range.number_of_lines - 1));
  161. TRY(stream.write_formatted(" ----\n"));
  162. for (auto const& line : new_lines) {
  163. if (color_output == ColorOutput::Yes && (line.operation == Line::Operation::Addition || line.operation == Line::Operation::Change))
  164. TRY(stream.write_formatted("\033[32;1m{} {}\033[0m\n", line.operation, line.content));
  165. else
  166. TRY(stream.write_formatted("{} {}\n", line.operation, line.content));
  167. }
  168. return {};
  169. }
  170. ErrorOr<void> write_context(Hunk const& hunk, Stream& stream, ColorOutput color_output)
  171. {
  172. auto const split_lines = TRY(split_hunk_into_old_and_new_lines(hunk));
  173. return write_hunk_as_context(split_lines.old_lines, split_lines.new_lines, hunk.location, stream, color_output);
  174. }
  175. ErrorOr<void> write_context_header(StringView old_path, StringView new_path, Stream& stream)
  176. {
  177. TRY(stream.write_formatted("*** {}\n", old_path));
  178. TRY(stream.write_formatted("--- {}\n", new_path));
  179. return stream.write_formatted("***************\n");
  180. }
  181. }