Skip to content

Commit 7d736fa

Browse files
committed
fix: do not suggest extension functions for scope completions
This ensures that extension functions whose receiver type is not available in the current scope are not suggested for scope completions Signed-off-by: Akash Yadav <akashyadav@appdevforall.org>
1 parent 4129a47 commit 7d736fa

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.analysis.api.KaContextParameterApi
1313
import org.jetbrains.kotlin.analysis.api.KaExperimentalApi
1414
import org.jetbrains.kotlin.analysis.api.KaSession
1515
import org.jetbrains.kotlin.analysis.api.analyzeCopy
16+
import org.jetbrains.kotlin.analysis.api.components.KaScopeContext
1617
import org.jetbrains.kotlin.analysis.api.projectStructure.KaDanglingFileResolutionMode
1718
import org.jetbrains.kotlin.analysis.api.renderer.types.KaTypeRenderer
1819
import org.jetbrains.kotlin.analysis.api.renderer.types.impl.KaTypeRendererForSource
@@ -122,8 +123,9 @@ fun CompilationEnvironment.complete(params: CompletionParams): CompletionResult
122123
when (completionContext) {
123124
CompletionContext.Scope ->
124125
collectScopeCompletions(
125-
ktElement = ktElement,
126+
scopeContext = scopeContext,
126127
scope = compositeScope,
128+
ktElement = ktElement,
127129
partial = partial,
128130
to = items
129131
)
@@ -238,27 +240,36 @@ private fun KaSession.collectExtensionFunctions(
238240
}
239241

240242
private fun KaSession.collectScopeCompletions(
241-
ktElement: KtElement,
243+
scopeContext: KaScopeContext,
242244
scope: KaScope,
245+
ktElement: KtElement,
243246
partial: String,
244247
to: MutableList<CompletionItem>,
245248
) {
246-
247249
logger.info(
248250
"Complete scope members of {}: [{}] matching '{}'",
249251
ktElement,
250252
ktElement.text,
251253
partial
252254
)
253255

254-
to += toCompletionItems(
255-
scope.callables { name -> matchesPrefix(name, partial) },
256-
partial
257-
)
258-
to += toCompletionItems(
259-
scope.classifiers { name -> matchesPrefix(name, partial) },
260-
partial
261-
)
256+
val callables =
257+
scope.callables { name -> matchesPrefix(name, partial) }
258+
.filter { symbol ->
259+
260+
// always include non-extension functions
261+
if (!symbol.isExtension) return@filter true
262+
263+
// include extension functions with matching implicit receivers
264+
val extReceiverType = symbol.receiverType ?: return@filter true
265+
scopeContext.implicitReceivers.any { receiver ->
266+
receiver.type.isSubtypeOf(extReceiverType)
267+
}
268+
}
269+
val classifiers = scope.classifiers { name -> matchesPrefix(name, partial) }
270+
271+
to += toCompletionItems(callables, partial)
272+
to += toCompletionItems(classifiers, partial)
262273
}
263274

264275
@JvmName("callablesToCompletionItems")

0 commit comments

Comments
 (0)