@@ -165,6 +165,8 @@ fileprivate struct SimpleDiagnostic: DiagnosticMessage {
165165
166166 let severity : DiagnosticSeverity
167167
168+ let category : DiagnosticCategory ?
169+
168170 var diagnosticID : MessageID {
169171 . init( domain: " SwiftCompiler " , id: " SimpleDiagnostic " )
170172 }
@@ -237,6 +239,10 @@ public func addQueuedDiagnostic(
237239 textLength: Int ,
238240 severity: BridgedDiagnosticSeverity ,
239241 cLoc: BridgedSourceLoc ,
242+ categoryName: UnsafePointer < UInt8 > ? ,
243+ categoryLength: Int ,
244+ documentationPath: UnsafePointer < UInt8 > ? ,
245+ documentationPathLength: Int ,
240246 highlightRangesPtr: UnsafePointer < BridgedSourceLoc > ? ,
241247 numHighlightRanges: Int
242248) {
@@ -333,13 +339,43 @@ public func addQueuedDiagnostic(
333339 }
334340 }
335341
342+ let category : DiagnosticCategory ? = categoryName. map { categoryNamePtr in
343+ let categoryNameBuffer = UnsafeBufferPointer (
344+ start: categoryNamePtr,
345+ count: categoryLength
346+ )
347+ let categoryName = String ( decoding: categoryNameBuffer, as: UTF8 . self)
348+
349+ let documentationURL = documentationPath. map { documentationPathPtr in
350+ let documentationPathBuffer = UnsafeBufferPointer (
351+ start: documentationPathPtr,
352+ count: documentationPathLength
353+ )
354+
355+ let documentationPath = String ( decoding: documentationPathBuffer, as: UTF8 . self)
356+
357+ // If this looks doesn't look like a URL, prepend file://.
358+ if !documentationPath. looksLikeURL {
359+ return " file:// \( documentationPath) "
360+ }
361+
362+ return documentationPath
363+ }
364+
365+ return DiagnosticCategory (
366+ name: categoryName,
367+ documentationURL: documentationURL
368+ )
369+ }
370+
336371 let textBuffer = UnsafeBufferPointer ( start: text, count: textLength)
337372 let diagnostic = Diagnostic (
338373 node: node,
339374 position: position,
340375 message: SimpleDiagnostic (
341376 message: String ( decoding: textBuffer, as: UTF8 . self) ,
342- severity: severity. asSeverity
377+ severity: severity. asSeverity,
378+ category: category
343379 ) ,
344380 highlights: highlights
345381 )
@@ -361,3 +397,30 @@ public func renderQueuedDiagnostics(
361397
362398 renderedStringOutPtr. pointee = allocateBridgedString ( renderedStr)
363399}
400+
401+ extension String {
402+ /// Simple check to determine whether the string looks like the start of a
403+ /// URL.
404+ fileprivate var looksLikeURL : Bool {
405+ var forwardSlashes : Int = 0
406+ for c in self {
407+ if c == " / " {
408+ forwardSlashes += 1
409+ if forwardSlashes > 2 {
410+ return true
411+ }
412+
413+ continue
414+ }
415+
416+ if c. isLetter || c. isNumber {
417+ forwardSlashes = 0
418+ continue
419+ }
420+
421+ return false
422+ }
423+
424+ return false
425+ }
426+ }
0 commit comments