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