-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/beta' into generic-event
# Conflicts: # src/main/java/at/hannibal2/skyhanni/features/dungeon/DungeonLividFinder.kt
- Loading branch information
Showing
54 changed files
with
670 additions
and
94 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,19 @@ | ||
package at.hannibal2.skyhanni.detektrules | ||
|
||
enum class PreprocessingPattern(val text: String) { | ||
IF("#if"), | ||
ELSE("#else"), | ||
ELSEIF("#elseif"), | ||
ENDIF("#endif"), | ||
DOLLAR_DOLLAR("$$"), | ||
; | ||
|
||
val asComment: String | ||
get() = "//$text" | ||
|
||
companion object { | ||
fun String.containsPreprocessingPattern(): Boolean { | ||
return entries.any { this.contains(it.text) } | ||
} | ||
} | ||
} |
This file contains 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 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,114 @@ | ||
package at.hannibal2.skyhanni.detektrules.imports | ||
|
||
import at.hannibal2.skyhanni.detektrules.PreprocessingPattern | ||
import at.hannibal2.skyhanni.detektrules.PreprocessingPattern.Companion.containsPreprocessingPattern | ||
import io.gitlab.arturbosch.detekt.api.CodeSmell | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Entity | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import org.jetbrains.kotlin.psi.KtImportDirective | ||
import org.jetbrains.kotlin.psi.KtImportList | ||
|
||
class CustomImportOrdering(config: Config) : Rule(config) { | ||
override val issue = Issue( | ||
"CustomImportOrdering", | ||
Severity.Style, | ||
"Enforces correct import ordering, taking into account preprocessed imports.", | ||
Debt.FIVE_MINS, | ||
) | ||
|
||
companion object { | ||
private val importOrder = ImportSorter() | ||
|
||
private val packageImportOrdering = listOf("java.", "javax.", "kotlin.") | ||
|
||
private class ImportSorter : Comparator<KtImportDirective> { | ||
override fun compare( | ||
import1: KtImportDirective, | ||
import2: KtImportDirective, | ||
): Int { | ||
val importPath1 = import1.importPath!!.pathStr | ||
val importPath2 = import2.importPath!!.pathStr | ||
|
||
val isTypeAlias1 = import1.aliasName != null | ||
val isTypeAlias2 = import2.aliasName != null | ||
|
||
val index1 = packageImportOrdering.indexOfFirst { importPath1.startsWith(it) } | ||
val index2 = packageImportOrdering.indexOfFirst { importPath2.startsWith(it) } | ||
|
||
return when { | ||
isTypeAlias1 && isTypeAlias2 -> importPath1.compareTo(importPath2) | ||
isTypeAlias1 && !isTypeAlias2 -> 1 | ||
!isTypeAlias1 && isTypeAlias2 -> -1 | ||
index1 == -1 && index2 == -1 -> importPath1.compareTo(importPath2) | ||
index1 == -1 -> -1 | ||
index2 == -1 -> 1 | ||
else -> index1.compareTo(index2) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun isImportsCorrectlyOrdered(imports: List<KtImportDirective>, rawText: List<String>): Boolean { | ||
if (rawText.any { it.isBlank() }) { | ||
return false | ||
} | ||
|
||
var inPreprocess = false | ||
val linesToIgnore = mutableListOf<String>() | ||
|
||
for (line in rawText) { | ||
if (line.contains(PreprocessingPattern.IF.asComment)) { | ||
inPreprocess = true | ||
continue | ||
} | ||
if (line.contains(PreprocessingPattern.ENDIF.asComment)) { | ||
inPreprocess = false | ||
continue | ||
} | ||
if (line.contains(PreprocessingPattern.DOLLAR_DOLLAR.asComment)) { | ||
continue | ||
} | ||
if (inPreprocess) { | ||
linesToIgnore.add(line) | ||
} | ||
} | ||
|
||
val originalImports = rawText.filter { !it.containsPreprocessingPattern() && !linesToIgnore.contains(it) } | ||
val formattedOriginal = originalImports.joinToString("\n") { it } | ||
|
||
val expectedImports = imports.sortedWith(importOrder).map { "import ${it.importPath}" } | ||
val formattedExpected = expectedImports.filter { !linesToIgnore.contains(it) }.joinToString("\n") | ||
|
||
return formattedOriginal == formattedExpected | ||
} | ||
|
||
override fun visitImportList(importList: KtImportList) { | ||
|
||
val testEntity = Entity.from(importList) | ||
|
||
val rawText = importList.text.trim() | ||
if (rawText.isBlank()) { | ||
return | ||
} | ||
|
||
val importsCorrect = isImportsCorrectlyOrdered(importList.imports, rawText.lines()) | ||
|
||
if (!importsCorrect) { | ||
report( | ||
CodeSmell( | ||
issue, | ||
testEntity, | ||
"Imports must be ordered in lexicographic order without any empty lines in-between " + | ||
"with \"java\", \"javax\", \"kotlin\" and aliases in the end. This should then be followed by " + | ||
"pre-processed imports.", | ||
), | ||
) | ||
} | ||
|
||
super.visitImportList(importList) | ||
} | ||
} |
This file contains 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,17 @@ | ||
package at.hannibal2.skyhanni.detektrules.imports | ||
|
||
import com.google.auto.service.AutoService | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.RuleSet | ||
import io.gitlab.arturbosch.detekt.api.RuleSetProvider | ||
|
||
@AutoService(RuleSetProvider::class) | ||
class ImportRuleSetProvider : RuleSetProvider { | ||
override val ruleSetId: String = "ImportRules" | ||
|
||
override fun instance(config: Config): RuleSet { | ||
return RuleSet(ruleSetId, listOf( | ||
CustomImportOrdering(config) | ||
)) | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.