Skip to content

Commit

Permalink
Fix and add test case for single line block parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Jul 14, 2023
1 parent cc9875f commit f6d5535
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Sources/Markdown/Base/DirectiveArgument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public struct DirectiveArgumentText: Equatable {
var line = TrimmedLine(untrimmedText[...],
source: range?.lowerBound.source,
lineNumber: range?.lowerBound.line,
parseIndex: parseIndex)
parseIndex: parseIndex,
startParseIndex: untrimmedText.index(parseIndex, offsetBy: 1 - (range?.lowerBound.column ?? 1)))
line.lexWhitespace()
while !line.isEmptyOrAllWhitespace {
let name: TrimmedLine.Lex?
Expand Down
4 changes: 2 additions & 2 deletions Sources/Markdown/Parser/BlockDirectiveParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,12 @@ struct TrimmedLine {
}

/// - parameter untrimmedText: ``untrimmedText``
init(_ untrimmedText: Substring, source: URL?, lineNumber: Int?, parseIndex: Substring.Index? = nil) {
init(_ untrimmedText: Substring, source: URL?, lineNumber: Int?, parseIndex: Substring.Index? = nil, startParseIndex: Substring.Index? = nil) {
self.untrimmedText = untrimmedText
self.source = source
self.parseIndex = parseIndex ?? untrimmedText.startIndex
self.lineNumber = lineNumber
self.startParseIndex = self.parseIndex
self.startParseIndex = startParseIndex ?? self.parseIndex
}

/// Return the UTF-8 source location of the parse index if the line
Expand Down
24 changes: 24 additions & 0 deletions Tests/MarkdownTests/Parsing/BlockDirectiveParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1042,5 +1042,29 @@ class BlockDirectiveArgumentParserTests: XCTestCase {
└─ Text "Line c This is a single-line comment"
"""
XCTAssertEqual(expected, documentation.debugDescription())

func testDirectiveArgumentOnNonfirstLineParsing() throws {
let source = """
@Options(scope: page)
"""

let line = 2
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = try XCTUnwrap(document.child(at: 0) as? BlockDirective)
let arguments = directive.argumentText.parseNameValueArguments()
let scopeArg = try XCTUnwrap(arguments["scope"])

XCTAssertEqual("scope", scopeArg.name)
XCTAssertEqual(
scopeArg.nameRange,
SourceLocation(line: line, column: 10, source: nil) ..< SourceLocation(line: line, column: 15, source: nil)
)

XCTAssertEqual("page", scopeArg.value)
XCTAssertEqual(
scopeArg.valueRange,
SourceLocation(line: line, column: 17, source: nil) ..< SourceLocation(line: line, column: 21, source: nil)
)
}
}

0 comments on commit f6d5535

Please sign in to comment.