Skip to content

Commit fb75f91

Browse files
committed
Open Implementation popup when click on "implementations" CodeLens. See
#181
1 parent 5b5dca0 commit fb75f91

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/editor/codelens/ReferencesCodeLens.java

+90-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
package ts.eclipse.ide.jsdt.internal.ui.editor.codelens;
22

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;
39
import org.eclipse.jface.text.provisional.codelens.CodeLens;
410
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;
518
import org.eclipse.wst.jsdt.internal.ui.search.SearchUtil;
619

720
import ts.TypeScriptException;
821
import ts.eclipse.ide.core.resources.IIDETypeScriptFile;
22+
import ts.eclipse.ide.ui.implementation.TypeScriptImplementationDialog;
923
import ts.eclipse.ide.ui.search.TypeScriptSearchQuery;
24+
import ts.eclipse.ide.ui.utils.EditorUtils;
1025

1126
public class ReferencesCodeLens extends CodeLens {
1227

28+
private final ITextViewer fTextViewer;
1329
private final IIDETypeScriptFile tsFile;
1430

15-
public ReferencesCodeLens(IIDETypeScriptFile tsFile, Range range) {
31+
public ReferencesCodeLens(ITextViewer textViewer, IIDETypeScriptFile tsFile, Range range) {
1632
super(range);
33+
this.fTextViewer = textViewer;
1734
this.tsFile = tsFile;
1835
}
1936

@@ -23,7 +40,9 @@ public IIDETypeScriptFile getTsFile() {
2340

2441
@Override
2542
public void open() {
26-
if (getCommand().getCommand().equals("references")) {
43+
String command = getCommand().getCommand();
44+
if (command.equals("references")) {
45+
// Execute Search
2746
try {
2847
int offset = tsFile.getPosition(getRange().startLineNumber, getRange().startColumn);
2948
TypeScriptSearchQuery query = new TypeScriptSearchQuery(tsFile.getResource(), offset);
@@ -32,8 +51,77 @@ public void open() {
3251
e.printStackTrace();
3352
}
3453
} 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+
});
3570

3671
}
3772
}
3873

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+
}
39127
}

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/editor/codelens/TypeScriptBaseCodeLensProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ICodeLens[] provideCodeLenses(ITextViewer textViewer) {
4646
if (tree != null && tree.hasChildItems()) {
4747
tree.getChildItems().forEach(item -> this.walkNavTree(tsFile, item, null, referenceableSpans));
4848
}
49-
return referenceableSpans.stream().map(span -> new ReferencesCodeLens(tsFile, span))
49+
return referenceableSpans.stream().map(span -> new ReferencesCodeLens(textViewer, tsFile, span))
5050
.collect(Collectors.toList()).toArray(new ICodeLens[0]);
5151
} catch (Exception e) {
5252
TypeScriptUIPlugin.log("Error while TypeScript codelens", e);

eclipse/ts.eclipse.ide.ui/src/ts/eclipse/ide/ui/implementation/TypeScriptImplementationDialog.java

+4
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,8 @@ protected ILabelProvider getLabelProvider() {
7272
return new TypeScriptImplementationLabelProvider();
7373
}
7474

75+
@Override
76+
public void setSize(int width, int height) {
77+
super.setSize(width, height);
78+
}
7579
}

0 commit comments

Comments
 (0)