[RDCT] CWM-9114, GTW-8233: Add listener for language console prompt

Bugs that are mentioned above are caused by a prompt set after the console was converted. This commit adds an ability to add a listener that is called on a prompt update, so our converter can keep it up to date.

GitOrigin-RevId: f9d5b0b6006c3fe95f87bfb65fec3e34b7dedd95
This commit is contained in:
Vyacheslav Moklev
2024-06-28 16:32:37 +03:00
committed by intellij-monorepo-bot
parent 9d6bf5f8a4
commit 3425ff4c8e

View File

@@ -2,7 +2,9 @@
package com.intellij.execution.console
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorLinePainter
import com.intellij.openapi.editor.LineExtensionInfo
@@ -11,14 +13,26 @@ import com.intellij.openapi.editor.colors.ColorKey
import com.intellij.openapi.editor.colors.EditorFontType
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.concurrency.ThreadingAssertions
import com.intellij.util.ui.UIUtil
import org.jetbrains.annotations.ApiStatus.Internal
import java.awt.Color
import java.util.function.Consumer
/**
* Created by Yuli Fiterman on 9/16/2016.
*/
class ConsolePromptDecorator(private val myEditorEx: EditorEx) : EditorLinePainter(), TextAnnotationGutterProvider {
/**
* The list of prompt change listeners. Must be accessed only from EDT.
*/
private val promptListeners = mutableListOf<Consumer<ConsolePromptDecorator>>()
get() {
ThreadingAssertions.assertEventDispatchThread()
return field
}
var mainPrompt: String = "> "
get() = if (myEditorEx.isRendererMode) "" else field
@@ -89,11 +103,33 @@ class ConsolePromptDecorator(private val myEditorEx: EditorEx) : EditorLinePaint
if (!myEditorEx.isDisposed) {
myEditorEx.gutterComponentEx.revalidateMarkup()
}
promptListeners.forEach {
try {
it.accept(this)
} catch (e: Exception) {
LOG.error("Failed to invoke prompt change listener ${it::class.java}", e)
}
}
}
}
@Internal
fun addChangeListener(promptChangeListener: Consumer<ConsolePromptDecorator>, disposable: Disposable) {
UIUtil.invokeLaterIfNeeded {
promptListeners.add(promptChangeListener)
Disposer.register(disposable) {
UIUtil.invokeLaterIfNeeded {
promptListeners.remove(promptChangeListener)
}
}
}
}
companion object {
private val promptColor = ColorKey.createColorKey("CONSOLE_PROMPT_COLOR")
private val LOG = Logger.getInstance(ConsolePromptDecorator::class.java)
}
}