compiler, cli: Support Nice Compilation Error Messages
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -210,6 +210,33 @@ public struct SourceCode {
|
||||
return self.code
|
||||
}
|
||||
|
||||
public func getSourceSnippet(
|
||||
location: SourceLocation, context: Int = 0
|
||||
) -> (FilePath, String, String, String)? {
|
||||
guard let path = self.pathForLocation(location.range.lowerBound) else {
|
||||
return .none
|
||||
}
|
||||
let lower = String.UTF8View.Index(utf16Offset: location.range.lowerBound, in: self.code)
|
||||
let upper = String.UTF8View.Index(utf16Offset: location.range.upperBound, in: self.code)
|
||||
let prior_start =
|
||||
if location.range.lowerBound - context >= 0 {
|
||||
String.UTF8View.Index(utf16Offset: location.range.lowerBound - context, in: self.code)
|
||||
} else {
|
||||
String.UTF8View.Index(utf16Offset: location.range.lowerBound, in: self.code)
|
||||
}
|
||||
let after_end =
|
||||
if location.range.upperBound + context < self.code.count {
|
||||
String.UTF8View.Index(utf16Offset: location.range.upperBound + context, in: self.code)
|
||||
} else {
|
||||
String.UTF8View.Index(utf16Offset: location.range.upperBound, in: self.code)
|
||||
}
|
||||
|
||||
let result = String(self.code.utf16[lower..<upper])!
|
||||
let prior = String(self.code.utf16[prior_start..<lower])!
|
||||
let after = String(self.code.utf16[upper...after_end])!
|
||||
return (path, result, prior, after)
|
||||
}
|
||||
|
||||
public func getLocations() -> FileSourceLocation {
|
||||
return self.locations
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user