Skip to content

Commit 9ddf78b

Browse files
fix line run positions from the middle of lines
1 parent 247b298 commit 9ddf78b

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

Sources/Markdown/Parser/BlockDirectiveParser.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,11 @@ private enum ParseContainer: CustomStringConvertible {
675675
// We need to keep track of what we removed because cmark will report different source locations than what we
676676
// had in the source. We'll adjust those when we get them back.
677677
let trimmedIndentationAndLines = lines.map { line -> (line: TrimmedLine,
678-
indentation: TrimmedLine.Lex?) in
678+
indentation: Int) in
679679
var trimmedLine = line
680680
let trimmedWhitespace = trimmedLine.lexWhitespace(maxLength: indentationColumnCount)
681-
return (trimmedLine, trimmedWhitespace)
681+
let indentation = (trimmedWhitespace?.text.count ?? 0) + line.untrimmedText.distance(from: line.untrimmedText.startIndex, to: line.parseIndex)
682+
return (trimmedLine, indentation)
682683
}
683684

684685
// Build the logical block of text that cmark will see.

Sources/Markdown/Parser/RangeAdjuster.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ struct RangeAdjuster: MarkupWalker {
1919

2020
/// An array of whitespace spans that were removed for each line, indexed
2121
/// by line number. `nil` means that no whitespace was removed on that line.
22-
var trimmedIndentationPerLine: [TrimmedLine.Lex?]
22+
var trimmedIndentationPerLine: [Int]
2323

2424
mutating func defaultVisit(_ markup: Markup) {
2525
/// This should only be used in the parser where ranges are guaranteed
2626
/// to be filled in from cmark.
2727
let adjustedRange = markup.range.map { range -> SourceRange in
2828
// Add back the offset to the column as if the indentation weren't stripped.
2929
let start = SourceLocation(line: startLine + range.lowerBound.line - 1,
30-
column: range.lowerBound.column + (trimmedIndentationPerLine[range.lowerBound.line - 1]?.text.count ?? 0),
30+
column: range.lowerBound.column + (trimmedIndentationPerLine[range.lowerBound.line - 1] ),
3131
source: range.lowerBound.source)
3232
let end = SourceLocation(line: startLine + range.upperBound.line - 1,
33-
column: range.upperBound.column + (trimmedIndentationPerLine[range.upperBound.line - 1]?.text.count ?? 0),
33+
column: range.upperBound.column + (trimmedIndentationPerLine[range.upperBound.line - 1]),
3434
source: range.upperBound.source)
3535
return start..<end
3636
}

Tests/MarkdownTests/Parsing/DoxygenCommandParserTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ class DoxygenCommandParserTests: XCTestCase {
170170
let expectedDump = """
171171
Document @1:1-2:39
172172
└─ DoxygenParam @1:1-2:39 parameter: thing
173-
└─ Paragraph @1:1-2:39
174-
├─ Text @1:1-1:11 "The thing."
173+
└─ Paragraph @1:14-2:39
174+
├─ Text @1:14-1:24 "The thing."
175175
├─ SoftBreak
176176
└─ Text @2:1-2:39 "This is the thing that is messed with."
177177
"""

0 commit comments

Comments
 (0)