How to add search functionality to `Editor`

Answered

In my plugin I receive messages from remote server, and it is useful to output some of those messages to user. For that purpose I found ConsoleViewImpl in IntelliJ Platform. But as there are a lot of messages, I also need to provide a search functionality, so that user can quickly navigate to needed message in the console. Right now I could not find a way to add search with selection and 'go to next search item' and 'go to previous search item', except implementing it manually. ConsoleViewImpl provides Editor.

My question is how do you add search to some JComponent that contains text component like Editor in IntelliJ Platform?

4 comments
Comment actions Permalink

Please share the code where you setup ConsoleViewImpl

0
Comment actions Permalink
class OutputConsole(name: String, project: Project) : ConsoleViewImpl(project, true) {
init {
setName(name)
}

fun info(message: String) = println(message, ConsoleViewContentType.NORMAL_OUTPUT)
fun error(message: String) = println(message, ConsoleViewContentType.LOG_ERROR_OUTPUT)

fun println(message: String, type: ConsoleViewContentType) {
print(message, type)
print("\n", ConsoleViewContentType.NORMAL_OUTPUT)
}
}

And then I use it in toolWindow: 

class ConsoleToolWindow(vararg consoles: OutputConsole) : SimpleToolWindowPanel(true, true) {
val consoleNames = CollectionComboBoxModel(consoles.map { it.name })
private var currentConsole = consoles[0]

private fun updateConsole() = setContent(currentConsole.component)

init {
updateConsole()
toolbar = BorderLayoutPanel().apply {
border = JBUI.Borders.empty()
withPreferredHeight(JBUI.scale(30))
withMinimumHeight(JBUI.scale(30))
addOnLeft(
horizontalFlow(
ComboBox(consoleNames).apply {
addItemListener { itemEvent ->
if (itemEvent.stateChange == ItemEvent.SELECTED) {
currentConsole =
consoles.find { it.name == (itemEvent.item as String) } ?: return@addItemListener
updateConsole()
}
}
},
))
}
}

private fun <C : JComponent> JPanel.addOnLeft(component: C): C {
add(component, BorderLayout.WEST)
return component
}

private fun horizontalFlow(vararg components: JComponent): JComponent {
return JPanel().apply {
layout = GridLayout(1, components.size)
components.forEach(::add)
}
}
}






The toolWindow is created like this: 

class ConsoleToolWindowProvider : ToolWindowFactory {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val contentManager = toolWindow.contentManager
val client = project.service<Client>()
val content =
contentManager.factory.createContent(ConsoleToolWindow(client.clientLog, client.serverLog), null, false)
toolWindow.contentManager.addContent(content)
}
}

 

 

 

 

0
Comment actions Permalink

The client does not do mush setup, it just creates consoles: 

class Client(val project: Project) : Disposable {
val clientLog = OutputConsole("Client log", project)
val serverLog = OutputConsole("Server log", project)
...
}
0
Comment actions Permalink

Sorry, for my needs cmd+F will do, so no need to implement anything on top of it, so this question is solved

 

0

Please sign in to leave a comment.