Browse Source

removed icon from intentions

renamed live template names
cleaned up interfaces
pull/1768/head
thelumiereguy 2 years ago
parent
commit
a72c049204
  1. 13
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/RemoveComposableIntention.kt
  2. 12
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/RemoveParentComposableIntention.kt
  3. 8
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/WrapWithComposableIntentionGroup.kt
  4. 36
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/PsiUtils.kt
  5. 4
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composableFinder/ChildComposableFinder.kt
  6. 5
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composableFinder/ComposableFunctionFinderImpl.kt
  7. 14
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/getRootPsiElement/GetRootPsiElement.kt
  8. 24
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/isIntentionAvailable/IsIntentionAvailable.kt
  9. 20
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/isPsiElementComposable/IsPsiElementComposable.kt
  10. 7
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/wrapActions/BaseWrapWithComposableAction.kt
  11. 12
      idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/wrapActions/WrapWithActions.kt
  12. 12
      idea-plugin/src/main/resources/META-INF/plugin.xml
  13. 2
      idea-plugin/src/main/resources/intentionDescriptions/RemoveComposableIntention/description.html
  14. 2
      idea-plugin/src/main/resources/intentionDescriptions/RemoveParentComposableIntention/description.html
  15. 42
      idea-plugin/src/main/resources/templates/ComposeMultiplatformTemplates.xml

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

@ -4,20 +4,15 @@ import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
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.composableFinder.ComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.composableFinder.ComposableFunctionFinderImpl
import org.jetbrains.compose.intentions.utils.getRootPsiElement.GetRootPsiElement
import org.jetbrains.compose.intentions.utils.isIntentionAvailable.IsIntentionAvailable
import javax.swing.Icon
import org.jetbrains.compose.intentions.utils.isIntentionAvailable
class RemoveComposableIntention :
PsiElementBaseIntentionAction(),
Iconable,
LowPriorityAction,
IsIntentionAvailable {
LowPriorityAction {
override fun getText(): String {
return "Remove this Composable"
@ -32,12 +27,10 @@ class RemoveComposableIntention :
private val getRootElement = GetRootPsiElement()
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.isAvailable(composableFunctionFinder)
return element.isIntentionAvailable(composableFunctionFinder)
}
override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
getRootElement(element.parent)?.delete()
}
override fun getIcon(flags: Int): Icon = PreviewIcons.COMPOSE
}

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

@ -6,19 +6,17 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Iconable
import com.intellij.psi.PsiElement
import javax.swing.Icon
import org.jetbrains.compose.desktop.ide.preview.PreviewIcons
import org.jetbrains.compose.intentions.utils.composableFinder.ChildComposableFinder
import org.jetbrains.compose.intentions.utils.composableFinder.ComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.getRootPsiElement.GetRootPsiElement
import org.jetbrains.compose.intentions.utils.isIntentionAvailable.IsIntentionAvailable
import org.jetbrains.compose.intentions.utils.isIntentionAvailable
import org.jetbrains.kotlin.psi.KtCallExpression
import javax.swing.Icon
class RemoveParentComposableIntention :
PsiElementBaseIntentionAction(),
Iconable,
PriorityAction,
IsIntentionAvailable {
PriorityAction {
override fun getText(): String {
return "Remove the parent Composable"
@ -33,7 +31,7 @@ class RemoveParentComposableIntention :
private val composableFunctionFinder: ComposableFunctionFinder = ChildComposableFinder()
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.isAvailable(composableFunctionFinder)
return element.isIntentionAvailable(composableFunctionFinder)
}
override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
@ -44,8 +42,6 @@ class RemoveParentComposableIntention :
callExpression.replace(lambdaBlock)
}
override fun getIcon(flags: Int): Icon = PreviewIcons.COMPOSE
override fun getPriority(): PriorityAction.Priority {
return PriorityAction.Priority.NORMAL
}

8
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/WrapWithComposableIntentionGroup.kt

@ -6,10 +6,8 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.ListPopup
import com.intellij.openapi.ui.popup.PopupStep
import com.intellij.openapi.ui.popup.util.BaseListPopupStep
import com.intellij.openapi.util.Iconable
import com.intellij.psi.PsiFile
import com.intellij.ui.popup.list.ListPopupImpl
import org.jetbrains.compose.desktop.ide.preview.PreviewIcons
import org.jetbrains.compose.intentions.wrapActions.BaseWrapWithComposableAction
import org.jetbrains.compose.intentions.wrapActions.WrapWithBoxIntention
import org.jetbrains.compose.intentions.wrapActions.WrapWithCardIntention
@ -17,7 +15,6 @@ import org.jetbrains.compose.intentions.wrapActions.WrapWithColumnIntention
import org.jetbrains.compose.intentions.wrapActions.WrapWithLzyColumnIntention
import org.jetbrains.compose.intentions.wrapActions.WrapWithLzyRowIntention
import org.jetbrains.compose.intentions.wrapActions.WrapWithRowIntention
import javax.swing.Icon
class WrapWithComposableIntentionGroup :
IntentionActionGroup<BaseWrapWithComposableAction>(
@ -29,8 +26,7 @@ class WrapWithComposableIntentionGroup :
WrapWithLzyColumnIntention(),
WrapWithLzyRowIntention()
)
),
Iconable {
) {
private fun createPopup(
project: Project,
@ -68,6 +64,4 @@ class WrapWithComposableIntentionGroup :
override fun getGroupText(actions: List<BaseWrapWithComposableAction>): String {
return "Wrap with Composable"
}
override fun getIcon(flags: Int): Icon = PreviewIcons.COMPOSE
}

36
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/PsiUtils.kt

@ -0,0 +1,36 @@
package org.jetbrains.compose.intentions.utils
import com.intellij.psi.PsiElement
import org.jetbrains.compose.desktop.ide.preview.isComposableFunction
import org.jetbrains.compose.intentions.utils.composableFinder.ComposableFunctionFinder
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.nj2k.postProcessing.resolve
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
internal fun KtCallExpression.isComposable(): Boolean {
return getChildOfType<KtNameReferenceExpression>()?.isComposable() ?: false
}
internal fun KtNameReferenceExpression.isComposable(): Boolean {
val ktNamedFunction = resolve() as? KtNamedFunction ?: return false
return ktNamedFunction.isComposableFunction()
}
internal fun PsiElement.isIntentionAvailable(
composableFunctionFinder: ComposableFunctionFinder
): Boolean {
if (language != KotlinLanguage.INSTANCE) {
return false
}
if (!isWritable) {
return false
}
return parent?.let { parentPsiElement ->
composableFunctionFinder.isFunctionComposable(parentPsiElement)
} ?: false
}

4
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composableFinder/ChildComposableFinder.kt

@ -1,12 +1,12 @@
package org.jetbrains.compose.intentions.utils.composableFinder
import com.intellij.psi.PsiElement
import org.jetbrains.compose.intentions.utils.isPsiElementComposable.IsPsiElementComposable
import org.jetbrains.compose.intentions.utils.isComposable
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class ChildComposableFinder : ComposableFunctionFinder, IsPsiElementComposable {
class ChildComposableFinder : ComposableFunctionFinder {
override fun isFunctionComposable(psiElement: PsiElement): Boolean {

5
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/composableFinder/ComposableFunctionFinderImpl.kt

@ -1,7 +1,7 @@
package org.jetbrains.compose.intentions.utils.composableFinder
import com.intellij.psi.PsiElement
import org.jetbrains.compose.intentions.utils.isPsiElementComposable.IsPsiElementComposable
import org.jetbrains.compose.intentions.utils.isComposable
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtProperty
@ -9,12 +9,11 @@ import org.jetbrains.kotlin.psi.KtPropertyDelegate
import org.jetbrains.kotlin.psi.KtValueArgumentList
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class ComposableFunctionFinderImpl : ComposableFunctionFinder, IsPsiElementComposable {
class ComposableFunctionFinderImpl : ComposableFunctionFinder {
override fun isFunctionComposable(psiElement: PsiElement): Boolean {
return when (psiElement) {
is KtNameReferenceExpression -> psiElement.isComposable()
is KtCallExpression -> psiElement.isComposable()
is KtProperty -> detectComposableFromKtProperty(psiElement)
is KtValueArgumentList -> {
val parent = psiElement.parent as? KtCallExpression ?: return false

14
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/getRootPsiElement/GetRootPsiElement.kt

@ -20,23 +20,17 @@ class GetRootPsiElement {
* 3. KtProperty - val systemUiController = rememberSystemUiController()
* 4. KtValueArgumentList - ()
*/
tailrec operator fun invoke(element: PsiElement, iteration: Int = 0): PsiElement? {
// To avoid infinite loops
if (iteration > 5) {
// Looking for a better way to handle this - throw error or return null
return null
}
tailrec operator fun invoke(element: PsiElement): PsiElement? {
return when (element) {
is KtProperty -> element
is KtNameReferenceExpression,
is KtValueArgumentList -> invoke(element.parent, iteration + 1)
is KtValueArgumentList -> invoke(element.parent)
is KtDotQualifiedExpression,
is KtCallExpression -> {
when (element.parent) {
is KtProperty,
is KtDotQualifiedExpression -> invoke(element.parent, iteration + 1) // composable dot expression
is KtPropertyDelegate -> invoke(element.parent.parent, iteration + 1) // composable dot expression
is KtDotQualifiedExpression -> invoke(element.parent) // composable dot expression
is KtPropertyDelegate -> invoke(element.parent.parent) // composable dot expression
else -> element
}
}

24
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/isIntentionAvailable/IsIntentionAvailable.kt

@ -1,24 +0,0 @@
package org.jetbrains.compose.intentions.utils.isIntentionAvailable
import com.intellij.psi.PsiElement
import org.jetbrains.compose.intentions.utils.composableFinder.ComposableFunctionFinder
import org.jetbrains.kotlin.idea.KotlinLanguage
interface IsIntentionAvailable {
fun PsiElement.isAvailable(
composableFunctionFinder: ComposableFunctionFinder
): Boolean {
if (language != KotlinLanguage.INSTANCE) {
return false
}
if (!isWritable) {
return false
}
return parent?.let { parentPsiElement ->
composableFunctionFinder.isFunctionComposable(parentPsiElement)
} ?: false
}
}

20
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/utils/isPsiElementComposable/IsPsiElementComposable.kt

@ -1,20 +0,0 @@
package org.jetbrains.compose.intentions.utils.isPsiElementComposable
import org.jetbrains.compose.desktop.ide.preview.isComposableFunction
import org.jetbrains.kotlin.nj2k.postProcessing.resolve
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
interface IsPsiElementComposable {
fun KtCallExpression.isComposable(): Boolean {
return getChildOfType<KtNameReferenceExpression>()?.isComposable() ?: false
}
fun KtNameReferenceExpression.isComposable(): Boolean {
val ktNamedFunction = resolve() as? KtNamedFunction ?: return false
return ktNamedFunction.isComposableFunction()
}
}

7
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/wrapActions/BaseWrapWithComposableAction.kt

@ -10,12 +10,11 @@ import com.intellij.psi.PsiElement
import org.jetbrains.compose.intentions.utils.composableFinder.ComposableFunctionFinder
import org.jetbrains.compose.intentions.utils.composableFinder.ComposableFunctionFinderImpl
import org.jetbrains.compose.intentions.utils.getRootPsiElement.GetRootPsiElement
import org.jetbrains.compose.intentions.utils.isIntentionAvailable.IsIntentionAvailable
import org.jetbrains.compose.intentions.utils.isIntentionAvailable
abstract class BaseWrapWithComposableAction :
PsiElementBaseIntentionAction(),
HighPriorityAction,
IsIntentionAvailable {
HighPriorityAction {
private val composableFunctionFinder: ComposableFunctionFinder by lazy {
ComposableFunctionFinderImpl()
@ -30,7 +29,7 @@ abstract class BaseWrapWithComposableAction :
}
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.isAvailable(composableFunctionFinder)
return element.isIntentionAvailable(composableFunctionFinder)
}
override fun startInWriteAction(): Boolean = true

12
idea-plugin/src/main/kotlin/org/jetbrains/compose/intentions/wrapActions/WrapWithActions.kt

@ -10,7 +10,7 @@ class WrapWithBoxIntention : BaseWrapWithComposableAction() {
}
override fun getTemplate(): TemplateImpl? {
return TemplateSettings.getInstance().getTemplate("WwB", "ComposeMultiplatformTemplates")
return TemplateSettings.getInstance().getTemplate("boxcomp", "ComposeMultiplatformTemplates")
}
}
@ -21,7 +21,7 @@ class WrapWithCardIntention : BaseWrapWithComposableAction() {
}
override fun getTemplate(): TemplateImpl? {
return TemplateSettings.getInstance().getTemplate("WwC", "ComposeMultiplatformTemplates")
return TemplateSettings.getInstance().getTemplate("cardcomp", "ComposeMultiplatformTemplates")
}
}
@ -32,7 +32,7 @@ class WrapWithColumnIntention : BaseWrapWithComposableAction() {
}
override fun getTemplate(): TemplateImpl? {
return TemplateSettings.getInstance().getTemplate("WwCol", "ComposeMultiplatformTemplates")
return TemplateSettings.getInstance().getTemplate("columncomp", "ComposeMultiplatformTemplates")
}
}
@ -43,7 +43,7 @@ class WrapWithRowIntention : BaseWrapWithComposableAction() {
}
override fun getTemplate(): TemplateImpl? {
return TemplateSettings.getInstance().getTemplate("WwRow", "ComposeMultiplatformTemplates")
return TemplateSettings.getInstance().getTemplate("rowcomp", "ComposeMultiplatformTemplates")
}
}
@ -54,7 +54,7 @@ class WrapWithLzyColumnIntention : BaseWrapWithComposableAction() {
}
override fun getTemplate(): TemplateImpl? {
return TemplateSettings.getInstance().getTemplate("WwLazyCol", "ComposeMultiplatformTemplates")
return TemplateSettings.getInstance().getTemplate("lazycolumncomp", "ComposeMultiplatformTemplates")
}
}
@ -65,6 +65,6 @@ class WrapWithLzyRowIntention : BaseWrapWithComposableAction() {
}
override fun getTemplate(): TemplateImpl? {
return TemplateSettings.getInstance().getTemplate("WwLazyRow", "ComposeMultiplatformTemplates")
return TemplateSettings.getInstance().getTemplate("lazyrowcomp", "ComposeMultiplatformTemplates")
}
}

12
idea-plugin/src/main/resources/META-INF/plugin.xml

@ -55,22 +55,22 @@
<defaultLiveTemplates file="templates/ComposeMultiplatformTemplates.xml"/>
<intentionAction id="wrap_group" order="first">
<intentionAction>
<className>org.jetbrains.compose.intentions.WrapWithComposableIntentionGroup
</className>
<category>Composable intentions</category>
<category>Compose Multiplatform</category>
</intentionAction>
<intentionAction id="remove_composable" order="last">
<intentionAction>
<className>org.jetbrains.compose.intentions.RemoveComposableIntention
</className>
<category>Composable intentions</category>
<category>Compose Multiplatform</category>
</intentionAction>
<intentionAction id="remove_parent_composable" order="last">
<intentionAction>
<className>org.jetbrains.compose.intentions.RemoveParentComposableIntention
</className>
<category>Composable intentions</category>
<category>Compose Multiplatform</category>
</intentionAction>
</extensions>

2
idea-plugin/src/main/resources/intentionDescriptions/RemoveComposableIntention/description.html

@ -1,7 +1,7 @@
<html lang="en">
<body>
<p>
A simple intention to remove the composable altogether.
A simple intention to remove a Composable altogether.
</p>
</body>
</html>

2
idea-plugin/src/main/resources/intentionDescriptions/RemoveParentComposableIntention/description.html

@ -1,7 +1,7 @@
<html lang="en">
<body>
<p>
A simple intention to just remove the parent composable, and unwrap its children.
An intention to remove a parent Composable, and unwrap its children.
</p>
</body>
</html>

42
idea-plugin/src/main/resources/templates/ComposeMultiplatformTemplates.xml

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<templateSet group="ComposeMultiplatformTemplates">
<template name="WwB" description="Wrap with Box"
value="$COMPOSABLE$(modifier = androidx.compose.ui.Modifier) {&#10; $SELECTION$ &#10; }"
<template name="boxcomp" description="Insert Box Composable"
value="$COMPOSABLE$ {&#10; $SELECTION$ &#10; }"
toReformat="true" toShortenFQNames="true">
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.layout.Box&quot;" alwaysStopAt="true"/>
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.layout.Box&quot;"
alwaysStopAt="true"/>
<context>
<option name="KOTLIN" value="true"/>
<option name="KOTLIN_COMMENT" value="false"/>
@ -12,50 +13,55 @@
</template>
<template name="WwC" description="Wrap with Card"
value="$COMPOSABLE$(modifier = androidx.compose.ui.Modifier) {&#10; $SELECTION$ &#10; }"
<template name="cardcomp" description="Insert Card Composable"
value="$COMPOSABLE$ {&#10; $SELECTION$ &#10; }"
toReformat="true" toShortenFQNames="true">
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.material.Card&quot;" alwaysStopAt="true"/>
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.material.Card&quot;"
alwaysStopAt="true"/>
<context>
<option name="KOTLIN" value="true"/>
<option name="KOTLIN_COMMENT" value="false"/>
</context>
</template>
<template name="WwCol" description="Wrap with Column"
value="$COMPOSABLE$(modifier = androidx.compose.ui.Modifier) {&#10; $SELECTION$ &#10; }"
<template name="columncomp" description="Insert Column Composable"
value="$COMPOSABLE$ {&#10; $SELECTION$ &#10; }"
toReformat="true" toShortenFQNames="true">
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.layout.Column&quot;" alwaysStopAt="true"/>
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.layout.Column&quot;"
alwaysStopAt="true"/>
<context>
<option name="KOTLIN" value="true"/>
<option name="KOTLIN_COMMENT" value="false"/>
</context>
</template>
<template name="WwRow" description="Wrap with Row"
value="$COMPOSABLE$(modifier = androidx.compose.ui.Modifier) {&#10; $SELECTION$ &#10; }"
<template name="rowcomp" description="Insert Row Composable"
value="$COMPOSABLE$ {&#10; $SELECTION$ &#10; }"
toReformat="true" toShortenFQNames="true">
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.layout.Row&quot;" alwaysStopAt="true"/>
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.layout.Row&quot;"
alwaysStopAt="true"/>
<context>
<option name="KOTLIN" value="true"/>
<option name="KOTLIN_COMMENT" value="false"/>
</context>
</template>
<template name="WwLazyCol" description="Wrap with Lazy Column"
value="$COMPOSABLE$(modifier = androidx.compose.ui.Modifier) { item {&#10; $SELECTION$ &#10;} }"
<template name="lazycolumncomp" description="Insert Lazy Column Composable"
value="$COMPOSABLE$ { item {&#10; $SELECTION$ &#10;} }"
toReformat="true" toShortenFQNames="true">
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.lazy.LazyColumn&quot;" alwaysStopAt="true"/>
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.lazy.LazyColumn&quot;"
alwaysStopAt="true"/>
<context>
<option name="KOTLIN" value="true"/>
<option name="KOTLIN_COMMENT" value="false"/>
</context>
</template>
<template name="WwLazyRow" description="Wrap with Lazy Row"
value="$COMPOSABLE$(modifier = androidx.compose.ui.Modifier) { item {&#10; $SELECTION$ &#10;} }"
<template name="lazyrowcomp" description="Insert Lazy Row Composable"
value="$COMPOSABLE$ { item {&#10; $SELECTION$ &#10;} }"
toReformat="true" toShortenFQNames="true">
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.lazy.LazyRow&quot;" alwaysStopAt="true"/>
<variable name="COMPOSABLE" expression="" defaultValue="&quot;androidx.compose.foundation.lazy.LazyRow&quot;"
alwaysStopAt="true"/>
<context>
<option name="KOTLIN" value="true"/>
<option name="KOTLIN_COMMENT" value="false"/>

Loading…
Cancel
Save