1
1
package luonq ;
2
2
3
3
import com .intellij .ide .projectView .ProjectView ;
4
- import com .intellij .ide .projectView .impl .ProjectViewImpl ;
5
4
import com .intellij .openapi .actionSystem .AnAction ;
6
5
import com .intellij .openapi .actionSystem .AnActionEvent ;
6
+ import com .intellij .openapi .actionSystem .CommonDataKeys ;
7
7
import com .intellij .openapi .diagnostic .Logger ;
8
8
import com .intellij .openapi .project .Project ;
9
-
10
- import java . lang . reflect . Field ;
11
- import java . lang . reflect . Method ;
9
+ import com . intellij . openapi . vfs . VirtualFile ;
10
+ import com . intellij . psi . PsiFileSystemItem ;
11
+ import com . intellij . psi . PsiManager ;
12
12
13
13
/**
14
14
* Created by Luonanqin on 11/13/14.
@@ -18,38 +18,22 @@ public class ScrollFromSource extends AnAction {
18
18
private static final Logger LOG = Logger .getInstance (ScrollFromSource .class );
19
19
20
20
public void actionPerformed (AnActionEvent e ) {
21
- try {
22
- Project project = e .getProject ();
23
- ProjectViewImpl projectView = (ProjectViewImpl ) ProjectView .getInstance (project );
24
-
25
- Class <ProjectViewImpl > clazz = ProjectViewImpl .class ;
26
-
27
- Field [] fields = clazz .getDeclaredFields ();
28
- Field myAutoScrollFromSourceHandlerField = null ;
29
- for (int i = 0 ; i < fields .length ; i ++) {
30
- Field field = fields [i ];
31
- // can't use field's name to find the variable of MyAutoScrollFromSourceHandler
32
- if ("MyAutoScrollFromSourceHandler" .equals (field .getType ().getSimpleName ())) {
33
- myAutoScrollFromSourceHandlerField = field ;
34
- }
35
- }
36
21
37
- myAutoScrollFromSourceHandlerField .setAccessible (true );
38
- Object handler = myAutoScrollFromSourceHandlerField .get (projectView );
39
-
40
- Class <?>[] clazzes = clazz .getDeclaredClasses ();
41
- for (int i = 0 ; i < clazzes .length ; i ++) {
42
- Class <?> clazze = clazzes [i ];
43
- String simpleName = clazze .getSimpleName ();
44
- if ("MyAutoScrollFromSourceHandler" .equals (simpleName )) {
45
- Method fromSource = clazze .getMethod ("scrollFromSource" );
46
- fromSource .setAccessible (true );
47
- fromSource .invoke (handler );
48
- return ;
22
+ Project project = e .getProject ();
23
+ VirtualFile file = CommonDataKeys .VIRTUAL_FILE .getData (e .getDataContext ());
24
+
25
+ if (project != null && file != null ) {
26
+ VirtualFile target = file .getCanonicalFile ();
27
+
28
+ if (target != null ) {
29
+ PsiManager psiManager = PsiManager .getInstance (project );
30
+ PsiFileSystemItem psiFile = target .isDirectory () ? psiManager .findDirectory (target ) : psiManager .findFile (target );
31
+
32
+ if (psiFile != null ) {
33
+ ProjectView .getInstance (project ).select (psiFile , target , false );
34
+
49
35
}
50
36
}
51
- } catch (Exception ex ) {
52
- LOG .error ("ScrollFromSource execute ERROR!" , ex );
53
37
}
54
38
}
55
39
}
0 commit comments