Last active
August 9, 2018 22:16
-
-
Save dkandalov/cccc4b5be7ffa897727dc69854d1ec22 to your computer and use it in GitHub Desktop.
Show man page mini-plugin for LivePlugin (see https://github.com/dkandalov/live-plugin)
This file contains hidden or 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.execution.ExecutionManager | |
| import com.intellij.execution.Executor | |
| import com.intellij.execution.executors.DefaultRunExecutor | |
| import com.intellij.execution.filters.TextConsoleBuilderFactory | |
| import com.intellij.execution.ui.ConsoleView | |
| import com.intellij.execution.ui.ConsoleViewContentType | |
| import com.intellij.execution.ui.ExecutionConsole | |
| import com.intellij.execution.ui.RunContentDescriptor | |
| import com.intellij.execution.ui.actions.CloseAction | |
| import com.intellij.icons.AllIcons | |
| import com.intellij.openapi.actionSystem.ActionGroup | |
| import com.intellij.openapi.actionSystem.ActionManager | |
| import com.intellij.openapi.actionSystem.ActionPlaces | |
| import com.intellij.openapi.actionSystem.DefaultActionGroup | |
| import com.intellij.openapi.project.Project | |
| import liveplugin.implementation.Misc | |
| import org.jetbrains.annotations.NotNull | |
| import org.jetbrains.annotations.Nullable | |
| import javax.swing.Icon | |
| import javax.swing.JPanel | |
| import java.awt.BorderLayout | |
| import java.util.concurrent.atomic.AtomicReference | |
| import static liveplugin.PluginUtil.* | |
| CloseAction lastCloseAction = null | |
| registerAction("Show man page", "ctrl shift M") { event -> | |
| def editor = currentEditorIn(event.project) | |
| def term = editor.selectionModel.selectedText | |
| def manText = execute("/usr/local/bin/man-text $term").stdout as String | |
| def (consoleView, closeAction) = showInConsole2(manText, "man $term", event.project, ConsoleViewContentType.NORMAL_OUTPUT) | |
| consoleView.scrollTo(0) | |
| if (lastCloseAction != null) { | |
| lastCloseAction.actionPerformed(anActionEvent()) | |
| } | |
| lastCloseAction = closeAction | |
| } | |
| // Copied from LivePlugin source to be able to invoke CloseAction. | |
| static showInConsole2(@Nullable message, String consoleTitle = "", @NotNull Project project, | |
| ConsoleViewContentType contentType = guessContentTypeOf(message)) { | |
| AtomicReference result = new AtomicReference(null) | |
| // Use reference for consoleTitle because get groovy Reference class like in this bug http://jira.codehaus.org/browse/GROOVY-5101 | |
| AtomicReference<String> titleRef = new AtomicReference(consoleTitle) | |
| invokeOnEDT { | |
| ConsoleView console = TextConsoleBuilderFactory.instance.createBuilder(project).console | |
| console.print(Misc.asString(message), contentType) | |
| DefaultActionGroup toolbarActions = new DefaultActionGroup() | |
| def consoleComponent = new MyConsolePanel(console, toolbarActions) | |
| RunContentDescriptor descriptor = new RunContentDescriptor(console, null, consoleComponent, titleRef.get()) { | |
| @Override boolean isContentReuseProhibited() { true } | |
| @Override Icon getIcon() { AllIcons.Nodes.Plugin } | |
| } | |
| Executor executor = DefaultRunExecutor.runExecutorInstance | |
| def closeAction = new CloseAction(executor, descriptor, project) | |
| toolbarActions.add(closeAction) | |
| console.createConsoleActions().each { toolbarActions.add(it) } | |
| ExecutionManager.getInstance(project).contentManager.showRunContent(executor, descriptor) | |
| result.set([console, closeAction]) | |
| } | |
| result.get() | |
| } | |
| class MyConsolePanel extends JPanel { | |
| MyConsolePanel(ExecutionConsole consoleView, ActionGroup toolbarActions) { | |
| super(new BorderLayout()) | |
| def toolbarPanel = new JPanel(new BorderLayout()) | |
| toolbarPanel.add(ActionManager.instance.createActionToolbar(ActionPlaces.UNKNOWN, toolbarActions, false).component) | |
| add(toolbarPanel, BorderLayout.WEST) | |
| add(consoleView.component, BorderLayout.CENTER) | |
| } | |
| } | |
| if (!isIdeStartup) { | |
| show("Reloaded man page plugin") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment