DiffViewer.cpp 7.8 KB

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