Skip to content

Commit

Permalink
Pushing changes to 2018.3 branch (v 0.4.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
thsaravana committed Aug 26, 2019
1 parent 043584f commit db40064
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Features
- Jump from post to subscribe and vice versa
- Shows marker only for `@Subscribe` methods that have correct signatures
- Fully supported for project using both Java and Kotlin

Testing
-----
There are no unit tests yet (I am writing them, but hit a roadblock. We will soon find a way). Any changes made to the plugin should be tested against <a href="https://github.com/thsaravana/eventbus-playground">this project</a>. This project contains all possible use cases of EventBus in both Java and Kotlin.
9 changes: 3 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.madrapps'
version '0.3.3.2018.3'
version '0.4.2.2018.3'

sourceCompatibility = 1.8

Expand Down Expand Up @@ -34,10 +34,7 @@ compileTestKotlin {

patchPluginXml {
changeNotes """
- Option to view usages in "Find" tool window <br>
- Improved the UI of the usages popup <br>
- Support for Enums <br>
- Support for `@file:JvmName()` <br>
- Performance improvement <br>
- Add breakpoints to `post` or `subscribe` in a single click <br>
- Option to clear added breakpoints as well <br>
"""
}
8 changes: 7 additions & 1 deletion src/main/kotlin/com/madrapps/eventbus/FindUsages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ internal fun UElement.getParentOfTypeCallExpression(): UCallExpression? {
.filterIsInstance<UQualifiedReferenceExpression>()
.firstOrNull()
?.selector as? UCallExpression
}
}

internal fun Usage.getPostStatementSourcePsi() = toUElement()?.getParentOfTypeCallExpression()?.sourcePsi

internal fun Usage.getSubscribeMethodSourcePsi() = getType<UMethod>()?.uastAnchor?.sourcePsi

internal fun Usage.file() = toUElement()?.sourcePsi?.containingFile
107 changes: 105 additions & 2 deletions src/main/kotlin/com/madrapps/eventbus/PopUpView.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.madrapps.eventbus

import com.intellij.debugger.DebuggerManagerEx
import com.intellij.debugger.ui.breakpoints.BreakpointManager
import com.intellij.debugger.ui.breakpoints.BreakpointWithHighlighter
import com.intellij.icons.AllIcons
import com.intellij.icons.AllIcons.Toolwindows.ToolWindowFind
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.ActionPlaces.USAGE_VIEW_TOOLBAR
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.actionSystem.IdeActions.ACTION_FIND_USAGES
import com.intellij.openapi.editor.Document
import com.intellij.openapi.ui.popup.JBPopup
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.psi.PsiDocumentManager
import com.intellij.ui.ScrollingUtil
import com.intellij.ui.SimpleColoredComponent
import com.intellij.ui.SimpleTextAttributes.REGULAR_ATTRIBUTES
Expand All @@ -20,6 +26,9 @@ import com.intellij.util.PlatformIcons
import com.intellij.util.ui.ColumnInfo
import com.intellij.util.ui.ListTableModel
import com.intellij.util.ui.UIUtil
import org.jetbrains.java.debugger.breakpoints.properties.JavaBreakpointProperties
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.uast.UClass
import org.jetbrains.uast.UFile
import org.jetbrains.uast.UMethod
Expand Down Expand Up @@ -91,7 +100,11 @@ private fun showTablePopUp(usages: List<Usage>, columnInfos: Array<MyColumnInfo>

if (usages.isNotEmpty()) {
val actionGroup = DefaultActionGroup()
actionGroup.add(ShowUsagesAction(usages) { popUp?.closeOk(it) })
val closePopUp: (InputEvent) -> Unit = { popUp?.closeOk(it) }
actionGroup.add(SetBreakpointAction(usages, closePopUp))
actionGroup.add(RemoveBreakpointAction(usages, closePopUp))
actionGroup.addSeparator()
actionGroup.add(ShowUsagesAction(usages, closePopUp))
val actionToolbar = ActionManager.getInstance().createActionToolbar(USAGE_VIEW_TOOLBAR, actionGroup, true)
actionToolbar.setReservePlaceAutoPopupIcon(false)
val toolBar = actionToolbar.component
Expand All @@ -103,7 +116,7 @@ private fun showTablePopUp(usages: List<Usage>, columnInfos: Array<MyColumnInfo>
return popUp
}

private fun getTitle(usages: List<Usage>) = if (usages.isEmpty()) "No usages found" else "Find Usages"
private fun getTitle(usages: List<Usage>) = if (usages.isEmpty()) "No usages found" else "Usages"

private fun resizeColumnWidth(table: JTable) {
val columnModel = table.columnModel
Expand Down Expand Up @@ -199,4 +212,94 @@ private class ShowUsagesAction(
usageViewPresentation
)
}
}

private class SetBreakpointAction(
private val usages: List<Usage>,
private val closePopUp: (InputEvent) -> Unit
) : AnAction(
"Set Breakpoints",
"Set breakpoints at all usages",
AllIcons.Debugger.Db_set_breakpoint
) {
override fun actionPerformed(e: AnActionEvent) {
closePopUp(e.inputEvent)
val breakpointManager = DebuggerManagerEx.getInstanceEx(e.project!!).breakpointManager
usages.forEach {
val containingFile = it.file()
if (containingFile != null) {
val document = PsiDocumentManager.getInstance(e.project!!).getDocument(containingFile)
if (document != null) {
val isBreakpointAdded = addLineBreakpoint(it, breakpointManager, document)
if (!isBreakpointAdded) {
addMethodBreakpoint(it, breakpointManager, document)
}
}
}
}
}

private fun addLineBreakpoint(
it: Usage,
breakpointManager: BreakpointManager,
document: Document
): Boolean {
val sourcePsi = it.getPostStatementSourcePsi()
if (sourcePsi != null) {
val lineNumber = sourcePsi.getLineNumber(true)
breakpointManager.addLineBreakpoint(document, lineNumber)
}
return sourcePsi != null
}

private fun addMethodBreakpoint(
it: Usage,
breakpointManager: BreakpointManager,
document: Document?
) {
val source = it.getSubscribeMethodSourcePsi()
if (source != null) {
val lineNumber = source.getLineNumber(true)
if (breakpointManager.findBreakpoint<BreakpointWithHighlighter<JavaBreakpointProperties<*>>>(
document,
source.startOffset,
null
) == null
) {
breakpointManager.addMethodBreakpoint(document, lineNumber)
}
}
}
}

private class RemoveBreakpointAction(
private val usages: List<Usage>,
private val closePopUp: (InputEvent) -> Unit
) : AnAction(
"Remove Breakpoints",
"Remove breakpoints at all usages",
AllIcons.Debugger.MuteBreakpoints
) {
override fun actionPerformed(e: AnActionEvent) {
closePopUp(e.inputEvent)
val project = e.project!!
val breakpointManager = DebuggerManagerEx.getInstanceEx(project).breakpointManager
val documentManager = PsiDocumentManager.getInstance(project)
usages.forEach {
val containingFile = it.file()
if (containingFile != null) {
val document = documentManager.getDocument(containingFile)
val source = it.getPostStatementSourcePsi() ?: it.getSubscribeMethodSourcePsi()
if (source != null && document != null) {
val breakPoint =
breakpointManager.findBreakpoint<BreakpointWithHighlighter<JavaBreakpointProperties<*>>>(
document,
source.startOffset,
null
)
breakpointManager.removeBreakpoint(breakPoint)
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<li>Supports <i>post</i>, <i>postSticky</i> and <i>@Subscribe</i></li>
<li>Jump from post to subscribe and vice versa</li>
<li>Shows marker only for <i>@Subscribe</i> methods that have correct signatures</li>
<li>Optionally show usages in 'Find' tool window</li>
<li>Add breakpoints to all usages in a single click</li>
<li>Fully supported for project using both Java and Kotlin</li>
</ul>]]>
</description>
Expand Down

0 comments on commit db40064

Please sign in to comment.