diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt index 12fcd3fdb0..063177d306 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt @@ -3,6 +3,8 @@ package com.github.shyiko.ktlint.ruleset.standard import com.github.shyiko.ktlint.core.Rule import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.com.intellij.lang.ASTNode +import org.jetbrains.kotlin.kdoc.lexer.KDocTokens +import org.jetbrains.kotlin.kdoc.psi.impl.KDocLink import org.jetbrains.kotlin.psi.KtImportDirective import org.jetbrains.kotlin.psi.KtPackageDirective import org.jetbrains.kotlin.psi.psiUtil.startOffset @@ -39,9 +41,14 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { if (node.elementType == KtStubElementTypes.FILE) { node.visit { node -> + val psi = node.psi val type = node.elementType + if (type == KDocTokens.MARKDOWN_LINK && psi is KDocLink) { + val linkText = psi.getLinkText().replace("`", "") + ref.add(linkText.split('.').first()) + } else if ((type == KtNodeTypes.REFERENCE_EXPRESSION || type == KtNodeTypes.OPERATION_REFERENCE) && - !node.psi.isPartOf(KtImportDirective::class)) { + !psi.isPartOf(KtImportDirective::class)) { ref.add(node.text.trim('`')) } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt index 755aaafcb7..f44b886c64 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt @@ -47,6 +47,42 @@ class NoUnusedImportsRuleTest { )).isEmpty() } + @Test + fun testLintKDocLinkImport() { + assertThat(NoUnusedImportsRule().lint( + """ + package kdoc + + import DRef + import p.PDRef + import DRef2 + import p.PDRef2 + import p.DRef3 + import p.PDRef3 + import p.PDRef4 + import p.PDRef5 + import p.O + + /** + * [DRef] DRef2 + * [O.method] + * [p.PDRef] p.PDRef2 + * [PDRef3](p.DRef3) p.PDRef4 PDRef5 + * [] text + */ + fun main() {} + """.trimIndent() + )).isEqualTo(listOf( + LintError(4, 1, "no-unused-imports", "Unused import"), + LintError(5, 1, "no-unused-imports", "Unused import"), + LintError(6, 1, "no-unused-imports", "Unused import"), + LintError(7, 1, "no-unused-imports", "Unused import"), + LintError(8, 1, "no-unused-imports", "Unused import"), + LintError(9, 1, "no-unused-imports", "Unused import"), + LintError(10, 1, "no-unused-imports", "Unused import") + )) + } + @Test fun testSamePackageImport() { assertThat(NoUnusedImportsRule().lint( @@ -123,4 +159,42 @@ class NoUnusedImportsRuleTest { ) } + @Test + fun testFormatKDocLinkImport() { + assertThat(NoUnusedImportsRule().format( + """ + package kdoc + + import DRef + import p.PDRef + import DRef2 + import p.PDRef2 + import p.DRef3 + import p.PDRef3 + import p.PDRef4 + import p.PDRef5 + + /** + * [DRef] DRef2 + * [p.PDRef] p.PDRef2 + * [PDRef3](p.DRef3) p.PDRef4 PDRef5 + */ + fun main() {} + """.trimIndent() + )).isEqualTo( + """ + package kdoc + + import DRef + + /** + * [DRef] DRef2 + * [p.PDRef] p.PDRef2 + * [PDRef3](p.DRef3) p.PDRef4 PDRef5 + */ + fun main() {} + """.trimIndent() + ) + } + }