DiffViewer.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "DiffViewer.h"
  8. #include <AK/Debug.h>
  9. #include <LibDiff/Hunks.h>
  10. #include <LibGUI/AbstractView.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Scrollbar.h>
  13. #include <LibGfx/Color.h>
  14. #include <LibGfx/Font/FontDatabase.h>
  15. #include <LibGfx/Palette.h>
  16. namespace HackStudio {
  17. void DiffViewer::paint_event(GUI::PaintEvent& event)
  18. {
  19. GUI::Painter painter(*this);
  20. painter.add_clip_rect(widget_inner_rect());
  21. painter.add_clip_rect(event.rect());
  22. painter.fill_rect(event.rect(), palette().color(background_role()));
  23. painter.translate(frame_thickness(), frame_thickness());
  24. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  25. // Why we need to translate here again? We've already translated the painter.
  26. // Anyways, it paints correctly so I'll leave it like this.
  27. painter.fill_rect_with_dither_pattern(
  28. separator_rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()),
  29. Gfx::Color::LightGray,
  30. Gfx::Color::White);
  31. size_t y_offset = 10;
  32. size_t current_original_line_index = 0;
  33. for (auto const& hunk : m_hunks) {
  34. for (size_t i = current_original_line_index; i < hunk.location.old_range.start_line; ++i) {
  35. draw_line(painter, m_original_lines[i], y_offset, LinePosition::Both, LineType::Normal);
  36. y_offset += line_height();
  37. }
  38. current_original_line_index = hunk.location.old_range.start_line + hunk.location.old_range.number_of_lines;
  39. size_t left_y_offset = y_offset;
  40. for (auto const& line : hunk.lines) {
  41. if (line.operation != Diff::Line::Operation::Removal)
  42. continue;
  43. draw_line(painter, line.content, left_y_offset, LinePosition::Left, LineType::Diff);
  44. left_y_offset += line_height();
  45. }
  46. for (int i = 0; i < (int)hunk.location.new_range.number_of_lines - (int)hunk.location.old_range.number_of_lines; ++i) {
  47. draw_line(painter, ""sv, left_y_offset, LinePosition::Left, LineType::Missing);
  48. left_y_offset += line_height();
  49. }
  50. size_t right_y_offset = y_offset;
  51. for (auto const& line : hunk.lines) {
  52. if (line.operation != Diff::Line::Operation::Addition)
  53. continue;
  54. draw_line(painter, line.content, right_y_offset, LinePosition::Right, LineType::Diff);
  55. right_y_offset += line_height();
  56. }
  57. for (int i = 0; i < (int)hunk.location.old_range.number_of_lines - (int)hunk.location.new_range.number_of_lines; ++i) {
  58. draw_line(painter, ""sv, right_y_offset, LinePosition::Right, LineType::Missing);
  59. right_y_offset += line_height();
  60. }
  61. VERIFY(left_y_offset == right_y_offset);
  62. y_offset = left_y_offset;
  63. }
  64. for (size_t i = current_original_line_index; i < m_original_lines.size(); ++i) {
  65. draw_line(painter, m_original_lines[i], y_offset, LinePosition::Both, LineType::Normal);
  66. y_offset += line_height();
  67. }
  68. }
  69. void DiffViewer::draw_line(GUI::Painter& painter, StringView line, size_t y_offset, LinePosition line_position, LineType line_type)
  70. {
  71. size_t line_width = font().width(line);
  72. constexpr size_t padding = 10;
  73. size_t left_side_x_offset = padding;
  74. size_t right_side_x_offset = separator_rect().x() + padding;
  75. // FIXME: Long lines will overflow out of their side of the diff view
  76. Gfx::IntRect left_line_rect { (int)left_side_x_offset, (int)y_offset, (int)line_width, (int)line_height() };
  77. Gfx::IntRect right_line_rect { (int)right_side_x_offset, (int)y_offset, (int)line_width, (int)line_height() };
  78. auto color = palette().color(foreground_role());
  79. if (line_position == LinePosition::Left || line_position == LinePosition::Both) {
  80. painter.draw_text(left_line_rect, line, Gfx::TextAlignment::TopLeft, color);
  81. if (line_type != LineType::Normal) {
  82. Gfx::IntRect outline = { (int)left_side_x_offset, ((int)y_offset) - 2, separator_rect().x() - (int)(padding * 2), (int)line_height() };
  83. if (line_type == LineType::Diff) {
  84. painter.fill_rect(
  85. outline,
  86. red_background());
  87. }
  88. if (line_type == LineType::Missing) {
  89. painter.fill_rect(
  90. outline,
  91. gray_background());
  92. }
  93. }
  94. }
  95. if (line_position == LinePosition::Right || line_position == LinePosition::Both) {
  96. painter.draw_text(right_line_rect, line, Gfx::TextAlignment::TopLeft, color);
  97. if (line_type != LineType::Normal) {
  98. Gfx::IntRect outline = { (int)right_side_x_offset, ((int)y_offset) - 2, frame_inner_rect().width() - separator_rect().x() - (int)(padding * 2) - 10, (int)line_height() };
  99. if (line_type == LineType::Diff) {
  100. painter.fill_rect(
  101. outline,
  102. green_background());
  103. }
  104. if (line_type == LineType::Missing) {
  105. painter.fill_rect(
  106. outline,
  107. gray_background());
  108. }
  109. }
  110. }
  111. }
  112. size_t DiffViewer::line_height() const
  113. {
  114. return font().pixel_size_rounded_up() + 4;
  115. }
  116. Gfx::IntRect DiffViewer::separator_rect() const
  117. {
  118. return Gfx::IntRect { frame_inner_rect().width() / 2 - 2,
  119. 0,
  120. 4,
  121. frame_inner_rect().height() };
  122. }
  123. void DiffViewer::set_content(DeprecatedString const& original, DeprecatedString const& diff)
  124. {
  125. m_original_lines = split_to_lines(original);
  126. m_hunks = Diff::parse_hunks(diff).release_value_but_fixme_should_propagate_errors();
  127. if constexpr (DIFF_DEBUG) {
  128. for (size_t i = 0; i < m_original_lines.size(); ++i)
  129. dbgln("{}:{}", i, m_original_lines[i]);
  130. }
  131. }
  132. DiffViewer::DiffViewer()
  133. {
  134. setup_properties();
  135. }
  136. DiffViewer::DiffViewer(DeprecatedString const& original, DeprecatedString const& diff)
  137. : m_original_lines(split_to_lines(original))
  138. , m_hunks(Diff::parse_hunks(diff).release_value_but_fixme_should_propagate_errors())
  139. {
  140. setup_properties();
  141. }
  142. void DiffViewer::setup_properties()
  143. {
  144. set_font(Gfx::FontDatabase::default_fixed_width_font());
  145. set_background_role(ColorRole::Base);
  146. set_foreground_role(ColorRole::BaseText);
  147. }
  148. Vector<DeprecatedString> DiffViewer::split_to_lines(DeprecatedString const& text)
  149. {
  150. // NOTE: This is slightly different than text.split('\n')
  151. Vector<DeprecatedString> lines;
  152. size_t next_line_start_index = 0;
  153. for (size_t i = 0; i < text.length(); ++i) {
  154. if (text[i] == '\n') {
  155. auto line_text = text.substring(next_line_start_index, i - next_line_start_index);
  156. lines.append(move(line_text));
  157. next_line_start_index = i + 1;
  158. }
  159. }
  160. lines.append(text.substring(next_line_start_index, text.length() - next_line_start_index));
  161. return lines;
  162. }
  163. Gfx::Color DiffViewer::red_background()
  164. {
  165. static Gfx::Color color = Gfx::Color::from_argb(0x88ff0000);
  166. return color;
  167. }
  168. Gfx::Color DiffViewer::green_background()
  169. {
  170. static Gfx::Color color = Gfx::Color::from_argb(0x8800ff00);
  171. return color;
  172. }
  173. Gfx::Color DiffViewer::gray_background()
  174. {
  175. static Gfx::Color color = Gfx::Color::from_argb(0x88888888);
  176. return color;
  177. }
  178. void DiffViewer::update_content_size()
  179. {
  180. if (m_hunks.is_empty()) {
  181. set_content_size({ 0, 0 });
  182. return;
  183. }
  184. size_t num_lines = 0;
  185. size_t current_original_line_index = 0;
  186. for (auto const& hunk : m_hunks) {
  187. num_lines += (hunk.location.old_range.start_line - (int)current_original_line_index);
  188. num_lines += hunk.location.old_range.number_of_lines;
  189. if (hunk.location.new_range.number_of_lines > hunk.location.old_range.number_of_lines) {
  190. num_lines += ((int)hunk.location.new_range.number_of_lines - (int)hunk.location.old_range.number_of_lines);
  191. }
  192. current_original_line_index = hunk.location.old_range.start_line + hunk.location.old_range.number_of_lines;
  193. }
  194. num_lines += ((int)m_original_lines.size() - (int)current_original_line_index);
  195. // TODO: Support Horizontal scrolling
  196. set_content_size({ 0, (int)(num_lines * line_height()) });
  197. }
  198. void DiffViewer::resize_event(GUI::ResizeEvent& event)
  199. {
  200. AbstractScrollableWidget::resize_event(event);
  201. update_content_size();
  202. }
  203. }