Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement FullQualifiedNameGuesser * Remove bad test * Refactor tests * Fix AnnotationExcluder * More exhaustive list of default import classes * Handle PR comments
- Loading branch information
1 parent
1ca3ab5
commit 2d00cab
Showing
7 changed files
with
434 additions
and
88 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
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
55 changes: 55 additions & 0 deletions
55
detekt-psi-utils/src/main/kotlin/io/github/detekt/psi/internal/FullQualifiedNameGuesser.kt
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,55 @@ | ||
package io.github.detekt.psi.internal | ||
|
||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.jetbrains.kotlin.psi.KtImportDirective | ||
import org.jetbrains.kotlin.utils.addIfNotNull | ||
import kotlin.LazyThreadSafetyMode.NONE | ||
|
||
class FullQualifiedNameGuesser internal constructor( | ||
private val packageName: String?, | ||
imports: List<KtImportDirective>, | ||
) { | ||
|
||
constructor(root: KtFile) : this( | ||
packageName = root.packageDirective?.qualifiedName?.ifBlank { null }, | ||
imports = root.importList?.imports.orEmpty(), | ||
) | ||
|
||
@Suppress("ClassOrdering") | ||
private val resolvedNames: Map<String, String> by lazy(NONE) { | ||
imports | ||
.asSequence() | ||
.filterNot { it.isAllUnder } | ||
.mapNotNull { import -> | ||
import.importedFqName?.toString()?.let { fqImport -> | ||
(import.alias?.name ?: fqImport.substringAfterLast('.')) to fqImport | ||
} | ||
} | ||
.toMap() | ||
} | ||
|
||
fun getFullQualifiedName(name: String): Set<String> { | ||
val resolvedName = findName(name) | ||
return if (resolvedName != null) { | ||
setOf(resolvedName) | ||
} else { | ||
mutableSetOf<String>() | ||
.apply { | ||
addIfNotNull(defaultImportClasses[name]) | ||
if (packageName != null) { | ||
add("$packageName.$name") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun findName(name: String): String? { | ||
val searchName = name.substringBefore('.') | ||
val resolvedName = resolvedNames[searchName] | ||
return if (name == searchName) { | ||
resolvedName | ||
} else { | ||
"$resolvedName.${name.substringAfter('.')}" | ||
} | ||
} | ||
} |
Oops, something went wrong.