1
1
package ts .eclipse .ide .jsdt .internal .ui .editor .codelens ;
2
2
3
+ import org .eclipse .jface .text .IRegion ;
4
+ import org .eclipse .jface .text .ITextSelection ;
5
+ import org .eclipse .jface .text .ITextViewer ;
6
+ import org .eclipse .jface .text .ITextViewerExtension5 ;
7
+ import org .eclipse .jface .text .Region ;
8
+ import org .eclipse .jface .text .TextSelection ;
3
9
import org .eclipse .jface .text .provisional .codelens .CodeLens ;
4
10
import org .eclipse .jface .text .provisional .codelens .Range ;
11
+ import org .eclipse .jface .util .Geometry ;
12
+ import org .eclipse .swt .SWT ;
13
+ import org .eclipse .swt .custom .StyledText ;
14
+ import org .eclipse .swt .graphics .Point ;
15
+ import org .eclipse .swt .graphics .Rectangle ;
16
+ import org .eclipse .swt .widgets .Display ;
17
+ import org .eclipse .swt .widgets .Shell ;
5
18
import org .eclipse .wst .jsdt .internal .ui .search .SearchUtil ;
6
19
7
20
import ts .TypeScriptException ;
8
21
import ts .eclipse .ide .core .resources .IIDETypeScriptFile ;
22
+ import ts .eclipse .ide .ui .implementation .TypeScriptImplementationDialog ;
9
23
import ts .eclipse .ide .ui .search .TypeScriptSearchQuery ;
24
+ import ts .eclipse .ide .ui .utils .EditorUtils ;
10
25
11
26
public class ReferencesCodeLens extends CodeLens {
12
27
28
+ private final ITextViewer fTextViewer ;
13
29
private final IIDETypeScriptFile tsFile ;
14
30
15
- public ReferencesCodeLens (IIDETypeScriptFile tsFile , Range range ) {
31
+ public ReferencesCodeLens (ITextViewer textViewer , IIDETypeScriptFile tsFile , Range range ) {
16
32
super (range );
33
+ this .fTextViewer = textViewer ;
17
34
this .tsFile = tsFile ;
18
35
}
19
36
@@ -23,7 +40,9 @@ public IIDETypeScriptFile getTsFile() {
23
40
24
41
@ Override
25
42
public void open () {
26
- if (getCommand ().getCommand ().equals ("references" )) {
43
+ String command = getCommand ().getCommand ();
44
+ if (command .equals ("references" )) {
45
+ // Execute Search
27
46
try {
28
47
int offset = tsFile .getPosition (getRange ().startLineNumber , getRange ().startColumn );
29
48
TypeScriptSearchQuery query = new TypeScriptSearchQuery (tsFile .getResource (), offset );
@@ -32,8 +51,77 @@ public void open() {
32
51
e .printStackTrace ();
33
52
}
34
53
} else {
54
+ // Open Implementation dialog
55
+ Display .getDefault ().asyncExec (() -> {
56
+ try {
57
+ Shell parent = Display .getDefault ().getActiveShell ();
58
+ TypeScriptImplementationDialog dialog = new TypeScriptImplementationDialog (parent , SWT .RESIZE ,
59
+ tsFile );
60
+ int offset = tsFile .getPosition (getRange ().startLineNumber , getRange ().startColumn );
61
+ ITextSelection selection = new TextSelection (offset , 1 );
62
+ Rectangle size = computeArea (new Region (offset , 1 ));
63
+ dialog .setSize (450 , 500 );
64
+ dialog .setInput (selection );
65
+ dialog .open ();
66
+ } catch (TypeScriptException e ) {
67
+ e .printStackTrace ();
68
+ }
69
+ });
35
70
36
71
}
37
72
}
38
73
74
+ /**
75
+ * Determines the graphical area covered by the given text region.
76
+ *
77
+ * @param region the region whose graphical extend must be computed
78
+ * @return the graphical extend of the given region
79
+ */
80
+ private Rectangle computeArea (IRegion region ) {
81
+
82
+ int start = 0 ;
83
+ int end = 0 ;
84
+
85
+ IRegion widgetRegion = modelRange2WidgetRange (region );
86
+ if (widgetRegion != null ) {
87
+ start = widgetRegion .getOffset ();
88
+ end = widgetRegion .getOffset () + widgetRegion .getLength ();
89
+ }
90
+
91
+ StyledText styledText = fTextViewer .getTextWidget ();
92
+ Rectangle bounds ;
93
+ if (end > 0 && start < end )
94
+ bounds = styledText .getTextBounds (start , end - 1 );
95
+ else {
96
+ Point loc = styledText .getLocationAtOffset (start );
97
+ bounds = new Rectangle (loc .x , loc .y , 0 , styledText .getLineHeight (start ));
98
+ }
99
+
100
+ Rectangle clientArea = styledText .getClientArea ();
101
+ Geometry .moveInside (bounds , clientArea );
102
+ return bounds ;
103
+ }
104
+
105
+ /**
106
+ * Translated the given range in the viewer's document into the corresponding
107
+ * range of the viewer's widget.
108
+ *
109
+ * @param region the range in the viewer's document
110
+ * @return the corresponding widget range
111
+ * @since 2.1
112
+ */
113
+ private IRegion modelRange2WidgetRange (IRegion region ) {
114
+ if (fTextViewer instanceof ITextViewerExtension5 ) {
115
+ ITextViewerExtension5 extension = (ITextViewerExtension5 ) fTextViewer ;
116
+ return extension .modelRange2WidgetRange (region );
117
+ }
118
+
119
+ IRegion visibleRegion = fTextViewer .getVisibleRegion ();
120
+ int start = region .getOffset () - visibleRegion .getOffset ();
121
+ int end = start + region .getLength ();
122
+ if (end > visibleRegion .getLength ())
123
+ end = visibleRegion .getLength ();
124
+
125
+ return new Region (start , end - start );
126
+ }
39
127
}
0 commit comments