Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 121641
Collapse All | Expand All

(-)src/org/eclipse/cdt/debug/internal/ui/editors/DebugTextHover.java (+40 lines)
Lines 13-23 Link Here
13
import java.util.regex.Matcher;
13
import java.util.regex.Matcher;
14
import java.util.regex.Pattern;
14
import java.util.regex.Pattern;
15
15
16
import org.eclipse.cdt.core.CCorePlugin;
17
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
18
import org.eclipse.cdt.core.model.CoreModel;
19
import org.eclipse.cdt.core.model.ITranslationUnit;
16
import org.eclipse.cdt.debug.core.model.ICStackFrame;
20
import org.eclipse.cdt.debug.core.model.ICStackFrame;
21
import org.eclipse.cdt.debug.internal.core.model.CDebugTarget;
17
import org.eclipse.cdt.debug.internal.ui.CDebugUIUtils;
22
import org.eclipse.cdt.debug.internal.ui.CDebugUIUtils;
18
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
23
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
19
import org.eclipse.cdt.ui.text.c.hover.ICEditorTextHover;
24
import org.eclipse.cdt.ui.text.c.hover.ICEditorTextHover;
25
import org.eclipse.core.resources.IFile;
26
import org.eclipse.core.resources.IProject;
27
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.IAdaptable;
28
import org.eclipse.core.runtime.IAdaptable;
29
import org.eclipse.core.runtime.Path;
21
import org.eclipse.debug.core.DebugException;
30
import org.eclipse.debug.core.DebugException;
22
import org.eclipse.debug.ui.IDebugUIConstants;
31
import org.eclipse.debug.ui.IDebugUIConstants;
23
import org.eclipse.jface.text.BadLocationException;
32
import org.eclipse.jface.text.BadLocationException;
Lines 147-152 Link Here
147
				if (match_found) {
156
				if (match_found) {
148
					return null;
157
					return null;
149
				}
158
				}
159
				if (!isValidHoverSymbol(hoverRegion))
160
					return null;
150
				StringBuffer buffer = new StringBuffer();
161
				StringBuffer buffer = new StringBuffer();
151
				String result = evaluateExpression(frame, expression);
162
				String result = evaluateExpression(frame, expression);
152
				if (result == null)
163
				if (result == null)
Lines 165-170 Link Here
165
		}
176
		}
166
		return null;
177
		return null;
167
	}
178
	}
179
	
180
	/**
181
	 * Checks if the hovered symbol is valid for evaluation.
182
	 * 
183
	 */
184
	public boolean isValidHoverSymbol(IRegion hoverRegion){
185
		boolean isValid = false;
186
		int offset = hoverRegion.getOffset();
187
		int length = hoverRegion.getLength();
188
		IProject project = ((CDebugTarget)(getFrame().getDebugTarget())).getProject();
189
		IFile inputFile = project.getFile(new Path(getFrame().getFile().substring(getFrame().getFile().indexOf('/'))));
190
		DebugTextVisitor astVisitor = new DebugTextVisitor();		
191
		astVisitor.shouldVisitNames = true;
192
		astVisitor.shouldVisitExpressions = true;					
193
		
194
		if (CoreModel.isTranslationUnit(inputFile)) {
195
			try {
196
				ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(inputFile);
197
				IASTTranslationUnit ast = tu.getAST(null,ITranslationUnit.AST_SKIP_ALL_HEADERS);
198
				ast.accept(astVisitor);
199
			} catch (CoreException x) {
200
				CDebugUIPlugin.log(x);
201
			}
202
		}		
203
		if (astVisitor.containsExpression(offset, length)){
204
			isValid = true;
205
		}
206
		return isValid;				
207
	}	
168
208
169
	/*
209
	/*
170
	 * (non-Javadoc)
210
	 * (non-Javadoc)
(-)src/org/eclipse/cdt/debug/internal/ui/editors/DebugTextVisitor.java (+76 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 STMicroelectronics.
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
 * STMicroelectronics - Debug text hover enhancement
10
 *******************************************************************************/
11
12
package org.eclipse.cdt.debug.internal.ui.editors;
13
14
import java.util.ArrayList;
15
16
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
17
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
18
import org.eclipse.cdt.core.dom.ast.IASTExpression;
19
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
20
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
21
import org.eclipse.cdt.core.dom.ast.IASTName;
22
23
/**
24
 * Enhances text hovering for C/C++ debugger
25
 * 
26
 */
27
public class DebugTextVisitor extends ASTVisitor {
28
29
	private ArrayList expressionList = new ArrayList();
30
31
	class ExpressionDescription {
32
		int length;
33
		int offset;
34
35
		public ExpressionDescription(int offset, int length) {
36
			this.offset = offset;
37
			this.length = length;
38
		}
39
40
		public boolean equals(Object obj) {
41
			return (this.length == ((ExpressionDescription) obj).length && this.offset == ((ExpressionDescription) obj).offset) ? true
42
					: false;
43
		}
44
	}
45
46
	public int visit(IASTExpression expression) {
47
		IASTFileLocation fileLocation = expression.getFileLocation();
48
		if (fileLocation != null) {
49
			ExpressionDescription exprDesc = new ExpressionDescription(
50
					fileLocation.getNodeOffset(), fileLocation.getNodeLength());
51
			if (!(expression instanceof IASTLiteralExpression)
52
					&& !expressionList.contains(exprDesc)) {
53
				expressionList.add(exprDesc);
54
			}
55
		}
56
		return super.visit(expression);
57
58
	}
59
60
	public boolean containsExpression(int offset, int length) {
61
		return (expressionList.contains(new ExpressionDescription(offset,
62
				length)));
63
	}
64
65
	public int visit(IASTName name) {
66
		IASTFileLocation fileLocation = name.getFileLocation();
67
		if (fileLocation != null) {
68
			ExpressionDescription exprDesc = new ExpressionDescription(
69
					fileLocation.getNodeOffset(), fileLocation.getNodeLength());
70
			if ((name.getParent() instanceof IASTDeclarator)
71
					&& !expressionList.contains(exprDesc))
72
				expressionList.add(exprDesc);
73
		}
74
		return super.visit(name);
75
	}
76
}

Return to bug 121641