Skip to content

Instantly share code, notes, and snippets.

@unennhexium
Last active July 14, 2024 08:32
Show Gist options
  • Save unennhexium/d4f34dd8ef38c96380de40b815c8c0f4 to your computer and use it in GitHub Desktop.
Save unennhexium/d4f34dd8ef38c96380de40b815c8c0f4 to your computer and use it in GitHub Desktop.
Live Plugin script processing Golang slog output for Grep Console
package example
import java.util.regex.Pattern
import java.util.regex.Matcher
import java.util.function.BiFunction
import static liveplugin.PluginUtil.*
import com.intellij.ide.plugins.PluginManager
import com.intellij.ide.plugins.IdeaPluginDescriptorImpl
import com.intellij.openapi.extensions.PluginId
import static com.intellij.openapi.util.text.StringUtil.newBombedCharSequence
String prevLevel = null
Boolean prevRemoved = false
registerFunction("GoSlogFormatter", new BiFunction<String, Matcher, String>() {
@Override
String apply(String text, Matcher matcher) {
String qk, uqk, qv, uqv, regex
qk = /(?<qk>\"[^\"]*\")/
uqk = /(?<uqk>\w+)/
qv = /(?<qv>\"[^\"]*\")/
uqv = /(?<uqv>[\w-:.+]+)/
regex = "($qk|$uqk)=($qv|$uqv)";
Pattern pattern = Pattern.compile(regex)
try {
int maxLength = 150
CharSequence textForMatching = limitAndCutNewline(text, maxLength, 1000)
Matcher m = pattern.matcher(textForMatching)
String result = ""
Boolean found = false
String level = null
while (m.find()) {
found = true
String key = m.group("qk")
String value = m.group("qv")
if (m.group("uqk") != null) {
key = '"' + m.group("uqk") + '"'
}
if (m.group("uqv") != null) {
value = '"' + m.group("uqv") + '"'
}
result += key + "=" + value + " "
if (key == '"level"') {
level = value
}
}
if (!found) {
prevLevel = null
prevRemoved = true
return null
}
result = result.trim()
if (level != prevLevel) {
prevLevel = level
result = "▾ " + result
} else if (level == null && prevRemoved) {
prevRemoved = false
result = "▾ " + result
} else {
result = " " + result
}
lackOfSpaces = maxLength - result.length()
trailingSpace = new String(new char[lackOfSpaces]).replace("\0", "\u00A0")
return result + trailingSpace + "\n"
} catch (com.intellij.openapi.progress.ProcessCanceledException ignored) {
show("Processing took too long for: " + text)
}
return text
}
})
static Class<?> getExtensionManager() {
IdeaPluginDescriptorImpl descriptor = PluginManager
.getPlugin(PluginId.getId("GrepConsole")
)
return descriptor
.getPluginClassLoader()
.loadClass("krasa.grepconsole.plugin.ExtensionManager")
}
static void registerFunction(String functionName, Object function) {
Class<?> clazz = getExtensionManager()
clazz.getMethod("register", String.class, Object.class)
.invoke(null, functionName, function);
liveplugin.PluginUtil.show("'" + functionName + "' registered")
}
static CharSequence limitAndCutNewline(String text, int maxLength, int milliseconds) {
int endIndex = text.length()
if (text.endsWith("\n")) {
--endIndex
}
if (maxLength >= 0) {
endIndex = Math.min(endIndex, maxLength)
}
def substring = text.substring(0, endIndex)
if (milliseconds > 0) {
return newBombedCharSequence(substring, milliseconds)
}
return substring
}
@unennhexium
Copy link
Author

unennhexium commented Jul 13, 2024

Grep Console settings

  1. Download archive
  2. Main menu > Manage IDE Settings > Import Settings > Select downloaded archive >
    > Check Grep Console settings group (component) to import > Click OK

Result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment