DiffViewer.cpp 7.8 KB

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