DiffViewer.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DiffViewer.h"
  27. #include <LibDiff/Hunks.h>
  28. #include <LibGUI/AbstractView.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGUI/ScrollBar.h>
  31. #include <LibGfx/Color.h>
  32. #include <LibGfx/Font.h>
  33. #include <LibGfx/Palette.h>
  34. // #define DEBUG_DIFF
  35. namespace HackStudio {
  36. DiffViewer::~DiffViewer()
  37. {
  38. }
  39. void DiffViewer::paint_event(GUI::PaintEvent& event)
  40. {
  41. GUI::Painter painter(*this);
  42. painter.add_clip_rect(widget_inner_rect());
  43. painter.add_clip_rect(event.rect());
  44. painter.fill_rect(event.rect(), palette().color(background_role()));
  45. painter.translate(frame_thickness(), frame_thickness());
  46. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  47. // Why we need to translate here again? We've already translated the painter.
  48. // Anyways, it paints correctly so I'll leave it like this.
  49. painter.fill_rect_with_dither_pattern(
  50. separator_rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()),
  51. Gfx::Color::LightGray,
  52. Gfx::Color::White);
  53. size_t y_offset = 10;
  54. size_t current_original_line_index = 0;
  55. for (const auto& hunk : m_hunks) {
  56. for (size_t i = current_original_line_index; i < hunk.original_start_line; ++i) {
  57. draw_line(painter, m_original_lines[i], y_offset, LinePosition::Both, LineType::Normal);
  58. y_offset += line_height();
  59. }
  60. current_original_line_index = hunk.original_start_line + hunk.removed_lines.size();
  61. size_t left_y_offset = y_offset;
  62. for (const auto& removed_line : hunk.removed_lines) {
  63. draw_line(painter, removed_line, left_y_offset, LinePosition::Left, LineType::Diff);
  64. left_y_offset += line_height();
  65. }
  66. for (int i = 0; i < (int)hunk.added_lines.size() - (int)hunk.removed_lines.size(); ++i) {
  67. draw_line(painter, "", left_y_offset, LinePosition::Left, LineType::Missing);
  68. left_y_offset += line_height();
  69. }
  70. size_t right_y_offset = y_offset;
  71. for (const auto& added_line : hunk.added_lines) {
  72. draw_line(painter, added_line, right_y_offset, LinePosition::Right, LineType::Diff);
  73. right_y_offset += line_height();
  74. }
  75. for (int i = 0; i < (int)hunk.removed_lines.size() - (int)hunk.added_lines.size(); ++i) {
  76. draw_line(painter, "", right_y_offset, LinePosition::Right, LineType::Missing);
  77. right_y_offset += line_height();
  78. }
  79. ASSERT(left_y_offset == right_y_offset);
  80. y_offset = left_y_offset;
  81. }
  82. for (size_t i = current_original_line_index; i < m_original_lines.size(); ++i) {
  83. draw_line(painter, m_original_lines[i], y_offset, LinePosition::Both, LineType::Normal);
  84. y_offset += line_height();
  85. }
  86. }
  87. void DiffViewer::draw_line(GUI::Painter& painter, const String& line, size_t y_offset, LinePosition line_position, LineType line_type)
  88. {
  89. size_t line_width = font().width(line);
  90. constexpr size_t padding = 10;
  91. size_t left_side_x_offset = padding;
  92. size_t right_side_x_offset = separator_rect().x() + padding;
  93. // FIXME: Long lines will overflow out of their side of the diff view
  94. Gfx::IntRect left_line_rect { (int)left_side_x_offset, (int)y_offset, (int)line_width, (int)line_height() };
  95. Gfx::IntRect right_line_rect { (int)right_side_x_offset, (int)y_offset, (int)line_width, (int)line_height() };
  96. auto color = palette().color(foreground_role());
  97. if (line_position == LinePosition::Left || line_position == LinePosition::Both) {
  98. painter.draw_text(left_line_rect, line, Gfx::TextAlignment::TopLeft, color);
  99. if (line_type != LineType::Normal) {
  100. Gfx::IntRect outline = { (int)left_side_x_offset, ((int)y_offset) - 2, separator_rect().x() - (int)(padding * 2), (int)line_height() };
  101. if (line_type == LineType::Diff) {
  102. painter.fill_rect(
  103. outline,
  104. red_background());
  105. }
  106. if (line_type == LineType::Missing) {
  107. painter.fill_rect(
  108. outline,
  109. gray_background());
  110. }
  111. }
  112. }
  113. if (line_position == LinePosition::Right || line_position == LinePosition::Both) {
  114. painter.draw_text(right_line_rect, line, Gfx::TextAlignment::TopLeft, color);
  115. if (line_type != LineType::Normal) {
  116. 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() };
  117. if (line_type == LineType::Diff) {
  118. painter.fill_rect(
  119. outline,
  120. green_background());
  121. }
  122. if (line_type == LineType::Missing) {
  123. painter.fill_rect(
  124. outline,
  125. gray_background());
  126. }
  127. }
  128. }
  129. }
  130. size_t DiffViewer::line_height() const
  131. {
  132. return font().glyph_height() + 4;
  133. }
  134. Gfx::IntRect DiffViewer::separator_rect() const
  135. {
  136. return Gfx::IntRect { frame_inner_rect().width() / 2 - 2,
  137. 0,
  138. 4,
  139. frame_inner_rect().height() };
  140. }
  141. void DiffViewer::set_content(const String& original, const String& diff)
  142. {
  143. m_original_lines = split_to_lines(original);
  144. m_hunks = Diff::parse_hunks(diff);
  145. #ifdef DEBUG_DIFF
  146. for (size_t i = 0; i < m_original_lines.size(); ++i) {
  147. dbg() << i << ":" << m_original_lines[i];
  148. }
  149. #endif
  150. }
  151. DiffViewer::DiffViewer()
  152. {
  153. setup_properties();
  154. }
  155. DiffViewer::DiffViewer(const String& original, const String& diff)
  156. : m_original_lines(split_to_lines(original))
  157. , m_hunks(Diff::parse_hunks(diff))
  158. {
  159. setup_properties();
  160. }
  161. void DiffViewer::setup_properties()
  162. {
  163. set_font(Gfx::Font::default_fixed_width_font());
  164. set_background_role(ColorRole::Base);
  165. set_foreground_role(ColorRole::BaseText);
  166. }
  167. Vector<String> DiffViewer::split_to_lines(const String& text)
  168. {
  169. // NOTE: This is slightly different than text.split('\n')
  170. Vector<String> lines;
  171. size_t next_line_start_index = 0;
  172. for (size_t i = 0; i < text.length(); ++i) {
  173. if (text[i] == '\n') {
  174. auto line_text = text.substring(next_line_start_index, i - next_line_start_index);
  175. lines.append(move(line_text));
  176. next_line_start_index = i + 1;
  177. }
  178. }
  179. lines.append(text.substring(next_line_start_index, text.length() - next_line_start_index));
  180. return lines;
  181. }
  182. Gfx::Color DiffViewer::red_background()
  183. {
  184. static Gfx::Color color = Gfx::Color::from_rgba(0x88ff0000);
  185. return color;
  186. }
  187. Gfx::Color DiffViewer::green_background()
  188. {
  189. static Gfx::Color color = Gfx::Color::from_rgba(0x8800ff00);
  190. return color;
  191. }
  192. Gfx::Color DiffViewer::gray_background()
  193. {
  194. static Gfx::Color color = Gfx::Color::from_rgba(0x88888888);
  195. return color;
  196. }
  197. void DiffViewer::update_content_size()
  198. {
  199. if (m_hunks.is_empty()) {
  200. set_content_size({ 0, 0 });
  201. return;
  202. }
  203. size_t num_lines = 0;
  204. size_t current_original_line_index = 0;
  205. for (const auto& hunk : m_hunks) {
  206. num_lines += ((int)hunk.original_start_line - (int)current_original_line_index);
  207. num_lines += hunk.removed_lines.size();
  208. if (hunk.added_lines.size() > hunk.removed_lines.size()) {
  209. num_lines += ((int)hunk.added_lines.size() - (int)hunk.removed_lines.size());
  210. }
  211. current_original_line_index = hunk.original_start_line + hunk.removed_lines.size();
  212. }
  213. num_lines += ((int)m_original_lines.size() - (int)current_original_line_index);
  214. // TODO: Support Horizontal scrolling
  215. set_content_size({ 0, (int)(num_lines * line_height()) });
  216. }
  217. void DiffViewer::resize_event(GUI::ResizeEvent& event)
  218. {
  219. ScrollableWidget::resize_event(event);
  220. update_content_size();
  221. }
  222. }