Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active February 4, 2017 14:47
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/5222759 to your computer and use it in GitHub Desktop.
Save dkandalov/5222759 to your computer and use it in GitHub Desktop.
Wrapping tab action
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.DocCommandGroupId
import com.intellij.openapi.editor.actionSystem.EditorAction
import com.intellij.openapi.editor.actionSystem.EditorActionHandler
import com.intellij.openapi.editor.actions.TabAction
import liveplugin.implementation.ActionWrapper
import static liveplugin.PluginUtil.*
// This is a micro-plugin which replaces "oh" with "overflow:hidden"
// See also http://devnet.jetbrains.com/thread/440364;jsessionid=194E24939C0ACEA50206122AD23E5892
//
// (Note that this code can only be executed in LivePlugin https://github.com/dkandalov/live-plugin)
wrapAction("EditorTab") { AnActionEvent event, Closure originalAction ->
currentEditorIn(event.project).with {
def from = document.getLineStartOffset(document.getLineNumber(caretModel.offset))
def to = caretModel.offset
def text = document.text.substring(from, to)
def abbreviation = "oh"
def replacement = "overflow:hidden"
if (text.endsWith(abbreviation)) {
runDocumentWriteAction(event.project, document) {
document.replaceString(to - abbreviation.length(), to, replacement)
caretModel.moveToOffset(to - abbreviation.length() + replacement.length())
}
} else {
originalAction()
}
}
}
// Note that there is no need for unwrapAction() between plugin reloads
// because wrapAction() will automatically remove one layer of wrapping.
// unwrapAction("EditorTab")
if (!isIdeStartup) show("Reloaded tab complete example")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment