|
1 | 1 | package club.bytecode.the.jda.gui.fileviewer;
|
2 | 2 |
|
| 3 | +import club.bytecode.the.jda.FileContainer; |
3 | 4 | import club.bytecode.the.jda.JDA;
|
4 | 5 | import club.bytecode.the.jda.gui.search.SearchDialog;
|
5 | 6 | import club.bytecode.the.jda.settings.Settings;
|
| 7 | +import com.github.javaparser.*; |
| 8 | +import com.github.javaparser.ast.CompilationUnit; |
| 9 | +import com.github.javaparser.ast.ImportDeclaration; |
| 10 | +import com.github.javaparser.ast.Node; |
| 11 | +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; |
| 12 | +import com.github.javaparser.ast.body.FieldDeclaration; |
| 13 | +import com.github.javaparser.ast.body.MethodDeclaration; |
| 14 | +import com.github.javaparser.ast.expr.*; |
| 15 | +import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt; |
| 16 | +import com.github.javaparser.ast.type.ClassOrInterfaceType; |
| 17 | +import com.github.javaparser.ast.visitor.VoidVisitor; |
| 18 | +import com.github.javaparser.symbolsolver.JavaSymbolSolver; |
| 19 | +import com.github.javaparser.symbolsolver.javaparser.Navigator; |
| 20 | +import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; |
| 21 | +import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory; |
| 22 | +import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; |
| 23 | +import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver; |
| 24 | +import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; |
| 25 | +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; |
6 | 26 | import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
|
7 | 27 | import org.fife.ui.rsyntaxtextarea.Token;
|
8 | 28 | import org.fife.ui.rsyntaxtextarea.TokenTypes;
|
|
13 | 33 | import javax.swing.text.BadLocationException;
|
14 | 34 | import java.awt.*;
|
15 | 35 | import java.awt.event.*;
|
| 36 | +import java.io.*; |
16 | 37 | import java.util.*;
|
17 | 38 | import java.util.List;
|
18 | 39 |
|
19 | 40 | public class JDATextArea extends RSyntaxTextArea {
|
20 | 41 | private List<String> lines;
|
21 | 42 | private Map<Integer, String> comments;
|
| 43 | + private CompilationUnit ast = null; |
22 | 44 |
|
23 | 45 | private TokenWrapper currentlySelectedToken;
|
24 | 46 |
|
25 | 47 | public JDATextArea(String text) {
|
| 48 | + this(text, JDAJavaTokenizer.SYNTAX_STYLE_JDA_JAVA); |
| 49 | + ast = JavaParser.parse(text); |
| 50 | + } |
| 51 | + |
| 52 | + public JDATextArea(String text, String language) { |
26 | 53 | comments = new HashMap<>();
|
27 | 54 |
|
28 |
| - setSyntaxEditingStyle(JDAJavaTokenizer.SYNTAX_STYLE_JDA_JAVA); |
| 55 | + setSyntaxEditingStyle(language); |
29 | 56 | setCodeFoldingEnabled(true);
|
30 | 57 | setAntiAliasingEnabled(true);
|
31 | 58 |
|
@@ -147,19 +174,104 @@ private boolean isStringSelected() {
|
147 | 174 | return currentlySelectedToken != null && currentlySelectedToken.getType() == TokenTypes.LITERAL_STRING_DOUBLE_QUOTE;
|
148 | 175 | }
|
149 | 176 |
|
| 177 | + interface NodeHandler { |
| 178 | + boolean handle(Node node); |
| 179 | + } |
150 | 180 | private void doXrefDialog() {
|
| 181 | + class NodeIterator { |
| 182 | + |
| 183 | + |
| 184 | + private NodeHandler nodeHandler; |
| 185 | + |
| 186 | + public NodeIterator(NodeHandler nodeHandler) { |
| 187 | + this.nodeHandler = nodeHandler; |
| 188 | + } |
| 189 | + |
| 190 | + public void explore(Node node) { |
| 191 | + if( nodeHandler.handle(node)){ |
| 192 | + for (Node child : node.getChildNodes()) { |
| 193 | + explore(child); |
| 194 | + }} |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (ast != null) { |
| 199 | + int startOff = Math.min(getCaret().getDot(), getCaret().getMark()); |
| 200 | + int endOff = Math.max(getCaret().getDot(), getCaret().getMark()); |
| 201 | + int caretLine = getCaretLineNumber()+1; |
| 202 | + Position caretPos = new Position(getCaretLineNumber()+1, getCaretOffsetFromLineStart()); |
| 203 | + CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(); |
| 204 | + JavaParser.getStaticConfiguration().setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver)); |
| 205 | + // combinedTypeSolver.add(new JavaParserTypeSolver(getText())); |
| 206 | + for(FileContainer fc : JDA.getOpenFiles()) { |
| 207 | + |
| 208 | + try { |
| 209 | + combinedTypeSolver.add(new JarTypeSolver(fc.file)); |
| 210 | + } catch (IOException e) { |
| 211 | + e.printStackTrace(); |
| 212 | + } |
| 213 | + } |
| 214 | + combinedTypeSolver.add(new ReflectionTypeSolver()); |
| 215 | + try { |
| 216 | + combinedTypeSolver.add(new JarTypeSolver(new File("C:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\rt.jar"))); |
| 217 | + } catch (IOException e) { |
| 218 | + e.printStackTrace(); |
| 219 | + } |
| 220 | + JavaParserFacade solver = JavaParserFacade.get(combinedTypeSolver); |
| 221 | + new NodeIterator(node -> { |
| 222 | + Range r = node.getRange().orElse(null); |
| 223 | + if (r == null) return false; |
| 224 | + if (r.begin.compareTo(caretPos) <= 0 && r.end.compareTo(caretPos) >= 0) { |
| 225 | + // if (r.begin.line == caretLine && r.end.line == caretLine) { |
| 226 | + if (node instanceof MethodCallExpr) { |
| 227 | + System.out.println(solver.solve((MethodCallExpr) node)); |
| 228 | + } else if (node instanceof ClassOrInterfaceType) { |
| 229 | + System.out.println(JavaParserFactory.getContext(node, solver.getTypeSolver()).solveType(((ClassOrInterfaceType) node).getName().getId(), solver.getTypeSolver())); |
| 230 | + } else if (node instanceof NameExpr) { |
| 231 | + System.out.println(JavaParserFactory.getContext(node, solver.getTypeSolver()).solveType(((NameExpr) node).getName().getId(), solver.getTypeSolver())); |
| 232 | + } else if (node instanceof SimpleName) { |
| 233 | + System.out.println(JavaParserFactory.getContext(node, solver.getTypeSolver()).solveType(((SimpleName) node).getId(), solver.getTypeSolver())); |
| 234 | + } else if (node instanceof FieldAccessExpr) { |
| 235 | + System.out.println(solver.solve((FieldAccessExpr) node)); |
| 236 | + } else if (node instanceof ExplicitConstructorInvocationStmt) { |
| 237 | + System.out.println(solver.solve((ExplicitConstructorInvocationStmt) node)); |
| 238 | + } else if (node instanceof MethodDeclaration) { |
| 239 | + System.out.println(solver.getTypeOfThisIn(node)); |
| 240 | + } else if (node instanceof FieldDeclaration) { |
| 241 | + System.out.println(solver.getTypeOfThisIn(node)); |
| 242 | + } else if (node instanceof ClassOrInterfaceDeclaration) { |
| 243 | + System.out.println(solver.getTypeOfThisIn(node)); |
| 244 | + } else if (node instanceof ImportDeclaration) { |
| 245 | + System.out.println(((ImportDeclaration) node).getName()); |
| 246 | + } |
| 247 | + // } |
| 248 | + // System.out.println(node); |
| 249 | + // System.out.println(); |
| 250 | + // } |
| 251 | + return true; |
| 252 | + } |
| 253 | + return false; |
| 254 | + }).explore(ast); |
| 255 | + // for (JavaToken tok = tr.getBegin(); tok != null; tok = tok.getNextToken().orElse(null)) { |
| 256 | + // Range r = tok.getRange().orElse(null); |
| 257 | + return; |
| 258 | + } |
| 259 | + |
151 | 260 | String tokenName;
|
152 |
| - if (getSelectedText() != null) |
| 261 | + if (getSelectedText() != null) { |
153 | 262 | tokenName = getSelectedText();
|
154 |
| - else if (isIdentifierSelected()) |
| 263 | + } else if (isIdentifierSelected()) { |
155 | 264 | tokenName = currentlySelectedToken.getLexeme();
|
156 |
| - else if (isStringSelected()) { |
| 265 | + } else if (isStringSelected()) { |
157 | 266 | tokenName = currentlySelectedToken.getLexeme();
|
158 | 267 | tokenName = tokenName.substring(1, tokenName.length() - 1);
|
159 |
| - } else |
| 268 | + new SearchDialog(tokenName, JDA.constantSearchCallback.apply(tokenName)).setVisible(true); |
160 | 269 | return;
|
| 270 | + } else { |
| 271 | + return; |
| 272 | + } |
161 | 273 |
|
162 |
| - new SearchDialog(tokenName, JDA.searchCallback.apply(tokenName)).setVisible(true); |
| 274 | + new SearchDialog(tokenName, JDA.constantSearchCallback.apply(tokenName)).setVisible(true); |
163 | 275 | }
|
164 | 276 |
|
165 | 277 | private void doRenameDialog() {
|
|
0 commit comments