@@ -17,20 +17,43 @@ import SourceKitD
17
17
import SourceKitLSP
18
18
import SwiftBasicFormat
19
19
20
+ fileprivate extension String {
21
+ func utf16Offset( of utf8Offset: Int , callerFile: StaticString = #fileID, callerLine: UInt = #line) -> Int {
22
+ guard
23
+ let stringIndex = self . utf8. index ( self . startIndex, offsetBy: utf8Offset, limitedBy: self . endIndex)
24
+ else {
25
+ logger. fault (
26
+ """
27
+ UTF-8 offset is past the end of the string while getting UTF-16 offset of \( utf8Offset) \
28
+ ( \( callerFile, privacy: . public) : \( callerLine, privacy: . public) )
29
+ """
30
+ )
31
+ return self . utf16. count
32
+ }
33
+ return self . utf16. distance ( from: self . startIndex, to: stringIndex)
34
+ }
35
+ }
36
+
20
37
fileprivate extension ParameterInformation {
21
- init ? ( _ parameter: SKDResponseDictionary , _ keys: sourcekitd_api_keys ) {
38
+ init ? ( _ parameter: SKDResponseDictionary , _ signatureLabel : String , _ keys: sourcekitd_api_keys ) {
22
39
guard let nameOffset = parameter [ keys. nameOffset] as Int ? ,
23
40
let nameLength = parameter [ keys. nameLength] as Int ?
24
41
else {
25
42
return nil
26
43
}
27
44
28
- let documentation = parameter [ keys. docComment] . map {
29
- StringOrMarkupContent . markupContent ( MarkupContent ( kind: . markdown, value: $0) )
30
- }
45
+ let documentation : StringOrMarkupContent ? =
46
+ if let docComment: String = parameter [ keys. docComment] {
47
+ . markupContent( MarkupContent ( kind: . markdown, value: docComment) )
48
+ } else {
49
+ nil
50
+ }
51
+
52
+ let labelStart = signatureLabel. utf16Offset ( of: nameOffset)
53
+ let labelEnd = signatureLabel. utf16Offset ( of: nameOffset + nameLength)
31
54
32
55
self . init (
33
- label: . offsets( start: nameOffset , end: nameOffset + nameLength ) ,
56
+ label: . offsets( start: labelStart , end: labelEnd ) ,
34
57
documentation: documentation
35
58
)
36
59
}
@@ -45,11 +68,14 @@ fileprivate extension SignatureInformation {
45
68
}
46
69
47
70
let activeParameter = signature [ keys. activeParameter] as Int ?
48
- let parameters = skParameters. compactMap { ParameterInformation ( $0, keys) }
49
-
50
- let documentation = signature [ keys. docComment] . map {
51
- StringOrMarkupContent . markupContent ( MarkupContent ( kind: . markdown, value: $0) )
52
- }
71
+ let parameters = skParameters. compactMap { ParameterInformation ( $0, label, keys) }
72
+
73
+ let documentation : StringOrMarkupContent ? =
74
+ if let docComment: String = signature [ keys. docComment] {
75
+ . markupContent( MarkupContent ( kind: . markdown, value: docComment) )
76
+ } else {
77
+ nil
78
+ }
53
79
54
80
self . init (
55
81
label: label,
0 commit comments