Name validation filters out invisible declarations
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
package org.jetbrains.kotlin.idea.core
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
@@ -25,11 +27,13 @@ import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
||||
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
import java.util.*
|
||||
import java.util.Collections
|
||||
import java.util.HashSet
|
||||
|
||||
public class CollectingNameValidator @jvmOverloads constructor(
|
||||
existingNames: Collection<String> = Collections.emptySet(),
|
||||
@@ -82,9 +86,18 @@ public class NewDeclarationNameValidator(
|
||||
}
|
||||
|
||||
private fun JetScope.hasConflict(name: Name): Boolean {
|
||||
val inDeclaration = getContainingDeclaration()
|
||||
|
||||
fun DeclarationDescriptor.isVisible(): Boolean {
|
||||
return when (this) {
|
||||
is DeclarationDescriptorWithVisibility -> isVisible(inDeclaration)
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
|
||||
return when(target) {
|
||||
Target.VARIABLES -> getProperties(name).any { !it.isExtension } || getLocalVariable(name) != null
|
||||
Target.FUNCTIONS_AND_CLASSES -> getFunctions(name).any { !it.isExtension } || getClassifier(name) != null
|
||||
Target.VARIABLES -> getProperties(name).any { !it.isExtension && it.isVisible() } || getLocalVariable(name) != null
|
||||
Target.FUNCTIONS_AND_CLASSES -> getFunctions(name).any { !it.isExtension && it.isVisible() } || getClassifier(name)?.let { it.isVisible() } ?: false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
open class Base {
|
||||
private val xxx = 1
|
||||
}
|
||||
|
||||
class For : Base() {
|
||||
fun foo(f: For) {
|
||||
<selection>f.xxx</selection>
|
||||
}
|
||||
}
|
||||
|
||||
val For.xxx: For
|
||||
get() = For()
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
open class Base {
|
||||
private val xxx = 1
|
||||
}
|
||||
|
||||
class For : Base() {
|
||||
fun foo(f: For) {
|
||||
val xxx = f.xxx
|
||||
}
|
||||
}
|
||||
|
||||
val For.xxx: For
|
||||
get() = For()
|
||||
+6
@@ -175,6 +175,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
||||
doIntroduceVariableTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NameSuggestionCheckVisibility.kt")
|
||||
public void testNameSuggestionCheckVisibility() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/NameSuggestionCheckVisibility.kt");
|
||||
doIntroduceVariableTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noConflictWithInnerVariable.kt")
|
||||
public void testNoConflictWithInnerVariable() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/noConflictWithInnerVariable.kt");
|
||||
|
||||
Reference in New Issue
Block a user