@@ -19,21 +19,29 @@ import org.jetbrains.kotlin.psi.KtCallExpression
19
19
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
20
20
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
21
21
import org.jetbrains.kotlin.psi.psiUtil.startOffset
22
+ import org.javacs.kt.LOG
22
23
23
24
fun fetchSignatureHelpAt (file : CompiledFile , cursor : Int ): SignatureHelp ? {
24
- val (signatures, activeDeclaration, activeParameter) = getSignatures (file, cursor) ? : return nullResult(" No call around ${file.describePosition(cursor)} " )
25
+ val (signatures, activeDeclaration, activeParameter) = getSignatureTriplet (file, cursor) ? : return nullResult(" No call around ${file.describePosition(cursor)} " )
25
26
return SignatureHelp (signatures, activeDeclaration, activeParameter)
26
27
}
27
28
29
+ /* *
30
+ * Returns the doc string of the first found CallableDescriptor
31
+ *
32
+ * Avoids fetching the SignatureHelp triplet due to an OutOfBoundsException that can occur due to the offset difference math.
33
+ * When hovering, the cursor param is set to the doc offset where the mouse is hovering over, rather than where the actual cursor is,
34
+ * hence this is seen to cause issues when slicing the param list string
35
+ */
28
36
fun getDocString (file : CompiledFile , cursor : Int ): String {
29
37
val signatures = getSignatures(file, cursor)
30
- if (signatures == null || signatures.first. size == 0 || signatures.first [0 ].documentation == null )
38
+ if (signatures == null || signatures.size == 0 || signatures[0 ].documentation == null )
31
39
return " "
32
- return if (signatures.first [0 ].documentation.isLeft()) signatures.first [0 ].documentation.left else " "
40
+ return if (signatures[0 ].documentation.isLeft()) signatures[0 ].documentation.left else " "
33
41
}
34
42
35
43
// TODO better function name?
36
- private fun getSignatures (file : CompiledFile , cursor : Int ): Triple <List <SignatureInformation >, Int, Int>? {
44
+ private fun getSignatureTriplet (file : CompiledFile , cursor : Int ): Triple <List <SignatureInformation >, Int, Int>? {
37
45
val call = file.parseAtPoint(cursor)?.findParent<KtCallExpression >() ? : return null
38
46
val candidates = candidates(call, file)
39
47
val activeDeclaration = activeDeclaration(call, candidates)
@@ -43,6 +51,12 @@ private fun getSignatures(file: CompiledFile, cursor: Int): Triple<List<Signatur
43
51
return Triple (signatures, activeDeclaration, activeParameter)
44
52
}
45
53
54
+ private fun getSignatures (file : CompiledFile , cursor : Int ): List <SignatureInformation >? {
55
+ val call = file.parseAtPoint(cursor)?.findParent<KtCallExpression >() ? : return null
56
+ val candidates = candidates(call, file)
57
+ return candidates.map(::toSignature)
58
+ }
59
+
46
60
private fun toSignature (desc : CallableDescriptor ): SignatureInformation {
47
61
val label = DECL_RENDERER .render(desc)
48
62
val params = desc.valueParameters.map(::toParameter)
@@ -109,7 +123,8 @@ private fun activeParameter(call: KtCallExpression, cursor: Int): Int {
109
123
val text = args.text
110
124
if (text.length == 2 )
111
125
return 0
112
- val beforeCursor = text.subSequence(0 , Math .max(args.textRange.startOffset, cursor) - Math .min(args.textRange.startOffset, cursor))
113
-
126
+ val min = Math .min(args.textRange.startOffset, cursor)
127
+ val max = Math .max(args.textRange.startOffset, cursor)
128
+ val beforeCursor = text.subSequence(0 , max- min)
114
129
return beforeCursor.count { it == ' ,' }
115
- }
130
+ }
0 commit comments