J2K ImportPath: kotlinify

This commit is contained in:
Pavel V. Talanov
2017-02-13 13:16:11 +03:00
parent 50d0f5bde6
commit babb3b557d
16 changed files with 45 additions and 77 deletions
@@ -384,7 +384,7 @@ class KtPsiFactory(private val project: Project) {
}
fun createImportDirective(importPath: ImportPath): KtImportDirective {
if (importPath.fqnPart().isRoot) {
if (importPath.fqName.isRoot) {
throw IllegalArgumentException("import path must not be empty")
}
@@ -20,29 +20,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.renderer.*
class ImportPath {
private val fqName: FqName
val alias: Name?
val isAllUnder: Boolean
@JvmOverloads constructor(fqName: FqName, isAllUnder: Boolean, alias: Name? = null) {
this.fqName = fqName
this.isAllUnder = isAllUnder
this.alias = alias
}
constructor(pathStr: String) {
if (pathStr.endsWith(".*")) {
this.isAllUnder = true
this.fqName = FqName(pathStr.substring(0, pathStr.length - 2))
}
else {
this.isAllUnder = false
this.fqName = FqName(pathStr)
}
alias = null
}
data class ImportPath @JvmOverloads constructor(val fqName: FqName, val isAllUnder: Boolean, val alias: Name? = null) {
val pathStr: String
get() = fqName.toUnsafe().render() + if (isAllUnder) ".*" else ""
@@ -51,10 +29,6 @@ class ImportPath {
return pathStr + if (alias != null) " as " + alias.asString() else ""
}
fun fqnPart(): FqName {
return fqName
}
fun hasAlias(): Boolean {
return alias != null
}
@@ -68,23 +42,15 @@ class ImportPath {
return null
}
override fun equals(o: Any?): Boolean {
if (this === o) return true
if (o == null || javaClass != o.javaClass) return false
companion object {
@JvmStatic fun fromString(pathStr: String): ImportPath {
if (pathStr.endsWith(".*")) {
return ImportPath(FqName(pathStr.substring(0, pathStr.length - 2)), isAllUnder = true)
}
else {
return ImportPath(FqName(pathStr), isAllUnder = false)
val path = o as ImportPath?
if (isAllUnder != path!!.isAllUnder) return false
if (if (alias != null) alias != path.alias else path.alias != null) return false
if (fqName != path.fqName) return false
return true
}
override fun hashCode(): Int {
var result = fqName.hashCode()
result = 31 * result + (alias?.hashCode() ?: 0)
result = 31 * result + if (isAllUnder) 1 else 0
return result
}
}
}
}
@@ -43,16 +43,18 @@ abstract class TargetPlatform(val platformName: String) {
object Default : TargetPlatform("Default") {
override fun getDefaultImports(languageVersionSettings: LanguageVersionSettings): List<ImportPath> = ArrayList<ImportPath>().apply {
add(ImportPath("kotlin.*"))
add(ImportPath("kotlin.annotation.*"))
add(ImportPath("kotlin.collections.*"))
add(ImportPath("kotlin.ranges.*"))
add(ImportPath("kotlin.sequences.*"))
add(ImportPath("kotlin.text.*"))
add(ImportPath("kotlin.io.*"))
listOf(
"kotlin.*",
"kotlin.annotation.*",
"kotlin.collections.*",
"kotlin.ranges.*",
"kotlin.sequences.*",
"kotlin.text.*",
"kotlin.io.*"
).forEach { add(ImportPath.fromString(it)) }
if (languageVersionSettings.supportsFeature(LanguageFeature.DefaultImportOfPackageKotlinComparisons)) {
add(ImportPath("kotlin.comparisons.*"))
add(ImportPath.fromString("kotlin.comparisons.*"))
}
}
@@ -55,7 +55,7 @@ class DefaultImportProvider(
defaultImports
.filter { it.isAllUnder }
.mapNotNull {
it.fqnPart().takeIf { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
it.fqName.takeIf { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
}
val nonKotlinAliasedTypeFqNames =
builtinTypeAliases
@@ -71,7 +71,7 @@ class FileScopeFactory(
val extraImports = file.originalFile.virtualFile?.let { vFile ->
val scriptExternalDependencies = getScriptExternalDependencies(vFile, file.project)
ktImportsFactory.createImportDirectives(scriptExternalDependencies?.imports?.map(::ImportPath).orEmpty())
ktImportsFactory.createImportDirectives(scriptExternalDependencies?.imports?.map { ImportPath.fromString(it) }.orEmpty())
}
val allImplicitImports = defaultImports concat extraImports