New J2K: Move imports into JKImportList
This commit is contained in:
committed by
Ilya Kirillov
parent
86fa54fae9
commit
6182edadfd
@@ -69,18 +69,24 @@ class JavaToJKTreeBuilder constructor(
|
|||||||
private fun PsiJavaFile.toJK(): JKFile =
|
private fun PsiJavaFile.toJK(): JKFile =
|
||||||
JKFileImpl(
|
JKFileImpl(
|
||||||
packageStatement?.toJK() ?: JKPackageDeclarationImpl(JKNameIdentifierImpl("")),
|
packageStatement?.toJK() ?: JKPackageDeclarationImpl(JKNameIdentifierImpl("")),
|
||||||
importList?.toJK().orEmpty(),
|
importList.toJK(filterOutUsedImports = true),
|
||||||
with(declarationMapper) { classes.map { it.toJK() } }
|
with(declarationMapper) { classes.map { it.toJK() } }
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun PsiImportList.toJK(): List<JKImportStatement> =
|
private fun PsiImportList?.toJK(filterOutUsedImports: Boolean): JKImportList =
|
||||||
importStatements.filter { import ->
|
JKImportListImpl(
|
||||||
when {
|
this?.importStatements?.let { imports ->
|
||||||
import.isSingleUnusedImport() -> true
|
if (filterOutUsedImports) {
|
||||||
import.isOnDemand -> true
|
imports.filter { import ->
|
||||||
else -> false
|
when {
|
||||||
}
|
import.isSingleUnusedImport() -> true
|
||||||
}.map { it.toJK() }
|
import.isOnDemand -> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else imports.toList()
|
||||||
|
}?.map { it.toJK() }.orEmpty()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
private fun PsiImportStatement.isSingleUnusedImport(): Boolean {
|
private fun PsiImportStatement.isSingleUnusedImport(): Boolean {
|
||||||
@@ -953,7 +959,7 @@ class JavaToJKTreeBuilder constructor(
|
|||||||
}.last()
|
}.last()
|
||||||
|
|
||||||
|
|
||||||
fun buildTree(psi: PsiElement): JKTreeElement? =
|
fun buildTree(psi: PsiElement): JKTreeRoot? =
|
||||||
when (psi) {
|
when (psi) {
|
||||||
is PsiJavaFile -> psi.toJK()
|
is PsiJavaFile -> psi.toJK()
|
||||||
is PsiExpression -> with(expressionTreeMapper) { psi.toJK() }
|
is PsiExpression -> with(expressionTreeMapper) { psi.toJK() }
|
||||||
@@ -962,9 +968,16 @@ class JavaToJKTreeBuilder constructor(
|
|||||||
is PsiField -> with(declarationMapper) { psi.toJK() }
|
is PsiField -> with(declarationMapper) { psi.toJK() }
|
||||||
is PsiMethod -> with(declarationMapper) { psi.toJK() }
|
is PsiMethod -> with(declarationMapper) { psi.toJK() }
|
||||||
is PsiAnnotation -> with(declarationMapper) { psi.toJK() }
|
is PsiAnnotation -> with(declarationMapper) { psi.toJK() }
|
||||||
else ->
|
is PsiImportList -> psi.toJK(filterOutUsedImports = false)
|
||||||
null
|
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>()
|
private val tokenCache = mutableMapOf<PsiElement, JKNonCodeElement>()
|
||||||
|
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ class NewCodeBuilder {
|
|||||||
if (file.packageDeclaration.packageName.value.isNotEmpty()) {
|
if (file.packageDeclaration.packageName.value.isNotEmpty()) {
|
||||||
file.packageDeclaration.accept(this)
|
file.packageDeclaration.accept(this)
|
||||||
}
|
}
|
||||||
file.importList.forEach { it.accept(this) }
|
file.importList.accept(this)
|
||||||
file.declarationList.forEach { it.accept(this) }
|
file.declarationList.forEach { it.accept(this) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,6 +188,10 @@ class NewCodeBuilder {
|
|||||||
printer.printlnWithNoIndent(packageNameEscaped)
|
printer.printlnWithNoIndent(packageNameEscaped)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitImportListRaw(importList: JKImportList) {
|
||||||
|
importList.imports.forEach { it.accept(this) }
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitImportStatementRaw(importStatement: JKImportStatement) {
|
override fun visitImportStatementRaw(importStatement: JKImportStatement) {
|
||||||
printer.printWithNoIndent("import ")
|
printer.printWithNoIndent("import ")
|
||||||
val importNameEscaped =
|
val importNameEscaped =
|
||||||
|
|||||||
@@ -6,16 +6,16 @@
|
|||||||
package org.jetbrains.kotlin.nj2k.conversions
|
package org.jetbrains.kotlin.nj2k.conversions
|
||||||
|
|
||||||
import org.jetbrains.kotlin.nj2k.ImportStorage
|
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
|
import org.jetbrains.kotlin.nj2k.tree.JKTreeElement
|
||||||
|
|
||||||
|
|
||||||
class FilterImportsConversion : RecursiveApplicableConversionBase() {
|
class FilterImportsConversion : RecursiveApplicableConversionBase() {
|
||||||
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
override fun applyToElement(element: JKTreeElement): JKTreeElement {
|
||||||
if (element is JKFile) {
|
if (element is JKImportList) {
|
||||||
for (import in element.importList) {
|
for (import in element.imports) {
|
||||||
if (!ImportStorage.isImportNeeded(import.name.value)) {
|
if (!ImportStorage.isImportNeeded(import.name.value)) {
|
||||||
element.importList -= import
|
element.imports -= import
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,14 +36,14 @@ class JKTreeRootImpl(element: JKTreeElement) : JKTreeRoot, JKBranchElementBase()
|
|||||||
|
|
||||||
class JKFileImpl(
|
class JKFileImpl(
|
||||||
packageDeclaration: JKPackageDeclaration,
|
packageDeclaration: JKPackageDeclaration,
|
||||||
importList: List<JKImportStatement>,
|
importList: JKImportList,
|
||||||
declarationList: List<JKDeclaration>
|
declarationList: List<JKDeclaration>
|
||||||
) : JKFile, JKBranchElementBase(),
|
) : JKFile, JKBranchElementBase(),
|
||||||
PsiOwner by PsiOwnerImpl() {
|
PsiOwner by PsiOwnerImpl() {
|
||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitFile(this, data)
|
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 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)
|
override var declarationList by children(declarationList)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -573,6 +573,12 @@ class JKImportStatementImpl(name: JKNameIdentifier) : JKImportStatement, JKBranc
|
|||||||
override var rightNonCodeElements: List<JKNonCodeElement> = listOf(JKSpaceElementImpl("\n"))
|
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() {
|
class JKAnnotationParameterImpl(value: JKAnnotationMemberValue) : JKAnnotationParameter, JKBranchElementBase() {
|
||||||
override var value: JKAnnotationMemberValue by child(value)
|
override var value: JKAnnotationMemberValue by child(value)
|
||||||
|
|
||||||
|
|||||||
@@ -48,9 +48,13 @@ interface JKImportStatement : JKTreeElement {
|
|||||||
val name: JKNameIdentifier
|
val name: JKNameIdentifier
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface JKImportList : JKTreeElement {
|
||||||
|
var imports: List<JKImportStatement>
|
||||||
|
}
|
||||||
|
|
||||||
interface JKFile : JKTreeElement, JKBranchElement {
|
interface JKFile : JKTreeElement, JKBranchElement {
|
||||||
var packageDeclaration: JKPackageDeclaration
|
var packageDeclaration: JKPackageDeclaration
|
||||||
var importList: List<JKImportStatement>
|
var importList: JKImportList
|
||||||
var declarationList: List<JKDeclaration>
|
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 visitTreeRoot(treeRoot: JKTreeRoot, data: D): R = visitTreeElement(treeRoot, data)
|
||||||
fun visitDeclaration(declaration: JKDeclaration, data: D): R = visitTreeElement(declaration, data)
|
fun visitDeclaration(declaration: JKDeclaration, data: D): R = visitTreeElement(declaration, data)
|
||||||
fun visitImportStatement(importStatement: JKImportStatement, data: D): R = visitTreeElement(importStatement, 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 visitFile(file: JKFile, data: D): R = visitTreeElement(file, data)
|
||||||
fun visitClass(klass: JKClass, data: D): R = visitDeclaration(klass, data)
|
fun visitClass(klass: JKClass, data: D): R = visitDeclaration(klass, data)
|
||||||
fun visitInheritanceInfo(inheritanceInfo: JKInheritanceInfo, data: D): R = visitTreeElement(inheritanceInfo, 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)
|
override fun visitDeclaration(declaration: JKDeclaration, data: Nothing?) = visitDeclaration(declaration)
|
||||||
fun visitImportStatement(importStatement: JKImportStatement) = visitTreeElement(importStatement, null)
|
fun visitImportStatement(importStatement: JKImportStatement) = visitTreeElement(importStatement, null)
|
||||||
override fun visitImportStatement(importStatement: JKImportStatement, data: Nothing?) = visitImportStatement(importStatement)
|
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)
|
fun visitFile(file: JKFile) = visitTreeElement(file, null)
|
||||||
override fun visitFile(file: JKFile, data: Nothing?) = visitFile(file)
|
override fun visitFile(file: JKFile, data: Nothing?) = visitFile(file)
|
||||||
fun visitClass(klass: JKClass) = visitDeclaration(klass, null)
|
fun visitClass(klass: JKClass) = visitDeclaration(klass, null)
|
||||||
|
|||||||
@@ -38,6 +38,14 @@ interface JKVisitorWithCommentsPrinting : JKVisitorVoid {
|
|||||||
|
|
||||||
fun visitImportStatementRaw(importStatement: JKImportStatement) = visitTreeElementRaw(importStatement)
|
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) {
|
override fun visitFile(file: JKFile) {
|
||||||
printLeftNonCodeElements(file)
|
printLeftNonCodeElements(file)
|
||||||
visitFileRaw(file)
|
visitFileRaw(file)
|
||||||
|
|||||||
Reference in New Issue
Block a user