Skip to content

Commit f5d6072

Browse files
authored
Merge pull request #1365 from DougGregor/grouped-diagnostics-cleanups
2 parents 7fa6666 + fb831cb commit f5d6072

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ extension Sequence where Element == Range<Int> {
2929
}
3030

3131
// If the ranges overlap, expand the prior range.
32+
assert(priorRange.lowerBound <= range.lowerBound)
3233
if priorRange.overlaps(range) {
33-
let lower = Swift.min(priorRange.lowerBound, range.lowerBound)
34+
let lower = priorRange.lowerBound
3435
let upper = Swift.max(priorRange.upperBound, range.upperBound)
3536
prior = lower..<upper
3637
continue
@@ -151,18 +152,19 @@ public struct DiagnosticsFormatter {
151152
}.mergingOverlappingRanges()
152153

153154
// Map the column ranges into index ranges within the source string itself.
154-
let sourceString = annotatedLine.sourceString
155+
let sourceStringUTF8 = annotatedLine.sourceString.utf8
155156
let highlightIndexRanges: [Range<String.Index>] = highlightRanges.map { highlightRange in
156-
let startIndex = sourceString.index(sourceString.startIndex, offsetBy: highlightRange.lowerBound - 1)
157-
let endIndex = sourceString.index(startIndex, offsetBy: highlightRange.count)
157+
let startIndex = sourceStringUTF8.index(sourceStringUTF8.startIndex, offsetBy: highlightRange.lowerBound - 1)
158+
let endIndex = sourceStringUTF8.index(startIndex, offsetBy: highlightRange.count)
158159
return startIndex..<endIndex
159160
}
160161

161162
// Form the annotated string by copying in text from the original source,
162163
// highlighting the column ranges.
163164
var resultSourceString: String = ""
164-
var sourceIndex = sourceString.startIndex
165165
let annotation = ANSIAnnotation.sourceHighlight
166+
let sourceString = annotatedLine.sourceString
167+
var sourceIndex = sourceString.startIndex
166168
for highlightRange in highlightIndexRanges {
167169
// Text before the highlight range
168170
resultSourceString += sourceString[sourceIndex..<highlightRange.lowerBound]
@@ -188,7 +190,7 @@ public struct DiagnosticsFormatter {
188190
tree: SyntaxType,
189191
diags: [Diagnostic],
190192
indentString: String,
191-
suffixTexts: [(AbsolutePosition, String)],
193+
suffixTexts: [AbsolutePosition: String],
192194
sourceLocationConverter: SourceLocationConverter? = nil
193195
) -> String {
194196
let slc = sourceLocationConverter ?? SourceLocationConverter(file: fileName ?? "", tree: tree)
@@ -318,7 +320,7 @@ public struct DiagnosticsFormatter {
318320
tree: tree,
319321
diags: diags,
320322
indentString: "",
321-
suffixTexts: []
323+
suffixTexts: [:]
322324
)
323325
}
324326

@@ -372,6 +374,7 @@ struct ANSIAnnotation {
372374
case magenta = 35
373375
case cyan = 36
374376
case white = 37
377+
case `default` = 39
375378
}
376379

377380
enum Trait: UInt8 {
@@ -414,6 +417,6 @@ struct ANSIAnnotation {
414417

415418
/// Annotation used for highlighting source text.
416419
static var sourceHighlight: ANSIAnnotation {
417-
ANSIAnnotation(color: .white, trait: .underline)
420+
ANSIAnnotation(color: .default, trait: .underline)
418421
}
419422
}

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,16 @@ extension GroupedDiagnostics {
149149

150150
let childPadding = String(slc.sourceLines.count + 1).count + 1;
151151

152-
let childSources: [(AbsolutePosition, String)] = sourceFiles[sourceFileID.id].children.map { childBufferID in
152+
// Collect the child sources.
153+
var childSources: [AbsolutePosition: String] = [:]
154+
for childBufferID in sourceFiles[sourceFileID.id].children {
153155
let childSource = annotateSource(
154156
childBufferID,
155157
formatter: formatter,
156158
indentString: indentString + String(repeating: " ", count: childPadding) + ""
157159
)
158160

159-
return (sourceFiles[childBufferID.id].parent!.1, childSource)
161+
childSources[sourceFiles[childBufferID.id].parent!.1, default: ""].append(childSource)
160162
}
161163

162164
// If this is a nested source file, draw a box around it.

Sources/SwiftSyntax/AbsolutePosition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/// An absolute position in a source file as text - the absolute utf8Offset from
1414
/// the start of the file.
15-
public struct AbsolutePosition: Comparable {
15+
public struct AbsolutePosition: Comparable, Hashable {
1616
public let utf8Offset: Int
1717

1818
static let startOfFile = AbsolutePosition(utf8Offset: 0)

Tests/SwiftDiagnosticsTest/DiagnosticsFormatterTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ final class DiagnosticsFormatterTests: XCTestCase {
123123

124124
func testColoringWithHighlights() {
125125
let source = """
126-
for (i = 0; i != 10; i += 1) { }
126+
for (i = 🐮; i != 👩‍👩‍👦‍👦; i += 1) { }
127127
"""
128128

129129
let expectedOutput = """
130-
\u{001B}[0;36m1 │\u{001B}[0;0m for \u{001B}[4;37m(i\u{001B}[0;0m \u{001B}[4;37m= 0; i != 10; i += 1)\u{001B}[0;0m { }
130+
\u{001B}[0;36m1 │\u{001B}[0;0m for \u{001B}[4;39m(i\u{001B}[0;0m \u{001B}[4;39m= 🐮; i != 👩‍👩‍👦‍👦; i += 1)\u{001B}[0;0m { }
131131
\u{001B}[0;36m∣\u{001B}[0;0m │ ╰─ \u{001B}[1;31merror: expected ')' to end tuple pattern\u{001B}[0;0m
132132
\u{001B}[0;36m∣\u{001B}[0;0m ╰─ \u{001B}[1;31merror: C-style for statement has been removed in Swift 3\u{001B}[0;0m
133133

0 commit comments

Comments
 (0)