Skip to content

Commit

Permalink
Issue #8: Support pasting layout XML from outside of IntelliJ / Andro…
Browse files Browse the repository at this point in the history
…id Studio

Closes #8.
  • Loading branch information
Foso authored Sep 30, 2020
1 parent b123b13 commit 9a0c15d
Showing 1 changed file with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.idea.KotlinFileType
import recompose.composer.Composer
import recompose.parser.Parser
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.Transferable

/**
Expand Down Expand Up @@ -58,14 +59,28 @@ class RecomposeCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransferable
override fun extractTransferableData(
content: Transferable
): List<TextBlockTransferableData> {
if (content.isDataFlavorSupported(CopiedXMLCode.DATA_FLAVOR)) {
// There's some matching data in this paste, let's return it to process it.
return listOf(
content.getTransferData(CopiedXMLCode.DATA_FLAVOR) as TextBlockTransferableData
)
}

return emptyList()
return if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
val text = content.getTransferData(DataFlavor.stringFlavor) as String
val xmlPreamble = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"

when {
content.isDataFlavorSupported(CopiedXMLCode.DATA_FLAVOR) -> {
// There's some matching data in this paste, let's return it to process it.
listOf(
content.getTransferData(CopiedXMLCode.DATA_FLAVOR) as TextBlockTransferableData
)
}
text.contains(xmlPreamble, true) -> {
listOf(CopiedXMLCode(text, intArrayOf(0), intArrayOf(0)) as TextBlockTransferableData)
}
else -> {
emptyList()
}
}
} else {
emptyList()
}
}

// Perform paste: Process transferable data
Expand Down

0 comments on commit 9a0c15d

Please sign in to comment.