Skip to content

Commit

Permalink
feat(shirelang): add SonarLint action and tool window integration
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 24, 2024
1 parent b90f450 commit 20e7f13
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ enum class ShireActionLocation(val location: String, val description: String) {
DATABASE_MENU("DatabaseMenu", "Show in Database panel menu bar"),
CONSOLE_MENU("ConsoleMenu", "Show in Console panel menu bar"),
VCS_LOG_MENU("VcsLogMenu", "Show in VCS Log panel menu bar"),

/// external plugins
EXT_SONARQUBE_MENU("ExtSonarQubeMenu", "Show in SonarQube panel menu bar"),
;

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.phodal.shirelang

import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.ActionToolbar
import com.intellij.openapi.actionSystem.Constraints
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.smartReadAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.ui.SimpleToolWindowPanel
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.openapi.wm.ex.ToolWindowManagerListener
import com.intellij.psi.PsiManager
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.ProjectScope
Expand Down Expand Up @@ -36,6 +42,7 @@ class ShireActionStartupActivity : ProjectActivity {

attachTerminalAction()
attachDatabaseAction(project)
attachSonarLintAction(project)
}
}

Expand Down Expand Up @@ -70,6 +77,11 @@ class ShireActionStartupActivity : ProjectActivity {
}
}

private fun attachSonarLintAction(project: Project) {
project.getMessageBus().connect()
.subscribe(ToolWindowManagerListener.TOPIC, SonarLintToolWindowListener(project));
}

companion object {
private fun obtainShireFiles(project: Project): List<ShireFile> {
ApplicationManager.getApplication().assertReadAccessAllowed()
Expand Down Expand Up @@ -99,3 +111,21 @@ class ShireActionStartupActivity : ProjectActivity {

}
}

class SonarLintToolWindowListener(private val project: Project) : ToolWindowManagerListener {
override fun toolWindowShown(toolWindow: ToolWindow) {
if (toolWindow.id != "SonarLint") return
val action = ActionManager.getInstance().getAction("ShireSonarLintAction")

val contentManager = toolWindow.contentManager
val content = contentManager.getContent(0) ?: return

val simpleToolWindowPanel = content.component as? SimpleToolWindowPanel
val actionToolbar = simpleToolWindowPanel?.toolbar?.components?.get(0) as? ActionToolbar ?: return
val actionGroup = actionToolbar.actionGroup as? DefaultActionGroup

if (actionGroup?.containsAction(action) == false) {
actionGroup?.add(action)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.phodal.shirelang.actions.external

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.Project
import com.phodal.shirecore.config.ShireActionLocation
import com.phodal.shirecore.variable.template.VariableActionEventDataHolder
import com.phodal.shirelang.actions.ShireRunFileAction
import com.phodal.shirelang.actions.base.DynamicShireActionService

class ShireSonarLintAction : AnAction() {
override fun getActionUpdateThread() = ActionUpdateThread.EDT

private fun shireActionConfigs(project: Project) =
DynamicShireActionService.getInstance(project).getActions(ShireActionLocation.EXT_SONARQUBE_MENU)

override fun update(e: AnActionEvent) {
val project = e.project ?: return
val isOnlyOneConfig = shireActionConfigs(project).size == 1

val hobbitHole = shireActionConfigs(project).firstOrNull()?.hole
e.presentation.isVisible = isOnlyOneConfig
e.presentation.isEnabled = hobbitHole != null && hobbitHole.enabled
if (hobbitHole != null) {
e.presentation.text = hobbitHole.name ?: "<Placeholder>"
}
}

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return

VariableActionEventDataHolder.putData(VariableActionEventDataHolder(e.dataContext))

val config = shireActionConfigs(project).firstOrNull() ?: return
ShireRunFileAction.executeShireFile(project, config, null)
}
}
8 changes: 8 additions & 0 deletions shirelang/src/main/resources/com.phodal.shirelang.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@

</action>

<action id="ShireSonarLintAction"
class="com.phodal.shirelang.actions.external.ShireSonarLintAction"
icon="com.phodal.shirelang.ShireIcons.DEFAULT"
text="Shire SonarLint Action"
description="Shire SonarLint action">

</action>

<action id="ShireVcsLogAction"
class="com.phodal.shirelang.actions.vcs.ShireVcsLogAction"
icon="com.phodal.shirelang.ShireIcons.DEFAULT"
Expand Down

0 comments on commit 20e7f13

Please sign in to comment.