Skip to content

Commit 9255021

Browse files
committed
support primitive type
1 parent 600689b commit 9255021

File tree

7 files changed

+61
-62
lines changed

7 files changed

+61
-62
lines changed

EventBus-Navigator.jar

835 Bytes
Binary file not shown.

ReadMe.md

+2-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
[中文介绍](ReadMe_zh.md)
66

77
- **Kotlin** language support
8-
- support encapsulated `post` function
8+
- Support encapsulated `post` function
9+
- Support primitive type
910
- based on [likfe/eventbus3-intellij-plugin](https://github.com/likfe/eventbus3-intellij-plugin)
1011

1112
## Preview
@@ -50,24 +51,6 @@ IDEA or Android Studio `Settings/Plugins/Marketplace` search `EventBus-Navigator
5051
`Settings/Plugins/` Install Plugin from Disk.
5152

5253

53-
## Warning
54-
55-
Not support navigate from that java or kotlin post primitive type to kotlin receiver function, but java receiver is ok.
56-
57-
primitive type :
58-
59-
```
60-
"kotlin.Int" to "java.lang.Integer",
61-
"kotlin.Boolean" to "java.lang.Boolean",
62-
"kotlin.Char" to "java.lang.Character",
63-
"kotlin.Double" to "java.lang.Double",
64-
"kotlin.Float" to "java.lang.Float",
65-
"kotlin.Long" to "java.lang.Long",
66-
"kotlin.Any" to "java.lang.Object",
67-
"kotlin.String" to "java.lang.String"
68-
```
69-
70-
7154
## Thanks
7255

7356
- [likfe/eventbus3-intellij-plugin](https://github.com/likfe/eventbus3-intellij-plugin)

ReadMe_zh.md

+1-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
- 支持Kotlin语言
66
- 支持封装的YourBus.post函数
7+
- 支持基础类型
78
- 修改于 [likfe/eventbus3-intellij-plugin](https://github.com/likfe/eventbus3-intellij-plugin)
89

910
![](/pic/s1.png)
@@ -49,23 +50,6 @@ IDEA or Android Studio `Settings/Plugins/Marketplace` search `EventBus-Navigator
4950
`Settings/Plugins/` Install Plugin from Disk.
5051

5152

52-
## Warning
53-
54-
目前(可能以后也)不支持java/kotlin post 基础类型 函数导航到kotlin接收函数,但Java可以。
55-
56-
基础类型包括:
57-
58-
```
59-
"kotlin.Int" to "java.lang.Integer",
60-
"kotlin.Boolean" to "java.lang.Boolean",
61-
"kotlin.Char" to "java.lang.Character",
62-
"kotlin.Double" to "java.lang.Double",
63-
"kotlin.Float" to "java.lang.Float",
64-
"kotlin.Long" to "java.lang.Long",
65-
"kotlin.Any" to "java.lang.Object",
66-
"kotlin.String" to "java.lang.String"
67-
```
68-
6953
## Thanks
7054

7155
- [likfe/eventbus3-intellij-plugin](https://github.com/likfe/eventbus3-intellij-plugin)

resources/META-INF/plugin.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<id>cn.vove7.ide.plugin.eventbus_navigator</id>
44
<name>EventBus-Navigator</name>
55
<vendor email="[email protected]" url="http://www.github.com/Vove7/EventBus-Navigator">Vove7</vendor>
6-
<version>1.1.0</version>
6+
<version>1.2</version>
77

88
<description><![CDATA[
99
<article class="markdown-body entry-content p-5" itemprop="text"><h1><a id="user-content-eventbus-navigator" class="anchor" aria-hidden="true" href="#eventbus-navigator"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>EventBus-Navigator</h1>

src/cn/vove7/eventbus_nav/action/ShowUsagesAction.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
import com.intellij.openapi.wm.IdeFocusManager;
6868
import com.intellij.psi.PsiDocumentManager;
6969
import com.intellij.psi.PsiElement;
70+
import com.intellij.psi.PsiJavaCodeReferenceElement;
71+
import com.intellij.psi.PsiMethod;
7072
import com.intellij.psi.search.GlobalSearchScope;
7173
import com.intellij.psi.search.ProjectScope;
7274
import com.intellij.psi.search.PsiElementProcessor;
@@ -80,6 +82,7 @@
8082
import com.intellij.ui.TableUtil;
8183
import com.intellij.ui.awt.RelativePoint;
8284
import com.intellij.ui.popup.AbstractPopup;
85+
import com.intellij.usageView.UsageInfo;
8386
import com.intellij.usageView.UsageViewBundle;
8487
import com.intellij.usages.PsiElementUsageTarget;
8588
import com.intellij.usages.Usage;
@@ -110,6 +113,8 @@
110113
import org.jetbrains.annotations.NonNls;
111114
import org.jetbrains.annotations.NotNull;
112115
import org.jetbrains.annotations.Nullable;
116+
import org.jetbrains.kotlin.psi.KtNameReferenceExpression;
117+
import org.jetbrains.kotlin.psi.KtNamedFunction;
113118

114119
import java.awt.BorderLayout;
115120
import java.awt.Component;
@@ -148,6 +153,11 @@ public class ShowUsagesAction extends AnAction implements PopupAction {
148153
private static final int USAGES_PAGE_SIZE = 100;
149154

150155
private Filter filter;
156+
//0 search receiver; 1 search post
157+
private int type;
158+
159+
public static final int TYPE_RECEIVER = 0;
160+
public static final int TYPE_POST = 1;
151161

152162
public final static NullUsage MORE_USAGES_SEPARATOR = NullUsage.INSTANCE;
153163
private static final UsageNode MORE_USAGES_SEPARATOR_NODE = UsageViewImpl.NULL_NODE;
@@ -193,8 +203,9 @@ public ShowUsagesAction() {
193203
myUsageViewSettings.GROUP_BY_SCOPE = false;
194204
}
195205

196-
public ShowUsagesAction(Filter filter) {
206+
public ShowUsagesAction(Filter filter, int type) {
197207
this.filter = filter;
208+
this.type = type;
198209
setInjectedContext(true);
199210

200211
final UsageViewSettings usageViewSettings = UsageViewSettings.getInstance();
@@ -465,8 +476,22 @@ private void useHandler(@NotNull final FindUsagesHandler handler,
465476
Disposer.register(popup, indicator::cancel);
466477
}
467478

468-
protected @Nullable
469-
Usage transform(@NotNull Usage usage) {
479+
/**
480+
* 转换usage 显示行
481+
* 当使用@Subscribe 进行搜索时,显示列不是函数所在行,需要转换至函数Usage
482+
*/
483+
@SuppressWarnings("ConstantConditions")
484+
private Usage transform(@NotNull Usage usage) {
485+
if (type == TYPE_RECEIVER) {
486+
PsiElement ele = ((UsageInfo2UsageAdapter) usage).getElement();
487+
if (ele instanceof PsiJavaCodeReferenceElement) {
488+
PsiElement method = ((PsiMethod) ele.getContext().getContext().getContext()).getIdentifyingElement();
489+
return new UsageInfo2UsageAdapter(new UsageInfo(method));
490+
} else if (ele instanceof KtNameReferenceExpression) {
491+
PsiElement method = ((KtNamedFunction) ele.getParent().getParent().getParent().getParent().getParent().getParent()).getNameIdentifier();
492+
return new UsageInfo2UsageAdapter(new UsageInfo(method));
493+
}
494+
}
470495
return usage;
471496
}
472497

src/cn/vove7/eventbus_nav/filter/ReceiverFilter.kt

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
package cn.vove7.eventbus_nav.filter
22

3-
import cn.vove7.eventbus_nav.util.PsiUtils
3+
import cn.vove7.eventbus_nav.util.firstParamTypeName
4+
import cn.vove7.eventbus_nav.util.toPsiMethod
45
import com.intellij.psi.PsiJavaCodeReferenceElement
6+
import com.intellij.psi.PsiMethod
57
import com.intellij.usages.Usage
68
import com.intellij.usages.UsageInfo2UsageAdapter
79
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
10+
import org.jetbrains.kotlin.psi.KtNamedFunction
811

12+
//import org.jetbrains.kotlin.psi.KtNameReferenceExpression
913

10-
class ReceiverFilter : Filter {
14+
15+
class ReceiverFilter(
16+
private val paramType: String
17+
) : Filter {
1118
override fun shouldShow(usage: Usage): Boolean {
1219
val element = (usage as UsageInfo2UsageAdapter).element
20+
1321
if (element is PsiJavaCodeReferenceElement) {
14-
val method = element.context?.context?.context?.context ?: return false
15-
return PsiUtils.isEventBusReceiver(method)
22+
val me = element.context?.context?.context ?: return false
23+
if (me is PsiMethod) {
24+
return me.firstParamTypeName() == paramType
25+
}
1626
}
1727
if (element is KtNameReferenceExpression) {
18-
return PsiUtils.isEventBusReceiver(element.parent.parent.parent.parent.parent)
28+
val me = element.parent.parent.parent.parent.parent.parent
29+
if (me is KtNamedFunction) {
30+
return me.toPsiMethod()?.firstParamTypeName() == paramType
31+
}
1932
}
2033
return false
2134
}

src/cn/vove7/eventbus_nav/lineprovider/EventBus3LineMarkerProvider.kt

+11-17
Original file line numberDiff line numberDiff line change
@@ -93,48 +93,42 @@ class EventBus3LineMarkerProvider : LineMarkerProvider {
9393
?: return@forEach
9494
findEleUsageList.add(myPostMethod)
9595
}
96-
ShowUsagesAction(PosterFilter(paramType)).startFindUsages(findEleUsageList.toTypedArray(), RelativePoint(e), PsiUtilBase.findEditor(psiElement), MAX_USAGES)
96+
ShowUsagesAction(PosterFilter(paramType), ShowUsagesAction.TYPE_POST).startFindUsages(findEleUsageList.toTypedArray(), RelativePoint(e), PsiUtilBase.findEditor(psiElement), MAX_USAGES)
9797
}
9898

9999
/**
100100
* 解析post 数据类型
101101
* 根据类型 搜索引用,过滤函数
102102
*/
103103
private fun showReceivers(e: MouseEvent, psiElement: PsiElement) {
104+
val global = GlobalSearchScope.allScope(psiElement.project)
105+
val javaPsiFacade = JavaPsiFacade.getInstance(psiElement.project)
106+
val subscribeCls = javaPsiFacade.findClass("org.greenrobot.eventbus.Subscribe", global)
107+
?: return
104108
//Java 函数调用
105109
if (psiElement is PsiMethodCallExpression) {
106110
val expressionTypes = psiElement.argumentList.expressionTypes
107111
if (expressionTypes.isNotEmpty()) {
108-
val eventClass = PsiUtils.getClass(expressionTypes[0], psiElement)
109-
if (eventClass != null) {
110-
ShowUsagesAction(ReceiverFilter()).startFindUsages(eventClass, RelativePoint(e), PsiUtilBase.findEditor(psiElement), MAX_USAGES)
112+
val eventClass = PsiUtils.getClass(expressionTypes[0], psiElement) ?: return
113+
eventClass.qualifiedName?.also {
114+
ShowUsagesAction(ReceiverFilter(it), ShowUsagesAction.TYPE_RECEIVER).startFindUsages(subscribeCls, RelativePoint(e), PsiUtilBase.findEditor(psiElement), MAX_USAGES)
111115
return
112116
}
113117
}
114118
}
115119
//Kotlin 函数调用
116120
if (psiElement is KtReferenceExpression) {
117121
val argList = psiElement.nextSibling as KtValueArgumentList
118-
119122
val type = argList.arguments[0].getArgumentExpression()?.resolveType() as KotlinType
120-
121-
val global = GlobalSearchScope.allScope(psiElement.project)
122123
val ktType = KotlinPsiUtil.getTypeWithoutTypeParameters(type)
123-
124-
val javaPsiFacade = JavaPsiFacade.getInstance(psiElement.project)
125-
126-
//todo kt 基础 类型引用
127124
val clsType = ktType.toJavaType()
128-
val javaTypeCls = javaPsiFacade.findClass(clsType, global)
129-
if (javaTypeCls != null) {
130-
ShowUsagesAction(ReceiverFilter()).startFindUsages(javaTypeCls,
131-
RelativePoint(e), PsiUtilBase.findEditor(psiElement), MAX_USAGES)
132-
}
125+
ShowUsagesAction(ReceiverFilter(clsType), ShowUsagesAction.TYPE_RECEIVER).startFindUsages(subscribeCls,
126+
RelativePoint(e), PsiUtilBase.findEditor(psiElement), MAX_USAGES)
133127
}
134128
}
135129

136130
private fun showReferences(e: MouseEvent, psiElement: PsiElement) {
137-
ShowUsagesAction(Filter { true }).startFindUsages(psiElement,
131+
ShowUsagesAction(Filter { true }, -1).startFindUsages(psiElement,
138132
RelativePoint(e), PsiUtilBase.findEditor(psiElement), MAX_USAGES)
139133
AllIcons.General.Remove
140134

0 commit comments

Comments
 (0)