@@ -25,6 +25,9 @@ public struct GroupedDiagnostics {
2525 /// The syntax tree for the source file.
2626 let tree : SourceFileSyntax
2727
28+ /// The source location converter for this source file.
29+ let sourceLocationConverter : SourceLocationConverter
30+
2831 /// The human-readable name to use to identify this source file.
2932 let displayName : String
3033
@@ -64,17 +67,24 @@ public struct GroupedDiagnostics {
6467 @discardableResult
6568 public mutating func addSourceFile(
6669 tree: SourceFileSyntax ,
70+ sourceLocationConverter: SourceLocationConverter ? = nil ,
6771 displayName: String ,
6872 parent: ( SourceFileID , AbsolutePosition ) ? = nil ,
6973 diagnostics: [ Diagnostic ] = [ ]
7074 ) -> SourceFileID {
7175 // Determine the ID this source file will have.
7276 let id = SourceFileID ( id: sourceFiles. count)
73-
77+ let slc =
78+ sourceLocationConverter
79+ ?? SourceLocationConverter (
80+ fileName: displayName,
81+ tree: tree
82+ )
7483 sourceFiles. append (
7584 SourceFile (
7685 id: id,
7786 tree: tree,
87+ sourceLocationConverter: slc,
7888 displayName: displayName,
7989 parent: parent,
8090 diagnostics: diagnostics
@@ -167,10 +177,7 @@ extension GroupedDiagnostics {
167177 indentString: String
168178 ) -> String {
169179 let sourceFile = sourceFiles [ sourceFileID. id]
170- let slc = SourceLocationConverter (
171- fileName: sourceFile. displayName,
172- tree: sourceFile. tree
173- )
180+ let slc = sourceFile. sourceLocationConverter
174181 let diagnosticDecorator = formatter. diagnosticDecorator
175182
176183 let childPadding = String ( slc. sourceLines. count + 1 ) . count + 1 ;
@@ -195,7 +202,7 @@ extension GroupedDiagnostics {
195202 if isRoot {
196203 // If there's a primary diagnostic, print it first.
197204 if let ( primaryDiagSourceFile, primaryDiag) = findPrimaryDiagnostic ( in: sourceFile) {
198- let primaryDiagSLC = SourceLocationConverter ( fileName : primaryDiagSourceFile. displayName , tree : primaryDiagSourceFile . tree )
205+ let primaryDiagSLC = primaryDiagSourceFile. sourceLocationConverter
199206 let location = primaryDiag. location ( converter: primaryDiagSLC)
200207
201208 // Display file/line/column and diagnostic text for the primary diagnostic.
0 commit comments