Skip to content

Commit c2b44cd

Browse files
Re-factor the action class. incorporated code review comments.
1 parent d42f39c commit c2b44cd

4 files changed

Lines changed: 35 additions & 47 deletions

File tree

org.eclipse.jdt.debug.ui/plugin.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ openDeclVarType.tooltip=Open the Variable's Declared Type
121121
openDeclVarTypeHierarchy.label=Open Declared Type &Hierarchy
122122
openDeclVarTypeHierarchy.tooltip=Open the Variable's Declared Type Hierarchy
123123

124-
openDeclLocalVarNavigation.label=Open Local Variable Navigation
125-
openDeclLocalVarNavigation.tooltip=Open Local Variable Navigation
124+
openDeclLocalVarNavigation.label=Navigate to Declaration
125+
openDeclLocalVarNavigation.tooltip=Navigates to variable's declaration
126126

127127
openConcreteVarType.label=Open Act&ual Type
128128
openConcreteVarType.tooltip=Open the Variable's Actual Implementation Type

org.eclipse.jdt.debug.ui/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@
751751
label="%openDeclLocalVarNavigation.label"
752752
helpContextId="open_variable_declared_type_hierarchy_action_context"
753753
tooltip="%openDeclLocalVarNavigation.tooltip"
754-
class="org.eclipse.jdt.internal.debug.ui.actions.OpenDeclLocalVarNavigationAction"
754+
class="org.eclipse.jdt.internal.debug.ui.actions.NavigateToVarDeclAction"
755755
menubarPath="emptyNavigationGroup"
756756
enablesFor="1"
757757
id="org.eclipse.jdt.debug.ui.actions.OpenDeclLocalVarNavigation">

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JavaVarActionFilter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import org.eclipse.jdt.debug.core.IJavaPrimitiveValue;
2828
import org.eclipse.jdt.debug.core.IJavaType;
2929
import org.eclipse.jdt.debug.core.IJavaVariable;
30-
import org.eclipse.jdt.internal.debug.core.logicalstructures.JDIReturnValueVariable;
31-
import org.eclipse.jdt.internal.debug.core.model.JDIFieldVariable;
3230
import org.eclipse.jdt.internal.debug.core.model.JDILocalVariable;
3331
import org.eclipse.jdt.internal.debug.core.model.JDINullValue;
3432
import org.eclipse.jdt.internal.debug.core.model.JDIObjectValue;
@@ -211,7 +209,7 @@ else if (value.equals("isValuePrimitive")) { //$NON-NLS-1$
211209
return var instanceof IJavaFieldVariable;
212210
}
213211
if (value.equals("isLocalVariableValue")) { //$NON-NLS-1$
214-
return (!(var instanceof JDILocalVariable) && (var instanceof JDIReturnValueVariable) && !(var instanceof JDIFieldVariable));
212+
return !(var instanceof JDILocalVariable);
215213
}
216214
}
217215
else if (name.equals("ConcreteVariableActionFilter") && value.equals("isConcrete")) { //$NON-NLS-1$ //$NON-NLS-2$

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenDeclLocalVarNavigationAction.java renamed to org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/NavigateToVarDeclAction.java

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.core.resources.ResourcesPlugin;
2222
import org.eclipse.core.runtime.CoreException;
2323
import org.eclipse.debug.core.model.IStackFrame;
24+
import org.eclipse.debug.internal.ui.DebugUIPlugin;
2425
import org.eclipse.debug.ui.DebugUITools;
2526
import org.eclipse.jdt.core.ICompilationUnit;
2627
import org.eclipse.jdt.core.IJavaProject;
@@ -30,7 +31,6 @@
3031
import org.eclipse.jdt.core.dom.ASTParser;
3132
import org.eclipse.jdt.core.dom.ASTVisitor;
3233
import org.eclipse.jdt.core.dom.CompilationUnit;
33-
import org.eclipse.jdt.core.dom.FieldDeclaration;
3434
import org.eclipse.jdt.core.dom.MethodDeclaration;
3535
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
3636
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
@@ -47,7 +47,7 @@
4747
import org.eclipse.ui.texteditor.IDocumentProvider;
4848
import org.eclipse.ui.texteditor.ITextEditor;
4949

50-
public class OpenDeclLocalVarNavigationAction extends ObjectActionDelegate {
50+
public class NavigateToVarDeclAction extends ObjectActionDelegate {
5151
@Override
5252
public void run(IAction action) {
5353
IStructuredSelection selection = getCurrentSelection();
@@ -61,14 +61,13 @@ public void run(IAction action) {
6161
String name = varE.getName();
6262
Object frame = DebugUITools.getDebugContext();
6363
if (frame instanceof IStackFrame jFrame) {
64-
if (jFrame instanceof IJavaStackFrame jf) {
65-
jf.getReferenceType().getAllFieldNames();
66-
String type = jf.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
64+
if (jFrame instanceof IJavaStackFrame javaStackFrame) {
65+
String type = javaStackFrame.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
6766
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type);
6867
IJavaProject iJavaProject = JavaCore.create(project);
69-
IType iType = iJavaProject.findType(jf.getReceivingTypeName());
70-
String nameMethod = jf.getMethodName();
71-
List<String> frameParams = jf.getArgumentTypeNames();
68+
IType iType = iJavaProject.findType(javaStackFrame.getReceivingTypeName());
69+
String currentMethod = javaStackFrame.getMethodName();
70+
List<String> frameParams = javaStackFrame.getArgumentTypeNames();
7271
List<String> ref = frameParams.stream().map(e -> {
7372
int dot = e.lastIndexOf('.');
7473
if (dot >= 0) {
@@ -87,7 +86,7 @@ public void run(IAction action) {
8786
boolean found = false;
8887
@Override
8988
public boolean visit(MethodDeclaration node) {
90-
if (node.getName().getIdentifier().equals(nameMethod)) {
89+
if (node.getName().getIdentifier().equals(currentMethod)) {
9190
List<Object> parameters = node.parameters();
9291
List<String> methodParams = parameters.stream().map(p -> ((SingleVariableDeclaration) p).getType().toString()).toList();
9392
if (methodParams.equals(ref)) {
@@ -113,48 +112,39 @@ public boolean visit(VariableDeclarationFragment node) {
113112
if (meth && node.getName().getIdentifier().equals(name)) {
114113
found = true;
115114
highlightLine(ast, cu, node.getStartPosition());
116-
return true;
117-
}
118-
return true;
119-
}
120-
@Override
121-
public boolean visit(FieldDeclaration node) {
122-
if (found) {
123115
return false;
124116
}
125-
for (Object op : node.fragments()) {
126-
VariableDeclarationFragment vdf = (VariableDeclarationFragment) op;
127-
128-
if (vdf.getName().getIdentifier().equals(name)) {
129-
found = true;
130-
highlightLine(ast, cu, node.getStartPosition());
131-
return true;
132-
}
133-
}
134117
return true;
135118
}
136-
private void highlightLine(CompilationUnit ast, ICompilationUnit cu, int startPos) {
137-
int line = ast.getLineNumber(startPos);
138-
try {
139-
IEditorPart editor = JavaUI.openInEditor(cu);
140-
141-
if (editor instanceof ITextEditor txtEd) {
142-
IDocumentProvider prov = txtEd.getDocumentProvider();
143-
IDocument doc = prov.getDocument(txtEd.getEditorInput());
144-
IRegion lineReg = doc.getLineInformation(line - 1);
145-
txtEd.selectAndReveal(lineReg.getOffset(), lineReg.getLength());
146-
}
147-
} catch (Exception e) {
148-
JDIDebugUIPlugin.log(e);
149-
}
150-
}
151119
});
152120
}
153121
}
154122
}
155123
}
156124
} catch (CoreException e) {
157-
JDIDebugUIPlugin.statusDialog(e.getStatus());
125+
DebugUIPlugin.log(e);
126+
}
127+
}
128+
129+
/**
130+
* This method is responsible for highlighting a line.
131+
*
132+
* @param ast
133+
* @param ICompilationUnit
134+
* @param startPos
135+
*/
136+
private void highlightLine(CompilationUnit ast, ICompilationUnit cu, int startPos) {
137+
int line = ast.getLineNumber(startPos);
138+
try {
139+
IEditorPart editor = JavaUI.openInEditor(cu);
140+
if (editor instanceof ITextEditor txtEd) {
141+
IDocumentProvider prov = txtEd.getDocumentProvider();
142+
IDocument doc = prov.getDocument(txtEd.getEditorInput());
143+
IRegion lineReg = doc.getLineInformation(line - 1);
144+
txtEd.selectAndReveal(lineReg.getOffset(), lineReg.getLength());
145+
}
146+
} catch (Exception e) {
147+
JDIDebugUIPlugin.log(e);
158148
}
159149
}
160150
}

0 commit comments

Comments
 (0)