Skip to content

Commit

Permalink
#5 used actual editor's content instead of file's (can be outdated)
Browse files Browse the repository at this point in the history
  • Loading branch information
artspb committed Dec 29, 2016
1 parent ab25c11 commit 31e793b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
8 changes: 6 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<idea-plugin>
<id>me.artspb.idea.eval.plugin</id>
<name>3v4l pl4g1n</name>
<version>0.2</version>
<version>0.3</version>
<vendor email="contact@artspb.me" url="https://artspb.me">Artem Khvastunov</vendor>

<description><![CDATA[
Expand All @@ -16,6 +16,10 @@
</description>

<change-notes><![CDATA[
<h3>0.3</h3>
<ul>
<li>Fixed bug: actual editor's content is used instead of file's (can be outdated).</li>
</ul>
<h3>0.2</h3>
<ul>
<li>Added ability to submit a selected code fragment instead of the whole file.</li>
Expand Down
35 changes: 11 additions & 24 deletions src/me/artspb/idea/eval/plugin/ContentUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,18 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.vfs.VirtualFile

val PHP_OPENING_TAG_BYTES = "<?php".toByteArray()
val LINE_SEPARATORS_BYTES = "\n\n".toByteArray()
val PHP_OPENING_TAG = "<?php"

fun getContent(e: AnActionEvent, file: VirtualFile): ByteArray {
val rawContent = getContentFromSelection(e) ?: file.contentsToByteArray()
return if (rawContent.startsWith(PHP_OPENING_TAG_BYTES)) {
rawContent
} else {
PHP_OPENING_TAG_BYTES + LINE_SEPARATORS_BYTES + rawContent
}
}

fun getContentFromSelection(e: AnActionEvent): ByteArray? {
fun getContent(e: AnActionEvent, file: VirtualFile): String {
val editor = e.dataContext.getData(CommonDataKeys.EDITOR)
return editor?.selectionModel?.selectedText?.toByteArray()
}

fun ByteArray.startsWith(arr: ByteArray): Boolean {
if (arr.size > this.size) {
return false
}
for (i in arr.indices) {
if (this[i] != arr[i]) {
return false
if (editor != null) {
val content = editor.selectionModel.selectedText ?: editor.document.text
return if (content.startsWith(PHP_OPENING_TAG)) {
content
} else {
PHP_OPENING_TAG + "\n\n" + content
}
} else {
return String(file.contentsToByteArray())
}
return true
}
}
2 changes: 1 addition & 1 deletion src/me/artspb/idea/eval/plugin/EvalAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EvalAction : AnAction() {
val post = HttpPost("https://3v4l.org/new")
post.entity = UrlEncodedFormEntity(listOf(
BasicNameValuePair("title", file.name),
BasicNameValuePair("code", String(content))
BasicNameValuePair("code", content)
))
val response = HttpClients.createDefault().execute(post)
val location = response.getFirstHeader("location")
Expand Down

0 comments on commit 31e793b

Please sign in to comment.