2121import org .eclipse .core .resources .ResourcesPlugin ;
2222import org .eclipse .core .runtime .CoreException ;
2323import org .eclipse .debug .core .model .IStackFrame ;
24+ import org .eclipse .debug .internal .ui .DebugUIPlugin ;
2425import org .eclipse .debug .ui .DebugUITools ;
2526import org .eclipse .jdt .core .ICompilationUnit ;
2627import org .eclipse .jdt .core .IJavaProject ;
3031import org .eclipse .jdt .core .dom .ASTParser ;
3132import org .eclipse .jdt .core .dom .ASTVisitor ;
3233import org .eclipse .jdt .core .dom .CompilationUnit ;
33- import org .eclipse .jdt .core .dom .FieldDeclaration ;
3434import org .eclipse .jdt .core .dom .MethodDeclaration ;
3535import org .eclipse .jdt .core .dom .SingleVariableDeclaration ;
3636import org .eclipse .jdt .core .dom .VariableDeclarationFragment ;
4747import org .eclipse .ui .texteditor .IDocumentProvider ;
4848import 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