Skip to content

Commit d3b51bc

Browse files
committed
Share Markdown source-range conversions
Task: 2026/10/29/share-markdown-source-range-conversions
1 parent e339e2f commit d3b51bc

4 files changed

Lines changed: 30 additions & 106 deletions

File tree

‎Telegram/SourceFiles/iv/markdown/iv_markdown_math.cpp‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -212,30 +212,6 @@ void OffsetToPosition(
212212
return cleaned;
213213
}
214214

215-
[[nodiscard]] SourceRange RangeForOffsets(
216-
const std::vector<int> &lineStarts,
217-
int sourceSize,
218-
int startOffset,
219-
int endOffset) {
220-
auto startLine = 0;
221-
auto startColumn = 0;
222-
auto endLine = 0;
223-
auto endColumn = 0;
224-
OffsetToPosition(lineStarts, startOffset, &startLine, &startColumn);
225-
OffsetToPosition(
226-
lineStarts,
227-
std::max(startOffset, endOffset - 1),
228-
&endLine,
229-
&endColumn);
230-
return RangeFromLineColumns(
231-
lineStarts,
232-
sourceSize,
233-
startLine,
234-
startColumn,
235-
endLine,
236-
endColumn);
237-
}
238-
239215
[[nodiscard]] bool ExceedsFormulaBytes(
240216
const QByteArray &content,
241217
int maxFormulaBytes) {
@@ -294,6 +270,30 @@ SourceRange RangeFromLineColumns(
294270
return result;
295271
}
296272

273+
SourceRange RangeForOffsets(
274+
const std::vector<int> &lineStarts,
275+
int sourceSize,
276+
int startOffset,
277+
int endOffset) {
278+
auto startLine = 0;
279+
auto startColumn = 0;
280+
auto endLine = 0;
281+
auto endColumn = 0;
282+
OffsetToPosition(lineStarts, startOffset, &startLine, &startColumn);
283+
OffsetToPosition(
284+
lineStarts,
285+
std::max(startOffset, endOffset - 1),
286+
&endLine,
287+
&endColumn);
288+
return RangeFromLineColumns(
289+
lineStarts,
290+
sourceSize,
291+
startLine,
292+
startColumn,
293+
endLine,
294+
endColumn);
295+
}
296+
297297
bool ExtractMathRegions(
298298
const QByteArray &source,
299299
const std::vector<bool> &mask,

‎Telegram/SourceFiles/iv/markdown/iv_markdown_math.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ struct MathScanBlock {
2828
int startColumn,
2929
int endLine,
3030
int endColumn);
31+
[[nodiscard]] SourceRange RangeForOffsets(
32+
const std::vector<int> &lineStarts,
33+
int sourceSize,
34+
int startOffset,
35+
int endOffset);
3136
[[nodiscard]] bool ExtractMathRegions(
3237
const QByteArray &source,
3338
const std::vector<bool> &mask,

‎Telegram/SourceFiles/iv/markdown/iv_markdown_parse_convert.cpp‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,12 @@ void AddWarning(ParserState *state, QString warning) {
101101
return raw.isEmpty() ? u"unknown"_q : raw;
102102
}
103103

104-
[[nodiscard]] SourceRange RangeFromCmarkLines(
105-
const std::vector<int> &lineStarts,
106-
int sourceSize,
107-
int startLine,
108-
int startColumn,
109-
int endLine,
110-
int endColumn) {
111-
auto result = SourceRange();
112-
result.startLine = startLine;
113-
result.startColumn = startColumn;
114-
result.endLine = endLine;
115-
result.endColumn = endColumn;
116-
const auto linesCount = static_cast<int>(lineStarts.size());
117-
if (startLine <= 0
118-
|| endLine <= 0
119-
|| startLine > linesCount
120-
|| endLine > linesCount) {
121-
return result;
122-
}
123-
const auto maxOffset = std::max(sourceSize, 0);
124-
const auto startOffset = lineStarts[startLine - 1]
125-
+ std::max(0, startColumn - 1);
126-
const auto endOffset = lineStarts[endLine - 1]
127-
+ std::max(0, endColumn);
128-
result.available = true;
129-
result.startOffset = std::clamp(startOffset, 0, maxOffset);
130-
result.endOffset = std::clamp(endOffset, 0, maxOffset);
131-
return result;
132-
}
133-
134104
[[nodiscard]] SourceRange NodeRange(
135105
cmark_node *node,
136106
const std::vector<int> &lineStarts,
137107
int sourceSize) {
138108
return node
139-
? RangeFromCmarkLines(
109+
? RangeFromLineColumns(
140110
lineStarts,
141111
sourceSize,
142112
cmark_node_get_start_line(node),

‎Telegram/SourceFiles/iv/markdown/iv_markdown_parse_finalize.cpp‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,6 @@ For license and copyright information please follow this link:
1717
namespace Iv::Markdown {
1818
namespace {
1919

20-
void OffsetToPosition(
21-
const std::vector<int> &lineStarts,
22-
int offset,
23-
int *line,
24-
int *column) {
25-
const auto clampedOffset = std::max(offset, 0);
26-
auto resultLine = 1;
27-
auto resultColumn = clampedOffset + 1;
28-
if (!lineStarts.empty()) {
29-
auto i = std::upper_bound(
30-
lineStarts.begin(),
31-
lineStarts.end(),
32-
clampedOffset);
33-
if (i != lineStarts.begin()) {
34-
--i;
35-
resultLine = static_cast<int>(i - lineStarts.begin()) + 1;
36-
resultColumn = clampedOffset - *i + 1;
37-
}
38-
}
39-
if (line) {
40-
*line = resultLine;
41-
}
42-
if (column) {
43-
*column = resultColumn;
44-
}
45-
}
46-
4720
[[nodiscard]] QString SourceSlice(
4821
const QByteArray &source,
4922
const SourceRange &range) {
@@ -57,30 +30,6 @@ void OffsetToPosition(
5730
return QString::fromUtf8(slice.constData(), slice.size());
5831
}
5932

60-
[[nodiscard]] SourceRange RangeForOffsets(
61-
const std::vector<int> &lineStarts,
62-
int sourceSize,
63-
int startOffset,
64-
int endOffset) {
65-
auto startLine = 0;
66-
auto startColumn = 0;
67-
auto endLine = 0;
68-
auto endColumn = 0;
69-
OffsetToPosition(lineStarts, startOffset, &startLine, &startColumn);
70-
OffsetToPosition(
71-
lineStarts,
72-
std::max(startOffset, endOffset - 1),
73-
&endLine,
74-
&endColumn);
75-
return RangeFromLineColumns(
76-
lineStarts,
77-
sourceSize,
78-
startLine,
79-
startColumn,
80-
endLine,
81-
endColumn);
82-
}
83-
8433
[[nodiscard]] QString PlainText(const MarkdownNode &node) {
8534
auto result = node.text;
8635
for (const auto &child : node.children) {

0 commit comments

Comments
 (0)