Skip to content

Commit 266d1dd

Browse files
committed
Seperated getting docstring from signature help due to reason explained in the docstring
1 parent e1c1dda commit 266d1dd

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

server/src/main/kotlin/org/javacs/kt/signaturehelp/SignatureHelp.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,29 @@ import org.jetbrains.kotlin.psi.KtCallExpression
1919
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
2020
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
2121
import org.jetbrains.kotlin.psi.psiUtil.startOffset
22+
import org.javacs.kt.LOG
2223

2324
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)}")
2526
return SignatureHelp(signatures, activeDeclaration, activeParameter)
2627
}
2728

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+
*/
2836
fun getDocString(file: CompiledFile, cursor: Int): String {
2937
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)
3139
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 ""
3341
}
3442

3543
// 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>? {
3745
val call = file.parseAtPoint(cursor)?.findParent<KtCallExpression>() ?: return null
3846
val candidates = candidates(call, file)
3947
val activeDeclaration = activeDeclaration(call, candidates)
@@ -43,6 +51,12 @@ private fun getSignatures(file: CompiledFile, cursor: Int): Triple<List<Signatur
4351
return Triple(signatures, activeDeclaration, activeParameter)
4452
}
4553

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+
4660
private fun toSignature(desc: CallableDescriptor): SignatureInformation {
4761
val label = DECL_RENDERER.render(desc)
4862
val params = desc.valueParameters.map(::toParameter)
@@ -109,7 +123,8 @@ private fun activeParameter(call: KtCallExpression, cursor: Int): Int {
109123
val text = args.text
110124
if (text.length == 2)
111125
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)
114129
return beforeCursor.count { it == ','}
115-
}
130+
}

0 commit comments

Comments
 (0)