Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid indentation of strings with short lines #148

Merged
merged 1 commit into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions Sources/ArgumentParser/Utilities/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,25 @@ extension String {
while true {
let nextChunk = self[currentIndex...].prefix(columns)
if let lastLineBreak = nextChunk.lastIndex(of: "\n") {
result.append(self[currentIndex..<lastLineBreak])
result.append(contentsOf: self[currentIndex..<lastLineBreak].split(separator: "\n", omittingEmptySubsequences: false))
currentIndex = index(after: lastLineBreak)
} else if nextChunk.endIndex == self.endIndex {
result.append(self[currentIndex...])
break
} else if let lastSpace = nextChunk.lastIndex(of: " ") {
result.append(self[currentIndex..<lastSpace])
currentIndex = index(after: lastSpace)
} else if let nextSpace = self[currentIndex...].firstIndex(of: " ") {
result.append(self[currentIndex..<nextSpace])
currentIndex = index(after: nextSpace)
} else {
if let lastSegment = result.last,
lastSegment.count + self[currentIndex...].count < columns
{
result[result.count - 1] = self[lastSegment.startIndex...]
break
}

result.append(self[currentIndex...])
break
}
}

return result
.map { String(repeating: " ", count: wrappingIndent) + $0 }
.map { $0.isEmpty ? $0 : String(repeating: " ", count: wrappingIndent) + $0 }
.joined(separator: "\n")
}

Expand Down
36 changes: 35 additions & 1 deletion Tests/ArgumentParserUnitTests/StringWrappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ Et leo duis ut diam quam.
Integer eget aliquet nibh praesent tristique magna sit. Faucibus turpis in eu mi bibendum neque egestas congue quisque. Risus nec feugiat in fermentum posuere urna nec tincidunt.
"""

let jsonSample = """
{
"level1": {
"level2": {
"level3": true
}
}
}
"""

// MARK: -

extension StringWrappingTests {
Expand Down Expand Up @@ -114,7 +124,7 @@ extension StringWrappingTests {
XCTAssertEqual(longSample.wrapped(to: 60, wrappingIndent: 10), """
Pretium vulputate sapien nec sagittis aliquam
malesuada bibendum. Ut diam quam nulla porttitor.

Egestas egestas fringilla phasellus faucibus.
Amet dictum sit amet justo donec enim diam.
Consectetur adipiscing elit duis tristique.
Expand All @@ -130,4 +140,28 @@ extension StringWrappingTests {
""")

}

func testJSON() {
XCTAssertEqual(jsonSample.wrapped(to: 80), """
{
"level1": {
"level2": {
"level3": true
}
}
}
""")
}

func testJSONWithIndent() {
XCTAssertEqual(jsonSample.wrapped(to: 80, wrappingIndent: 10), """
{
"level1": {
"level2": {
"level3": true
}
}
}
""")
}
}