Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active December 23, 2015 13:39
Show Gist options
  • Save dkandalov/6643735 to your computer and use it in GitHub Desktop.
Save dkandalov/6643735 to your computer and use it in GitHub Desktop.
Mini-plugin for IntelliJ to move current tab left/right with splitting. This code can be executed using this plugin https://github.com/dkandalov/live-plugin
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx
import com.intellij.openapi.project.Project
import org.jetbrains.annotations.NonNls
import javax.swing.SwingConstants
import static liveplugin.PluginUtil.*
def editorManagerIn(Project project) {
FileEditorManagerEx.getInstanceEx(project)
}
def currentSplitIndex(FileEditorManagerEx fileEditorManagerEx) {
fileEditorManagerEx.windows.findIndexOf{ it == fileEditorManagerEx.currentWindow }
}
int wrap(int value, int ceiling) {
(value + ceiling) % ceiling
}
def moveRight(FileEditorManagerEx editorManager, AnActionEvent event) {
if (editorManager.currentWindow == null) return
def file = currentFileIn(event.project)
int index = currentSplitIndex(editorManager)
def isRightmostSplit = (index == editorManager.windows.size() - 1)
if (isRightmostSplit && editorManager.currentWindow.tabCount == 1) return
if (isRightmostSplit) {
editorManager.createSplitter(SwingConstants.VERTICAL, editorManager.currentWindow)
editorManager.windows[index].closeFile(file)
editorManager.windows[index + 1].setAsCurrentWindow(true)
} else {
def wasTheOnlyTab = editorManager.windows[index].files.size() == 1
def shift = (wasTheOnlyTab ? 0 : 1)
editorManager.windows[index].closeFile(file)
editorManager.windows[index + shift].setAsCurrentWindow(true)
editorManager.openFile(file, true)
}
}
def moveLeft(FileEditorManagerEx editorManager, AnActionEvent event) {
if (editorManager.currentWindow == null) return
if (editorManager.windows.size() == 1) return moveRight(editorManager, event)
def file = currentFileIn(event.project)
int index = currentSplitIndex(editorManager)
def isLeftmostSplit = (index == 0)
if (isLeftmostSplit && editorManager.currentWindow.tabCount == 1) return
editorManager.windows[index].closeFile(file)
editorManager.windows[wrap(index - 1, editorManager.windows.size())].setAsCurrentWindow(true)
editorManager.openFile(file, true)
}
registerAction("splitAndMoveRight", "ctrl alt shift CLOSE_BRACKET"){ AnActionEvent event ->
moveRight(editorManagerIn(event.project), event)
}
registerAction("splitAndMoveLeft", "ctrl alt shift OPEN_BRACKET"){ AnActionEvent event ->
moveLeft(editorManagerIn(event.project), event)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment