How do I get the selected text in the terminal view in my plugin?

已回答

How do I get the selected text in the terminal view in my plugin?

I tried to write an action to get, but the action wouldn't work.

0

com.intellij.terminal.JBTerminalWidget#SELECTED_TEXT_DATA_KEY

0

Yes, I also found this key, but I can't make my action work. Because:

  1. The shortcut key I set for my action is invalid in the terminal window.
  2. I didn't find any extension points to add my action to the popup menu of the terminal window.

What should I do? Thank you very much!

 

Simple code:

<action id="GetTextInTerminalAction"
class="com.myplugin.action.GetTextInTerminalAction"
text="Get Text in Terminal">
<keyboard-shortcut keymap="$default" first-keystroke="control shift Y"/>
</action>
class GetTextInTerminalAction : AnAction(), DumbAware {

override fun update(e: AnActionEvent) {
// e.presentation.isEnabledAndVisible = !e.getSelectedTextFromTerminal().isNullOrBlank()
}

override fun actionPerformed(e: AnActionEvent) {
// Can't execute here
println(e.getSelectedTextFromTerminal())
}

companion object {
fun AnActionEvent.getSelectedTextFromTerminal(): String? = getData(JBTerminalWidget.SELECTED_TEXT_DATA_KEY)
}
}

 

0

Currently the only way would be to override com.intellij.terminal.JBTerminalWidget#getActions and provide your additional actions from there.

0

Thank you very much!

0
Avatar
Permanently deleted user

Hİ;

 

Could you show any example?

0

Hi, muslu.

 

This is currently not possible unless the terminal plugin provides some expansion points.

0
Avatar
Permanently deleted user

Hi Yii;

 

Thank you. If you find any solution then please tell me too.

0

I need too, is there a solution now?

0

Hi Yann Cebron  ,

Do you have an example for overriding ‘com.intellij.terminal.JBTerminalWidget#getActions’ ? Is this basically replacing the default terminal thats used in IntelliJ ? 

For my plugin, I need to display a popup when user right click over a highlighted text in the terminal (and being able to read what the user has highlighted, obviously).

0

Please see IJPL-164451 to have a convenient way to contribute action to the classic terminal.
Workaround: to add an action to `JBTerminalWidget`, please try this example:
 

private fun addActionToJediTerm(project: Project, parentDisposable: Disposable) {
  TerminalToolWindowManager.getInstance(project).addNewTerminalSetupHandler(Consumer {
    val widget = ShellTerminalWidget.asShellJediTermWidget(it)
    if (widget != null) {
      widget.nextProvider = object : TerminalActionProvider {
        override fun getActions(): List<TerminalAction> {
          val presentation = TerminalActionPresentation("Hello", KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.META_DOWN_MASK))
          return listOf(TerminalAction(presentation) {
            println("Hello, World!")
            return@TerminalAction true
          })
        }

        override fun getNextProvider(): TerminalActionProvider? = null

        override fun setNextProvider(provider: TerminalActionProvider?) {
        }
      }
    }
  }, parentDisposable)
}
0

请先登录再写评论。