Skip to content

Commit

Permalink
refactoring: introduced ContentUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
artspb committed Oct 12, 2016
1 parent d8cecad commit 797bee3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
34 changes: 34 additions & 0 deletions src/me/artspb/idea/eval/plugin/ContentUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.artspb.idea.eval.plugin

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()

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? {
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
}
}
return true
}
23 changes: 0 additions & 23 deletions src/me/artspb/idea/eval/plugin/EvalAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.intellij.ide.BrowserUtil
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.vfs.VirtualFile
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.HttpClients
Expand All @@ -27,26 +26,4 @@ class EvalAction : AnAction() {
}
}
}

private fun getContent(e: AnActionEvent, file: VirtualFile): ByteArray {
val rawContent = getContentFromSelection(e) ?: file.contentsToByteArray()
return if (rawContent.startsWith("<?php".toByteArray())) rawContent else "<?php\n\n".toByteArray() + rawContent
}

private fun getContentFromSelection(e: AnActionEvent): ByteArray? {
val editor = e.dataContext.getData(CommonDataKeys.EDITOR)
return editor?.selectionModel?.selectedText?.toByteArray()
}

private 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
}
}
return true
}
}

0 comments on commit 797bee3

Please sign in to comment.