-
-
Notifications
You must be signed in to change notification settings - Fork 66
Console filtering
Dmitry Kandalov edited this page Sep 11, 2021
·
4 revisions
This is a mini-plugin for IntelliJ to filter and modify console output. The main point is that it's possible to add console extension points at runtime (in particular ConsoleInputFilterProvider and ConsoleFilterProvider).
For example, consider this test and its output (really useful!):
The source code (also as gist) is below. To execute it you'll need to install https://github.com/dkandalov/live-plugin and copy-paste it as a new live plugin.
import com.intellij.execution.filters.ConsoleInputFilterProvider
import com.intellij.execution.filters.InputFilter
import com.intellij.execution.ui.ConsoleViewContentType as ContentType
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Pair
import org.jetbrains.annotations.NotNull
import static liveplugin.PluginUtil.*
import com.intellij.openapi.extensions.Extensions
import static com.intellij.execution.filters.ConsoleInputFilterProvider.*
import static com.intellij.execution.ui.ConsoleViewContentType.NORMAL_OUTPUT
if (isIdeStartup) return
def beautifyExceptions() {
new InputFilter() {
@Override List<Pair<String, ContentType>> applyFilter(String consoleText, ContentType contentType) {
if (!consoleText.contains("Exception")) null
else [new Pair(consoleText.replace("Exception", "Usualness"), contentType)]
}
}
}
def linesWithout(String text) {
new InputFilter() {
@Override List<Pair<String, ContentType>> applyFilter(String consoleText, ContentType contentType) {
consoleText.contains(text) ? [] : null
}
}
}
def createInputFilterProvider() {
new ConsoleInputFilterProvider() {
@Override InputFilter[] getDefaultFilters(@NotNull Project project) {
[beautifyExceptions(), linesWithout("INFO - ")]
}
}
}
def extensionPoint = Extensions.rootArea.getExtensionPoint(INPUT_FILTER_PROVIDERS)
def inputFilterProvider = changeGlobalVar("myConsoleFilter") { lastInputFilterProvider ->
if (lastInputFilterProvider != null && extensionPoint.hasExtension(lastInputFilterProvider)) {
extensionPoint.unregisterExtension(lastInputFilterProvider)
}
createInputFilterProvider()
}
extensionPoint.registerExtension(inputFilterProvider)
def console = showInConsole("-----\n", project)
console.print("INFO - which will be invisible\n", NORMAL_OUTPUT)
console.print("ERROR - beautified java.lang.NullPointerException\n", NORMAL_OUTPUT)
console.print("-----\n", NORMAL_OUTPUT)