Browse Source

renamed classes

added comments wherever necessary
formatted with ktlint
pull/1768/head
thelumiereguy 3 years ago
parent
commit
05f2cf6fa7
  1. 14
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/remove_composable/RemoveComposableIntention.kt
  2. 17
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/remove_parent_composable/RemoveParentComposableIntention.kt
  3. 2
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composable_finder/ChildComposableFinder.kt
  4. 1
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composable_finder/ComposableFunctionFinder.kt
  5. 2
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composable_finder/ComposableFunctionFinderImpl.kt
  6. 24
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/get_root_psi_element/GetRootPsiElement.kt
  7. 8
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/wrap_with_composable/wrap_with_actions/BaseWrapWithComposableAction.kt
  8. 4
      idea-plugin/src/main/resources/intentionDescriptions/RemoveComposableIntention/after.kt.template
  9. 1
      idea-plugin/src/main/resources/intentionDescriptions/RemoveComposableIntention/before.kt.template
  10. 2
      idea-plugin/src/main/resources/intentionDescriptions/RemoveParentComposableIntention/after.kt.template
  11. 48
      idea-plugin/src/test/kotlin/org/jetbrains/compose/intentions/utils/get_root_psi_element/GetRootPsiElementTest.kt

14
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/remove_composable/RemoveComposableIntention.kt

@ -8,12 +8,16 @@ import com.intellij.openapi.util.Iconable
import com.intellij.psi.PsiElement
import org.jetbrains.compose.desktop.ide.preview.PreviewIcons
import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.composable_finder.DeepComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.get_root_element.GetRootElement
import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinderImpl
import org.jetbrains.compose.intentions.utils.get_root_psi_element.GetRootPsiElement
import org.jetbrains.compose.intentions.utils.is_intention_available.IsIntentionAvailable
import javax.swing.Icon
class RemoveComposableIntention : PsiElementBaseIntentionAction(), Iconable, LowPriorityAction, IsIntentionAvailable {
class RemoveComposableIntention :
PsiElementBaseIntentionAction(),
Iconable,
LowPriorityAction,
IsIntentionAvailable {
override fun getText(): String {
return "Remove this Composable"
@ -23,9 +27,9 @@ class RemoveComposableIntention : PsiElementBaseIntentionAction(), Iconable, Low
return "Compose Multiplatform intentions"
}
private val composableFunctionFinder: ComposableFunctionFinder = DeepComposableFunctionFinder()
private val composableFunctionFinder: ComposableFunctionFinder = ComposableFunctionFinderImpl()
private val getRootElement = GetRootElement()
private val getRootElement = GetRootPsiElement()
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.isAvailable(composableFunctionFinder)

17
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/remove_parent_composable/RemoveParentComposableIntention.kt

@ -6,14 +6,12 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Iconable
import com.intellij.psi.PsiElement
import com.intellij.psi.util.parentOfType
import org.jetbrains.compose.desktop.ide.preview.PreviewIcons
import org.jetbrains.compose.intentions.utils.composable_finder.ChildComposableFinder
import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.composable_finder.NestedComposableFinder
import org.jetbrains.compose.intentions.utils.get_root_psi_element.GetRootPsiElement
import org.jetbrains.compose.intentions.utils.is_intention_available.IsIntentionAvailable
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtValueArgumentList
import javax.swing.Icon
class RemoveParentComposableIntention :
@ -30,19 +28,16 @@ class RemoveParentComposableIntention :
return "Compose Multiplatform intentions"
}
private val composableFunctionFinder: ComposableFunctionFinder = NestedComposableFinder()
private val getRootElement = GetRootPsiElement()
private val composableFunctionFinder: ComposableFunctionFinder = ChildComposableFinder()
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.isAvailable(composableFunctionFinder)
}
override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
val wrapper = if (element.parent is KtValueArgumentList) {
element.parent.prevSibling as? KtNameReferenceExpression ?: return
} else {
element.parentOfType() ?: return
}
val callExpression = (wrapper.parent as? KtCallExpression) ?: return
val callExpression = getRootElement(element.parent) as? KtCallExpression ?: return
val lambdaBlock =
callExpression.lambdaArguments.firstOrNull()?.getLambdaExpression()?.functionLiteral?.bodyExpression
?: return

2
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composable_finder/NestedComposableFinder.kt → idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composable_finder/ChildComposableFinder.kt

@ -6,7 +6,7 @@ import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class NestedComposableFinder : ComposableFunctionFinder, IsPsiElementComposable {
class ChildComposableFinder : ComposableFunctionFinder, IsPsiElementComposable {
override fun isFunctionComposable(psiElement: PsiElement): Boolean {

1
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composable_finder/ComposableFunctionFinder.kt

@ -3,6 +3,5 @@ package org.jetbrains.compose.intentions.utils.composable_finder
import com.intellij.psi.PsiElement
interface ComposableFunctionFinder {
fun isFunctionComposable(psiElement: PsiElement): Boolean
}

2
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composable_finder/DeepComposableFunctionFinder.kt → idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composable_finder/ComposableFunctionFinderImpl.kt

@ -9,7 +9,7 @@ import org.jetbrains.kotlin.psi.KtPropertyDelegate
import org.jetbrains.kotlin.psi.KtValueArgumentList
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class DeepComposableFunctionFinder : ComposableFunctionFinder, IsPsiElementComposable {
class ComposableFunctionFinderImpl : ComposableFunctionFinder, IsPsiElementComposable {
override fun isFunctionComposable(psiElement: PsiElement): Boolean {
return when (psiElement) {

24
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/get_root_element/GetRootElement.kt → idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/get_root_psi_element/GetRootPsiElement.kt

@ -1,4 +1,4 @@
package org.jetbrains.compose.intentions.utils.get_root_element
package org.jetbrains.compose.intentions.utils.get_root_psi_element
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtCallExpression
@ -9,19 +9,21 @@ import org.jetbrains.kotlin.psi.KtPropertyDelegate
import org.jetbrains.kotlin.psi.KtValueArgumentList
/**
* KtValueArgumentList -> Parent -> KtNameReferenceExpression -> Parent -> KtCallExpression -> Parent -> KtPropertyDelegate -> Parent -> Property
* KtNameReferenceExpression -> Parent -> KtCallExpression -> Parent -> KtDotQualifiedExpression -> Parent -> KtPropertyDelegate -> Property
* KtNameReferenceExpression -> Parent -> KtCallExpression -> Parent -> KtPropertyDelegate -> Parent -> Property
* KtNameReferenceExpression -> Parent -> KtCallExpression -> Parent -> Property
* KtNameReferenceExpression -> Parent -> KtCallExpression
**/
class GetRootElement {
* To get the root element of a selected Psi element
*/
class GetRootPsiElement {
/**
* element can be CallExpression (Composable Function) or Property (Composable Property like remember)
* @param element can be
* 1. KtCallExpression, KtNameReferenceExpression - Box()
* 2. KtDotQualifiedExpression - repeatingAnimation.animateFloat
* 3. KtProperty - val systemUiController = rememberSystemUiController()
* 4. KtValueArgumentList - ()
*/
tailrec operator fun invoke(element: PsiElement, iteration: Int = 0): PsiElement? {
if (iteration > 5) { // fail safe
// To avoid infinite loops
if (iteration > 5) {
// Looking for a better way to handle this - throw error or return null
return null
}
@ -38,7 +40,7 @@ class GetRootElement {
else -> element
}
}
else -> element
else -> null
}
}
}

8
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/wrap_with_composable/wrap_with_actions/BaseWrapWithComposableAction.kt

@ -8,8 +8,8 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.composable_finder.DeepComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.get_root_element.GetRootElement
import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinderImpl
import org.jetbrains.compose.intentions.utils.get_root_psi_element.GetRootPsiElement
import org.jetbrains.compose.intentions.utils.is_intention_available.IsIntentionAvailable
abstract class BaseWrapWithComposableAction :
@ -18,11 +18,11 @@ abstract class BaseWrapWithComposableAction :
IsIntentionAvailable {
private val composableFunctionFinder: ComposableFunctionFinder by lazy {
DeepComposableFunctionFinder()
ComposableFunctionFinderImpl()
}
private val getRootElement by lazy {
GetRootElement()
GetRootPsiElement()
}
override fun getFamilyName(): String {

4
idea-plugin/src/main/resources/intentionDescriptions/RemoveComposableIntention/after.kt.template

@ -0,0 +1,4 @@
@Composable
fun Column() {
}

1
idea-plugin/src/main/resources/intentionDescriptions/RemoveComposableIntention/before.kt.template

@ -1,5 +1,4 @@
@Composable
fun Column() {
Text("Abc")
Text("Abc")
}

2
idea-plugin/src/main/resources/intentionDescriptions/RemoveParentComposableIntention/after.kt.template

@ -1,4 +1,4 @@
@Composable
Button() {
fun Column() {
Text("Abc")
}

48
idea-plugin/src/test/kotlin/org/jetbrains/compose/intentions/utils/get_root_element/GetRootElementTest.kt → idea-plugin/src/test/kotlin/org/jetbrains/compose/intentions/utils/get_root_psi_element/GetRootPsiElementTest.kt

@ -1,4 +1,4 @@
package org.jetbrains.compose.intentions.utils.get_root_element
package org.jetbrains.compose.intentions.utils.get_root_psi_element
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
import junit.framework.TestCase
@ -15,9 +15,9 @@ import org.junit.runner.RunWith
import org.junit.runners.JUnit4
@RunWith(JUnit4::class)
class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
class GetRootPsiElementTest : LightJavaCodeInsightFixtureTestCase() {
private val getRootElement = GetRootElement()
private val getRootElement = GetRootPsiElement()
@Test
fun `when a name reference expression is selected , but root is a property , the property should be returned as root element`() {
@ -27,7 +27,6 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val template = """
val systemUiController = rememberSystemUiController()
""".trimIndent()
.trim()
val file = ktPsiFactory.createFile(template)
@ -35,7 +34,9 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val ktNameReferenceExpression = (property.lastChild as KtCallExpression).firstChild as KtNameReferenceExpression
TestCase.assertEquals(property, getRootElement(ktNameReferenceExpression))
TestCase.assertEquals("rememberSystemUiController", ktNameReferenceExpression.text)
TestCase.assertEquals(property, getRootElement.invoke(ktNameReferenceExpression))
}
@Test
@ -44,15 +45,11 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
@Language("Kotlin")
val template = """
@Composable
fun Box(block:()->Unit) {
}
fun OuterComposable() {
// Call Expression - Box
// |
// v
Box() {
}
@ -64,10 +61,13 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val ktNamedFunction = file.lastChild as KtNamedFunction
val callExpression = ktNamedFunction.lastChild.children.find { it is KtCallExpression }!!
val ktNameReferenceExpression = callExpression.firstChild as KtNameReferenceExpression
TestCase.assertEquals("Box", ktNameReferenceExpression.text)
TestCase.assertEquals(
callExpression,
getRootElement(callExpression.firstChild as KtNameReferenceExpression)
getRootElement.invoke(ktNameReferenceExpression)
)
}
@ -83,7 +83,7 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
}
fun OuterComposable() {
// Argument List Element - (
// Argument List Element
// |
// v
Box() {
@ -100,9 +100,11 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val argumentListElement = callExpression.firstChild.nextSibling as KtValueArgumentList
TestCase.assertEquals("()", argumentListElement.text)
TestCase.assertEquals(
callExpression,
getRootElement(argumentListElement)
getRootElement.invoke(argumentListElement)
)
}
@ -112,9 +114,6 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
@Language("Kotlin")
val template = """
// Delegated property
// |
// v
var isComposable by remember {
true
}
@ -126,9 +125,11 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val referenceExpression = property.lastChild.lastChild.firstChild as KtNameReferenceExpression
TestCase.assertEquals("remember", referenceExpression.text)
TestCase.assertEquals(
property,
getRootElement(referenceExpression)
getRootElement.invoke(referenceExpression)
)
}
@ -140,9 +141,6 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val template = """
val repeatingAnimation = rememberInfiniteTransition()
// Dot qualified expression
// |
// v
val offset by repeatingAnimation.animateFloat(
0f,
-20f,
@ -164,9 +162,12 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val referenceExpression = dotQualifiedExpression.lastChild.firstChild as KtNameReferenceExpression
TestCase.assertEquals("animateFloat", referenceExpression.text)
TestCase.assertEquals(
property,
getRootElement(referenceExpression)
getRootElement.invoke(referenceExpression)
)
}
@ -178,9 +179,6 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val template = """
val repeatingAnimation = rememberInfiniteTransition()
// Dot qualified expression
// |
// v
val offset = repeatingAnimation.animateFloat(
0f,
-20f,
@ -202,9 +200,11 @@ class GetRootElementTest : LightJavaCodeInsightFixtureTestCase() {
val referenceExpression = dotQualifiedExpression.lastChild.firstChild as KtNameReferenceExpression
TestCase.assertEquals("animateFloat", referenceExpression.text)
TestCase.assertEquals(
property,
getRootElement(referenceExpression)
getRootElement.invoke(referenceExpression)
)
}
}
Loading…
Cancel
Save