-
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
Conversation
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.
Thanks for your contribution @gcx11 ! Please, take a look at my commentary
Feel free to ask questions if something is not clear
} | ||
|
||
fun matchesPackageName(otherPackageName: String): Boolean { | ||
if (this == ALL_OTHER_IMPORTS_ENTRY || this == ALL_OTHER_ALIAS_IMPORTS_ENTRY) return true |
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.
isSpecial
can be used here, I guess
} | ||
|
||
val isSpecial: Boolean | ||
get() { |
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.
You can rewrite it as an expression, without return
if (other !is KotlinPackageEntryTable) return false | ||
if (other.entries.size != entries.size) return false | ||
|
||
return entries.zip(other.entries).any { (entry, otherEntry) -> entry != otherEntry } |
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 looks like a regular ==
for lists
Also, it seems strange that when any package is not equals, equals
returns true
for (entry in entries) { | ||
val element = Element("package") | ||
parentNode.addContent(element) | ||
val name = if (entry == KotlinPackageEntry.ALL_OTHER_IMPORTS_ENTRY || entry == KotlinPackageEntry.ALL_OTHER_ALIAS_IMPORTS_ENTRY) "" else entry.packageName |
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.
isSpecial
should work here too
|
||
operator fun contains(packageName: String): Boolean { | ||
for (entry in entries) { | ||
if (packageName.startsWith(entry.packageName)) { |
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 code is actually repeated in the matchesPackageName
return false | ||
} | ||
|
||
fun isBetterMatchForPackageThan(entry: KotlinPackageEntry?, path: ImportPath): Boolean { |
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.
I don't think that this KotlinPackageEntry?
is necessary here, you can just check for null before passing it
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.
@fedochet I don't think that would work
I would have to extract call to matchesPackageName
as well otherwise we would end up selecting incorrect package in method bestMatchIndex
|
||
when (entry) { | ||
KotlinPackageEntry.ALL_OTHER_IMPORTS_ENTRY -> append( | ||
"all other imports", |
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.
Please, use ApplicationBundle.message
here
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.
@fedochet I couldn't find property with such text, does that mean I should create new one?
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.
@gcx11 I am sorry, but it seems like I've misguided you a little bit about what messages should be externalized
It seems like the only message that should be externalized is the "Import aliases separately"
message
You can add this message in this file and then use KotlinBundle.message
to obtain it: https://github.com/JetBrains/kotlin/blob/f494b4ce11b3f950199242d035ee803ac7c59a57/idea/resources/messages/KotlinBundle.properties
The messages that you've fixed actually do not require fixing, because
setButtonComparator
expects buttons names, which seems to be hardcoded, and replacing them with even slightly different messages will lead to broken (or just default) sorting- In the case of
import all other imports
orimport aliases
it would be better to have them in English, as it is more a code example then a UI message
I am sorry that you have to revert some of your changes back, I should have paid more attention to the code
If it is troublesome for you to revert those changes, please let me know here and I will just amend them into your commits myself before the merge (your authorship of the code will remain intact, but I will merge the PR by hand, not on the Github)
Of course, you are welcome to fix the problem yourself if you like to
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.
@fedochet I have sent new commit that should address your last comment, would you mind to look at the PR again?
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.
@gcx11 sure! I will take a look tomorrow
.setAddAction { addPackage() } | ||
.setRemoveAction { removePackage() } | ||
.setButtonComparator( | ||
"Add", |
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.
Please, use ApplicationBundle.message
here
entry != null && entry != KotlinPackageEntry.ALL_OTHER_IMPORTS_ENTRY && entry != KotlinPackageEntry.ALL_OTHER_ALIAS_IMPORTS_ENTRY | ||
}.setButtonComparator( | ||
ApplicationBundle.message("button.add.package"), | ||
"Remove", |
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.
Please, use ApplicationBundle.message
here
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.
I am sure that there are some preexisting messages for this
class ImportPathComparator( | ||
private val packageTable: KotlinPackageEntryTable | ||
) : Comparator<ImportPath> { | ||
override fun compare(import1: ImportPath, import2: ImportPath): Int { |
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.
I would rewrite it like this:
private val comparator = compareBy<ImportPath>(
{ import -> bestMatchIndex(import) },
{ import -> import.toString() }
)
override fun compare(import1: ImportPath, import2: ImportPath): Int =
comparator.compare(import1, import2)
IMO it is clearer and leaves less room for error
@gcx11 hi! I've had to change the sorting implementation a little bit, since it wasn't compatible with the legacy implementation. The problem was that the aliased imports were not sorted according to the order of the entries, and they should (so adding this sorting layout would not change the default import sorting in all projects) I've decided to amend the fix into your commit and merge it by hand, I hope you don't mind that Commits from this PR in the master start from 8849a66 Thanks again for your contribution! |
When will this be available? Is there any build we can have this working? |
https://youtrack.jetbrains.com/issue/KT-10974