Cleanup FqNamesUtil.kt
Delete unused, move some utilities to fqNameUtil.kt in IDE, rewrite comments
This commit is contained in:
+7
-6
@@ -26,15 +26,9 @@ import org.jetbrains.kotlin.resolve.lazy.ResolveSessionUtils
|
|||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.properties.Delegates
|
import kotlin.properties.Delegates
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import org.jetbrains.kotlin.name.tail
|
import org.jetbrains.kotlin.name.tail
|
||||||
import org.jetbrains.kotlin.name.each
|
|
||||||
|
|
||||||
public class LazyResolveBasedCache() : JavaResolverCache {
|
public class LazyResolveBasedCache() : JavaResolverCache {
|
||||||
class object {
|
|
||||||
private val LOG = Logger.getInstance(javaClass<TraceBasedJavaResolverCache>())
|
|
||||||
}
|
|
||||||
|
|
||||||
private var resolveSession by Delegates.notNull<ResolveSession>()
|
private var resolveSession by Delegates.notNull<ResolveSession>()
|
||||||
private val traceBasedCache = TraceBasedJavaResolverCache()
|
private val traceBasedCache = TraceBasedJavaResolverCache()
|
||||||
|
|
||||||
@@ -89,4 +83,11 @@ public class LazyResolveBasedCache() : JavaResolverCache {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tailRecursive
|
||||||
|
private fun FqName.each(operation: (FqName) -> Boolean) {
|
||||||
|
if (operation(this) && !isRoot()) {
|
||||||
|
parent().each(operation)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.resolve.scopes.JetScope;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ResolveSessionUtils {
|
public class ResolveSessionUtils {
|
||||||
|
|
||||||
@@ -105,15 +106,16 @@ public class ResolveSessionUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Name firstName = NamePackage.getFirstSegment(path);
|
List<Name> pathSegments = path.pathSegments();
|
||||||
|
Name firstName = pathSegments.get(0);
|
||||||
|
|
||||||
// Search in internal class
|
|
||||||
ClassifierDescriptor classifier = jetScope.getClassifier(firstName);
|
ClassifierDescriptor classifier = jetScope.getClassifier(firstName);
|
||||||
if (classifier instanceof ClassDescriptor) {
|
if (classifier instanceof ClassDescriptor) {
|
||||||
return findByQualifiedName(
|
return findByQualifiedName(
|
||||||
((ClassDescriptor) classifier).getUnsubstitutedInnerClassesScope(),
|
((ClassDescriptor) classifier).getUnsubstitutedInnerClassesScope(),
|
||||||
NamePackage.withoutFirstSegment(path),
|
new FqName(path.asString().substring(firstName.asString().length() + 1)),
|
||||||
filter);
|
filter
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: search in class object
|
// TODO: search in class object
|
||||||
|
|||||||
@@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.name
|
package org.jetbrains.kotlin.name
|
||||||
|
|
||||||
import org.jetbrains.kotlin.resolve.ImportPath
|
|
||||||
|
|
||||||
public fun FqName.isSubpackageOf(packageName: FqName): Boolean {
|
public fun FqName.isSubpackageOf(packageName: FqName): Boolean {
|
||||||
return when {
|
return when {
|
||||||
this == packageName -> true
|
this == packageName -> true
|
||||||
@@ -26,56 +24,36 @@ public fun FqName.isSubpackageOf(packageName: FqName): Boolean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun FqName.isParent(child: FqName): Boolean = child.isSubpackageOf(this)
|
private fun isSubpackageOf(subpackageNameStr: String, packageNameStr: String): Boolean {
|
||||||
|
return subpackageNameStr.startsWith(packageNameStr) && subpackageNameStr[packageNameStr.length()] == '.'
|
||||||
|
}
|
||||||
|
|
||||||
public fun FqName.isOneSegmentFQN(): Boolean = !isRoot() && parent().isRoot()
|
public fun FqName.isOneSegmentFQN(): Boolean = !isRoot() && parent().isRoot()
|
||||||
|
|
||||||
public fun FqName.withoutFirstSegment(): FqName {
|
|
||||||
if (isRoot() || parent().isRoot()) return FqName.ROOT
|
|
||||||
|
|
||||||
val fqNameStr = asString()
|
|
||||||
return FqName(fqNameStr.substring(fqNameStr.indexOf('.') + 1, fqNameStr.length()))
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun FqName.numberOfSegments(): Int {
|
|
||||||
return if (isRoot()) 0 else 1 + parent().numberOfSegments()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get tail part of the full fqn by subtracting head part.
|
* Get the tail part of the FQ name by stripping a prefix. If FQ name does not begin with the given prefix, it will be returned as is.
|
||||||
*
|
*
|
||||||
* @param headFQN
|
* Examples:
|
||||||
* @return tail fqn. If first part is not a begging of the full fqn, fullFQN will be returned.
|
* "org.jetbrains.kotlin".tail("org") = "jetbrains.kotlin"
|
||||||
|
* "org.jetbrains.kotlin".tail("") = "org.jetbrains.kotlin"
|
||||||
|
* "org.jetbrains.kotlin".tail("org.jetbrains.kotlin") = ""
|
||||||
|
* "org.jetbrains.kotlin".tail("org.jetbrains.gogland") = "org.jetbrains.kotlin"
|
||||||
*/
|
*/
|
||||||
public fun FqName.tail(headFQN: FqName): FqName {
|
public fun FqName.tail(prefix: FqName): FqName {
|
||||||
return when {
|
return when {
|
||||||
!isSubpackageOf(headFQN) || headFQN.isRoot() -> this
|
!isSubpackageOf(prefix) || prefix.isRoot() -> this
|
||||||
this == headFQN -> FqName.ROOT
|
this == prefix -> FqName.ROOT
|
||||||
else -> FqName(asString().substring(headFQN.asString().length + 1))
|
else -> FqName(asString().substring(prefix.asString().length() + 1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun FqName.isImported(importPath: ImportPath, skipAliasedImports: Boolean = true): Boolean {
|
|
||||||
return when {
|
|
||||||
skipAliasedImports && importPath.hasAlias() -> false
|
|
||||||
importPath.isAllUnder() && !isRoot() -> importPath.fqnPart() == this.parent()
|
|
||||||
else -> importPath.fqnPart() == this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun ImportPath.isImported(alreadyImported: ImportPath): Boolean {
|
|
||||||
return if (isAllUnder() || hasAlias()) this == alreadyImported else fqnPart().isImported(alreadyImported)
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun ImportPath.isImported(imports: Iterable<ImportPath>): Boolean = imports.any { isImported(it) }
|
|
||||||
|
|
||||||
// Check that it is javaName(\.javaName)* or an empty string
|
|
||||||
private enum class State {
|
private enum class State {
|
||||||
BEGINNING
|
BEGINNING
|
||||||
MIDDLE
|
MIDDLE
|
||||||
AFTER_DOT
|
AFTER_DOT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check that it is javaName(\.javaName)* or an empty string
|
||||||
public fun isValidJavaFqName(qualifiedName: String?): Boolean {
|
public fun isValidJavaFqName(qualifiedName: String?): Boolean {
|
||||||
if (qualifiedName == null) return false
|
if (qualifiedName == null) return false
|
||||||
|
|
||||||
@@ -98,22 +76,3 @@ public fun isValidJavaFqName(qualifiedName: String?): Boolean {
|
|||||||
|
|
||||||
return state != State.AFTER_DOT
|
return state != State.AFTER_DOT
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun FqName.getFirstSegment(): Name = this.pathSegments().first()
|
|
||||||
|
|
||||||
tailRecursive
|
|
||||||
public fun FqName.each(operation: (FqName) -> Boolean) {
|
|
||||||
if (operation(this) && !isRoot()) {
|
|
||||||
parent().each(operation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSubpackageOf(subpackageNameStr: String, packageNameStr: String): Boolean {
|
|
||||||
return subpackageNameStr == packageNameStr ||
|
|
||||||
(subpackageNameStr.startsWith(packageNameStr) && subpackageNameStr[packageNameStr.length()] == '.')
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getFirstSegment(fqn: String): String {
|
|
||||||
val dotIndex = fqn.indexOf('.')
|
|
||||||
return if ((dotIndex != -1)) fqn.substring(0, dotIndex) else fqn
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.psi.JetElement
|
|||||||
import org.jetbrains.kotlin.psi.JetCallExpression
|
import org.jetbrains.kotlin.psi.JetCallExpression
|
||||||
import org.jetbrains.kotlin.psi.JetUserType
|
import org.jetbrains.kotlin.psi.JetUserType
|
||||||
import org.jetbrains.kotlin.psi
|
import org.jetbrains.kotlin.psi
|
||||||
|
import org.jetbrains.kotlin.resolve.ImportPath
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns FqName for given declaration (either Java or Kotlin)
|
* Returns FqName for given declaration (either Java or Kotlin)
|
||||||
@@ -74,3 +75,17 @@ fun JetSimpleNameExpression.changeQualifiedName(fqName: FqName): JetElement {
|
|||||||
else -> elementToReplace.replace(psiFactory.createExpression(text))
|
else -> elementToReplace.replace(psiFactory.createExpression(text))
|
||||||
} as JetElement
|
} as JetElement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun FqName.isImported(importPath: ImportPath, skipAliasedImports: Boolean = true): Boolean {
|
||||||
|
return when {
|
||||||
|
skipAliasedImports && importPath.hasAlias() -> false
|
||||||
|
importPath.isAllUnder() && !isRoot() -> importPath.fqnPart() == this.parent()
|
||||||
|
else -> importPath.fqnPart() == this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun ImportPath.isImported(alreadyImported: ImportPath): Boolean {
|
||||||
|
return if (isAllUnder() || hasAlias()) this == alreadyImported else fqnPart().isImported(alreadyImported)
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun ImportPath.isImported(imports: Iterable<ImportPath>): Boolean = imports.any { isImported(it) }
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import java.util.HashSet
|
|||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
|
import org.jetbrains.kotlin.idea.refactoring.fqName.isImported
|
||||||
|
|
||||||
public class KotlinImportOptimizer() : ImportOptimizer {
|
public class KotlinImportOptimizer() : ImportOptimizer {
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.psi.JetNamedDeclaration
|
|||||||
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToDeclarationUtil
|
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToDeclarationUtil
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import org.jetbrains.kotlin.name.isImported
|
import org.jetbrains.kotlin.idea.refactoring.fqName.isImported
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
|
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ import com.intellij.openapi.project.Project
|
|||||||
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
||||||
import org.jetbrains.kotlin.idea.util.ImportInsertHelper.ImportDescriptorResult
|
import org.jetbrains.kotlin.idea.util.ImportInsertHelper.ImportDescriptorResult
|
||||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||||
|
import org.jetbrains.kotlin.idea.refactoring.fqName.isImported
|
||||||
|
|
||||||
public class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper() {
|
public class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper() {
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user