Use PsiElement#getResolveScope() instead of custom helper
Fix extensions and types completion completion in code fragments in context of library source files Also affects completion in files not in our project (not tested)
This commit is contained in:
@@ -33,11 +33,4 @@ public fun Project.allScope(): GlobalSearchScope = GlobalSearchScope.allScope(th
|
||||
|
||||
public fun Project.projectScope(): GlobalSearchScope = GlobalSearchScope.projectScope(this)
|
||||
|
||||
public fun PsiFile.fileScope(): GlobalSearchScope = GlobalSearchScope.fileScope(this)
|
||||
|
||||
public fun searchScopeForSourceElementDependencies(element: PsiElement): GlobalSearchScope? {
|
||||
val module = ModuleUtilCore.findModuleForPsiElement(element) ?: return null
|
||||
val virtualFile = element.getContainingFile()?.getVirtualFile() ?: return null
|
||||
val isInTests = ModuleRootManager.getInstance(module).getFileIndex().isInTestSourceContent(virtualFile)
|
||||
return GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, isInTests)
|
||||
}
|
||||
public fun PsiFile.fileScope(): GlobalSearchScope = GlobalSearchScope.fileScope(this)
|
||||
@@ -32,7 +32,6 @@ import org.jetbrains.jet.plugin.references.JetSimpleNameReference
|
||||
import org.jetbrains.jet.plugin.project.ResolveSessionForBodies
|
||||
import org.jetbrains.jet.plugin.caches.KotlinIndicesHelper
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.jet.plugin.search.searchScopeForSourceElementDependencies
|
||||
|
||||
class CompletionSessionConfiguration(
|
||||
val completeNonImportedDeclarations: Boolean,
|
||||
@@ -62,7 +61,7 @@ abstract class CompletionSessionBase(protected val configuration: CompletionSess
|
||||
protected val collector: LookupElementsCollector = LookupElementsCollector(prefixMatcher, resolveSession)
|
||||
|
||||
protected val project: Project = position.getProject()
|
||||
protected val searchScope: GlobalSearchScope = searchScopeForSourceElementDependencies(parameters.getOriginalFile()) ?: GlobalSearchScope.EMPTY_SCOPE
|
||||
protected val searchScope: GlobalSearchScope = parameters.getOriginalFile().getResolveScope()
|
||||
protected val indicesHelper: KotlinIndicesHelper = KotlinIndicesHelper(project, resolveSession, searchScope) { isVisibleDescriptor(it) }
|
||||
|
||||
protected fun isVisibleDescriptor(descriptor: DeclarationDescriptor): Boolean {
|
||||
|
||||
@@ -29,7 +29,6 @@ import org.jetbrains.jet.plugin.caches.JetFromJavaDescriptorHelper
|
||||
import org.jetbrains.jet.plugin.project.ProjectStructureUtil
|
||||
import org.jetbrains.jet.plugin.project.ResolveSessionForBodies
|
||||
import org.jetbrains.jet.plugin.caches.KotlinIndicesHelper
|
||||
import org.jetbrains.jet.plugin.search.searchScopeForSourceElementDependencies
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
|
||||
|
||||
class TypesCompletion(val parameters: CompletionParameters,
|
||||
@@ -40,12 +39,13 @@ class TypesCompletion(val parameters: CompletionParameters,
|
||||
result.addDescriptorElements(KotlinBuiltIns.getInstance().getNonPhysicalClasses().filter { prefixMatcher.prefixMatches(it.getName().asString()) },
|
||||
suppressAutoInsertion = true)
|
||||
|
||||
val project = parameters.getOriginalFile().getProject()
|
||||
val searchScope = searchScopeForSourceElementDependencies(parameters.getOriginalFile()) ?: return
|
||||
val file = parameters.getOriginalFile()
|
||||
val project = file.getProject()
|
||||
val searchScope = file.getResolveScope()
|
||||
result.addDescriptorElements(KotlinIndicesHelper(project, resolveSession, searchScope, visibilityFilter).getClassDescriptors { prefixMatcher.prefixMatches(it) },
|
||||
suppressAutoInsertion = true)
|
||||
|
||||
if (!ProjectStructureUtil.isJsKotlinModule(parameters.getOriginalFile() as JetFile)) {
|
||||
if (!ProjectStructureUtil.isJsKotlinModule(file as JetFile)) {
|
||||
addAdaptedJavaCompletion(result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,6 @@ import org.jetbrains.jet.plugin.codeInsight.DescriptorToDeclarationUtil
|
||||
import com.intellij.openapi.module.ModuleUtilCore
|
||||
import org.jetbrains.jet.plugin.util.ProjectRootsUtil
|
||||
import org.jetbrains.jet.asJava.unwrapped
|
||||
import org.jetbrains.jet.plugin.search.searchScopeForSourceElementDependencies
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorWithVisibility
|
||||
import org.jetbrains.jet.lang.descriptors.Visibilities
|
||||
@@ -119,7 +118,7 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<Jet
|
||||
|
||||
val resolveSession = element.getLazyResolveSession()
|
||||
|
||||
val searchScope = searchScopeForSourceElementDependencies(file) ?: return listOf()
|
||||
val searchScope = file.getResolveScope()
|
||||
|
||||
val bindingContext = resolveSession.resolveToElement(element)
|
||||
|
||||
|
||||
@@ -2,4 +2,7 @@ package customLibrary
|
||||
|
||||
public fun foo(parameter: Int): Int {
|
||||
return 1
|
||||
}
|
||||
|
||||
private fun Int.extOnInt() {
|
||||
}
|
||||
+25
-4
@@ -51,17 +51,38 @@ public class CodeFragmentCompletionInLibraryTest : AbstractJvmBasicCompletionTes
|
||||
}
|
||||
|
||||
public fun testCompletionInCustomLibrary() {
|
||||
testCompletionInLibraryCodeFragment("<caret>", "EXIST: parameter")
|
||||
}
|
||||
|
||||
public fun testSecondCompletionInCustomLibrary() {
|
||||
testCompletionInLibraryCodeFragment("Sh<caret>", "EXIST: ShortRange", "EXIST: Short", "INVOCATION_COUNT: 2")
|
||||
}
|
||||
|
||||
public fun testExtensionCompletionInCustomLibrary() {
|
||||
testCompletionInLibraryCodeFragment("3.extOn<caret>", "EXIST: extOnInt")
|
||||
}
|
||||
|
||||
public fun testJavaTypesCompletion() {
|
||||
testCompletionInLibraryCodeFragment("Hash<caret>", "EXIST: HashMap", "EXIST: HashSet")
|
||||
}
|
||||
|
||||
private fun testCompletionInLibraryCodeFragment(fragmentText: String, vararg completionDirectives: String) {
|
||||
setupFixtureByCodeFragment(fragmentText)
|
||||
val directives = completionDirectives.map { "//$it" }.joinToString(separator = "\n")
|
||||
testCompletion(directives, TargetPlatform.JVM, {
|
||||
myFixture.complete(CompletionType.BASIC)
|
||||
})
|
||||
}
|
||||
|
||||
private fun setupFixtureByCodeFragment(fragmentText: String) {
|
||||
val sourceFile = findLibrarySourceDir().findChild("customLibrary.kt")
|
||||
val jetFile = PsiManager.getInstance(getProject()).findFile(sourceFile) as JetFile
|
||||
val fooFunctionFromLibrary = jetFile.getDeclarations().first() as JetFunction
|
||||
val codeFragment = JetPsiFactory(fooFunctionFromLibrary).createExpressionCodeFragment(
|
||||
"<caret>",
|
||||
fragmentText,
|
||||
KotlinCodeFragmentFactory.getContextElement(fooFunctionFromLibrary.getBodyExpression())
|
||||
)
|
||||
myFixture.configureFromExistingVirtualFile(codeFragment.getVirtualFile())
|
||||
testCompletion("//EXIST: parameter", TargetPlatform.JVM, {
|
||||
myFixture.complete(CompletionType.BASIC)
|
||||
})
|
||||
}
|
||||
|
||||
private fun findLibrarySourceDir(): VirtualFile {
|
||||
|
||||
Reference in New Issue
Block a user