/
ErlTextHover.java
341 lines (316 loc) · 13.6 KB
/
ErlTextHover.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*******************************************************************************
* Copyright (c) 2004 Vlad Dumitrescu and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Vlad Dumitrescu
*******************************************************************************/
package org.erlide.ui.editors.erl.hover;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.core.model.IVariable;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextHoverExtension;
import org.eclipse.jface.text.ITextHoverExtension2;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.information.IInformationProviderExtension2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.editors.text.EditorsUI;
import org.erlide.backend.BackendCore;
import org.erlide.backend.IBackend;
import org.erlide.backend.IBackendManager;
import org.erlide.core.model.erlang.ErlToken;
import org.erlide.core.model.erlang.IErlFunction;
import org.erlide.core.model.erlang.IErlModule;
import org.erlide.core.model.erlang.IErlPreprocessorDef;
import org.erlide.core.model.root.ErlModelManager;
import org.erlide.core.model.root.IErlModel;
import org.erlide.core.model.root.IErlProject;
import org.erlide.core.model.util.ModelUtils;
import org.erlide.core.services.search.ErlideDoc;
import org.erlide.core.services.search.OpenResult;
import org.erlide.jinterface.ErlLogger;
import org.erlide.jinterface.rpc.IRpcCallSite;
import org.erlide.ui.actions.OpenAction;
import org.erlide.ui.editors.erl.ErlangEditor;
import org.erlide.ui.internal.ErlBrowserInformationControlInput;
import org.erlide.ui.internal.ErlideUIPlugin;
import org.erlide.ui.internal.information.ErlInformationPresenter;
import org.erlide.ui.internal.information.PresenterControlCreator;
import org.erlide.ui.util.eclipse.text.BrowserInformationControl;
import org.erlide.ui.util.eclipse.text.HTMLPrinter;
import org.erlide.utils.Util;
import org.osgi.framework.Bundle;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangTuple;
public class ErlTextHover implements ITextHover,
IInformationProviderExtension2, ITextHoverExtension,
ITextHoverExtension2 {
private static URL fgStyleSheet = null;
private IInformationControlCreator fHoverControlCreator;
private PresenterControlCreator fPresenterControlCreator;
private final ErlangEditor fEditor;
public ErlTextHover(final ErlangEditor editor) {
fEditor = editor;
initStyleSheet();
}
@Override
public IRegion getHoverRegion(final ITextViewer textViewer, final int offset) {
if (fEditor == null) {
return null;
}
fEditor.reconcileNow();
final ErlToken token = fEditor.getModule().getScannerTokenAt(offset);
if (token == null) {
return null;
}
return new Region(token.getOffset(), token.getLength());
}
private void initStyleSheet() {
final Bundle bundle = Platform.getBundle(ErlideUIPlugin.PLUGIN_ID);
if (fgStyleSheet != null) {
return;
}
fgStyleSheet = bundle.getEntry("/edoc.css"); //$NON-NLS-1$
if (fgStyleSheet != null) {
try {
fgStyleSheet = FileLocator.toFileURL(fgStyleSheet);
} catch (final Exception e) {
}
}
}
/**
* @param textViewer
* @param offset
* @param length
*/
private static String makeDebuggerVariableHover(
final ITextViewer textViewer, final int offset, final int length) {
final IAdaptable adaptable = DebugUITools.getDebugContext();
if (adaptable != null) {
final IStackFrame frame = (IStackFrame) adaptable
.getAdapter(IStackFrame.class);
try {
if (frame != null && frame.hasVariables()) {
String varName = "";
try {
varName = textViewer.getDocument().get(offset, length);
} catch (final BadLocationException e) {
}
if (varName.length() > 0) {
final String firstLetter = varName.substring(0, 1);
if (firstLetter.toUpperCase().equals(firstLetter)) {
final IVariable[] vars = frame.getVariables();
for (final IVariable variable : vars) {
if (variable.getName().equals(varName)) {
final String value = variable.getValue()
.getValueString();
return makeVariablePresentation(varName,
value);
}
}
}
}
}
} catch (final DebugException e) {
}
}
return "";
}
private static String makeVariablePresentation(final String varName,
final String value) {
return varName + " = " + value;
}
@Override
public IInformationControlCreator getInformationPresenterControlCreator() {
if (fPresenterControlCreator == null) {
fPresenterControlCreator = new PresenterControlCreator(fEditor);
}
return fPresenterControlCreator;
}
@Override
public IInformationControlCreator getHoverControlCreator() {
if (fHoverControlCreator == null) {
fHoverControlCreator = new HoverControlCreator(
getInformationPresenterControlCreator());
}
return fHoverControlCreator;
}
public static final class HoverControlCreator extends
AbstractReusableInformationControlCreator {
IInformationControlCreator fInformationPresenterControlCreator;
public HoverControlCreator(
final IInformationControlCreator informationPresenterControlCreator) {
fInformationPresenterControlCreator = informationPresenterControlCreator;
}
@Override
protected IInformationControl doCreateInformationControl(
final Shell parent) {
IInformationControl control;
if (BrowserInformationControl.isAvailable(parent)) {
control = new BrowserInformationControl(parent,
JFaceResources.DIALOG_FONT,
EditorsUI.getTooltipAffordanceString()) {
@Override
public IInformationControlCreator getInformationPresenterControlCreator() {
return fInformationPresenterControlCreator;
}
@Override
public void setSize(int width, int height) {
// default size is too small
final Point bounds = getSizeConstraints();
if (bounds != null) {
if (bounds.x != SWT.DEFAULT) {
width = Math.min(bounds.x, width * 2);
}
if (bounds.y != SWT.DEFAULT) {
height = Math.min(bounds.y, height * 2);
}
}
super.setSize(width, height);
}
};
} else {
control = new DefaultInformationControl(parent,
EditorsUI.getTooltipAffordanceString(),
new ErlInformationPresenter(true));
}
return control;
}
}
public static String getHoverTextForOffset(final int offset,
final ErlangEditor editor) {
final ErlTextHover h = new ErlTextHover(editor);
final ITextViewer tv = editor.getViewer();
final IRegion r = h.getHoverRegion(tv, offset);
if (r != null) {
return h.getHoverInfo(tv, r);
}
return null;
}
@Override
public String getHoverInfo(final ITextViewer textViewer,
final IRegion hoverRegion) {
final ErlBrowserInformationControlInput input = (ErlBrowserInformationControlInput) getHoverInfo2(
textViewer, hoverRegion);
return input == null ? "" : input.getHtml();
}
@Override
public Object getHoverInfo2(final ITextViewer textViewer,
final IRegion hoverRegion) {
return internalGetHoverInfo(fEditor, textViewer, hoverRegion);
}
private static ErlBrowserInformationControlInput internalGetHoverInfo(
final ErlangEditor editor, final ITextViewer textViewer,
final IRegion hoverRegion) {
if (editor == null) {
return null;
}
final IErlModule module = editor.getModule();
if (module == null) {
return null;
}
final StringBuffer result = new StringBuffer();
Object element = null;
// TODO our model is too coarse, here we need access to expressions
// try {
// element = module.getElementAt(hoverRegion.getOffset());
// } catch (Exception e) {
// }
final Collection<OtpErlangObject> fImports = ModelUtils
.getImportsAsList(module);
final int offset = hoverRegion.getOffset();
OtpErlangObject r1 = null;
final int length = hoverRegion.getLength();
final String debuggerVar = makeDebuggerVariableHover(textViewer,
offset, length);
if (debuggerVar.length() > 0) {
result.append(debuggerVar);
}
final String stateDir = ErlideUIPlugin.getDefault().getStateLocation()
.toString();
final IErlProject erlProject = module.getProject();
final IBackendManager backendManager = BackendCore.getBackendManager();
final IBackend ide = backendManager.getIdeBackend();
try {
final IProject project = erlProject == null ? null : erlProject
.getWorkspaceProject();
final IRpcCallSite b = erlProject == null ? ide : backendManager
.getBuildBackend(project);
final IErlModel model = ErlModelManager.getErlangModel();
final String externalModulesString = erlProject != null ? erlProject
.getExternalModulesString() : null;
r1 = ErlideDoc.getOtpDoc(ide, b, offset, stateDir,
module.getScannerName(), fImports, externalModulesString,
model.getPathVars());
// ErlLogger.debug("getHoverInfo getDocFromScan " + r1);
final OtpErlangTuple t = (OtpErlangTuple) r1;
if (Util.isOk(t)) {
final String docStr = Util.stringValue(t.elementAt(1));
final OpenResult or = new OpenResult(t.elementAt(2));
result.append(docStr);
element = or;
} else {
final OpenResult or = new OpenResult(t);
final Object found = OpenAction.findOpenResult(editor, module,
b, erlProject, or, offset);
if (found instanceof IErlFunction) {
final IErlFunction function = (IErlFunction) found;
String comment = function.getComment();
if (comment == null) {
return null;
}
comment = fixEncoding(comment);
result.append(comment);
} else if (found instanceof IErlPreprocessorDef) {
final IErlPreprocessorDef preprocessorDef = (IErlPreprocessorDef) found;
result.append(preprocessorDef.getExtra());
}
}
} catch (final Exception e) {
ErlLogger.warn(e);
}
if (result.length() > 0) {
HTMLPrinter.insertPageProlog(result, 0, fgStyleSheet);
HTMLPrinter.addPageEpilog(result);
}
// TODO set element
return new ErlBrowserInformationControlInput(null, element,
result.toString(), 20);
}
private static String fixEncoding(final String comment) {
try {
final byte[] bytes = comment.getBytes("ISO8859-1");
final ByteBuffer bb = ByteBuffer.wrap(bytes);
final CharBuffer cb = Charset.forName("UTF-8").newDecoder()
.decode(bb);
return cb.toString();
} catch (final Exception e) {
// it was Latin-1
}
return comment;
}
}