-
Notifications
You must be signed in to change notification settings - Fork 12
feat: improve support for parsing Kotlin DSL, using KotlinEditor. #109
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ kotlin { | |
|
||
dependencies { | ||
api libs.grammar | ||
api libs.kotlinEditor.core | ||
|
||
testImplementation libs.spock | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.squareup.sort | ||
|
||
internal class Configuration( | ||
private val configuration: String, | ||
val level: Int, | ||
/** | ||
* Android support. A "variant" configuration looks like "debugApi", "releaseImplementation", etc. | ||
* The variant will be "debug", "release", etc. | ||
*/ | ||
var variant: String? = null | ||
) { | ||
|
||
companion object { | ||
val values = listOf( | ||
"api" to { Configuration("api", 0) }, | ||
"implementation" to { Configuration("implementation", 1) }, | ||
"compileOnlyApi" to { Configuration("compileOnlyApi", 2) }, | ||
"compileOnly" to { Configuration("compileOnly", 3) }, | ||
"runtimeOnly" to { Configuration("runtimeOnly", 4) }, | ||
"annotationProcessor" to { Configuration("annotationProcessor", 5) }, | ||
"kapt" to { Configuration("kapt", 6) }, | ||
"testImplementation" to { Configuration("testImplementation", 7) }, | ||
"testCompileOnly" to { Configuration("testCompileOnly", 8) }, | ||
"testRuntimeOnly" to { Configuration("testRuntimeOnly", 9) }, | ||
"androidTestImplementation" to { Configuration("androidTestImplementation", 10) }, | ||
) | ||
|
||
fun of(configuration: String): Configuration? { | ||
fun findConfiguration( | ||
predicate: (Pair<String, () -> Configuration>) -> Boolean | ||
): Configuration? { | ||
return values.find(predicate)?.second?.invoke() | ||
} | ||
|
||
// Try to find an exact match | ||
var matchingConfiguration = findConfiguration { it.first == configuration } | ||
|
||
// If that failed, look for a variant | ||
if (matchingConfiguration == null) { | ||
matchingConfiguration = findConfiguration { configuration.endsWith(it.first, true) } | ||
if (matchingConfiguration != null) { | ||
matchingConfiguration.variant = configuration.substring( | ||
0, | ||
configuration.length - matchingConfiguration.configuration.length | ||
) | ||
} | ||
} | ||
|
||
// Look for a variant again | ||
if (matchingConfiguration == null) { | ||
matchingConfiguration = findConfiguration { configuration.startsWith(it.first, true) } | ||
if (matchingConfiguration != null) { | ||
matchingConfiguration.variant = configuration.substring( | ||
configuration.length - matchingConfiguration.configuration.length, | ||
configuration.length | ||
) | ||
} | ||
} | ||
|
||
return matchingConfiguration | ||
} | ||
|
||
@JvmStatic | ||
fun stringCompare( | ||
left: String, | ||
right: String | ||
): Int { | ||
val leftC = of(left) | ||
val rightC = of(right) | ||
|
||
// Null means they don't map to a known configuration. So, compare by String natural order. | ||
if (leftC == null && rightC == null) return left.compareTo(right) | ||
// Unknown configuration is "higher than" known | ||
if (rightC == null) return 1 | ||
if (leftC == null) return -1 | ||
|
||
val c = leftC.level.compareTo(rightC.level) | ||
|
||
// If each maps to a known configuration, and they're different, we can return that value | ||
if (c != 0) return c | ||
// If each maps to the same configuration, we now differentiate based on whether variants are | ||
// involved. Non-variants are "higher than" variants. | ||
if (leftC.variant != null && rightC.variant != null) { | ||
return rightC.variant!!.compareTo(leftC.variant!!) | ||
} | ||
return if (rightC.variant != null) return -1 else 1 | ||
} | ||
} | ||
} |
99 changes: 0 additions & 99 deletions
99
sort/src/main/kotlin/com/squareup/sort/ConfigurationComparator.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.