37
|
1 |
/*******************************************************************************
|
|
2 |
* Copyright (c) 2000, 2007 QNX Software Systems and others.
|
|
3 |
* All rights reserved. This program and the accompanying materials
|
|
4 |
* are made available under the terms of the Eclipse Public License v1.0
|
|
5 |
* which accompanies this distribution, and is available at
|
|
6 |
* http://www.eclipse.org/legal/epl-v10.html
|
|
7 |
*
|
|
8 |
* Contributors:
|
|
9 |
* QNX Software Systems - Initial API and implementation
|
110
|
10 |
* Nokia - Refactored from DebugTextHover to remove CDI dependency
|
|
11 |
*
|
37
|
12 |
*******************************************************************************/
|
110
|
13 |
package org.eclipse.cdt.debug.ui.editors;
|
37
|
14 |
|
|
15 |
import java.util.regex.Matcher;
|
|
16 |
import java.util.regex.Pattern;
|
|
17 |
|
|
18 |
import org.eclipse.cdt.debug.internal.ui.CDebugUIUtils;
|
|
19 |
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
|
|
20 |
import org.eclipse.cdt.ui.text.c.hover.ICEditorTextHover;
|
111
|
21 |
import org.eclipse.core.runtime.IAdaptable;
|
37
|
22 |
import org.eclipse.debug.core.DebugException;
|
|
23 |
import org.eclipse.debug.ui.IDebugUIConstants;
|
|
24 |
import org.eclipse.jface.text.BadLocationException;
|
|
25 |
import org.eclipse.jface.text.IDocument;
|
|
26 |
import org.eclipse.jface.text.IInformationControlCreator;
|
|
27 |
import org.eclipse.jface.text.IRegion;
|
|
28 |
import org.eclipse.jface.text.ITextHoverExtension;
|
|
29 |
import org.eclipse.jface.text.ITextViewer;
|
|
30 |
import org.eclipse.jface.viewers.ISelection;
|
111
|
31 |
import org.eclipse.jface.viewers.IStructuredSelection;
|
37
|
32 |
import org.eclipse.ui.IEditorPart;
|
|
33 |
import org.eclipse.ui.IPartListener;
|
|
34 |
import org.eclipse.ui.ISelectionListener;
|
|
35 |
import org.eclipse.ui.IWorkbenchPage;
|
|
36 |
import org.eclipse.ui.IWorkbenchPart;
|
|
37 |
|
|
38 |
/* The class LanguageOperators protects some language specific
|
|
39 |
* operator information used by the DebugTextHover class.
|
|
40 |
*/
|
|
41 |
|
|
42 |
class LanguageOperators {
|
|
43 |
public String getAssignmentOperator() {
|
|
44 |
return "="; //$NON-NLS-1$
|
|
45 |
}
|
|
46 |
|
|
47 |
public String getGreaterThanEqualToOperator() {
|
|
48 |
return ">="; //$NON-NLS-1$
|
|
49 |
}
|
|
50 |
|
|
51 |
public String getEqualToOperator() {
|
|
52 |
return "=="; //$NON-NLS-1$
|
|
53 |
}
|
|
54 |
|
|
55 |
public String getNotEqualToOperator() {
|
|
56 |
return "!="; //$NON-NLS-1$
|
|
57 |
}
|
|
58 |
|
|
59 |
public String getLessThenEqualToOperator() {
|
|
60 |
return "<="; //$NON-NLS-1$
|
|
61 |
}
|
|
62 |
|
|
63 |
public String getValueChangeOperatorsRegex() {
|
|
64 |
return "(\\+\\+)|(\\-\\-)|(\\+\\=)|" //$NON-NLS-1$
|
|
65 |
+ "(\\-\\=)|(\\*\\=)|(/\\=)|(\\&\\=)" //$NON-NLS-1$
|
|
66 |
+ "(\\%\\=)|(\\^\\=)|(\\|\\=)|(\\<\\<\\=)|(\\>\\>\\=)"; //$NON-NLS-1$
|
|
67 |
}
|
|
68 |
|
|
69 |
public String getEqualToOperatorsRegex() {
|
|
70 |
return "\\=\\=|\\<\\=|\\>\\=|!\\="; //$NON-NLS-1$
|
|
71 |
}
|
|
72 |
|
|
73 |
public String getIdentifierRegex() {
|
|
74 |
return "[_A-Za-z][_A-Za-z0-9]*"; //$NON-NLS-1$
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* The text hovering support for C/C++ debugger.
|
111
|
80 |
*
|
|
81 |
* @since 6.1
|
37
|
82 |
*/
|
|
83 |
|
110
|
84 |
public abstract class AbstractDebugTextHover implements ICEditorTextHover, ITextHoverExtension,
|
37
|
85 |
ISelectionListener, IPartListener {
|
|
86 |
|
|
87 |
static final private int MAX_HOVER_INFO_SIZE = 100;
|
|
88 |
|
111
|
89 |
private ISelection fSelection = null;
|
37
|
90 |
|
111
|
91 |
private IEditorPart fEditor;
|
37
|
92 |
|
110
|
93 |
protected abstract boolean canEvaluate();
|
|
94 |
|
|
95 |
protected abstract String evaluateExpression(String expression);
|
|
96 |
|
37
|
97 |
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
|
110
|
98 |
if (canEvaluate()) {
|
37
|
99 |
try {
|
|
100 |
IDocument document = textViewer.getDocument();
|
|
101 |
if (document == null)
|
|
102 |
return null;
|
|
103 |
String expression = document.get(hoverRegion.getOffset(),
|
|
104 |
hoverRegion.getLength());
|
|
105 |
if (expression == null)
|
|
106 |
return null;
|
|
107 |
expression = expression.trim();
|
|
108 |
if (expression.length() == 0)
|
|
109 |
return null;
|
|
110 |
LanguageOperators operatorsObj = new LanguageOperators();
|
|
111 |
|
|
112 |
Pattern pattern = Pattern.compile(operatorsObj
|
|
113 |
.getValueChangeOperatorsRegex());
|
|
114 |
Matcher matcher = pattern.matcher(expression);
|
|
115 |
|
|
116 |
boolean match_found = matcher.find();
|
|
117 |
// Get matching string
|
|
118 |
// If the expression has some operators which can change the
|
|
119 |
// value of a variable, that expresssion should not be
|
|
120 |
// evaluated.
|
|
121 |
if (match_found) {
|
|
122 |
return null;
|
|
123 |
}
|
|
124 |
pattern = Pattern.compile(operatorsObj
|
|
125 |
.getEqualToOperatorsRegex());
|
|
126 |
String[] tokens = pattern.split(expression);
|
|
127 |
for (int i = 0; i < tokens.length; i++) {
|
|
128 |
//If the expression contains assignment operator that
|
|
129 |
// can change the value of a variable, the expression
|
|
130 |
// should not be evaluated.
|
|
131 |
if (tokens[i].indexOf(operatorsObj
|
|
132 |
.getAssignmentOperator()) != -1)
|
|
133 |
return null;
|
|
134 |
}
|
|
135 |
//Supressing function calls from evaluation.
|
|
136 |
String functionCallRegex = operatorsObj.getIdentifierRegex() + "\\s*\\("; //$NON-NLS-1$
|
|
137 |
pattern = Pattern.compile(functionCallRegex);
|
|
138 |
matcher = pattern.matcher(expression);
|
|
139 |
match_found = matcher.find();
|
|
140 |
if (match_found) {
|
|
141 |
return null;
|
|
142 |
}
|
|
143 |
StringBuffer buffer = new StringBuffer();
|
110
|
144 |
String result = evaluateExpression(expression);
|
37
|
145 |
if (result == null)
|
|
146 |
return null;
|
|
147 |
try {
|
|
148 |
appendVariable(buffer, makeHTMLSafe(expression), makeHTMLSafe(result.trim()));
|
|
149 |
} catch (DebugException x) {
|
|
150 |
CDebugUIPlugin.log(x);
|
|
151 |
}
|
|
152 |
if (buffer.length() > 0) {
|
|
153 |
return buffer.toString();
|
|
154 |
}
|
|
155 |
} catch (BadLocationException x) {
|
|
156 |
CDebugUIPlugin.log(x);
|
|
157 |
}
|
|
158 |
}
|
|
159 |
return null;
|
|
160 |
}
|
|
161 |
|
|
162 |
public IRegion getHoverRegion(ITextViewer viewer, int offset) {
|
|
163 |
/*
|
|
164 |
* Point selectedRange = viewer.getSelectedRange(); if ( selectedRange.x >= 0 && selectedRange.y > 0 && offset >= selectedRange.x && offset <=
|
|
165 |
* selectedRange.x + selectedRange.y ) return new Region( selectedRange.x, selectedRange.y );
|
|
166 |
*/
|
|
167 |
if (viewer != null)
|
|
168 |
return CDebugUIUtils.findWord(viewer.getDocument(), offset);
|
|
169 |
return null;
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Append HTML for the given variable to the given buffer
|
|
174 |
*/
|
|
175 |
private static void appendVariable(StringBuffer buffer, String expression, String value) throws DebugException {
|
|
176 |
if (value.length() > MAX_HOVER_INFO_SIZE)
|
|
177 |
value = value.substring(0, MAX_HOVER_INFO_SIZE) + " ..."; //$NON-NLS-1$
|
|
178 |
buffer.append("<p>"); //$NON-NLS-1$
|
|
179 |
buffer.append("<pre>").append(expression).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
180 |
buffer.append(" = "); //$NON-NLS-1$
|
|
181 |
buffer.append("<b><pre>").append(value).append("</pre></b>"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
182 |
buffer.append("</p>"); //$NON-NLS-1$
|
|
183 |
}
|
|
184 |
|
|
185 |
public void setEditor(IEditorPart editor) {
|
|
186 |
if (editor != null) {
|
|
187 |
fEditor = editor;
|
|
188 |
final IWorkbenchPage page = editor.getSite().getPage();
|
|
189 |
page.addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
|
|
190 |
page.addPartListener(this);
|
|
191 |
// initialize selection
|
|
192 |
Runnable r = new Runnable() {
|
|
193 |
|
|
194 |
public void run() {
|
|
195 |
fSelection = page
|
|
196 |
.getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
|
|
197 |
}
|
|
198 |
};
|
|
199 |
CDebugUIPlugin.getStandardDisplay().asyncExec(r);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
|
|
204 |
fSelection = selection;
|
|
205 |
}
|
|
206 |
|
|
207 |
public void partActivated(IWorkbenchPart part) {
|
|
208 |
}
|
|
209 |
|
|
210 |
public void partBroughtToTop(IWorkbenchPart part) {
|
|
211 |
}
|
|
212 |
|
|
213 |
public void partClosed(IWorkbenchPart part) {
|
|
214 |
if (part.equals(fEditor)) {
|
|
215 |
IWorkbenchPage page = fEditor.getSite().getPage();
|
|
216 |
page.removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
|
|
217 |
page.removePartListener(this);
|
|
218 |
fSelection = null;
|
|
219 |
fEditor = null;
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
public void partDeactivated(IWorkbenchPart part) {
|
|
224 |
}
|
|
225 |
|
|
226 |
public void partOpened(IWorkbenchPart part) {
|
|
227 |
}
|
|
228 |
|
|
229 |
public IInformationControlCreator getHoverControlCreator() {
|
|
230 |
return null;
|
|
231 |
}
|
|
232 |
|
111
|
233 |
protected ISelection getSelection() {
|
|
234 |
return fSelection;
|
|
235 |
}
|
|
236 |
|
|
237 |
protected IAdaptable getSelectionAdaptable() {
|
|
238 |
if (fSelection instanceof IStructuredSelection) {
|
|
239 |
IStructuredSelection selection = (IStructuredSelection) fSelection;
|
|
240 |
if (selection.size() == 1) {
|
|
241 |
Object element = selection.getFirstElement();
|
|
242 |
if (element instanceof IAdaptable) {
|
|
243 |
return (IAdaptable) element;
|
|
244 |
}
|
|
245 |
}
|
|
246 |
}
|
|
247 |
return null;
|
|
248 |
}
|
|
249 |
|
|
250 |
|
|
251 |
protected IEditorPart getEditor() {
|
|
252 |
return fEditor;
|
|
253 |
}
|
|
254 |
|
37
|
255 |
/**
|
|
256 |
* Replace any characters in the given String that would confuse an HTML parser with their escape sequences.
|
|
257 |
*/
|
|
258 |
private static String makeHTMLSafe(String string) {
|
|
259 |
StringBuffer buffer = new StringBuffer(string.length());
|
|
260 |
for (int i = 0; i != string.length(); i++) {
|
|
261 |
char ch = string.charAt(i);
|
|
262 |
switch (ch) {
|
|
263 |
case '&':
|
|
264 |
buffer.append("&"); //$NON-NLS-1$
|
|
265 |
break;
|
|
266 |
case '<':
|
|
267 |
buffer.append("<"); //$NON-NLS-1$
|
|
268 |
break;
|
|
269 |
case '>':
|
|
270 |
buffer.append(">"); //$NON-NLS-1$
|
|
271 |
break;
|
|
272 |
default:
|
|
273 |
buffer.append(ch);
|
|
274 |
break;
|
|
275 |
}
|
|
276 |
}
|
|
277 |
return buffer.toString();
|
|
278 |
}
|
|
279 |
}
|