11std::string format_header(
const std::string &file_path, SourcePosition pos,
const std::string &message)
13 if (!file_path.empty()) {
14 return std::format(
"{}:{}:{}: {}", file_path, pos.line, pos.column, message);
16 return std::format(
"{}:{}: {}", pos.line, pos.column, message);
22 gsl::not_null<
const std::vector<std::string> *> file_lines,
23 gsl::not_null<const TextFormatter *> format,
24 std::string file_path)
25 : file_lines_{file_lines.get()}, format_{format.get()}, file_path_{std::move(file_path)}
31 std::vector<std::string> lines;
34 lines.push_back(format_header(file_path_, pos, message));
37 if (!file_lines_->empty() && pos.
line > 0 && pos.
line <= file_lines_->size()) {
39 const std::size_t line_idx = pos.
line - 1;
40 const std::string &target_line = (*file_lines_)[line_idx];
45 if (pos.
column > 0 && pos.
column <= target_line.size()) {
47 const std::size_t col_idx = pos.
column - 1;
48 auto highlight_lines = printer.print(*file_lines_, line_idx, col_idx);
49 for (
auto &hl : highlight_lines) {
50 lines.push_back(std::move(hl));
53 else if (!target_line.empty()) {
55 auto highlight_lines = printer.print(*file_lines_, std::vector{line_idx});
56 for (
auto &hl : highlight_lines) {
57 lines.push_back(std::move(hl));
62 auto highlight_lines = printer.print(*file_lines_, std::vector{line_idx});
63 for (
auto &hl : highlight_lines) {
64 lines.push_back(std::move(hl));
CParserContext(gsl::not_null< const std::vector< std::string > * > file_lines, gsl::not_null< const TextFormatter * > format, std::string file_path="")
Constructs a parser context with formatting dependencies.
FormattableError make_error(SourcePosition pos, const std::string &message) const
Creates a FormattableError with source context.
const TextFormatter * formatter() const
Returns the text formatter used by this context.
const std::string & file_path() const
Returns the file path associated with this context.
const std::vector< std::string > * file_lines() const
Returns the file lines held by this context.
A service for printing file lines with highlighted lines and line numbers.
Abstract base class for applying text styling with context-aware formatting.
Represents a position within source content.
std::size_t line
1-based line number
std::size_t column
1-based column number