2
2
3
3
import com .github .minecraft_ta .totalDebugCompanion .CompanionApp ;
4
4
import com .github .minecraft_ta .totalDebugCompanion .messages .codeView .DecompileOrOpenMessage ;
5
+ import com .github .minecraft_ta .totalDebugCompanion .ui .components .global .BottomInformationBar ;
6
+ import com .github .minecraft_ta .totalDebugCompanion .util .UIUtils ;
5
7
import org .eclipse .jdt .core .IJavaElement ;
6
8
import org .eclipse .jdt .core .JavaModelException ;
9
+ import org .eclipse .jdt .core .SourceRange ;
7
10
import org .eclipse .jdt .internal .core .*;
8
11
import org .fife .ui .rsyntaxtextarea .LinkGenerator ;
9
12
import org .fife .ui .rsyntaxtextarea .LinkGeneratorResult ;
10
13
import org .fife .ui .rsyntaxtextarea .RSyntaxTextArea ;
14
+ import org .fife .ui .rtextarea .RTextScrollPane ;
11
15
12
16
import javax .swing .event .HyperlinkEvent ;
13
17
import java .util .Arrays ;
14
18
15
19
public class CustomJavaLinkGenerator implements LinkGenerator {
16
20
17
21
private final String identifier ;
22
+ private final BottomInformationBar informationBar ;
18
23
19
- public CustomJavaLinkGenerator (String identifier ) {
24
+ public CustomJavaLinkGenerator (String identifier , BottomInformationBar informationBar ) {
20
25
this .identifier = identifier ;
26
+ this .informationBar = informationBar ;
21
27
}
22
28
23
29
@ Override
@@ -35,47 +41,73 @@ public LinkGeneratorResult isLinkAtOffset(RSyntaxTextArea textArea, int offs) {
35
41
return null ;
36
42
}
37
43
38
- return new LinkResult (elements [0 ], offs );
44
+ return new LinkResult (textArea , elements [0 ], offs );
39
45
} catch (JavaModelException e ) {
40
46
e .printStackTrace ();
41
47
return null ;
42
48
}
43
49
}
44
50
45
- private static class LinkResult implements LinkGeneratorResult {
51
+ private class LinkResult implements LinkGeneratorResult {
46
52
53
+ private final RSyntaxTextArea textArea ;
47
54
private final IJavaElement el ;
48
55
private final int offs ;
49
56
50
- public LinkResult (IJavaElement el , int offs ) {
57
+ public LinkResult (RSyntaxTextArea textArea , IJavaElement el , int offs ) {
58
+ this .textArea = textArea ;
51
59
this .el = el ;
52
60
this .offs = offs ;
53
61
}
54
62
55
63
@ Override
56
64
public HyperlinkEvent execute () {
57
- //TODO: Handle these differently
58
- if ( el instanceof SourceMethod || el instanceof SourceField || el instanceof SourceType )
65
+ if (! CompanionApp . SERVER . isClientConnected ()) {
66
+ informationBar . setFailureInfoText ( "Not connected to game client!" );
59
67
return null ;
68
+ }
60
69
61
- String className ;
62
- int targetMemberType = -1 ;
63
- String targetMemberIdentifier = "" ;
64
- if (el instanceof ResolvedBinaryMethod method ) {
65
- className = method .getDeclaringType ().getFullyQualifiedName ();
66
- targetMemberType = method .getElementType ();
67
- targetMemberIdentifier = method .getKey ();
68
- } else if (el instanceof ResolvedBinaryField field ) {
69
- className = field .getDeclaringType ().getFullyQualifiedName ();
70
- targetMemberType = field .getElementType ();
71
- targetMemberIdentifier = field .getElementName ();
72
- } else if (el instanceof ResolvedBinaryType type ) {
73
- className = type .getFullyQualifiedName ();
74
- } else {
75
- return null ;
70
+ try {
71
+ if (el instanceof LocalVariable || el instanceof SourceMethod || el instanceof SourceField || el instanceof SourceType ) {
72
+ var sourceRange = switch (el ) {
73
+ case LocalVariable lv ->
74
+ SourceRange .isAvailable (lv .getNameRange ()) ? lv .getNameRange () : lv .getSourceRange ();
75
+ case SourceRefElement sr ->
76
+ SourceRange .isAvailable (sr .getNameRange ()) ? sr .getNameRange () : sr .getSourceRange ();
77
+ default -> throw new IllegalStateException ();
78
+ };
79
+
80
+ if (!SourceRange .isAvailable (sourceRange )) {
81
+ System .err .println ("SourceRange is not available for " + el );
82
+ return null ;
83
+ }
84
+
85
+ UIUtils .centerViewportOnRange (((RTextScrollPane ) textArea .getParent ().getParent ()), sourceRange .getOffset (), sourceRange .getOffset ());
86
+ return null ;
87
+ }
88
+
89
+ String className ;
90
+ int targetMemberType = -1 ;
91
+ String targetMemberIdentifier = "" ;
92
+ if (el instanceof ResolvedBinaryMethod method ) {
93
+ className = method .getDeclaringType ().getFullyQualifiedName ();
94
+ targetMemberType = method .getElementType ();
95
+ targetMemberIdentifier = method .getKey ();
96
+ } else if (el instanceof ResolvedBinaryField field ) {
97
+ className = field .getDeclaringType ().getFullyQualifiedName ();
98
+ targetMemberType = field .getElementType ();
99
+ targetMemberIdentifier = field .getElementName ();
100
+ } else if (el instanceof ResolvedBinaryType type ) {
101
+ className = type .getFullyQualifiedName ();
102
+ } else {
103
+ return null ;
104
+ }
105
+
106
+ CompanionApp .SERVER .getMessageProcessor ().enqueueMessage (new DecompileOrOpenMessage (className , targetMemberType , targetMemberIdentifier ));
107
+ } catch (JavaModelException e ) {
108
+ e .printStackTrace ();
76
109
}
77
110
78
- CompanionApp .SERVER .getMessageProcessor ().enqueueMessage (new DecompileOrOpenMessage (className , targetMemberType , targetMemberIdentifier ));
79
111
return null ;
80
112
}
81
113
0 commit comments