Skip to content

Commit

Permalink
Merge pull request #547 from jonas-w/fix-negative-signature-help
Browse files Browse the repository at this point in the history
Fix negative active signature/parameter
  • Loading branch information
fwcd authored Jan 24, 2024
2 parents 5f12bbf + d09344d commit 9ae4ede
Showing 1 changed file with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.javacs.kt.LOG

fun fetchSignatureHelpAt(file: CompiledFile, cursor: Int): SignatureHelp? {
val (signatures, activeDeclaration, activeParameter) = getSignatureTriplet(file, cursor) ?: return nullResult("No call around ${file.describePosition(cursor)}")
return SignatureHelp(signatures, activeDeclaration, activeParameter)
val (signatures, activeSignature, activeParameter) = getSignatureTriplet(file, cursor) ?: return nullResult("No call around ${file.describePosition(cursor)}")
return SignatureHelp(signatures, activeSignature, activeParameter)
}

/**
Expand All @@ -41,14 +41,18 @@ fun getDocString(file: CompiledFile, cursor: Int): String {
}

// TODO better function name?
private fun getSignatureTriplet(file: CompiledFile, cursor: Int): Triple<List<SignatureInformation>, Int, Int>? {
@Suppress("ReturnCount")
private fun getSignatureTriplet(file: CompiledFile, cursor: Int): Triple<List<SignatureInformation>, Int?, Int?>? {
val call = file.parseAtPoint(cursor)?.findParent<KtCallExpression>() ?: return null
val candidates = candidates(call, file)
val activeDeclaration = activeDeclaration(call, candidates)
if (candidates.isEmpty()) {
return null
}
val activeSignature = activeSignature(call, candidates)
val activeParameter = activeParameter(call, cursor)
val signatures = candidates.map(::toSignature)

return Triple(signatures, activeDeclaration, activeParameter)
return Triple(signatures, activeSignature, activeParameter)
}

private fun getSignatures(file: CompiledFile, cursor: Int): List<SignatureInformation>? {
Expand Down Expand Up @@ -97,8 +101,13 @@ private fun candidates(call: KtCallExpression, file: CompiledFile): List<Callabl
return emptyList()
}

private fun activeDeclaration(call: KtCallExpression, candidates: List<CallableDescriptor>): Int {
return candidates.indexOfFirst { isCompatibleWith(call, it) }
private fun activeSignature(call: KtCallExpression, candidates: List<CallableDescriptor>): Int? {
val activeIndex = candidates.indexOfFirst { isCompatibleWith(call, it) }
if (activeIndex < 0) {
LOG.warn("No activeSignature found, omitting from SignatureHelp response.")
return null
}
return activeIndex
}

private fun isCompatibleWith(call: KtCallExpression, candidate: CallableDescriptor): Boolean {
Expand All @@ -118,8 +127,9 @@ private fun isCompatibleWith(call: KtCallExpression, candidate: CallableDescript
return true
}

private fun activeParameter(call: KtCallExpression, cursor: Int): Int {
val args = call.valueArgumentList ?: return -1
@Suppress("ReturnCount")
private fun activeParameter(call: KtCallExpression, cursor: Int): Int? {
val args = call.valueArgumentList ?: return null
val text = args.text
if (text.length == 2)
return 0
Expand Down

0 comments on commit 9ae4ede

Please sign in to comment.