Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pasting layout XML from outside of IntelliJ / Android Studio #8 #81

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\"?>"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is a good start. Eventually we probably want to support incomplete snippets too. But that's also #41 and #5. I guess we can just file a follow-up issue and then see what a good strategy would be. Maybe looking for the start of known elements.


when {
content.isDataFlavorSupported(CopiedXMLCode.DATA_FLAVOR) -> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we ever get here if this block is wrapped in DataFlavor.stringFlavor? Is that an extension of the string flavor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can only get here when the content has one flavour with DataFlavor.stringFlavor, see row 63.
I'm first checking if the pasted content is a string and not e.g. an image file. Then i'm checking if the content also has the dataflavor of CopiedXMLCode. If it's not, i'm checking if it looks like XML.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright 👍

// 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we set the endOffset to the last index of text? Right now the offsets are not used, but eventually they will, I guess (#5).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, i wasn't sure what the correct values for the offsets are. I looked at what offsets are used in collectTransferableData() when i copy the text inside Intellij and they we both 0.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. Then this is something to look at when tackling #5. I was expecting the offsets to mark the selection area inside the text, since right now that always contains the whole document. But if it's always 0 right now then let's just use 0 here for now too. 👍

}
else -> {
emptyList()
}
}
} else {
emptyList()
}
}

// Perform paste: Process transferable data
Expand Down