Last active
June 8, 2017 14:40
Code meltdown mini-plugin for IntelliJ (written for https://github.com/dkandalov/live-plugin)
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
import com.intellij.openapi.actionSystem.AnAction | |
import com.intellij.openapi.actionSystem.AnActionEvent | |
import com.intellij.openapi.editor.Document | |
import com.intellij.openapi.editor.Editor | |
import com.intellij.openapi.editor.LogicalPosition | |
import com.intellij.openapi.progress.ProgressIndicator | |
import static liveplugin.PluginUtil.* | |
import static liveplugin.PluginUtil.runReadAction | |
registerAction("CodeMeltdown", "ctrl shift M") { AnActionEvent event -> | |
def project = event.project | |
def editor = currentEditorIn(project) | |
doInBackground { ProgressIndicator indicator -> | |
def fallingSymbols = runReadAction { | |
def lineNumber = editor.document.getLineNumber(editor.caretModel.offset) | |
def lineStart = editor.document.getLineStartOffset(lineNumber) | |
def lineEnd = editor.document.getLineEndOffset(lineNumber) | |
def result = [] | |
editor.document.text.chars.toList().subList(lineStart, lineEnd).eachWithIndex { c, i -> | |
result.add(new FallingSymbol(editor.offsetToLogicalPosition(lineStart + i))) | |
} | |
result | |
} | |
while (!fallingSymbols.empty && !indicator.canceled) { | |
runDocumentWriteAction(project, editor.document) { | |
fallingSymbols = fallingSymbols.findAll{ it.fall(editor) } | |
} | |
} | |
} | |
} | |
if (!isIdeStartup) show("Reloaded CodeMeltdown") | |
def randomSubset(List list, int amount) { | |
amount = Math.min(list.size(), amount) | |
(0..amount).collect{ list[new Random().nextInt(list.size())] }.unique() | |
} | |
class FallingSymbol { | |
private LogicalPosition position | |
private final int speed | |
boolean dead = false | |
FallingSymbol(LogicalPosition position) { | |
this.position = position | |
this.speed = 1 | |
} | |
boolean fall(Editor editor) { | |
editor.with { | |
if (dead) return false | |
def offset = logicalPositionToOffset(position) | |
def nextLineOffset = logicalPositionToOffset(new LogicalPosition( | |
position.line + speed, | |
position.column | |
)) | |
def nextLinePosition = offsetToLogicalPosition(nextLineOffset) | |
if (nextLineOffset == document.textLength) { | |
document.replaceString(offset, offset + 1, " ") | |
dead = true | |
return false | |
} | |
if (nextLinePosition.column >= position.column && document.text[nextLineOffset] != " " && document.text[nextLineOffset] != "\n") return false | |
def symbol = document.text[offset] | |
document.replaceString(offset, offset + 1, " ") | |
def prefix = " " * (position.column - nextLinePosition.column) | |
if (!prefix.empty || document.text[nextLineOffset] == "\n") { | |
document.insertString(nextLineOffset, prefix + symbol) | |
} else { | |
document.replaceString(nextLineOffset, nextLineOffset + 1, symbol) | |
} | |
position = new LogicalPosition(nextLinePosition.line, position.column) | |
true | |
} | |
} | |
@Override String toString() { | |
"FallingSymbol{" + "position=" + position + '}' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment