Skip to content

Commit

Permalink
Revert "Rename named argument usage on column rename (sqldelight#4027)…
Browse files Browse the repository at this point in the history
…" (sqldelight#4051)

This reverts commit 1c00bff.
  • Loading branch information
Alec Kazakova authored Apr 12, 2023
1 parent ec3c578 commit d19e01c
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 232 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import app.cash.sqldelight.core.lang.SqlDelightFile
import app.cash.sqldelight.core.lang.psi.StmtIdentifierMixin
import app.cash.sqldelight.core.lang.queriesName
import app.cash.sqldelight.core.psi.SqlDelightStmtIdentifier
import app.cash.sqldelight.core.psi.SqlDelightStmtList
import app.cash.sqldelight.intellij.usages.ReflectiveKotlinFindUsagesFactory
import com.alecstrong.sql.psi.core.psi.SqlColumnName
import com.alecstrong.sql.psi.core.psi.SqlStmt
import com.intellij.find.findUsages.FindUsagesHandler
import com.intellij.find.findUsages.FindUsagesHandlerFactory
import com.intellij.find.findUsages.FindUsagesOptions
Expand All @@ -21,17 +19,14 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiManager
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.parentOfType
import com.intellij.usageView.UsageInfo
import com.intellij.util.Processor
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.getValueParameters

internal class SqlDelightFindUsagesHandlerFactory : FindUsagesHandlerFactory() {
class SqlDelightFindUsagesHandlerFactory : FindUsagesHandlerFactory() {
override fun canFindUsages(element: PsiElement): Boolean {
val module = ModuleUtil.findModuleForPsiElement(element) ?: return false
val isConfigured = SqlDelightFileIndex.getInstance(module).isConfigured
Expand All @@ -44,7 +39,7 @@ internal class SqlDelightFindUsagesHandlerFactory : FindUsagesHandlerFactory() {
): FindUsagesHandler = SqlDelightIdentifierHandler(element)
}

internal class SqlDelightIdentifierHandler(
private class SqlDelightIdentifierHandler(
private val element: PsiElement,
) : FindUsagesHandler(element) {
private val factory = ReflectiveKotlinFindUsagesFactory(element.project)
Expand Down Expand Up @@ -133,19 +128,7 @@ internal fun PsiElement.generatedKtFiles(): List<KtFile> {
}

internal fun SqlColumnName.generatedProperties(): Collection<KtNamedDeclaration> {
val identifierList = parentOfType<SqlDelightStmtList>()?.stmtIdentifierList.orEmpty()
val namedStmts = identifierList.associateBy { it.getNextSiblingIgnoringWhitespaceAndComments() as? SqlStmt }
val stmtsWithColumn = namedStmts.keys.filterNotNull()
.filter { stmt ->
PsiTreeUtil.findChildrenOfType(stmt, SqlColumnName::class.java).any { it.textMatches(name) }
}

val generatedMethodParameters = stmtsWithColumn.mapNotNull { namedStmts[it] as? StmtIdentifierMixin }
.flatMap { it.generatedMethods() }
.flatMap { it.getValueParameters() }
.filter { it.name == name }

return generatedMethodParameters + generatedKtFiles()
return generatedKtFiles()
.flatMap { it.declarations.filterIsInstance<KtClass>() }
.filter(KtClass::isData)
.flatMap(KtClass::getPrimaryConstructorParameters)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,73 @@
package app.cash.sqldelight.intellij.inspections

import app.cash.sqldelight.core.lang.psi.StmtIdentifierMixin
import app.cash.sqldelight.core.lang.queriesName
import app.cash.sqldelight.core.lang.util.findChildOfType
import app.cash.sqldelight.core.psi.SqlDelightStmtIdentifier
import app.cash.sqldelight.core.psi.SqlDelightVisitor
import app.cash.sqldelight.intellij.SqlDelightFindUsagesHandlerFactory
import com.alecstrong.sql.psi.core.psi.SqlStmtList
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.codeInspection.LocalInspectionToolSession
import com.intellij.codeInspection.LocalQuickFixOnPsiElement
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.find.findUsages.FindUsagesOptions
import com.intellij.openapi.application.ReadActionProcessor
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.ReadOnlyModificationException
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiFile
import com.intellij.psi.SmartPointerManager
import com.intellij.psi.search.FilenameIndex
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.usageView.UsageInfo
import com.intellij.util.CommonProcessors
import org.jetbrains.kotlin.asJava.toLightMethods
import org.jetbrains.kotlin.psi.KtFile

internal class UnusedQueryInspection : LocalInspectionTool() {

private val findUsagesFactory = SqlDelightFindUsagesHandlerFactory()
override fun buildVisitor(
holder: ProblemsHolder,
isOnTheFly: Boolean,
session: LocalInspectionToolSession,
): PsiElementVisitor {
return ensureReady(session.file) {
object : SqlDelightVisitor() {
override fun visitStmtIdentifier(o: SqlDelightStmtIdentifier) {
ignoreInvalidElements {
if (o !is StmtIdentifierMixin || o.identifier() == null) {
return
}
if (!hasUsages(o)) {
holder.registerProblem(o, "Unused symbol", ProblemHighlightType.LIKE_UNUSED_SYMBOL, SafeDeleteQuickFix(o))
}
) = ensureReady(session.file) {
val fileName = "${sqlDelightFile.virtualFile?.queriesName}.kt"
val generatedFile = FilenameIndex.getFilesByName(
sqlDelightFile.project,
fileName,
GlobalSearchScope.moduleScope(module),
).firstOrNull() as KtFile? ?: return PsiElementVisitor.EMPTY_VISITOR
val allMethods = generatedFile.classes.firstOrNull()?.methods

if (allMethods == null) {
return PsiElementVisitor.EMPTY_VISITOR
}

return object : SqlDelightVisitor() {
override fun visitStmtIdentifier(o: SqlDelightStmtIdentifier) = ignoreInvalidElements {
if (o !is StmtIdentifierMixin || o.identifier() == null) {
return
}
val generatedMethods = allMethods.filter { namedFunction ->
namedFunction.name == o.identifier()?.text
}
for (generatedMethod in generatedMethods) {
val lightMethods = generatedMethod.toLightMethods()
if (lightMethods.any { ReferencesSearch.search(it, it.useScope).findFirst() != null }) {
return
}
}
holder.registerProblem(
o,
"Unused symbol",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
SafeDeleteQuickFix(o),
)
}
}
}

private fun hasUsages(stmtIdentifier: SqlDelightStmtIdentifier): Boolean {
if (!findUsagesFactory.canFindUsages(stmtIdentifier)) {
return false
}
val findFirstProcessor = CommonProcessors.FindFirstProcessor<UsageInfo>()
val readActionProcessor = ReadActionProcessor.wrapInReadAction(findFirstProcessor)
val findUsagesOptions = FindUsagesOptions(stmtIdentifier.project).apply {
isUsages = true
isSearchForTextOccurrences = false
}
val findUsagesHandler = findUsagesFactory.createFindUsagesHandler(stmtIdentifier, true)
findUsagesHandler.processElementUsages(stmtIdentifier, readActionProcessor, findUsagesOptions)
return findFirstProcessor.isFound
}

class SafeDeleteQuickFix(element: PsiElement) : LocalQuickFixOnPsiElement(element) {
private val ref = SmartPointerManager.getInstance(element.project)
.createSmartPsiElementPointer(element, element.containingFile)
Expand Down
3 changes: 0 additions & 3 deletions sqldelight-idea-plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@
implementation="app.cash.sqldelight.intellij.SqlDelightFindUsagesHandlerFactory"/>
<renamePsiElementProcessor
implementation="app.cash.sqldelight.intellij.SqlDelightRenameProcessor"/>
<renamePsiElementProcessor
implementation="app.cash.sqldelight.intellij.SqlDelightColumnRenameProcessor"/>
<psi.referenceContributor
language="SqlDelight"
implementation="app.cash.sqldelight.intellij.SqlDelightReferenceContributor"/>

<lang.findUsagesProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance

class FindUsagesTest : SqlDelightProjectTestCase() {

fun testFindsBothKotlinAndJavaUsages() {
// With the kotlin 1.3.30 plugin the test breaks. Find usages still works. Investigate later.
fun ignoretestFindsBothKotlinAndJavaUsages() {
myFixture.openFileInEditor(
tempRoot.findFileByRelativePath("src/main/java/com/example/SampleClass.java")!!,
)
Expand All @@ -40,7 +40,7 @@ class FindUsagesTest : SqlDelightProjectTestCase() {
}

// With the kotlin 1.3.30 plugin the test breaks. Find usages still works. Investigate later.
fun testFindsUsagesOfAllGeneratedMethods() {
fun ignoretestFindsUsagesOfAllGeneratedMethods() {
myFixture.openFileInEditor(
tempRoot.findFileByRelativePath("src/main/kotlin/com/example/KotlinClass.kt")!!,
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class SqlDelightFixtureTestCase : LightJavaCodeInsightFixtureTestCase()
override val deriveSchemaFromMigrations = false

override fun outputDirectory(file: SqlDelightFile) = outputDirectories()
override fun outputDirectories() = listOf("build")
override fun outputDirectories() = listOf("")

override fun sourceFolders(
file: SqlDelightFile,
Expand Down

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions sqldelight-idea-plugin/testData/find-usages/Example.kt

This file was deleted.

10 changes: 0 additions & 10 deletions sqldelight-idea-plugin/testData/find-usages/Example.sq

This file was deleted.

11 changes: 0 additions & 11 deletions sqldelight-idea-plugin/testData/find-usages/ExampleQueries.kt

This file was deleted.

8 changes: 0 additions & 8 deletions sqldelight-idea-plugin/testData/find-usages/Main.kt

This file was deleted.

8 changes: 0 additions & 8 deletions sqldelight-idea-plugin/testData/find-usages/MainAfter.kt

This file was deleted.

5 changes: 0 additions & 5 deletions sqldelight-idea-plugin/testData/find-usages/Query.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit d19e01c

Please sign in to comment.