Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

gcx11
Copy link
Contributor

@gcx11 gcx11 commented Apr 25, 2020

@abelkov abelkov added the IDEA label Apr 27, 2020
@fedochet fedochet self-assigned this Apr 28, 2020
Copy link
Member

@fedochet fedochet left a 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
Copy link
Member

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() {
Copy link
Member

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 }
Copy link
Member

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
Copy link
Member

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)) {
Copy link
Member

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 {
Copy link
Member

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

Copy link
Contributor Author

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",
Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

@fedochet fedochet May 7, 2020

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

  1. 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
  2. In the case of import all other imports or import 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

Copy link
Contributor Author

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?

Copy link
Member

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",
Copy link
Member

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",
Copy link
Member

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

Copy link
Member

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 {
Copy link
Member

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

@fedochet
Copy link
Member

@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!

@fedochet fedochet closed this May 25, 2020
@vojkny
Copy link

vojkny commented May 25, 2020

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants