New J2K: Move imports into JKImportList

This commit is contained in:
Ilya Kirillov
2019-03-29 15:17:09 +03:00
committed by Ilya Kirillov
parent 86fa54fae9
commit 6182edadfd
8 changed files with 59 additions and 21 deletions
@@ -69,18 +69,24 @@ class JavaToJKTreeBuilder constructor(
private fun PsiJavaFile.toJK(): JKFile =
JKFileImpl(
packageStatement?.toJK() ?: JKPackageDeclarationImpl(JKNameIdentifierImpl("")),
importList?.toJK().orEmpty(),
importList.toJK(filterOutUsedImports = true),
with(declarationMapper) { classes.map { it.toJK() } }
)
private fun PsiImportList.toJK(): List<JKImportStatement> =
importStatements.filter { import ->
when {
import.isSingleUnusedImport() -> true
import.isOnDemand -> true
else -> false
}
}.map { it.toJK() }
private fun PsiImportList?.toJK(filterOutUsedImports: Boolean): JKImportList =
JKImportListImpl(
this?.importStatements?.let { imports ->
if (filterOutUsedImports) {
imports.filter { import ->
when {
import.isSingleUnusedImport() -> true
import.isOnDemand -> true
else -> false
}
}
} else imports.toList()
}?.map { it.toJK() }.orEmpty()
)
private fun PsiImportStatement.isSingleUnusedImport(): Boolean {
@@ -953,7 +959,7 @@ class JavaToJKTreeBuilder constructor(
}.last()
fun buildTree(psi: PsiElement): JKTreeElement? =
fun buildTree(psi: PsiElement): JKTreeRoot? =
when (psi) {
is PsiJavaFile -> psi.toJK()
is PsiExpression -> with(expressionTreeMapper) { psi.toJK() }
@@ -962,9 +968,16 @@ class JavaToJKTreeBuilder constructor(
is PsiField -> with(declarationMapper) { psi.toJK() }
is PsiMethod -> with(declarationMapper) { psi.toJK() }
is PsiAnnotation -> with(declarationMapper) { psi.toJK() }
else ->
null
}
is PsiImportList -> psi.toJK(filterOutUsedImports = false)
is PsiImportStatement -> psi.toJK()
is PsiJavaCodeReferenceElement ->
if (psi.parent is PsiReferenceList) {
val factory = JavaPsiFacade.getInstance(psi.project).elementFactory
val type = factory.createType(psi)
JKTypeElementImpl(type.toJK(symbolProvider).updateNullabilityRecursively(Nullability.NotNull))
} else null
else -> null
}?.let { JKTreeRootImpl(it) }
private val tokenCache = mutableMapOf<PsiElement, JKNonCodeElement>()
@@ -172,7 +172,7 @@ class NewCodeBuilder {
if (file.packageDeclaration.packageName.value.isNotEmpty()) {
file.packageDeclaration.accept(this)
}
file.importList.forEach { it.accept(this) }
file.importList.accept(this)
file.declarationList.forEach { it.accept(this) }
}
@@ -188,6 +188,10 @@ class NewCodeBuilder {
printer.printlnWithNoIndent(packageNameEscaped)
}
override fun visitImportListRaw(importList: JKImportList) {
importList.imports.forEach { it.accept(this) }
}
override fun visitImportStatementRaw(importStatement: JKImportStatement) {
printer.printWithNoIndent("import ")
val importNameEscaped =
@@ -6,16 +6,16 @@
package org.jetbrains.kotlin.nj2k.conversions
import org.jetbrains.kotlin.nj2k.ImportStorage
import org.jetbrains.kotlin.nj2k.tree.JKFile
import org.jetbrains.kotlin.nj2k.tree.JKImportList
import org.jetbrains.kotlin.nj2k.tree.JKTreeElement
class FilterImportsConversion : RecursiveApplicableConversionBase() {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
if (element is JKFile) {
for (import in element.importList) {
if (element is JKImportList) {
for (import in element.imports) {
if (!ImportStorage.isImportNeeded(import.name.value)) {
element.importList -= import
element.imports -= import
}
}
}
@@ -36,14 +36,14 @@ class JKTreeRootImpl(element: JKTreeElement) : JKTreeRoot, JKBranchElementBase()
class JKFileImpl(
packageDeclaration: JKPackageDeclaration,
importList: List<JKImportStatement>,
importList: JKImportList,
declarationList: List<JKDeclaration>
) : JKFile, JKBranchElementBase(),
PsiOwner by PsiOwnerImpl() {
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitFile(this, data)
override var packageDeclaration: JKPackageDeclaration by child(packageDeclaration)
override var importList: List<JKImportStatement> by children(importList)
override var importList: JKImportList by child(importList)
override var declarationList by children(declarationList)
}
@@ -573,6 +573,12 @@ class JKImportStatementImpl(name: JKNameIdentifier) : JKImportStatement, JKBranc
override var rightNonCodeElements: List<JKNonCodeElement> = listOf(JKSpaceElementImpl("\n"))
}
class JKImportListImpl(imports: List<JKImportStatement>) : JKImportList, JKBranchElementBase() {
override var imports by children(imports)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitImportList(this, data)
}
class JKAnnotationParameterImpl(value: JKAnnotationMemberValue) : JKAnnotationParameter, JKBranchElementBase() {
override var value: JKAnnotationMemberValue by child(value)
@@ -48,9 +48,13 @@ interface JKImportStatement : JKTreeElement {
val name: JKNameIdentifier
}
interface JKImportList : JKTreeElement {
var imports: List<JKImportStatement>
}
interface JKFile : JKTreeElement, JKBranchElement {
var packageDeclaration: JKPackageDeclaration
var importList: List<JKImportStatement>
var importList: JKImportList
var declarationList: List<JKDeclaration>
}
@@ -7,6 +7,7 @@ interface JKVisitor<out R, in D> {
fun visitTreeRoot(treeRoot: JKTreeRoot, data: D): R = visitTreeElement(treeRoot, data)
fun visitDeclaration(declaration: JKDeclaration, data: D): R = visitTreeElement(declaration, data)
fun visitImportStatement(importStatement: JKImportStatement, data: D): R = visitTreeElement(importStatement, data)
fun visitImportList(importList: JKImportList, data: D): R = visitTreeElement(importList, data)
fun visitFile(file: JKFile, data: D): R = visitTreeElement(file, data)
fun visitClass(klass: JKClass, data: D): R = visitDeclaration(klass, data)
fun visitInheritanceInfo(inheritanceInfo: JKInheritanceInfo, data: D): R = visitTreeElement(inheritanceInfo, data)
@@ -11,6 +11,8 @@ interface JKVisitorVoid : JKVisitor<Unit, Nothing?> {
override fun visitDeclaration(declaration: JKDeclaration, data: Nothing?) = visitDeclaration(declaration)
fun visitImportStatement(importStatement: JKImportStatement) = visitTreeElement(importStatement, null)
override fun visitImportStatement(importStatement: JKImportStatement, data: Nothing?) = visitImportStatement(importStatement)
fun visitImportList(importList: JKImportList) = visitTreeElement(importList, null)
override fun visitImportList(importList: JKImportList, data: Nothing?) = visitImportList(importList)
fun visitFile(file: JKFile) = visitTreeElement(file, null)
override fun visitFile(file: JKFile, data: Nothing?) = visitFile(file)
fun visitClass(klass: JKClass) = visitDeclaration(klass, null)
@@ -38,6 +38,14 @@ interface JKVisitorWithCommentsPrinting : JKVisitorVoid {
fun visitImportStatementRaw(importStatement: JKImportStatement) = visitTreeElementRaw(importStatement)
override fun visitImportList(importList: JKImportList) {
printLeftNonCodeElements(importList)
visitImportListRaw(importList)
printRightNonCodeElements(importList)
}
fun visitImportListRaw(importList: JKImportList) = visitTreeElementRaw(importList)
override fun visitFile(file: JKFile) {
printLeftNonCodeElements(file)
visitFileRaw(file)