Code improvements in NameValidatorImpl

This commit is contained in:
Valentin Kipyatkov
2015-06-26 20:03:08 +03:00
parent e00294d17b
commit ed267d9949
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.JetElement import org.jetbrains.kotlin.psi.JetElement
import org.jetbrains.kotlin.psi.JetExpression import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.psi.JetVisitorVoid import org.jetbrains.kotlin.psi.JetVisitorVoid
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.JetScope import org.jetbrains.kotlin.resolve.scopes.JetScope
@@ -43,35 +44,30 @@ public class NameValidatorImpl(
override fun invoke(name: String): Boolean { override fun invoke(name: String): Boolean {
val visitedScopes = HashSet<JetScope>() val visitedScopes = HashSet<JetScope>()
var sibling: PsiElement? val identifier = Name.identifier(name)
if (anchor != null) {
sibling = anchor val sibling = if (anchor != null) {
anchor
} }
else { else {
if (container is JetExpression) { if (container is JetExpression) {
return checkElement(name, container, visitedScopes) return !hasConflict(identifier, container, visitedScopes)
} }
sibling = container.getFirstChild() container.getFirstChild() ?: return true
} }
while (sibling != null) { return sibling.siblings().none { hasConflict(identifier, it, visitedScopes) }
if (!checkElement(name, sibling, visitedScopes)) return false
sibling = sibling.getNextSibling()
}
return true
} }
private fun checkElement(name: String, sibling: PsiElement, visitedScopes: MutableSet<JetScope>): Boolean { private fun hasConflict(name: Name, sibling: PsiElement, visitedScopes: MutableSet<JetScope>): Boolean {
if (sibling !is JetElement) return true if (sibling !is JetElement) return false
val context = sibling.analyze(BodyResolveMode.FULL) val context = sibling.analyze(BodyResolveMode.FULL)
val identifier = Name.identifier(name)
val result = Ref(true) var conflictFound = false
val visitor = object : JetVisitorVoid() { sibling.accept(object : JetVisitorVoid() {
override fun visitElement(element: PsiElement) { override fun visitElement(element: PsiElement) {
if (result.get()) { if (!conflictFound) {
element.acceptChildren(this) element.acceptChildren(this)
} }
} }
@@ -81,23 +77,19 @@ public class NameValidatorImpl(
if (!visitedScopes.add(resolutionScope)) return if (!visitedScopes.add(resolutionScope)) return
val noConflict: Boolean val conflict = if (target === Target.PROPERTIES)
if (target === Target.PROPERTIES) { resolutionScope.getProperties(name).isNotEmpty() || resolutionScope.getLocalVariable(name) != null
noConflict = resolutionScope.getProperties(identifier).isEmpty() && resolutionScope.getLocalVariable(identifier) == null else
} resolutionScope.getFunctions(name).isNotEmpty() || resolutionScope.getClassifier(name) != null
else {
noConflict = resolutionScope.getFunctions(identifier).isEmpty() && resolutionScope.getClassifier(identifier) == null
}
if (!noConflict) { if (conflict) {
result.set(false) conflictFound = true
return return
} }
super.visitExpression(expression) super.visitExpression(expression)
} }
} })
sibling.accept(visitor) return conflictFound
return result.get()
} }
} }