mirror of
https://github.com/processing/processing4.git
synced 2026-01-29 11:21:06 +01:00
Set error checker listeners only once
This commit is contained in:
@@ -140,8 +140,15 @@ public class JavaEditor extends Editor {
|
||||
hasJavaTabs = checkForJavaTabs();
|
||||
//initializeErrorChecker();
|
||||
|
||||
errorCheckerService = new ErrorCheckerService(this);
|
||||
errorCheckerService.start();
|
||||
{ // Init error checker
|
||||
errorCheckerService = new ErrorCheckerService(this);
|
||||
Document currentDocument = currentDocument();
|
||||
if (currentDocument != null) {
|
||||
errorCheckerService.addListener(currentDocument);
|
||||
}
|
||||
errorCheckerService.start();
|
||||
errorCheckerService.request();
|
||||
}
|
||||
|
||||
// hack to add a JPanel to the right-hand side of the text area
|
||||
JPanel textAndError = new JPanel();
|
||||
@@ -2330,10 +2337,18 @@ public class JavaEditor extends Editor {
|
||||
*/
|
||||
@Override
|
||||
public void setCode(SketchCode code) {
|
||||
|
||||
Document oldDoc = code.getDocument();
|
||||
|
||||
//System.out.println("tab switch: " + code.getFileName());
|
||||
// set the new document in the textarea, etc. need to do this first
|
||||
super.setCode(code);
|
||||
|
||||
Document newDoc = code.getDocument();
|
||||
if (oldDoc != newDoc && errorCheckerService != null) {
|
||||
errorCheckerService.addListener(newDoc);
|
||||
}
|
||||
|
||||
// set line background colors for tab
|
||||
final JavaTextArea ta = getJavaTextArea();
|
||||
// can be null when setCode is called the first time (in constructor)
|
||||
|
||||
@@ -235,7 +235,6 @@ public class ErrorCheckerService {
|
||||
astGenerator = new ASTGenerator(this);
|
||||
errorMsgSimplifier = new ErrorMessageSimplifier();
|
||||
tempErrorLog = new TreeMap<>();
|
||||
sketchChangedListener = new SketchChangedListener();
|
||||
// for (final SketchCode sc : editor.getSketch().getCode()) {
|
||||
// sc.getDocument().addDocumentListener(sketchChangedListener);
|
||||
// }
|
||||
@@ -287,16 +286,16 @@ public class ErrorCheckerService {
|
||||
try {
|
||||
|
||||
Messages.log("Starting error check");
|
||||
lastCodeCheckResult = checkCode();
|
||||
CodeCheckResult result = checkCode();
|
||||
|
||||
if (!JavaMode.errorCheckEnabled) {
|
||||
lastCodeCheckResult.problems.clear();
|
||||
Messages.log("Error Check disabled, so not updating UI.");
|
||||
}
|
||||
|
||||
checkForMissingImports();
|
||||
lastCodeCheckResult = result;
|
||||
|
||||
updateSketchCodeListeners();
|
||||
checkForMissingImports(lastCodeCheckResult);
|
||||
|
||||
if (JavaMode.errorCheckEnabled) {
|
||||
if (scheduledUiUpdate != null) {
|
||||
@@ -319,7 +318,6 @@ public class ErrorCheckerService {
|
||||
editor.updateErrorBar(result.problems);
|
||||
editor.getTextArea().repaint();
|
||||
editor.updateErrorToggle(result.containsErrors);
|
||||
updateSketchCodeListeners();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -375,35 +373,15 @@ public class ErrorCheckerService {
|
||||
}
|
||||
|
||||
|
||||
protected void updateSketchCodeListeners() {
|
||||
for (SketchCode sc : editor.getSketch().getCode()) {
|
||||
SyntaxDocument doc = (SyntaxDocument) sc.getDocument();
|
||||
if (!hasSketchChangedListener(doc)) {
|
||||
doc.addDocumentListener(sketchChangedListener);
|
||||
}
|
||||
}
|
||||
public void addListener(Document doc) {
|
||||
doc.addDocumentListener(sketchChangedListener);
|
||||
}
|
||||
|
||||
|
||||
boolean hasSketchChangedListener(SyntaxDocument doc) {
|
||||
if (doc != null && doc.getDocumentListeners() != null) {
|
||||
for (DocumentListener dl : doc.getDocumentListeners()) {
|
||||
if (dl.equals(sketchChangedListener)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
protected void checkForMissingImports() {
|
||||
// Atomic access
|
||||
CodeCheckResult lastCodeCheckResult = this.lastCodeCheckResult;
|
||||
|
||||
protected void checkForMissingImports(CodeCheckResult result) {
|
||||
if (Preferences.getBoolean(JavaMode.SUGGEST_IMPORTS_PREF)) {
|
||||
for (Problem p : lastCodeCheckResult.problems) {
|
||||
if(p.getIProblem().getID() == IProblem.UndefinedType) {
|
||||
for (Problem p : result.problems) {
|
||||
if (p.getIProblem().getID() == IProblem.UndefinedType) {
|
||||
String args[] = p.getIProblem().getArguments();
|
||||
if (args.length > 0) {
|
||||
String missingClass = args[0];
|
||||
@@ -423,37 +401,22 @@ public class ErrorCheckerService {
|
||||
}
|
||||
|
||||
|
||||
protected SketchChangedListener sketchChangedListener;
|
||||
protected class SketchChangedListener implements DocumentListener{
|
||||
|
||||
private SketchChangedListener(){
|
||||
}
|
||||
|
||||
protected final DocumentListener sketchChangedListener = new DocumentListener() {
|
||||
@Override
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
if (JavaMode.errorCheckEnabled) {
|
||||
request();
|
||||
//log("doc insert update, man error check..");
|
||||
}
|
||||
if (JavaMode.errorCheckEnabled) request();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
if (JavaMode.errorCheckEnabled){
|
||||
request();
|
||||
//log("doc remove update, man error check..");
|
||||
}
|
||||
if (JavaMode.errorCheckEnabled) request();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
if (JavaMode.errorCheckEnabled){
|
||||
request();
|
||||
//log("doc changed update, man error check..");
|
||||
}
|
||||
if (JavaMode.errorCheckEnabled) request();
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
public static class CodeCheckResult {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user