-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathPrettyPrinting.cpp
More file actions
193 lines (167 loc) · 6.35 KB
/
PrettyPrinting.cpp
File metadata and controls
193 lines (167 loc) · 6.35 KB
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
#include <Ark/Error/PrettyPrinting.hpp>
#include <Ark/Constants.hpp>
#include <Ark/Utils/Files.hpp>
#include <Ark/Utils/Utils.hpp>
#include <array>
#include <cassert>
#include <fmt/color.h>
#include <fmt/ostream.h>
namespace Ark::Diagnostics
{
[[nodiscard]] bool isPairableChar(const char c)
{
return c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}';
}
void colorizeLine(const std::string& line, LineColorContextCounts& ctx, std::ostream& os)
{
// clang-format off
constexpr std::array pairing_color {
fmt::color::light_blue,
fmt::color::light_green,
fmt::color::light_salmon,
fmt::color::light_yellow,
fmt::color::light_cyan,
fmt::color::light_coral
};
// clang-format on
constexpr std::size_t pairing_color_size = pairing_color.size();
for (const char c : line)
{
if (isPairableChar(c))
{
int idx = 0;
switch (c)
{
case '(':
idx = ctx.open_parentheses;
ctx.open_parentheses++;
break;
case ')':
ctx.open_parentheses--;
idx = ctx.open_parentheses;
break;
case '[':
idx = ctx.open_square_braces;
ctx.open_square_braces++;
break;
case ']':
ctx.open_square_braces--;
idx = ctx.open_square_braces;
break;
case '{':
idx = ctx.open_curly_braces;
ctx.open_curly_braces++;
break;
case '}':
ctx.open_curly_braces--;
idx = ctx.open_curly_braces;
break;
default:
break;
}
const std::size_t pairing_color_index = static_cast<std::size_t>(std::abs(idx)) % pairing_color_size;
fmt::print(os, "{}", fmt::styled(c, fmt::fg(pairing_color[pairing_color_index])));
}
else
fmt::print(os, "{}", c);
}
}
Printer::Printer(
const std::string& filename, const std::size_t target_line,
const std::optional<std::size_t> end_target_line, const bool colorize,
const std::optional<std::string>& maybe_content) :
m_should_colorize(colorize)
{
const std::string code = filename == ARK_NO_NAME_FILE ? maybe_content.value_or("") : Utils::readFile(filename);
m_source = Utils::splitString(code, '\n');
m_window = Window(target_line, end_target_line.value_or(target_line), m_source.size());
m_current_line = m_window.start;
}
std::string Printer::sliceCode(const internal::FilePos start, const std::optional<internal::FilePos>& end) const
{
std::string code;
if (!end)
{
code = m_source[start.line];
Utils::ltrim(Utils::rtrim(code));
}
else
{
if (start.line == end->line)
code = m_source[start.line].substr(start.column, end->column);
else
{
code = m_source[start.line].substr(start.column);
if (end->line - start.line > 1)
{
for (std::size_t i = start.line + 1; i <= end->line - 1; ++i)
{
code += "\n";
code += m_source[i];
}
}
code += "\n" + m_source[end->line].substr(0, end->column);
}
}
return code;
}
void Printer::extendWindow(const std::size_t line_to_include)
{
// showing the context will require an ellipsis, to avoid showing too many lines in the error message
if (line_to_include + 3 < m_window.start)
m_window.skip_start_at = line_to_include + 3;
m_window.resume_at = m_window.start;
assert(line_to_include <= m_window.start && "line_to_include has to be before the start of our base context, source of errors are always before our errors");
// due to how context works, if it points to the same file,
// we are guaranteed it will be before our error
m_window.start = line_to_include >= 3 ? line_to_include - 3 : 0;
m_current_line = m_window.start;
}
void Printer::printLine(std::ostream& os)
{
if (!hasContent())
return;
if (m_window.hasSkip() &&
m_current_line >= m_window.skip_start_at.value() &&
m_current_line < m_window.resume_at.value())
{
// do not print the current line, we want to skip 1 or more lines
++m_current_line;
return;
}
// show current line with its number
fmt::print(os, "{: >5} |{}", m_current_line + 1, !m_source[m_current_line].empty() ? " " : "");
if (m_should_colorize)
colorizeLine(m_source[m_current_line], m_color_ctx, os);
else
fmt::print(os, "{}", m_source[m_current_line]);
fmt::print(os, "\n");
++m_current_line;
// if skip_start_at is equal to the next line to print, and we have to skip,
// display an ellipsis
if (m_window.skip_start_at &&
m_current_line == m_window.skip_start_at.value())
fmt::print(os, " ... |\n");
}
bool Printer::isTargetLine() const
{
return m_window.target + 1 <= m_current_line && m_current_line <= m_window.target_end + 1;
}
bool Printer::isNextLineTheFirstLineOfTarget() const
{
return m_window.target == m_current_line;
}
bool Printer::isLastLineOfTarget() const
{
return m_window.target != m_window.target_end &&
m_current_line == m_window.target_end + 1;
}
bool Printer::hasContent() const
{
return m_current_line < m_window.end && !m_source.empty() && m_window.target < m_window.end;
}
bool Printer::coversLine(const std::size_t line_number) const
{
return m_window.start <= line_number && line_number < m_window.end;
}
}