@@ -22,8 +22,8 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
2222import org.javacs.kt.LOG
2323
2424fun 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, activeDeclaration , activeParameter)
25+ val (signatures, activeSignature , activeParameter) = getSignatureTriplet(file, cursor) ? : return nullResult(" No call around ${file.describePosition(cursor)} " )
26+ return SignatureHelp (signatures, activeSignature , activeParameter)
2727}
2828
2929/* *
@@ -41,14 +41,18 @@ fun getDocString(file: CompiledFile, cursor: Int): String {
4141}
4242
4343// TODO better function name?
44- private fun getSignatureTriplet (file : CompiledFile , cursor : Int ): Triple <List <SignatureInformation >, Int, Int>? {
44+ @Suppress(" ReturnCount" )
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,9 @@ 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+ @Suppress(" ReturnCount" )
131+ private fun activeParameter (call : KtCallExpression , cursor : Int ): Int? {
132+ val args = call.valueArgumentList ? : return null
123133 val text = args.text
124134 if (text.length == 2 )
125135 return 0
0 commit comments