-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters