DiffViewer.cpp 7.8 KB

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