ASTGen: better handling of AST update

This commit is contained in:
Jakub Valtar
2016-03-20 11:57:42 +01:00
parent 91fa5630fb
commit a6511af39d
2 changed files with 67 additions and 74 deletions

View File

@@ -37,7 +37,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.TreeMap;
import java.util.regex.Pattern;
@@ -63,14 +62,12 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ArrayAccess;
import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
@@ -133,70 +130,6 @@ public class ASTGenerator {
}
protected DefaultMutableTreeNode buildAST(String source, CompilationUnit cu) {
Messages.log("* buildAST");
if (cu == null) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
Map<String, String> options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options);
parser.setCompilerOptions(options);
compilationUnit = (CompilationUnit) parser.createAST(null);
} else {
compilationUnit = cu;
//log("Other cu");
}
// OutlineVisitor visitor = new OutlineVisitor();
// compilationUnit.accept(visitor);
codeTree = new DefaultMutableTreeNode(new ASTNodeWrapper((ASTNode) compilationUnit
.types().get(0)));
//log("Total CU " + compilationUnit.types().size());
if(compilationUnit.types() == null || compilationUnit.types().isEmpty()){
Messages.loge("No CU found!");
}
visitRecur((ASTNode) compilationUnit.types().get(0), codeTree);
if (SHOW_DEBUG_TREE) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
gui.updateDebugTree(codeTree);
}
});
}
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// if (codeTree != null) {
// if (!frameAutoComp.isVisible()) {
//
// frameAutoComp.setVisible(true);
//
// }
// if (!frmJavaDoc.isVisible()) {
// long t = System.currentTimeMillis();
// loadJavaDoc();
// log("Time taken: "
// + (System.currentTimeMillis() - t));
// frmJavaDoc.setBounds(new Rectangle(errorCheckerService.getEditor()
// .getX() + errorCheckerService.getEditor().getWidth(),
// errorCheckerService.getEditor()
// .getY(), 450, 600));
// frmJavaDoc.setVisible(true);
// }
// }
// }
// });
// Base.loge("++>" + System.getProperty("java.class.path"));
// log(System.getProperty("java.class.path"));
// log("-------------------------------");
return codeTree;
}
protected final ClassPathFactory factory = new ClassPathFactory();
/**
@@ -3166,6 +3099,52 @@ public class ASTGenerator {
}
/// Error checker ------------------------------------------------------------
protected static DefaultMutableTreeNode buildTree(CompilationUnit cu) {
if (cu.types().isEmpty()){
Messages.loge("No Type found in CU");
return null;
}
ASTNode type0 = (ASTNode) cu.types().get(0);
ASTNodeWrapper w = new ASTNodeWrapper(type0);
DefaultMutableTreeNode codeTree = new DefaultMutableTreeNode(w);
visitRecur(type0, codeTree);
return codeTree;
}
protected void updateAST(CompilationUnit cu, DefaultMutableTreeNode tree) {
compilationUnit = cu;
codeTree = tree;
if (SHOW_DEBUG_TREE) {
gui.updateDebugTree(codeTree);
}
// if (codeTree != null) {
// if (!frameAutoComp.isVisible()) {
//
// frameAutoComp.setVisible(true);
//
// }
// if (!frmJavaDoc.isVisible()) {
// long t = System.currentTimeMillis();
// loadJavaDoc();
// log("Time taken: "
// + (System.currentTimeMillis() - t));
// frmJavaDoc.setBounds(new Rectangle(errorCheckerService.getEditor()
// .getX() + errorCheckerService.getEditor().getWidth(),
// errorCheckerService.getEditor()
// .getY(), 450, 600));
// frmJavaDoc.setVisible(true);
// }
// }
}
/// Editor stuff -------------------------------------------------------------

View File

@@ -25,6 +25,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
@@ -49,6 +50,7 @@ import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.PlainDocument;
import javax.swing.tree.DefaultMutableTreeNode;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.compiler.CharOperation;
@@ -209,9 +211,19 @@ public class ErrorCheckerService {
// This is when the loaded sketch already has syntax errors.
// Completion wouldn't be complete, but it'd be still something
// better than nothing
synchronized (astGenerator) {
astGenerator.buildAST(lastCodeCheckResult.sourceCode,
lastCodeCheckResult.compilationUnit);
try {
final DefaultMutableTreeNode tree =
ASTGenerator.buildTree(lastCodeCheckResult.compilationUnit);
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
synchronized (astGenerator) {
astGenerator.updateAST(lastCodeCheckResult.compilationUnit, tree);
}
}
});
} catch (InterruptedException | InvocationTargetException e) {
Messages.loge("exception during initial AST update", e);
}
while (running) {
@@ -234,6 +246,9 @@ public class ErrorCheckerService {
lastCodeCheckResult = result;
final DefaultMutableTreeNode tree =
ASTGenerator.buildTree(lastCodeCheckResult.compilationUnit);
checkForMissingImports(lastCodeCheckResult);
if (JavaMode.errorCheckEnabled) {
@@ -252,6 +267,9 @@ public class ErrorCheckerService {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
synchronized (astGenerator) {
astGenerator.updateAST(lastCodeCheckResult.compilationUnit, tree);
}
calcPdeOffsetsForProbList(result);
updateErrorTable(result.problems);
editor.updateErrorBar(result.problems);
@@ -483,10 +501,6 @@ public class ErrorCheckerService {
}
}
synchronized (astGenerator) {
astGenerator.buildAST(result.sourceCode, result.compilationUnit);
}
return result;
}