-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCodeFrame.cpp
More file actions
263 lines (217 loc) · 6.95 KB
/
Copy pathCodeFrame.cpp
File metadata and controls
263 lines (217 loc) · 6.95 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
*
* @file CodeFrame.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/errors/CodeFrame.hpp>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <vix/cli/Style.hpp>
using namespace vix::cli::style;
namespace vix::cli::errors
{
static bool readAllLines(const std::string &file, std::vector<std::string> &out)
{
std::ifstream in(file);
if (!in.is_open())
return false;
std::string line;
while (std::getline(in, line))
out.push_back(line);
return true;
}
static std::string expandTabs(const std::string &s, int tabWidth)
{
if (tabWidth <= 0)
tabWidth = 4;
std::string out;
out.reserve(s.size());
int col = 0;
for (char ch : s)
{
if (ch == '\t')
{
int spaces = tabWidth - (col % tabWidth);
out.append(static_cast<std::size_t>(spaces), ' ');
col += spaces;
}
else
{
out.push_back(ch);
col += 1;
}
}
return out;
}
static std::string makeCaretLine(int caretCol1Based, int tabWidth, const std::string &originalLine)
{
std::string expanded = expandTabs(originalLine, tabWidth);
int col = std::max(1, caretCol1Based);
int maxPos = static_cast<int>(expanded.size()) + 1;
col = std::min(col, maxPos);
std::string caret;
caret.reserve(static_cast<std::size_t>(col));
caret.append(static_cast<std::size_t>(col - 1), ' ');
caret.push_back('^');
return caret;
}
static void printTruncatedLineWithPrefix(
const std::string &line,
const std::string &caret,
int maxWidth,
const std::string &prefix,
const std::string &lineColor,
const std::string &caretColor,
const std::string &resetColor)
{
auto caretPos = [](const std::string &c) -> int
{
int pos = 0;
for (char ch : c)
{
if (ch == '^')
break;
++pos;
}
return pos;
};
// No truncation needed
if (maxWidth <= 0 || static_cast<int>(line.size()) <= maxWidth)
{
std::cerr << lineColor << prefix << line << resetColor << "\n";
std::cerr << std::string(prefix.size(), ' ')
<< caretColor << caret << resetColor << "\n";
return;
}
const int caretP = caretPos(caret);
// Window [start, end)
const int half = maxWidth / 2;
int start = std::max(0, caretP - half);
int end = std::min(static_cast<int>(line.size()), start + maxWidth);
start = std::max(0, end - maxWidth);
std::string slice = line.substr(
static_cast<std::size_t>(start),
static_cast<std::size_t>(end - start));
// caret inside slice
int caretInSlice = caretP - start;
caretInSlice = std::max(0, std::min(caretInSlice, maxWidth - 1));
std::string caretSlice;
caretSlice.reserve(static_cast<std::size_t>(maxWidth));
caretSlice.append(static_cast<std::size_t>(caretInSlice), ' ');
caretSlice.push_back('^');
const bool leftCut = (start > 0);
const bool rightCut = (end < static_cast<int>(line.size()));
if (leftCut && !slice.empty())
slice = "…" + slice.substr(1);
if (rightCut && !slice.empty())
slice = slice.substr(0, slice.size() - 1) + "…";
std::cerr << lineColor << prefix << slice << resetColor << "\n";
std::cerr << std::string(prefix.size(), ' ')
<< caretColor << caretSlice << resetColor << "\n";
}
static bool isContextNoiseLine(const std::string &s)
{
// trim spaces/tabs
std::size_t i = 0;
while (i < s.size() && (s[i] == ' ' || s[i] == '\t' || s[i] == '\r'))
++i;
std::size_t j = s.size();
while (j > i && (s[j - 1] == ' ' || s[j - 1] == '\t' || s[j - 1] == '\r'))
--j;
if (j <= i)
return true; // empty/whitespace
const std::string_view t(&s[i], j - i);
// treat only braces as noise in context
if (t == "{" || t == "}" || t == "};")
return true;
return false;
}
void printCodeFrame(
const CompilerError &err,
const ErrorContext &ctx,
const CodeFrameOptions &opt)
{
(void)ctx;
if (err.file.empty() || err.line <= 0)
return;
std::vector<std::string> lines;
if (!readAllLines(err.file, lines))
return;
const int n = static_cast<int>(lines.size());
if (err.line > n)
return;
int from = std::max(1, err.line - opt.contextLines);
int to = std::min(n, err.line + opt.contextLines);
// trim bottom noise lines (never trim the main line)
while (to > err.line && isContextNoiseLine(lines[static_cast<std::size_t>(to - 1)]))
--to;
// trim top noise lines (never trim the main line)
while (from < err.line && isContextNoiseLine(lines[static_cast<std::size_t>(from - 1)]))
++from;
const std::size_t width = std::to_string(to).size();
if (opt.leadingBlankLine)
{
std::cerr << "\n";
}
std::cerr << GRAY << "--> " << RESET
<< err.file << ":" << err.line << ":" << err.column << "\n";
std::cerr << GRAY << "code:" << RESET << "\n";
for (int ln = from; ln <= to; ++ln)
{
const bool isMain = (ln == err.line);
const std::string &rawLine = lines[static_cast<std::size_t>(ln - 1)];
const std::string expanded = expandTabs(rawLine, opt.tabWidth);
const std::string lnStr = std::to_string(ln);
const std::size_t pad = (width > lnStr.size()) ? (width - lnStr.size()) : 0;
std::ostringstream p;
p << " " << std::string(pad, ' ') << lnStr << " |";
const std::string prefixBase = p.str();
const bool addGap = !expanded.empty();
const std::string prefixPrint = addGap ? (prefixBase + " ") : prefixBase;
if (!isMain)
{
if (opt.maxLineWidth > 0 && static_cast<int>(expanded.size()) > opt.maxLineWidth)
{
const std::size_t maxW = static_cast<std::size_t>(opt.maxLineWidth);
std::string cropped;
if (maxW <= 1)
{
cropped = "…";
}
else
{
cropped = expanded.substr(0, maxW - 1);
cropped += "…";
}
if (cropped.empty())
std::cerr << GRAY << prefixPrint << RESET << "\n";
else
std::cerr << GRAY << prefixPrint << cropped << RESET << "\n";
}
else
{
// no crop needed
if (expanded.empty())
std::cerr << GRAY << prefixPrint << RESET << "\n";
else
std::cerr << GRAY << prefixPrint << expanded << RESET << "\n";
}
continue;
}
const std::string caret = makeCaretLine(err.column, opt.tabWidth, rawLine);
printTruncatedLineWithPrefix(expanded, caret, opt.maxLineWidth, prefixPrint, "", RED, RESET);
}
}
} // namespace vix::cli::errors