forked from cashapp/sqldelight
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "Rename named argument usage on column rename (cashapp…
…#4027)" (cashapp#4051)" This reverts commit d19e01c.
- Loading branch information
1 parent
a40ae74
commit b45b706
Showing
21 changed files
with
266 additions
and
46 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...ea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightColumnRenameProcessor.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,42 @@ | ||
package app.cash.sqldelight.intellij | ||
|
||
import com.alecstrong.sql.psi.core.psi.SqlColumnName | ||
import com.intellij.find.findUsages.FindUsagesOptions | ||
import com.intellij.openapi.application.ReadActionProcessor | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiReference | ||
import com.intellij.psi.search.SearchScope | ||
import com.intellij.refactoring.rename.RenamePsiElementProcessor | ||
import com.intellij.usageView.UsageInfo | ||
import com.intellij.util.CommonProcessors | ||
|
||
class SqlDelightColumnRenameProcessor : RenamePsiElementProcessor() { | ||
|
||
private val findUsagesHandlerFactory = SqlDelightFindUsagesHandlerFactory() | ||
override fun canProcessElement(element: PsiElement): Boolean { | ||
return element is SqlColumnName && findUsagesHandlerFactory.canFindUsages(element) | ||
} | ||
|
||
override fun findReferences( | ||
element: PsiElement, | ||
searchScope: SearchScope, | ||
searchInCommentsAndStrings: Boolean, | ||
): Collection<PsiReference> { | ||
if (element !is SqlColumnName) { | ||
return super.findReferences(element, searchScope, searchInCommentsAndStrings) | ||
} | ||
|
||
val collectProcessor = CommonProcessors.CollectProcessor<UsageInfo>() | ||
val readActionProcessor = ReadActionProcessor.wrapInReadAction(collectProcessor) | ||
val findUsagesHandler = findUsagesHandlerFactory.createFindUsagesHandler(element, false) | ||
findUsagesHandler.processElementUsages( | ||
/* element = */ element, | ||
/* processor = */ readActionProcessor, | ||
/* options = */ FindUsagesOptions(element.project).apply { | ||
isUsages = true | ||
isSearchForTextOccurrences = false | ||
}, | ||
) | ||
return collectProcessor.results.mapNotNull { it.reference } | ||
} | ||
} |
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
72 changes: 33 additions & 39 deletions
72
...-plugin/src/main/kotlin/app/cash/sqldelight/intellij/inspections/UnusedQueryInspection.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
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
21 changes: 21 additions & 0 deletions
21
...lugin/src/test/kotlin/app/cash/sqldelight/intellij/SqlDelightColumnRenameProcessorTest.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,21 @@ | ||
package app.cash.sqldelight.intellij | ||
|
||
import com.alecstrong.sql.psi.core.psi.SqlColumnName | ||
|
||
class SqlDelightColumnRenameProcessorTest : SqlDelightFixtureTestCase() { | ||
|
||
override val fixtureDirectory: String = "find-usages" | ||
|
||
fun testColumnRename() { | ||
myFixture.configureByFile("Example.sq") | ||
myFixture.copyFileToProject("Main.kt", "main/kotlin/com/example/Main.kt") | ||
myFixture.copyFileToProject("Example.kt", "build/com/example/Example.kt") | ||
myFixture.copyFileToProject("ExampleQueries.kt", "build/com/example/ExampleQueries.kt") | ||
myFixture.copyFileToProject("Query.kt", "build/com/example/Query.kt") | ||
val sqlColumnName = myFixture.findElementByText("id", SqlColumnName::class.java) | ||
|
||
myFixture.renameElement(sqlColumnName, "newId") | ||
|
||
myFixture.checkResultByFile("main/kotlin/com/example/Main.kt", "MainAfter.kt", false) | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
...ea-plugin/src/test/kotlin/app/cash/sqldelight/intellij/SqlDelightIdentifierHandlerTest.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,22 @@ | ||
package app.cash.sqldelight.intellij | ||
|
||
import com.alecstrong.sql.psi.core.psi.SqlColumnName | ||
import com.google.common.truth.Truth.assertThat | ||
|
||
class SqlDelightIdentifierHandlerTest : SqlDelightFixtureTestCase() { | ||
|
||
override val fixtureDirectory: String = "find-usages" | ||
|
||
fun testFindColumnUsages() { | ||
myFixture.configureByFile("Example.sq") | ||
myFixture.copyFileToProject("Main.kt", "main/kotlin/com/example/Main.kt") | ||
myFixture.copyFileToProject("Example.kt", "build/com/example/Example.kt") | ||
myFixture.copyFileToProject("ExampleQueries.kt", "build/com/example/ExampleQueries.kt") | ||
myFixture.copyFileToProject("Query.kt", "build/com/example/Query.kt") | ||
val sqlColumnName = myFixture.findElementByText("id", SqlColumnName::class.java) | ||
|
||
val usageInfos = myFixture.findUsages(sqlColumnName) | ||
|
||
assertThat(usageInfos.size).isEqualTo(6) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...gin/src/test/kotlin/app/cash/sqldelight/intellij/inspections/UnusedQueryInspectionTest.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,18 @@ | ||
package app.cash.sqldelight.intellij.inspections | ||
|
||
import app.cash.sqldelight.intellij.SqlDelightFixtureTestCase | ||
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper | ||
|
||
class UnusedQueryInspectionTest : SqlDelightFixtureTestCase() { | ||
|
||
override val fixtureDirectory: String = "unused-query-inspection" | ||
|
||
fun testInspection() { | ||
myFixture.copyFileToProject("Main.kt", "main/kotlin/com/example/Main.kt") | ||
myFixture.copyFileToProject("SomeTableQueries.kt", "build/com/example/SomeTableQueries.kt") | ||
myFixture.copyFileToProject("Example.kt", "build/com/example/Example.kt") | ||
myFixture.copyFileToProject("Query.kt", "build/com/example/Query.kt") | ||
|
||
myFixture.testInspection("", LocalInspectionToolWrapper(UnusedQueryInspection())) | ||
} | ||
} |
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,9 @@ | ||
package com.example | ||
|
||
import kotlin.Long | ||
import kotlin.String | ||
|
||
public data class Example( | ||
public val id: Long, | ||
public val name: String, | ||
) |
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,10 @@ | ||
CREATE TABLE example ( | ||
id INTEGER NOT NULL PRIMARY KEY, | ||
name TEXT NOT NULL | ||
); | ||
|
||
selectAll: | ||
SELECT id, name FROM example; | ||
|
||
selectById: | ||
SELECT id, name FROM example WHERE id = ?; |
11 changes: 11 additions & 0 deletions
11
sqldelight-idea-plugin/testData/find-usages/ExampleQueries.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,11 @@ | ||
package com.example | ||
|
||
import Query | ||
import kotlin.Long | ||
|
||
public class ExampleQueries() { | ||
|
||
public fun selectById(id: Long): Query<Example> { | ||
return Query(Example(1, "foo")) | ||
} | ||
} |
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,8 @@ | ||
// ktlint-disable filename | ||
package com.example | ||
|
||
fun main() { | ||
val exampleQueries = ExampleQueries() | ||
val query = exampleQueries.selectById(id = 1L) | ||
val id = query.executeAsOneOrNull()?.id | ||
} |
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,8 @@ | ||
// ktlint-disable filename | ||
package com.example | ||
|
||
fun main() { | ||
val exampleQueries = ExampleQueries() | ||
val query = exampleQueries.selectById(newId = 1L) | ||
val id = query.executeAsOneOrNull()?.newId | ||
} |
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,5 @@ | ||
class Query<T>(val value: T) { | ||
fun executeAsOneOrNull(): T { | ||
return value | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
sqldelight-idea-plugin/testData/unused-query-inspection/Example.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,9 @@ | ||
package com.example | ||
|
||
import kotlin.Long | ||
import kotlin.String | ||
|
||
public data class Example( | ||
public val id: Long, | ||
public val name: String, | ||
) |
6 changes: 6 additions & 0 deletions
6
sqldelight-idea-plugin/testData/unused-query-inspection/Main.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,6 @@ | ||
package com.example | ||
|
||
fun main() { | ||
val someTableQueries = SomeTableQueries() | ||
someTableQueries.selectAll() | ||
} |
5 changes: 5 additions & 0 deletions
5
sqldelight-idea-plugin/testData/unused-query-inspection/Query.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,5 @@ | ||
class Query<T>(val value: T) { | ||
fun executeAsOneOrNull(): T { | ||
return value | ||
} | ||
} |
Oops, something went wrong.