Skip to content

Commit

Permalink
Merge pull request #64 from orangain/menu-to-disable-plugin
Browse files Browse the repository at this point in the history
Add context menu to temporarily disable formatting
  • Loading branch information
orangain authored Sep 8, 2024
2 parents e9c8c56 + 91e8b09 commit be79dda
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.orangain.prettyjsonlog.action

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.DumbAwareToggleAction
import io.github.orangain.prettyjsonlog.service.EphemeralStateService

class ToggleEnabledAction : DumbAwareToggleAction() {
override fun isSelected(e: AnActionEvent): Boolean {
val consoleView = e.getData(LangDataKeys.CONSOLE_VIEW) ?: return false
val project = e.project ?: return false
val service = project.service<EphemeralStateService>()
return service.isEnabled(consoleView)
}

override fun setSelected(e: AnActionEvent, state: Boolean) {
thisLogger().debug("setSelected: $state")
val consoleView = e.getData(LangDataKeys.CONSOLE_VIEW) ?: return
val project = e.project ?: return
val service = project.service<EphemeralStateService>()
service.setEnabled(consoleView, state)
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
package io.github.orangain.prettyjsonlog.console

import com.intellij.execution.ConsoleFolding
import com.intellij.execution.ui.ConsoleView
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import io.github.orangain.prettyjsonlog.json.isPartOfPrettyJson
import io.github.orangain.prettyjsonlog.service.EphemeralStateService

class MyConsoleFolding : ConsoleFolding() {
private var consoleView: ConsoleView? = null

override fun getPlaceholderText(project: Project, lines: List<String>): String {
return "{...}"
}

override fun shouldFoldLine(project: Project, line: String): Boolean {
thisLogger().debug("shouldFoldLine: $line")
if (!isEnabled(project)) {
return false
}
return isPartOfPrettyJson(line)
}

override fun isEnabledForConsole(consoleView: ConsoleView): Boolean {
// This method "isEnabledForConsole" is not for storing consoleView, but we use it for that purpose because
// there is no other way to get consoleView reference in "shouldFoldLine" method.
this.consoleView = consoleView
return true
}

private fun isEnabled(project: Project): Boolean {
val service = project.service<EphemeralStateService>()
val consoleView = this.consoleView ?: return false
return service.isEnabled(consoleView)
}

// override fun shouldBeAttachedToThePreviousLine(): Boolean {
// return false
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
package io.github.orangain.prettyjsonlog.console

import com.intellij.execution.filters.ConsoleInputFilterProvider
import com.intellij.execution.filters.ConsoleDependentInputFilterProvider
import com.intellij.execution.filters.InputFilter
import com.intellij.execution.ui.ConsoleView
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Pair
import com.intellij.psi.search.GlobalSearchScope
import io.github.orangain.prettyjsonlog.json.parseJson
import io.github.orangain.prettyjsonlog.json.prettyPrintJson
import io.github.orangain.prettyjsonlog.logentry.*
import io.github.orangain.prettyjsonlog.service.EphemeralStateService
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class MyConsoleInputFilterProvider : ConsoleInputFilterProvider {
override fun getDefaultFilters(project: Project): Array<InputFilter> {
// We use ConsoleDependentInputFilterProvider instead of ConsoleInputFilterProvider because we need to access
// ConsoleView and Project in the filter.
class MyConsoleInputFilterProvider : ConsoleDependentInputFilterProvider() {
override fun getDefaultFilters(
consoleView: ConsoleView,
project: Project,
scope: GlobalSearchScope
): MutableList<InputFilter> {
thisLogger().debug("getDefaultFilters")
return arrayOf(MyConsoleInputFilter())
return mutableListOf(MyConsoleInputFilter(consoleView, project))
}
}

private val zoneId = ZoneId.systemDefault()
private val timestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")

class MyConsoleInputFilter : InputFilter {
class MyConsoleInputFilter(
private val consoleView: ConsoleView,
private val project: Project
) : InputFilter {
override fun applyFilter(
text: String,
contentType: ConsoleViewContentType
): MutableList<Pair<String, ConsoleViewContentType>>? {
thisLogger().debug("contentType: $contentType, applyFilter: $text")
if (!isEnabled()) {
return null
}
val (node, suffixWhitespaces) = parseJson(text) ?: return null

val timestamp = extractTimestamp(node)
Expand All @@ -49,6 +65,11 @@ class MyConsoleInputFilter : InputFilter {
),
)
}

private fun isEnabled(): Boolean {
val service = project.service<EphemeralStateService>()
return service.isEnabled(consoleView)
}
}

private fun contentTypeOf(level: Level?, inputContentType: ConsoleViewContentType): ConsoleViewContentType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.orangain.prettyjsonlog.service

import com.intellij.execution.ui.ConsoleView
import com.intellij.openapi.components.Service
import java.util.*

@Service(Service.Level.PROJECT)
class EphemeralStateService {
private val enabledMap = WeakHashMap<ConsoleView, Boolean>()

/**
* Returns true if the formatting is enabled for the given console view.
*/
fun isEnabled(consoleView: ConsoleView): Boolean {
return enabledMap[consoleView] ?: true // default is true
}

/**
* Sets the enabled state of the formatting for the given console view.
*/
fun setEnabled(consoleView: ConsoleView, value: Boolean) {
enabledMap[consoleView] = value
}
}
14 changes: 14 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@
<listener class="io.github.orangain.prettyjsonlog.listeners.MyApplicationActivationListener"
topic="com.intellij.openapi.application.ApplicationActivationListener"/>
</applicationListeners>

<actions>
<group id="io.github.orangain.prettyjsonlog.ConsoleContextMenuRoot">
<add-to-group group-id="ConsoleView.PopupMenu" anchor="first"/>

<group id="io.github.orangain.prettyjsonlog.ConsoleContextMenuGroup" popup="true" text="Pretty JSON Log">
<action
id="io.github.orangain.prettyjsonlog.action.ToggleEnabledAction"
class="io.github.orangain.prettyjsonlog.action.ToggleEnabledAction">
</action>
</group>
<separator/>
</group>
</actions>
</idea-plugin>
2 changes: 2 additions & 0 deletions src/main/resources/messages/MyBundle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
applicationActivated=Application activated
action.io.github.orangain.prettyjsonlog.action.ToggleEnabledAction.text=Formatting Enabled
action.io.github.orangain.prettyjsonlog.action.ToggleEnabledAction.description=Enable or disable formatting of JSON logs in the console

0 comments on commit be79dda

Please sign in to comment.