File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,12 +13,46 @@ public struct StdoutDiagnosticReporter: DiagnosticReporter {
1313 public init ( ) { }
1414
1515 public func report( diagnostic: Diagnostic , source: String , fileName: String ) {
16- let source = source [ diagnostic. location. range]
16+ let range = diagnostic. location. range
17+ let start = startOfLine ( index: range. lowerBound, source: source)
18+ // Note: This uses `lowerBound` as well to make sure we only get one line
19+ let end = endOfLine ( index: range. lowerBound, source: source)
20+ let source = source [ start ..< end]
21+ let distanceToStart = source. distance ( from: start, to: range. lowerBound)
22+ let indent = String ( repeating: " " , count: distanceToStart)
23+ let underline = String ( repeating: " ^ " , count: source. distance ( from: range. lowerBound, to: range. upperBound) )
1724
1825 print ( """
26+ Error in \( fileName)
27+
1928 \( source)
29+ \( indent) \( underline)
2030
2131 \( diagnostic. message)
2232 """ )
2333 }
34+
35+ private func startOfLine(
36+ index: String . Index ,
37+ source: String
38+ ) -> Substring . Index {
39+ var index = index
40+ while index > source. startIndex {
41+ let nextIndex = source. index ( before: index)
42+ guard !source[ nextIndex] . isNewline else { break }
43+ index = nextIndex
44+ }
45+ return index
46+ }
47+
48+ private func endOfLine(
49+ index: String . Index ,
50+ source: String
51+ ) -> Substring . Index {
52+ var index = index
53+ while index < source. endIndex, !source[ index] . isNewline {
54+ index = source. index ( after: index)
55+ }
56+ return index
57+ }
2458}
You can’t perform that action at this time.
0 commit comments