Skip to content

Commit 398e1ad

Browse files
committed
[Diagnostics] Always provide a URL for the category documentation
1 parent db2fa7b commit 398e1ad

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

+38-3
Original file line numberDiff line numberDiff line change
@@ -346,17 +346,25 @@ public func addQueuedDiagnostic(
346346
)
347347
let categoryName = String(decoding: categoryNameBuffer, as: UTF8.self)
348348

349-
let documentationPath = documentationPath.map { documentationPathPtr in
349+
let documentationURL = documentationPath.map { documentationPathPtr in
350350
let documentationPathBuffer = UnsafeBufferPointer(
351351
start: documentationPathPtr,
352352
count: documentationPathLength
353353
)
354-
return String(decoding: documentationPathBuffer, as: UTF8.self)
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
355363
}
356364

357365
return DiagnosticCategory(
358366
name: categoryName,
359-
documentationPath: documentationPath
367+
documentationURL: documentationURL
360368
)
361369
}
362370

@@ -389,3 +397,30 @@ public func renderQueuedDiagnostics(
389397

390398
renderedStringOutPtr.pointee = allocateBridgedString(renderedStr)
391399
}
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

Comments
 (0)