Skip to content

Commit

Permalink
Capture group and parsing format selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemKar123 authored and mfilippov committed Dec 11, 2023
1 parent fb23ace commit ea47623
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,35 @@ class LogHighlightingIterator(startOffset: Int, private val myEditor: Editor, va
val highlightingSet = myEditor.getUserData(highlightingSetUserKey) ?: emptySet()

for ((pattern, info) in myPatterns) {
if (!fileFormat.validateFormatUUID(info.formatId)) {
continue
}
if (info.action == LogHighlightingAction.HIGHLIGHT_LINE) {
for (it in columnValues) {
if (pattern.matcher(it).find()) {
lineBackground = info.backgroundColor ?: lineBackground
lineForeground = info.foregroundColor ?: lineForeground
italic = info.italic
bold = info.bold
break
when {
info.captureGroup >= 0 -> {
val it = columnValues.getOrNull(info.captureGroup) ?: break
if (pattern.matcher(it).find()) {
lineBackground = info.backgroundColor ?: lineBackground
lineForeground = info.foregroundColor ?: lineForeground
italic = info.italic
bold = info.bold
break
}
}

else -> {
for (it in columnValues) {
if (pattern.matcher(it).find()) {
lineBackground = info.backgroundColor ?: lineBackground
lineForeground = info.foregroundColor ?: lineForeground
italic = info.italic
bold = info.bold
break
}
}
}
}

}
}

Expand All @@ -172,10 +191,7 @@ class LogHighlightingIterator(startOffset: Int, private val myEditor: Editor, va

var valueIndex = 0
val timeIndex = fileFormat.getTimeFieldIndex()
parsedTokens.forEach { token ->
if(token.isSeparator)
return@forEach

parsedTokens.filter{ !it.isSeparator }.forEachIndexed { captureGroup, token ->
val value = token.takeFrom(event)
var valueForeground = lineForeground
var valueBackground = lineBackground
Expand All @@ -190,6 +206,11 @@ class LogHighlightingIterator(startOffset: Int, private val myEditor: Editor, va
}

for ((pattern, info) in valueHighlighters) {
if (!fileFormat.validateFormatUUID(info.formatId)
|| (info.captureGroup >= 0 && info.captureGroup != captureGroup)) {
continue
}

if (pattern.matcher(value).find()) {
valueForeground = info.foregroundColor ?: valueForeground
valueBackground = info.backgroundColor ?: valueBackground
Expand All @@ -200,7 +221,9 @@ class LogHighlightingIterator(startOffset: Int, private val myEditor: Editor, va
}

// find all matches for every patttern
val matchedPieces = partHighlighters.flatMap { (pattern, info) ->
val matchedPieces = partHighlighters.filter{
(_, info) -> fileFormat.validateFormatUUID(info.formatId) && (info.captureGroup < 0 || info.captureGroup == captureGroup)
}.flatMap { (pattern, info) ->
pattern.toRegex().findAll(value).flatMap {
it.groups.drop(1).mapNotNull { matchGroup ->
matchGroup?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ class LogFormatTableModel(private var state: LogHighlightingSettingsStore.State)
state.parsingPatterns.removeAt(index)
fireTableRowsDeleted(index, index)
}

fun getParsingPatterns() = state.parsingPatterns.map {
it.uuid to it.name
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class LogHighlightingConfigurable : BaseConfigurable() {
val selectedIndex = patternsTable.selectedRow
if (selectedIndex >= 0) {
LogHighlightingPatternSettingsDialog(
patternTableModel.getValueAt(selectedIndex, 2) as LogHighlightingPattern).show()
patternTableModel.getValueAt(selectedIndex, 2) as LogHighlightingPattern,
formatsTableModel.getParsingPatterns()
).show()
}
}
}.createPanel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.ColorPanel
import com.intellij.ui.EditorTextField
import com.intellij.ui.JBIntSpinner
import java.awt.Component
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import java.util.*
import javax.swing.*

class LogHighlightingPatternSettingsDialog(private val item: LogHighlightingPattern) : DialogWrapper(null, true, IdeModalityType.PROJECT) {
class LogHighlightingPatternSettingsDialog(
private val item: LogHighlightingPattern,
private val parsingPatterns: List<Pair<UUID, String>>
) :
DialogWrapper(null, true, IdeModalityType.PROJECT) {
private var myPatternText: EditorTextField? = null
private var myActionCombo: JComboBox<LogHighlightingAction>? = null
private var myFormatCombo: JComboBox<Pair<UUID?, String>>? = null
private var myCaptureGroupId: JBIntSpinner? = null
private var myForegroundCheck: JCheckBox? = null
private var myBackgroundCheck: JCheckBox? = null
private var myForegroundColor: ColorPanel? = null
Expand Down Expand Up @@ -59,9 +66,57 @@ class LogHighlightingPatternSettingsDialog(private val item: LogHighlightingPatt
actionSelection.selectedItem = item.action
myActionCombo = actionSelection
panel.add(actionSelection, constraints)
constraints.gridx = 1
constraints.gridx = 0
panel.add(JLabel("Action"), constraints)

constraints.gridx = 0
constraints.gridy = 2
panel.add(JLabel("Format"), constraints)
constraints.gridx = 1

val formatSelection = ComboBox(
(listOf(Pair(null, "Any")) + parsingPatterns).toTypedArray()
)
formatSelection.renderer = object : DefaultListCellRenderer() {
override fun getListCellRendererComponent(
list: JList<*>?,
value: Any?,
index: Int,
isSelected: Boolean,
cellHasFocus: Boolean
): Component {
text = if (value is Pair<*, *>) {
if (value.first is UUID? && value.second is String) {
(value.second as String)
} else {
""
}
} else {
""
}
return this
}
}

formatSelection.selectedIndex = 0
for (i in 0 until formatSelection.itemCount) {
if (formatSelection.getItemAt(i).first == item.formatId) {
formatSelection.selectedIndex = i
break
}
}

panel.add(formatSelection, constraints)
myFormatCombo = formatSelection

constraints.gridx = 0
constraints.gridy = 3
panel.add(JLabel("Capture group"), constraints)
val groupSpinner = JBIntSpinner(item.captureGroup + 1, 0, 100)
myCaptureGroupId = groupSpinner
constraints.gridx++
panel.add(groupSpinner, constraints)

val fgColor = ColorPanel()
myForegroundColor = fgColor
fgColor.selectedColor = item.foregroundColor
Expand All @@ -87,20 +142,20 @@ class LogHighlightingPatternSettingsDialog(private val item: LogHighlightingPatt
myItalicCheck = italicCheck

constraints.gridx = 0
constraints.gridy = 2
constraints.gridy = 4
panel.add(boldCheck, constraints)

constraints.gridx = 1
panel.add(italicCheck, constraints)

constraints.gridx = 0
constraints.gridy = 3
constraints.gridy = 5
panel.add(fgCheck, constraints)

constraints.gridx = 1
panel.add(fgColor, constraints)

constraints.gridy = 4
constraints.gridy = 6
panel.add(bgColor, constraints)

constraints.gridx = 0
Expand All @@ -120,6 +175,8 @@ class LogHighlightingPatternSettingsDialog(private val item: LogHighlightingPatt
}
myPatternText?.let { item.pattern = it.text }
myActionCombo?.let { item.action = it.selectedItem as LogHighlightingAction }
myFormatCombo?.let { item.formatId = (it.selectedItem as Pair<*, *>).first as UUID? }
myCaptureGroupId?.let { item.captureGroup = it.number - 1 }
myForegroundCheck?.let {
item.foregroundColor = if (it.isSelected) myForegroundColor?.selectedColor else null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ object DefaultSettingsStoreItems {
val Error = LogHighlightingPattern(
true,
"^\\s*(e(rror)?|severe)\\s*$",
null,
-1,
LogHighlightingAction.HIGHLIGHT_LINE,
JBColor.RED.rgb,
null,
Expand All @@ -121,6 +123,8 @@ object DefaultSettingsStoreItems {
val Warning = LogHighlightingPattern(
true,
"^\\s*w(arn(ing)?)?\\s*$",
null,
-1,
LogHighlightingAction.HIGHLIGHT_LINE,
JBColor.ORANGE.rgb,
null,
Expand All @@ -132,6 +136,8 @@ object DefaultSettingsStoreItems {
val Info = LogHighlightingPattern(
true,
"^\\s*i(nfo)?\\s*$",
null,
-1,
LogHighlightingAction.HIGHLIGHT_LINE,
JBColor.GREEN.rgb,
null,
Expand Down Expand Up @@ -434,6 +440,8 @@ data class LogParsingPattern(@Attribute("enabled") var enabled: Boolean,
@Tag("LogHighlightingPattern")
data class LogHighlightingPattern(@Attribute("enabled") var enabled: Boolean,
@Attribute("pattern") @Language("RegExp") var pattern: String,
@Attribute("formatId", converter = UUIDConverter::class) var formatId: UUID?,
@Attribute("captureGroup") var captureGroup: Int,
@Attribute("action") var action: LogHighlightingAction,
@Attribute("fg") var fgRgb: Int?,
@Attribute("bg") var bgRgb: Int?,
Expand All @@ -443,7 +451,7 @@ data class LogHighlightingPattern(@Attribute("enabled") var enabled: Boolean,
@Attribute("uuid", converter = UUIDConverter::class) var uuid: UUID) : Cloneable {

@Suppress("unused")
constructor() : this(true, "", LogHighlightingAction.HIGHLIGHT_FIELD, null, null, false, false, false, UUID.randomUUID())
constructor() : this(true, "", null, 0, LogHighlightingAction.HIGHLIGHT_FIELD, null, null, false, false, false, UUID.randomUUID())

var foregroundColor: Color?
@Transient get() = fgRgb?.let { JBColor(it, it) }
Expand All @@ -458,7 +466,7 @@ data class LogHighlightingPattern(@Attribute("enabled") var enabled: Boolean,
}

public override fun clone(): LogHighlightingPattern {
return LogHighlightingPattern(enabled, pattern, action, fgRgb, bgRgb, bold, italic, showOnStripe, uuid)
return LogHighlightingPattern(enabled, pattern, formatId, captureGroup, action, fgRgb, bgRgb, bold, italic, showOnStripe, uuid)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LogPatternTableModel(private var store: LogHighlightingSettingsStore.State
}

fun addNewPattern(pattern: String) {
store.patterns.add(LogHighlightingPattern(true, pattern, LogHighlightingAction.HIGHLIGHT_FIELD, null, null, bold = false, italic = false, showOnStripe = true, UUID.randomUUID()))
store.patterns.add(LogHighlightingPattern(true, pattern, null, 0, LogHighlightingAction.HIGHLIGHT_FIELD, null, null, bold = false, italic = false, showOnStripe = true, UUID.randomUUID()))
val index = store.patterns.size - 1
fireTableRowsInserted(index, index)
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/intellij/ideolog/lex/LogFileFormats.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class LogFileFormat(val myRegexLogParser: RegexLogParser?) {
return tokens.last { !it.isSeparator }
}

fun validateFormatUUID(uuid: UUID?): Boolean =
uuid == null || myRegexLogParser?.uuid == uuid


fun parseLogEventTimeSeconds(time: CharSequence): Long? {
return myRegexLogParser?.let {
try {
Expand Down

0 comments on commit ea47623

Please sign in to comment.