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 com.intellij.psi.PsiElement
import org.jetbrains.compose.desktop.ide.preview.PreviewIcons 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.ComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.composable_finder.DeepComposableFunctionFinder import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinderImpl
import org.jetbrains.compose.intentions.utils.get_root_element.GetRootElement import org.jetbrains.compose.intentions.utils.get_root_psi_element.GetRootPsiElement
import org.jetbrains.compose.intentions.utils.is_intention_available.IsIntentionAvailable import org.jetbrains.compose.intentions.utils.is_intention_available.IsIntentionAvailable
import javax.swing.Icon import javax.swing.Icon
class RemoveComposableIntention : PsiElementBaseIntentionAction(), Iconable, LowPriorityAction, IsIntentionAvailable { class RemoveComposableIntention :
PsiElementBaseIntentionAction(),
Iconable,
LowPriorityAction,
IsIntentionAvailable {
override fun getText(): String { override fun getText(): String {
return "Remove this Composable" return "Remove this Composable"
@ -23,9 +27,9 @@ class RemoveComposableIntention : PsiElementBaseIntentionAction(), Iconable, Low
return "Compose Multiplatform intentions" 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 { override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.isAvailable(composableFunctionFinder) 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.project.Project
import com.intellij.openapi.util.Iconable import com.intellij.openapi.util.Iconable
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.util.parentOfType
import org.jetbrains.compose.desktop.ide.preview.PreviewIcons 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.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.compose.intentions.utils.is_intention_available.IsIntentionAvailable
import org.jetbrains.kotlin.psi.KtCallExpression import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtValueArgumentList
import javax.swing.Icon import javax.swing.Icon
class RemoveParentComposableIntention : class RemoveParentComposableIntention :
@ -30,19 +28,16 @@ class RemoveParentComposableIntention :
return "Compose Multiplatform intentions" 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 { override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.isAvailable(composableFunctionFinder) return element.isAvailable(composableFunctionFinder)
} }
override fun invoke(project: Project, editor: Editor?, element: PsiElement) { override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
val wrapper = if (element.parent is KtValueArgumentList) { val callExpression = getRootElement(element.parent) as? KtCallExpression ?: return
element.parent.prevSibling as? KtNameReferenceExpression ?: return
} else {
element.parentOfType() ?: return
}
val callExpression = (wrapper.parent as? KtCallExpression) ?: return
val lambdaBlock = val lambdaBlock =
callExpression.lambdaArguments.firstOrNull()?.getLambdaExpression()?.functionLiteral?.bodyExpression callExpression.lambdaArguments.firstOrNull()?.getLambdaExpression()?.functionLiteral?.bodyExpression
?: return ?: 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.KtLambdaArgument
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class NestedComposableFinder : ComposableFunctionFinder, IsPsiElementComposable { class ChildComposableFinder : ComposableFunctionFinder, IsPsiElementComposable {
override fun isFunctionComposable(psiElement: PsiElement): Boolean { 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 import com.intellij.psi.PsiElement
interface ComposableFunctionFinder { interface ComposableFunctionFinder {
fun isFunctionComposable(psiElement: PsiElement): Boolean 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.KtValueArgumentList
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class DeepComposableFunctionFinder : ComposableFunctionFinder, IsPsiElementComposable { class ComposableFunctionFinderImpl : ComposableFunctionFinder, IsPsiElementComposable {
override fun isFunctionComposable(psiElement: PsiElement): Boolean { override fun isFunctionComposable(psiElement: PsiElement): Boolean {
return when (psiElement) { 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 com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtCallExpression import org.jetbrains.kotlin.psi.KtCallExpression
@ -9,19 +9,21 @@ import org.jetbrains.kotlin.psi.KtPropertyDelegate
import org.jetbrains.kotlin.psi.KtValueArgumentList import org.jetbrains.kotlin.psi.KtValueArgumentList
/** /**
* KtValueArgumentList -> Parent -> KtNameReferenceExpression -> Parent -> KtCallExpression -> Parent -> KtPropertyDelegate -> Parent -> Property * To get the root element of a selected Psi element
* KtNameReferenceExpression -> Parent -> KtCallExpression -> Parent -> KtDotQualifiedExpression -> Parent -> KtPropertyDelegate -> Property */
* KtNameReferenceExpression -> Parent -> KtCallExpression -> Parent -> KtPropertyDelegate -> Parent -> Property class GetRootPsiElement {
* KtNameReferenceExpression -> Parent -> KtCallExpression -> Parent -> Property
* KtNameReferenceExpression -> Parent -> KtCallExpression
**/
class GetRootElement {
/** /**
* 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? { 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 return null
} }
@ -38,7 +40,7 @@ class GetRootElement {
else -> element 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.openapi.project.Project
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinder import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.composable_finder.DeepComposableFunctionFinder import org.jetbrains.compose.intentions.utils.composable_finder.ComposableFunctionFinderImpl
import org.jetbrains.compose.intentions.utils.get_root_element.GetRootElement import org.jetbrains.compose.intentions.utils.get_root_psi_element.GetRootPsiElement
import org.jetbrains.compose.intentions.utils.is_intention_available.IsIntentionAvailable import org.jetbrains.compose.intentions.utils.is_intention_available.IsIntentionAvailable
abstract class BaseWrapWithComposableAction : abstract class BaseWrapWithComposableAction :
@ -18,11 +18,11 @@ abstract class BaseWrapWithComposableAction :
IsIntentionAvailable { IsIntentionAvailable {
private val composableFunctionFinder: ComposableFunctionFinder by lazy { private val composableFunctionFinder: ComposableFunctionFinder by lazy {
DeepComposableFunctionFinder() ComposableFunctionFinderImpl()
} }
private val getRootElement by lazy { private val getRootElement by lazy {
GetRootElement() GetRootPsiElement()
} }
override fun getFamilyName(): String { 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 @Composable
fun Column() { fun Column() {
Text("Abc") Text("Abc")
Text("Abc")
} }

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

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