-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KT-10974 Add Code Style: Import Layout Configuration Table #3336
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.idea.core.formatter | ||
|
||
import com.intellij.openapi.application.ApplicationBundle | ||
import org.jetbrains.kotlin.resolve.ImportPath | ||
|
||
class KotlinPackageEntry( | ||
packageName: String, | ||
val withSubpackages: Boolean | ||
) { | ||
val packageName = packageName.removeSuffix(".*") | ||
|
||
companion object { | ||
@JvmField | ||
val ALL_OTHER_IMPORTS_ENTRY = | ||
KotlinPackageEntry(ApplicationBundle.message("listbox.import.all.other.imports"), withSubpackages = true) | ||
|
||
@JvmField | ||
val ALL_OTHER_ALIAS_IMPORTS_ENTRY = KotlinPackageEntry("<all other alias imports>", withSubpackages = true) | ||
} | ||
|
||
fun matchesPackageName(otherPackageName: String): Boolean { | ||
if (isSpecial) return true | ||
|
||
if (otherPackageName.startsWith(packageName)) { | ||
if (otherPackageName.length == packageName.length) return true | ||
if (withSubpackages) { | ||
if (otherPackageName[packageName.length] == '.') return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
fun isBetterMatchForPackageThan(entry: KotlinPackageEntry?, path: ImportPath): Boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fedochet I don't think that would work I would have to extract call to |
||
if (!matchesPackageName(path.pathStr)) return false | ||
if (entry == null) return true | ||
|
||
if (this == ALL_OTHER_ALIAS_IMPORTS_ENTRY && path.hasAlias()) return true | ||
if (entry == ALL_OTHER_ALIAS_IMPORTS_ENTRY && path.hasAlias()) return false | ||
|
||
if (entry == ALL_OTHER_IMPORTS_ENTRY && this != ALL_OTHER_ALIAS_IMPORTS_ENTRY) return true | ||
if (this == ALL_OTHER_IMPORTS_ENTRY && entry != ALL_OTHER_ALIAS_IMPORTS_ENTRY) return false | ||
if (entry.withSubpackages != withSubpackages) return !withSubpackages | ||
|
||
return entry.packageName.count { it == '.' } < packageName.count { it == '.' } | ||
} | ||
|
||
val isSpecial: Boolean get() = this == ALL_OTHER_IMPORTS_ENTRY || this == ALL_OTHER_ALIAS_IMPORTS_ENTRY | ||
|
||
override fun toString(): String { | ||
return packageName | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.idea.core.formatter | ||
|
||
import com.intellij.openapi.util.InvalidDataException | ||
import com.intellij.openapi.util.JDOMExternalizable | ||
import org.jdom.Element | ||
|
||
class KotlinPackageEntryTable : JDOMExternalizable, Cloneable { | ||
private val entries = mutableListOf<KotlinPackageEntry>() | ||
|
||
val entryCount: Int get() = entries.size | ||
|
||
public override fun clone(): KotlinPackageEntryTable { | ||
val clone = KotlinPackageEntryTable() | ||
clone.copyFrom(this) | ||
return clone | ||
} | ||
|
||
fun copyFrom(packageTable: KotlinPackageEntryTable) { | ||
entries.clear() | ||
entries.addAll(packageTable.entries) | ||
} | ||
|
||
fun getEntries(): Array<KotlinPackageEntry> { | ||
return entries.toTypedArray() | ||
} | ||
|
||
fun insertEntryAt(entry: KotlinPackageEntry, index: Int) { | ||
entries.add(index, entry) | ||
} | ||
|
||
fun removeEntryAt(index: Int) { | ||
entries.removeAt(index) | ||
} | ||
|
||
fun getEntryAt(index: Int): KotlinPackageEntry { | ||
return entries[index] | ||
} | ||
|
||
fun setEntryAt(entry: KotlinPackageEntry, index: Int) { | ||
entries[index] = entry | ||
} | ||
|
||
operator fun contains(packageName: String): Boolean { | ||
return entries.any { !it.isSpecial && it.matchesPackageName(packageName) } | ||
} | ||
|
||
fun removeEmptyPackages() { | ||
entries.removeAll { it.packageName.isBlank() } | ||
} | ||
|
||
fun addEntry(entry: KotlinPackageEntry) { | ||
entries.add(entry) | ||
} | ||
|
||
override fun readExternal(element: Element) { | ||
entries.clear() | ||
|
||
element.children.forEach { | ||
if (it.name == "package") { | ||
val packageName = it.getAttributeValue("name") ?: throw InvalidDataException() | ||
val alias = it.getAttributeValue("alias")?.toBoolean() ?: false | ||
val withSubpackages = it.getAttributeValue("withSubpackages")?.toBoolean() ?: false | ||
|
||
val entry = when { | ||
packageName.isEmpty() && !alias -> KotlinPackageEntry.ALL_OTHER_IMPORTS_ENTRY | ||
packageName.isEmpty() && alias -> KotlinPackageEntry.ALL_OTHER_ALIAS_IMPORTS_ENTRY | ||
else -> KotlinPackageEntry(packageName, withSubpackages) | ||
} | ||
|
||
entries.add(entry) | ||
} | ||
} | ||
} | ||
|
||
override fun writeExternal(parentNode: Element) { | ||
for (entry in entries) { | ||
val element = Element("package") | ||
parentNode.addContent(element) | ||
val name = if (entry.isSpecial) "" else entry.packageName | ||
val alias = (entry == KotlinPackageEntry.ALL_OTHER_ALIAS_IMPORTS_ENTRY) | ||
|
||
element.setAttribute("name", name) | ||
element.setAttribute("alias", alias.toString()) | ||
element.setAttribute("withSubpackages", entry.withSubpackages.toString()) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function can be private