Debian Codegraph @

src/snippets/snippet.cpp

raw
1 #include "snippets/snippet.hpp"
2
3 #include <algorithm>
4 #include <cstdint>
5 #include <mutex>
6 #include <unordered_map>
7 #include <vector>
8 #include <fstream>
9 #include <iostream>
10
11 #include "utils/common.hpp"
12 #include "utils/microfmt.hpp"
13 #include "utils/utils.hpp"
14
15 CPPTRACE_BEGIN_NAMESPACE
16 namespace detail {
17 constexpr std::int64_t max_size = 1024 * 1024 * 10; // 10 MiB
18
19 struct line_range {
20 std::size_t begin;
21 std::size_t end; // one past the end
22 };
23
24 class snippet_manager {
25 bool loaded_contents;
26 std::string contents;
27 // 1-based indexing
28 std::vector<line_range> line_table;
29 public:
30 snippet_manager(const std::string& path) : loaded_contents(false) {
31 std::ifstream file;
32 try {
33 file.open(path, std::ios::ate);
34 if(file.is_open()) {
35 std::ifstream::pos_type size = file.tellg();
36 if(size == std::ifstream::pos_type(-1) || size > max_size) {
37 return;
38 }
39 // else load file
40 file.seekg(0, std::ios::beg);
41 contents.resize(to<std::size_t>(size));
42 if(!file.read(&contents[0], size)) {
43 // error ...
44 }
45 build_line_table();
46 loaded_contents = true;
47 }
48 } catch(const std::ifstream::failure&) {
49 // ...
50 }
51 }
52
53 // takes a 1-index line
54 std::string get_line(std::size_t line) const {
55 if(!loaded_contents || line > line_table.size()) {
56 return "";
57 } else {
58 return contents.substr(line_table[line].begin, line_table[line].end - line_table[line].begin);
59 }
60 }
61
62 std::size_t num_lines() const {
63 return line_table.size();
64 }
65
66 bool ok() const {
67 return loaded_contents;
68 }
69 private:
70 void build_line_table() {
71 line_table.push_back({0, 0});
72 std::size_t pos = 0; // stores the start of the current line
73 while(true) {
74 // find the end of the current line
75 std::size_t terminator_pos = contents.find('\n', pos);
76 if(terminator_pos == std::string::npos) {
77 line_table.push_back({pos, contents.size()});
78 break;
79 } else {
80 std::size_t end_pos = terminator_pos; // one past the end of the current line
81 if(end_pos > 0 && contents[end_pos - 1] == '\r') {
82 end_pos--;
83 }
84 line_table.push_back({pos, end_pos});
85 pos = terminator_pos + 1;
86 }
87 }
88 }
89 };
90
91 const snippet_manager& get_manager(const std::string& path) {
92 static std::mutex snippet_manager_mutex;
93 static std::unordered_map<std::string, const snippet_manager> snippet_managers;
94 std::unique_lock<std::mutex> lock(snippet_manager_mutex);
95 auto it = snippet_managers.find(path);
96 if(it == snippet_managers.end()) {
97 return snippet_managers.insert({path, snippet_manager(path)}).first->second;
98 } else {
99 return it->second;
100 }
101 }
102
103 // how wide the margin for the line number should be
104 constexpr std::size_t margin_width = 8;
105
106 struct snippet_context {
107 std::size_t original_begin;
108 std::size_t begin;
109 std::size_t end;
110 std::vector<std::string> lines;
111 };
112
113 optional<snippet_context> get_lines(const std::string& path, std::size_t target_line, std::size_t context_size) {
114 const auto& manager = get_manager(path);
115 if(!manager.ok()) {
116 return nullopt;
117 }
118 auto begin = target_line <= context_size + 1 ? 1 : target_line - context_size;
119 auto original_begin = begin;
120 auto end = std::min(target_line + context_size, manager.num_lines() - 1);
121 std::vector<std::string> lines;
122 for(auto line = begin; line <= end; line++) {
123 lines.push_back(manager.get_line(line));
124 }
125 // trim blank lines
126 while(begin < target_line && lines[begin - original_begin].empty()) {
127 begin++;
128 }
129 while(end > target_line && lines[end - original_begin].empty()) {
130 end--;
131 }
132 return snippet_context{original_begin, begin, end, std::move(lines)};
133 }
134
135 std::string write_line_number(std::size_t line, std::size_t target_line, bool color) {
136 std::string snippet;
137 auto line_str = std::to_string(line);
138 if(line == target_line) {
139 if(color) {
140 snippet += YELLOW;
141 }
142 auto line_width = line_str.size() + 3;
143 snippet += microfmt::format(
144 "{>{}} > {}: ",
145 line_width > margin_width ? 0 : margin_width - line_width,
146 "",
147 line_str
148 );
149 if(color) {
150 snippet += RESET;
151 }
152 } else {
153 snippet += microfmt::format("{>{}}: ", margin_width, line_str);
154 }
155 return snippet;
156 }
157
158 std::string write_carrot(std::uint32_t column, bool color) {
159 std::string snippet;
160 snippet += microfmt::format("\n{>{}}", margin_width + 2 + column - 1, "");
161 if(color) {
162 snippet += YELLOW;
163 }
164 snippet += "^";
165 if(color) {
166 snippet += RESET;
167 }
168 return snippet;
169 }
170
171 std::string write_line(
172 std::size_t line,
173 nullable<std::uint32_t> column,
174 std::size_t target_line,
175 bool color,
176 const snippet_context& context
177 ) {
178 std::string snippet;
179 snippet += write_line_number(line, target_line, color);
180 snippet += context.lines[line - context.original_begin];
181 if(line == target_line && column.has_value()) {
182 snippet += write_carrot(column.value(), color);
183 }
184 return snippet;
185 }
186
187 // 1-indexed line
188 std::string get_snippet(
189 const std::string& path,
190 std::size_t target_line,
191 nullable<std::uint32_t> column,
192 std::size_t context_size,
193 bool color
194 ) {
195 const auto context_res = get_lines(path, target_line, context_size);
196 if(!context_res) {
197 return "";
198 }
199 const auto& context = context_res.unwrap();
200 std::string snippet;
201 for(auto line = context.begin; line <= context.end; line++) {
202 snippet += write_line(line, column, target_line, color, context);
203 if(line != context.end) {
204 snippet += '\n';
205 }
206 }
207 return snippet;
208 }
209 }
210 CPPTRACE_END_NAMESPACE