Skip to content

Commit

Permalink
Add Confirmation Dialog, check if target file is Kotlin, use runWrite…
Browse files Browse the repository at this point in the history
…Action (Closes #6, #51, #7)
  • Loading branch information
Foso committed Sep 26, 2020
1 parent 1d833c3 commit 0e2c2af
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 24 deletions.
4 changes: 2 additions & 2 deletions recompose-composer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ repositories {
mavenCentral()
}
dependencies {
implementation(project(":recompose-ast"))
implementation(project(":recompose-ast", "default"))

testImplementation(kotlin("test-junit"))
testImplementation(project(":recompose-parser"))
testImplementation(project(":recompose-parser", "default"))
testImplementation(project(":recompose-test"))
}
tasks.withType<KotlinCompile>() {
Expand Down
6 changes: 3 additions & 3 deletions recompose-idea/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ repositories {
}

dependencies {
implementation(project(":recompose-ast"))
implementation(project(":recompose-parser"))
implementation(project(":recompose-composer"))
implementation(project(":recompose-ast", "default"))
implementation(project(":recompose-parser", "default"))
implementation(project(":recompose-composer", "default"))
}

intellij {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020 Sebastian Kaspari
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package recompose.plugin.copypaste
import com.intellij.CommonBundle
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import java.awt.Container
import javax.swing.Action
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel

/**
* This dialog asks if the user wants to convert the pasted Xml to Compose
*/
class ConvertXmlToComposeConfirmationDialog(project: Project) : DialogWrapper(project, true) {
lateinit var panel: JPanel
@Suppress("unused") lateinit var questionLabel: JLabel

init {
init()
isModal = true
title = "Convert XML to Compose"
}
override fun createCenterPanel(): JComponent = panel

override fun getContentPane(): Container = panel

override fun createActions(): Array<Action> {
setOKButtonText(CommonBundle.getYesButtonText())
setCancelButtonText(CommonBundle.getNoButtonText())
return arrayOf(okAction, cancelAction)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import java.awt.datatransfer.DataFlavor
*/
class CopiedXMLCode(
val text: String,
val startOffsets: IntArray,
val endOffsets: IntArray
private val startOffsets: IntArray,
private val endOffsets: IntArray
) : TextBlockTransferableData {
override fun getFlavor() = DATA_FLAVOR
override fun getOffsetCount() = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="recompose.plugin.copypaste.ConvertXmlToComposeConfirmationDialog">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="746" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="746fc" class="javax.swing.JLabel" binding="questionLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Clipboard content seems to be XML code. Do you want to convert it to Compose?"/>
</properties>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ package recompose.plugin.copypaste

import com.intellij.codeInsight.editorActions.CopyPastePostProcessor
import com.intellij.codeInsight.editorActions.TextBlockTransferableData
import com.intellij.ide.highlighter.XmlFileType
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.RangeMarker
import com.intellij.openapi.editor.impl.EditorImpl
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.idea.KotlinFileType
import recompose.composer.Composer
import recompose.parser.Parser
import java.awt.datatransfer.Transferable
Expand All @@ -32,14 +36,15 @@ import java.awt.datatransfer.Transferable
* `Composable`s.
*/
class RecomposeCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransferableData>() {

// On copy: Collect transferable data
override fun collectTransferableData(
file: PsiFile,
editor: Editor,
startOffsets: IntArray,
endOffsets: IntArray
): List<TextBlockTransferableData> {
if (file.fileType.defaultExtension != "xml") {
if (file.fileType != XmlFileType.INSTANCE) {
// If this is not an XML file then we do not need to care about this copy operation.
return emptyList()
}
Expand Down Expand Up @@ -72,26 +77,40 @@ class RecomposeCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransferable
indented: Ref<Boolean>,
values: List<TextBlockTransferableData>
) {
// We may want to check whether we are pasting into a Kotlin file here. If not then there's no reason to

// We check whether we are pasting into a Kotlin file here. If not then there's no reason to
// paste the Composable code.
if ((editor as EditorImpl).virtualFile.fileType != KotlinFileType.INSTANCE) {
return
}

val value = values[0] as CopiedXMLCode
if (confirmConvertXmlOnPaste(project)) {

val parser = Parser()
// Currently we parse all text of the document we copied from. Obviously this is wrong and we should only
// take the selection and parse this. But this may require that we fix up the XML since the copied part may
// be incomplete.
val layout = parser.parse(value.text)
val value = values.single() as CopiedXMLCode

val composer = Composer()
val code = composer.compose(layout)
val parser = Parser()
// Currently we parse all text of the document we copied from. Obviously this is wrong and we should only
// take the selection and parse this. But this may require that we fix up the XML since the copied part may
// be incomplete.
val layout = parser.parse(value.text)

// We also should update the list of imports here to include the necessary classes.
val composer = Composer()
val code = composer.compose(layout)

editor.document.replaceString(
bounds.startOffset,
bounds.endOffset,
code
)
// We also should update the list of imports here to include the necessary classes.
runWriteAction {
editor.document.replaceString(
bounds.startOffset,
bounds.endOffset,
code
)
}
}
}

private fun confirmConvertXmlOnPaste(project: Project): Boolean {
val dialog = ConvertXmlToComposeConfirmationDialog(project)
dialog.show()
return dialog.isOK
}
}
2 changes: 1 addition & 1 deletion recompose-parser/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {
mavenCentral()
}
dependencies {
implementation(project(":recompose-ast"))
implementation(project(":recompose-ast", "default"))
implementation("xpp3:xpp3:1.1.4c")
implementation("junit:junit:4.12")

Expand Down

0 comments on commit 0e2c2af

Please sign in to comment.