@@ -20,10 +20,11 @@ import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
2020import org.jetbrains.kotlin.psi.KtNameReferenceExpression
2121import org.jetbrains.kotlin.psi.psiUtil.startOffset
2222import org.javacs.kt.LOG
23+ import kotlin.math.abs
2324
2425fun fetchSignatureHelpAt (file : CompiledFile , cursor : Int ): SignatureHelp ? {
25- val (signatures, activeDeclaration , activeParameter) = getSignatureTriplet(file, cursor) ? : return nullResult(" No call around ${file.describePosition(cursor)} " )
26- return SignatureHelp (signatures, ( if (activeDeclaration >= 0 ) activeDeclaration else null ), ( if (activeDeclaration >= 0 ) activeParameter else null ) )
26+ val (signatures, activeSignature , activeParameter) = getSignatureTriplet(file, cursor) ? : return nullResult(" No call around ${file.describePosition(cursor)} " )
27+ return SignatureHelp (signatures, activeSignature, activeParameter)
2728}
2829
2930/* *
@@ -41,14 +42,17 @@ fun getDocString(file: CompiledFile, cursor: Int): String {
4142}
4243
4344// TODO better function name?
44- private fun getSignatureTriplet (file : CompiledFile , cursor : Int ): Triple <List <SignatureInformation >, Int, Int>? {
45+ private fun getSignatureTriplet (file : CompiledFile , cursor : Int ): Triple <List <SignatureInformation >, Int? , Int? >? {
4546 val call = file.parseAtPoint(cursor)?.findParent<KtCallExpression >() ? : return null
4647 val candidates = candidates(call, file)
47- val activeDeclaration = activeDeclaration(call, candidates)
48+ if (candidates.isEmpty()) {
49+ return null
50+ }
51+ val activeSignature = activeSignature(call, candidates)
4852 val activeParameter = activeParameter(call, cursor)
4953 val signatures = candidates.map(::toSignature)
5054
51- return Triple (signatures, activeDeclaration , activeParameter)
55+ return Triple (signatures, activeSignature , activeParameter)
5256}
5357
5458private fun getSignatures (file : CompiledFile , cursor : Int ): List <SignatureInformation >? {
@@ -97,8 +101,13 @@ private fun candidates(call: KtCallExpression, file: CompiledFile): List<Callabl
97101 return emptyList()
98102}
99103
100- private fun activeDeclaration (call : KtCallExpression , candidates : List <CallableDescriptor >): Int {
101- return candidates.indexOfFirst { isCompatibleWith(call, it) }
104+ private fun activeSignature (call : KtCallExpression , candidates : List <CallableDescriptor >): Int? {
105+ val activeIndex = candidates.indexOfFirst { isCompatibleWith(call, it) }
106+ if (activeIndex < 0 ) {
107+ LOG .warn(" No activeSignature found, omitting from SignatureHelp response." )
108+ return null
109+ }
110+ return activeIndex
102111}
103112
104113private fun isCompatibleWith (call : KtCallExpression , candidate : CallableDescriptor ): Boolean {
@@ -118,8 +127,8 @@ private fun isCompatibleWith(call: KtCallExpression, candidate: CallableDescript
118127 return true
119128}
120129
121- private fun activeParameter (call : KtCallExpression , cursor : Int ): Int {
122- val args = call.valueArgumentList ? : return - 1
130+ private fun activeParameter (call : KtCallExpression , cursor : Int ): Int? {
131+ val args = call.valueArgumentList ? : return null
123132 val text = args.text
124133 if (text.length == 2 )
125134 return 0
0 commit comments