Skip to content

Commit

Permalink
Suppress suggested refactoring for unused private and local declarations
Browse files Browse the repository at this point in the history
(cherry picked from commit 084276e)
  • Loading branch information
valentinkip authored and Alefas committed Mar 20, 2020
1 parent 05c3366 commit 6473e35
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jetbrains.kotlin.idea.refactoring.suggested

import com.intellij.openapi.util.Key
import com.intellij.psi.PsiNamedElement
import com.intellij.psi.search.LocalSearchScope
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.refactoring.suggested.*
import com.intellij.refactoring.suggested.SuggestedRefactoringSupport.Parameter
import com.intellij.refactoring.suggested.SuggestedRefactoringSupport.Signature
Expand All @@ -21,6 +24,21 @@ import kotlin.contracts.contract
class KotlinSuggestedRefactoringAvailability(refactoringSupport: SuggestedRefactoringSupport) :
SuggestedRefactoringAvailability(refactoringSupport)
{
private val HAS_USAGES = Key<Boolean>("KotlinSuggestedRefactoringAvailability.HAS_USAGES")

override fun amendStateInBackground(state: SuggestedRefactoringState): Iterator<SuggestedRefactoringState> {
return iterator {
if (state.additionalData[HAS_USAGES] == null) {
val declarationCopy = state.restoredDeclarationCopy()
val useScope = declarationCopy.useScope
if (useScope is LocalSearchScope) {
val hasUsages = ReferencesSearch.search(declarationCopy, useScope).findFirst() != null
yield(state.withAdditionalData(HAS_USAGES, hasUsages))
}
}
}
}

override fun refineSignaturesWithResolve(state: SuggestedRefactoringState): SuggestedRefactoringState {
val newDeclaration = state.declaration as? KtCallableDeclaration ?: return state
val oldDeclaration = state.restoredDeclarationCopy() as KtCallableDeclaration
Expand Down Expand Up @@ -108,9 +126,13 @@ class KotlinSuggestedRefactoringAvailability(refactoringSupport: SuggestedRefact
override fun detectAvailableRefactoring(state: SuggestedRefactoringState): SuggestedRefactoringData? {
val declaration = state.declaration
if (declaration !is KtCallableDeclaration || KotlinSuggestedRefactoringSupport.isOnlyRenameSupported(declaration)) {
if (state.additionalData[HAS_USAGES] == false) return null
return SuggestedRenameData(declaration as PsiNamedElement, state.oldSignature.name)
}

val overridesName = declaration.overridesName()
if (overridesName == null && state.additionalData[HAS_USAGES] == false) return null

val oldSignature = state.oldSignature
val newSignature = state.newSignature
val updateUsagesData = SuggestedChangeSignatureData.create(state, USAGES)
Expand All @@ -126,7 +148,7 @@ class KotlinSuggestedRefactoringAvailability(refactoringSupport: SuggestedRefact
return if (nameChanges > 0)
updateUsagesData
else
declaration.overridesName()?.let { updateUsagesData.copy(nameOfStuffToUpdate = it) }
overridesName?.let { updateUsagesData.copy(nameOfStuffToUpdate = it) }
}

return when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,36 @@ class KotlinSuggestedRefactoringAvailabilityTest : BaseSuggestedRefactoringAvail
)
}


fun testUnusedLocal() {
doTest(
"""
fun foo() {
val local<caret> = 0
}
""".trimIndent(),
{
myFixture.type("123")
},
expectedAvailability = Availability.Available(renameAvailableTooltip("local", "local123")),
expectedAvailabilityAfterBackgroundAmend = Availability.Disabled
)
}

fun testPrivateMethod() {
doTest(
"""
private fun foo(<caret>) {
}
""".trimIndent(),
{
myFixture.type("p: Int")
},
expectedAvailability = Availability.Available(changeSignatureAvailableTooltip("foo", "usages")),
expectedAvailabilityAfterBackgroundAmend = Availability.Disabled
)
}

private fun addImport(fqName: String) {
(file as KtFile).importList!!.add(KtPsiFactory(project).createImportDirective(ImportPath.fromString(fqName)))
}
Expand Down

0 comments on commit 6473e35

Please sign in to comment.