Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Created October 12, 2012 15:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkandalov/3879764 to your computer and use it in GitHub Desktop.
Save dkandalov/3879764 to your computer and use it in GitHub Desktop.
"grep" action for intellij-eval
import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.actionSystem.KeyboardShortcut
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorAction
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler
import com.intellij.openapi.editor.markup.EffectType
import com.intellij.openapi.editor.markup.HighlighterTargetArea
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.keymap.KeymapManager
import javax.swing.*
import java.awt.*
static show(String htmlBody, String title = "", NotificationType notificationType = NotificationType.INFORMATION) {
SwingUtilities.invokeLater({
def notification = new Notification("", title, htmlBody, notificationType)
ApplicationManager.application.messageBus.syncPublisher(Notifications.TOPIC).notify(notification)
} as Runnable)
}
class MyEditorAction extends EditorAction {
public MyEditorAction(Closure closure) {
super(new MyEditorWriteActionHandler(closure))
}
}
class MyEditorWriteActionHandler extends EditorWriteActionHandler {
final Closure closure
MyEditorWriteActionHandler(Closure closure) {
this.closure = closure
}
@Override void executeWriteAction(Editor editor, DataContext dataContext) {
closure.call(editor)
}
}
static registerTextEditorAction(String actionId, String keyStroke = "", Closure closure) {
def actionManager = ActionManager.instance
def keymap = KeymapManager.instance.activeKeymap
def alreadyRegistered = (actionManager.getAction(actionId) != null)
if (alreadyRegistered) {
keymap.removeAllActionShortcuts(actionId)
actionManager.unregisterAction(actionId)
}
if (!keyStroke.empty) keymap.addShortcut(actionId, new KeyboardShortcut(KeyStroke.getKeyStroke(keyStroke), null))
actionManager.registerAction(actionId, new MyEditorAction(closure))
show("Loaded '${actionId}'<br/>Press ctrl+shift+alt+G to run it")
}
registerTextEditorAction("HelloTextEditorAction", "ctrl shift alt G", { Editor editor ->
def grepExpression = { it.contains("aaa") }
editor.document.text = editor.document.text.split("\n").findAll(grepExpression).join("\n")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment