|
20 | 20 | import org.eclipse.core.resources.IProject; |
21 | 21 | import org.eclipse.core.resources.ResourcesPlugin; |
22 | 22 | import org.eclipse.core.runtime.CoreException; |
23 | | -import org.eclipse.debug.core.model.IDebugElement; |
24 | 23 | import org.eclipse.debug.core.model.IStackFrame; |
| 24 | +import org.eclipse.debug.ui.DebugUITools; |
25 | 25 | import org.eclipse.jdt.core.ICompilationUnit; |
26 | 26 | import org.eclipse.jdt.core.IJavaProject; |
27 | 27 | import org.eclipse.jdt.core.IType; |
|
30 | 30 | import org.eclipse.jdt.core.dom.ASTParser; |
31 | 31 | import org.eclipse.jdt.core.dom.ASTVisitor; |
32 | 32 | import org.eclipse.jdt.core.dom.CompilationUnit; |
| 33 | +import org.eclipse.jdt.core.dom.FieldDeclaration; |
33 | 34 | import org.eclipse.jdt.core.dom.MethodDeclaration; |
34 | 35 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
35 | 36 | import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
36 | 37 | import org.eclipse.jdt.debug.core.IJavaStackFrame; |
37 | | -import org.eclipse.jdt.debug.core.IJavaType; |
38 | 38 | import org.eclipse.jdt.debug.core.IJavaVariable; |
39 | 39 | import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; |
40 | 40 | import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; |
41 | 41 | import org.eclipse.jdt.ui.JavaUI; |
| 42 | +import org.eclipse.jface.action.IAction; |
42 | 43 | import org.eclipse.jface.text.IDocument; |
43 | 44 | import org.eclipse.jface.text.IRegion; |
| 45 | +import org.eclipse.jface.viewers.IStructuredSelection; |
44 | 46 | import org.eclipse.ui.IEditorPart; |
45 | 47 | import org.eclipse.ui.texteditor.IDocumentProvider; |
46 | 48 | import org.eclipse.ui.texteditor.ITextEditor; |
47 | 49 |
|
48 | | -public class OpenDeclLocalVarNavigationAction extends OpenVariableTypeAction { |
| 50 | +public class OpenDeclLocalVarNavigationAction extends ObjectActionDelegate { |
49 | 51 | @Override |
50 | | - protected IJavaType getTypeToOpen(IDebugElement element) throws CoreException { |
| 52 | + public void run(IAction action) { |
| 53 | + IStructuredSelection selection = getCurrentSelection(); |
| 54 | + if (selection == null) { |
| 55 | + return; |
| 56 | + } |
51 | 57 | try { |
52 | | - if (element instanceof IJavaVariable varE) { |
53 | | - IJavaType javaType = varE.getJavaType(); |
54 | | - String name = varE.getName(); |
55 | | - IStackFrame jFrame = javaType.getDebugTarget().getThreads()[0].getTopStackFrame(); |
56 | | - if (jFrame instanceof IJavaStackFrame jf) { |
57 | | - jf.getReferenceType().getAllFieldNames(); |
58 | | - String type = jf.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null); |
59 | | - IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type); |
60 | | - IJavaProject iJavaProject = JavaCore.create(project); |
61 | | - IType iType = iJavaProject.findType(jf.getReceivingTypeName()); |
62 | | - String nameMethod = jf.getMethodName(); |
63 | | - List<String> frameParams = jf.getArgumentTypeNames(); |
64 | | - List<String> ref = frameParams.stream().map(e -> { |
65 | | - int dot = e.lastIndexOf('.'); |
66 | | - if(dot>=0) { |
67 | | - return e.substring(dot+1); |
68 | | - } |
69 | | - return e; |
70 | | - }).collect(Collectors.toList()); |
71 | | - ICompilationUnit cu = iType.getCompilationUnit(); |
72 | | - ASTParser parse = ASTParser.newParser(AST.getJLSLatest()); |
73 | | - parse.setSource(cu); |
74 | | - parse.setKind(ASTParser.K_COMPILATION_UNIT); |
| 58 | + if (selection.getFirstElement() != null) { |
| 59 | + Object element = selection.getFirstElement(); |
| 60 | + if (element instanceof IJavaVariable varE) { |
| 61 | + String name = varE.getName(); |
| 62 | + Object frame = DebugUITools.getDebugContext(); |
| 63 | + 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); |
| 67 | + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type); |
| 68 | + IJavaProject iJavaProject = JavaCore.create(project); |
| 69 | + IType iType = iJavaProject.findType(jf.getReceivingTypeName()); |
| 70 | + String nameMethod = jf.getMethodName(); |
| 71 | + List<String> frameParams = jf.getArgumentTypeNames(); |
| 72 | + List<String> ref = frameParams.stream().map(e -> { |
| 73 | + int dot = e.lastIndexOf('.'); |
| 74 | + if (dot >= 0) { |
| 75 | + return e.substring(dot + 1); |
| 76 | + } |
| 77 | + return e; |
| 78 | + }).collect(Collectors.toList()); |
| 79 | + ICompilationUnit cu = iType.getCompilationUnit(); |
| 80 | + ASTParser parse = ASTParser.newParser(AST.getJLSLatest()); |
| 81 | + parse.setSource(cu); |
| 82 | + parse.setKind(ASTParser.K_COMPILATION_UNIT); |
| 83 | + parse.setResolveBindings(true); |
| 84 | + CompilationUnit ast = (CompilationUnit) parse.createAST(null); |
| 85 | + ast.accept(new ASTVisitor() { |
| 86 | + boolean meth = false; |
| 87 | + boolean found = false; |
| 88 | + @Override |
| 89 | + public boolean visit(MethodDeclaration node) { |
| 90 | + if (node.getName().getIdentifier().equals(nameMethod)) { |
| 91 | + List<Object> parameters = node.parameters(); |
| 92 | + List<String> methodParams = parameters.stream().map(p -> ((SingleVariableDeclaration) p).getType().toString()).toList(); |
| 93 | + if (methodParams.equals(ref)) { |
| 94 | + meth = true; |
| 95 | + for (Object op : node.parameters()) { |
| 96 | + SingleVariableDeclaration parm = (SingleVariableDeclaration) op; |
| 97 | + if (parm.getName().getIdentifier().equals(name)) { |
| 98 | + highlightLine(ast, cu, node.getStartPosition()); |
| 99 | + found = true; |
| 100 | + return true; |
| 101 | + } |
| 102 | + } |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + return true; |
| 107 | + } |
| 108 | + @Override |
| 109 | + public boolean visit(VariableDeclarationFragment node) { |
| 110 | + if (found) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + if (meth && node.getName().getIdentifier().equals(name)) { |
| 114 | + found = true; |
| 115 | + highlightLine(ast, cu, node.getStartPosition()); |
| 116 | + return true; |
| 117 | + } |
| 118 | + return true; |
| 119 | + } |
| 120 | + @Override |
| 121 | + public boolean visit(FieldDeclaration node) { |
| 122 | + if (found) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + for (Object op : node.fragments()) { |
| 126 | + VariableDeclarationFragment vdf = (VariableDeclarationFragment) op; |
75 | 127 |
|
76 | | - parse.setResolveBindings(true); |
77 | | - CompilationUnit ast = (CompilationUnit) parse.createAST(null); |
78 | | - ast.accept(new ASTVisitor() { |
79 | | - boolean meth = false; |
80 | | - boolean found = false; |
81 | | - @Override |
82 | | - public boolean visit(MethodDeclaration node) { |
83 | | - if (node.getName().getIdentifier().equals(nameMethod)) { |
84 | | - List<Object> parameters = node.parameters(); |
85 | | - List<String> methodParams = parameters.stream().map(p -> ((SingleVariableDeclaration) p).getType().toString()).toList(); |
86 | | - if (methodParams.equals(ref)) { |
87 | | - meth = true; |
88 | | - for (Object op : node.parameters()) { |
89 | | - SingleVariableDeclaration parm = (SingleVariableDeclaration) op; |
90 | | - if (parm.getName().getIdentifier().equals(name)) { |
91 | | - highlightLine(ast, cu, node.getStartPosition()); |
| 128 | + if (vdf.getName().getIdentifier().equals(name)) { |
92 | 129 | found = true; |
| 130 | + highlightLine(ast, cu, node.getStartPosition()); |
93 | 131 | return true; |
94 | 132 | } |
95 | 133 | } |
96 | 134 | return true; |
97 | 135 | } |
98 | | - } |
99 | | - return true; |
100 | | - } |
101 | | - @Override |
102 | | - public boolean visit(VariableDeclarationFragment node) { |
103 | | - if (found) { |
104 | | - return false; |
105 | | - } |
106 | | - if (meth && node.getName().getIdentifier().equals(name)) { |
107 | | - found = true; |
108 | | - highlightLine(ast, cu, node.getStartPosition()); |
109 | | - return true; |
110 | | - } |
111 | | - return true; |
112 | | - } |
113 | | - private void highlightLine(CompilationUnit ast, ICompilationUnit cu, int startPos) { |
114 | | - int line = ast.getLineNumber(startPos); |
115 | | - try { |
116 | | - IEditorPart editor = JavaUI.openInEditor(cu); |
117 | | - if (editor instanceof ITextEditor txtEd) { |
118 | | - IDocumentProvider prov = txtEd.getDocumentProvider(); |
119 | | - IDocument doc = prov.getDocument(txtEd.getEditorInput()); |
120 | | - IRegion lineReg = doc.getLineInformation(line - 1); |
121 | | - txtEd.selectAndReveal(lineReg.getOffset(), lineReg.getLength()); |
| 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 | + } |
122 | 150 | } |
123 | | - } catch (Exception e) { |
124 | | - System.out.println("Exception: " + e.getMessage()); //$NON-NLS-1$ |
125 | | - } |
| 151 | + }); |
126 | 152 | } |
127 | | - }); |
128 | | - ResourcesPlugin.getWorkspace(); |
| 153 | + } |
129 | 154 | } |
130 | 155 | } |
131 | 156 | } catch (CoreException e) { |
132 | 157 | JDIDebugUIPlugin.statusDialog(e.getStatus()); |
133 | 158 | } |
134 | | - |
135 | | - return null; |
136 | 159 | } |
137 | 160 | } |
0 commit comments