1717import java .util .List ;
1818import java .util .stream .Collectors ;
1919
20+ import org .eclipse .core .resources .IProject ;
21+ import org .eclipse .core .resources .ResourcesPlugin ;
2022import org .eclipse .debug .core .model .IStackFrame ;
2123import org .eclipse .debug .internal .ui .DebugUIPlugin ;
2224import org .eclipse .debug .ui .DebugUITools ;
23- import org .eclipse .jdt .core .IClassFile ;
2425import org .eclipse .jdt .core .ICompilationUnit ;
25- import org .eclipse .jdt .core .IJavaElement ;
26- import org .eclipse .jdt .core .WorkingCopyOwner ;
26+ import org .eclipse .jdt .core .IJavaProject ;
27+ import org .eclipse .jdt .core .IType ;
28+ import org .eclipse .jdt .core .JavaCore ;
2729import org .eclipse .jdt .core .dom .AST ;
2830import org .eclipse .jdt .core .dom .ASTParser ;
2931import org .eclipse .jdt .core .dom .ASTVisitor ;
3032import org .eclipse .jdt .core .dom .CompilationUnit ;
31- import org .eclipse .jdt .core .dom .LambdaExpression ;
3233import org .eclipse .jdt .core .dom .MethodDeclaration ;
3334import org .eclipse .jdt .core .dom .SingleVariableDeclaration ;
3435import org .eclipse .jdt .core .dom .VariableDeclarationFragment ;
3536import org .eclipse .jdt .debug .core .IJavaStackFrame ;
3637import org .eclipse .jdt .debug .core .IJavaVariable ;
38+ import org .eclipse .jdt .launching .IJavaLaunchConfigurationConstants ;
3739import org .eclipse .jdt .ui .JavaUI ;
3840import org .eclipse .jface .action .IAction ;
3941import org .eclipse .jface .text .IDocument ;
4042import org .eclipse .jface .text .IRegion ;
4143import org .eclipse .jface .viewers .IStructuredSelection ;
4244import org .eclipse .ui .IEditorPart ;
43- import org .eclipse .ui .IWorkbenchWindow ;
4445import org .eclipse .ui .texteditor .IDocumentProvider ;
4546import org .eclipse .ui .texteditor .ITextEditor ;
4647
@@ -57,7 +58,10 @@ public void run(IAction action) {
5758 Object frame = DebugUITools .getDebugContext ();
5859 if (frame instanceof IStackFrame jFrame ) {
5960 if (jFrame instanceof IJavaStackFrame javaStackFrame ) {
60- int currentLine = javaStackFrame .getLineNumber ();
61+ String type = javaStackFrame .getLaunch ().getLaunchConfiguration ().getAttribute (IJavaLaunchConfigurationConstants .ATTR_PROJECT_NAME , (String ) null );
62+ IProject project = ResourcesPlugin .getWorkspace ().getRoot ().getProject (type );
63+ IJavaProject iJavaProject = JavaCore .create (project );
64+ IType iType = iJavaProject .findType (javaStackFrame .getReceivingTypeName ());
6165 String currentMethod = javaStackFrame .getMethodName ();
6266 List <String > frameParams = javaStackFrame .getArgumentTypeNames ();
6367 List <String > ref = frameParams .stream ().map (e -> {
@@ -67,112 +71,49 @@ public void run(IAction action) {
6771 }
6872 return e ;
6973 }).collect (Collectors .toList ());
70- final ICompilationUnit [] cu = { null };
71- IWorkbenchWindow window = getWorkbenchWindow ();
72- IEditorPart editor = window .getActivePage ().getActiveEditor ();
73- IJavaElement element = JavaUI .getEditorInputJavaElement (editor .getEditorInput ());
74-
75- if (element instanceof ICompilationUnit icu ) {
76- cu [0 ] = icu ;
77- } else if (element instanceof IClassFile icf ) {
78- cu [0 ] = icf .getWorkingCopy (new WorkingCopyOwner () {
79- }, null );
80- } else {
81- cu [0 ] = null ;
82- }
83-
84- ASTParser parser = ASTParser .newParser (AST .getJLSLatest ());
85- parser .setSource (cu [0 ]);
86- parser .setKind (ASTParser .K_COMPILATION_UNIT );
87- parser .setResolveBindings (true );
88-
89- if (parser .createAST (null ) instanceof CompilationUnit ast ) {
90- ast .accept (new ASTVisitor () {
91- boolean meth = false ;
92- boolean found = false ;
93- boolean inTargetContext = false ;
94- @ Override
95- public boolean visit (MethodDeclaration node ) {
96- if (node .getName ().getIdentifier ().equals (currentMethod )) {
97- List <Object > parameters = node .parameters ();
98- List <String > methodParams = parameters .stream ().map (p -> ((SingleVariableDeclaration ) p ).getType ().toString ()).toList ();
99- int start = node .getStartPosition ();
100- int end = start + node .getLength ();
101- int startLine = ast .getLineNumber (start );
102- int endLine = ast .getLineNumber (end );
103- if (currentLine >= startLine && currentLine <= endLine ) {
104- inTargetContext = true ;
105- if (methodParams .equals (ref )) {
106- meth = true ;
107- for (Object op : node .parameters ()) {
108- if (op instanceof SingleVariableDeclaration param ) {
109- if (param .getName ().getIdentifier ().equals (name )) {
110- final ICompilationUnit finalCu = cu [0 ];
111- highlightLine (ast , finalCu , param .getStartPosition (), editor );
112- found = true ;
113- return false ;
114- }
115- }
116- }
117- return true ;
118- }
119- }
120- }
121- return true ;
122- }
123-
124- @ Override
125- public void endVisit (MethodDeclaration node ) {
126- inTargetContext = false ;
127-
128- }
129-
130- @ Override
131- public boolean visit (VariableDeclarationFragment node ) {
132- if (found ) {
133- return false ;
134- }
135- if ((meth || inTargetContext ) && node .getName ().getIdentifier ().equals (name )) {
136- found = true ;
137- final ICompilationUnit finalCu = cu [0 ];
138- highlightLine (ast , finalCu , node .getStartPosition (), editor );
139- return false ;
140- }
141- return true ;
142- }
143-
144- @ Override
145- public boolean visit (LambdaExpression node ) {
146- if (found ) {
147- return false ;
148- }
74+ ICompilationUnit cu = iType .getCompilationUnit ();
75+ ASTParser parse = ASTParser .newParser (AST .getJLSLatest ());
76+ parse .setSource (cu );
77+ parse .setKind (ASTParser .K_COMPILATION_UNIT );
78+ parse .setResolveBindings (true );
79+ CompilationUnit ast = (CompilationUnit ) parse .createAST (null );
80+ ast .accept (new ASTVisitor () {
81+ boolean meth = false ;
82+ boolean found = false ;
83+ @ Override
84+ public boolean visit (MethodDeclaration node ) {
85+ if (node .getName ().getIdentifier ().equals (currentMethod )) {
14986 List <Object > parameters = node .parameters ();
150- int start = node .getStartPosition ();
151- int end = start + node .getLength ();
152- int startLine = ast .getLineNumber (start );
153- int endLine = ast .getLineNumber (end );
154- if (currentLine >= startLine && currentLine <= endLine ) {
155- inTargetContext = true ;
156- for (Object param : parameters ) {
157- if (param instanceof SingleVariableDeclaration svd ) {
158- if (svd .getName ().getIdentifier ().equals (name )) {
159- highlightLine (ast , cu [0 ], svd .getStartPosition (), editor );
160- found = true ;
161- return false ;
162- }
87+ List <String > methodParams = parameters .stream ().map (p -> ((SingleVariableDeclaration ) p ).getType ().toString ()).toList ();
88+ if (methodParams .equals (ref )) {
89+ meth = true ;
90+ for (Object op : node .parameters ()) {
91+ SingleVariableDeclaration parm = (SingleVariableDeclaration ) op ;
92+ if (parm .getName ().getIdentifier ().equals (name )) {
93+ highlightLine (ast , cu , node .getStartPosition ());
94+ found = true ;
95+ return false ;
16396 }
16497 }
98+ return true ;
16599 }
166- return true ;
167100 }
101+ return true ;
102+ }
168103
169- @ Override
170- public void endVisit (LambdaExpression node ) {
171- inTargetContext = false ;
104+ @ Override
105+ public boolean visit (VariableDeclarationFragment node ) {
106+ if (found ) {
107+ return false ;
172108 }
173- });
174-
175- }
109+ if (meth && node .getName ().getIdentifier ().equals (name )) {
110+ found = true ;
111+ highlightLine (ast , cu , node .getStartPosition ());
112+ return false ;
113+ }
114+ return true ;
115+ }
116+ });
176117 }
177118 }
178119 }
@@ -190,21 +131,19 @@ public void endVisit(LambdaExpression node) {
190131 * compilation unit of the code
191132 * @param startPos
192133 * start position of the code want to highlight
193- * @param editor
194- * active editor info
195134 */
196- private void highlightLine (CompilationUnit ast , ICompilationUnit cu , int startPos , IEditorPart editor ) {
135+ private void highlightLine (CompilationUnit ast , ICompilationUnit cu , int startPos ) {
197136 int line = ast .getLineNumber (startPos );
198137 try {
138+ IEditorPart editor = JavaUI .openInEditor (cu );
199139 if (editor instanceof ITextEditor txtEd ) {
200140 IDocumentProvider prov = txtEd .getDocumentProvider ();
201141 IDocument doc = prov .getDocument (txtEd .getEditorInput ());
202- int adjustedLine = Math .max (0 , line - 1 );
203- IRegion lineReg = doc .getLineInformation (adjustedLine );
142+ IRegion lineReg = doc .getLineInformation (line - 1 );
204143 txtEd .selectAndReveal (lineReg .getOffset (), lineReg .getLength ());
205144 }
206145 } catch (Exception e ) {
207146 DebugUIPlugin .log (e );
208147 }
209148 }
210- }
149+ }
0 commit comments