From 62249e218a535d0b6407db7ed00d0fb78ca4e43a Mon Sep 17 00:00:00 2001 From: A Pottinger Date: Sun, 25 Jul 2021 13:50:38 -0700 Subject: [PATCH] Close to good solution. Just need to confirm .java file updates. --- .../processing/mode/java/ErrorChecker.java | 4 +- .../processing/mode/java/PreprocService.java | 101 +++++++++++++++--- .../processing/mode/java/PreprocSketch.java | 33 +++++- 3 files changed, 120 insertions(+), 18 deletions(-) diff --git a/java/src/processing/mode/java/ErrorChecker.java b/java/src/processing/mode/java/ErrorChecker.java index d6a26f609..0a0510cde 100644 --- a/java/src/processing/mode/java/ErrorChecker.java +++ b/java/src/processing/mode/java/ErrorChecker.java @@ -89,7 +89,7 @@ class ErrorChecker { if (ps.compilationUnit == null) { iproblems = new IProblem[0]; } else { - iproblems = ps.compilationUnit.getProblems(); + iproblems = ps.iproblems; } final List problems = new ArrayList<>(ps.otherProblems); @@ -117,6 +117,8 @@ class ErrorChecker { // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=405780 .filter(iproblem -> !iproblem.getMessage() .contains("Syntax error, insert \":: IdentifierOrNew\"")) + .filter(iproblem -> !iproblem.getMessage() + .contains("must be defined in its own file")) // Transform into our Problems .map(iproblem -> { JavaProblem p = convertIProblem(iproblem, ps); diff --git a/java/src/processing/mode/java/PreprocService.java b/java/src/processing/mode/java/PreprocService.java index ba92a0210..e881517be 100644 --- a/java/src/processing/mode/java/PreprocService.java +++ b/java/src/processing/mode/java/PreprocService.java @@ -353,7 +353,7 @@ public class PreprocService { // Combine code into one buffer int numLines = 1; IntList tabStartsList = new IntList(); - List javaFiles = new ArrayList<>(); + List javaFiles = new ArrayList<>(); List tabLineStarts = new ArrayList<>(); for (SketchCode sc : sketch.getCode()) { if (sc.isExtension("pde")) { @@ -376,7 +376,14 @@ public class PreprocService { numLines += SourceUtil.getCount(newPieceBuilt, "\n"); workBuffer.append(newPieceBuilt); } else if (sc.isExtension("java")) { - javaFiles.add(sc); + tabStartsList.append(workBuffer.length()); + tabLineStarts.add(numLines); + + javaFiles.add(new JavaSketchCode(sc, tabStartsList.size()-1)); + + String newPieceBuilt = "\n//Skip java file\n"; + numLines += SourceUtil.getCount(newPieceBuilt, "\n"); + workBuffer.append(newPieceBuilt); } } result.tabStartOffsets = tabStartsList.array(); @@ -500,15 +507,15 @@ public class PreprocService { // Generate bindings after getting problems - avoids // 'missing type' errors when there are syntax problems - CompilationUnit bindingsCU; + FinalCompileResults finalCompile; if (javaFiles.size() == 0) { - bindingsCU = compileInMemory( + finalCompile = compileInMemory( compilableStage, className, result.classPathArray ); } else { - bindingsCU = compileFromDisk( + finalCompile = compileFromDisk( compilableStage, className, result.classPathArray, @@ -517,14 +524,16 @@ public class PreprocService { } // Get compilation problems - List bindingsProblems = Arrays.asList(bindingsCU.getProblems()); + List bindingsProblems = finalCompile.getProblems(); result.hasCompilationErrors = bindingsProblems.stream().anyMatch(IProblem::isError); // Update builder result.offsetMapper = parsableMapper.thenMapping(compilableMapper); result.javaCode = compilableStage; - result.compilationUnit = bindingsCU; + result.compilationUnit = finalCompile.getCompilationUnit(); + result.javaFileMapping = finalCompile.getJavaFileMapping(); + result.iproblems = finalCompile.getProblems(); // Build it return result.build(); @@ -532,7 +541,7 @@ public class PreprocService { /// FINAL COMPILATION ------------------------------------------------------- - private CompilationUnit compileInMemory(String compilableStage, + private FinalCompileResults compileInMemory(String compilableStage, String className, String[] classPathArray) { parser.setSource(compilableStage.toCharArray()); @@ -542,13 +551,18 @@ public class PreprocService { parser.setUnitName(className); parser.setEnvironment(classPathArray, null, null, false); parser.setResolveBindings(true); - return (CompilationUnit) parser.createAST(null); + + CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null); + List problems = Arrays.asList(compilationUnit.getProblems()); + return new FinalCompileResults(compilationUnit, problems, new HashMap<>()); } - private CompilationUnit compileFromDisk(String compilableStage, - String className, String[] classPathArray, List javaFiles) { + private FinalCompileResults compileFromDisk(String compilableStage, + String className, String[] classPathArray, + List javaFiles) { List temporaryFilesList = new ArrayList<>(); + Map javaFileMapping = new HashMap<>(); // Prepare preprocessor parser.setKind(ASTParser.K_COMPILATION_UNIT); @@ -564,8 +578,9 @@ public class PreprocService { temporaryFilesList.add(mainTemporaryFile); // Write temporary java files - for (SketchCode javaFile : javaFiles) { - Path newPath = createTemporaryFile(javaFile.getProgram()); + for (JavaSketchCode javaFile : javaFiles) { + Path newPath = createTemporaryFile(javaFile.getSketchCode().getProgram()); + javaFileMapping.put(newPath.toString(), javaFile.getTabIndex()); temporaryFilesList.add(newPath); } @@ -577,7 +592,8 @@ public class PreprocService { } // Compile - MutableCompiledUnit compiledHolder = new MutableCompiledUnit();; + final MutableCompiledUnit compiledHolder = new MutableCompiledUnit(); + final List problems = new ArrayList<>(); parser.createASTs( temporaryFilesArray, null, @@ -587,6 +603,12 @@ public class PreprocService { if (source.equals(mainSource)) { compiledHolder.set(ast); } + + for (IProblem problem : ast.getProblems()) { + System.out.println("Found problem in " + source); + System.out.println(problem); + problems.add(problem); + } } }, null @@ -596,7 +618,11 @@ public class PreprocService { deleteFiles(temporaryFilesList); // Return - return compiledHolder.get(); + return new FinalCompileResults( + compiledHolder.get(), + problems, + javaFileMapping + ); } private Path createTemporaryFile(String content) { @@ -631,6 +657,51 @@ public class PreprocService { } } + private class FinalCompileResults { + private final CompilationUnit compilationUnit; + private final List problems; + private final Map javaFileMapping; + + public FinalCompileResults(CompilationUnit newCompilationUnit, + List newProblems, Map newJavaFileMapping) { + + compilationUnit = newCompilationUnit; + problems = newProblems; + javaFileMapping = newJavaFileMapping; + } + + public CompilationUnit getCompilationUnit() { + return compilationUnit; + } + + public List getProblems() { + return problems; + } + + public Map getJavaFileMapping() { + return javaFileMapping; + } + } + + private class JavaSketchCode { + private SketchCode sketchCode; + private int tabIndex; + + public JavaSketchCode(SketchCode newSketchCode, int newTabIndex) { + sketchCode = newSketchCode; + tabIndex = newTabIndex; + } + + public SketchCode getSketchCode() { + return sketchCode; + } + + public int getTabIndex() { + return tabIndex; + } + + } + /// IMPORTS ----------------------------------------------------------------- private List coreAndDefaultImports; diff --git a/java/src/processing/mode/java/PreprocSketch.java b/java/src/processing/mode/java/PreprocSketch.java index 3ec9e463b..e4ab92017 100644 --- a/java/src/processing/mode/java/PreprocSketch.java +++ b/java/src/processing/mode/java/PreprocSketch.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import processing.app.Problem; import processing.app.Sketch; @@ -44,6 +45,9 @@ public class PreprocSketch { public final List coreAndDefaultImports; public final List codeFolderImports; public final List otherProblems; + public final List iproblems; + + public final Map javaFileMapping; /// JAVA -> SKETCH ----------------------------------------------------------- @@ -69,8 +73,27 @@ public class PreprocSketch { public SketchInterval mapJavaToSketch(IProblem iproblem) { - return mapJavaToSketch(iproblem.getSourceStart(), - iproblem.getSourceEnd() + 1); // make it exclusive + String originalFile = new String(iproblem.getOriginatingFileName()); + System.out.println("here!!"); + System.out.println(originalFile); + System.out.println(javaFileMapping.keySet()); + System.out.println(iproblem); + boolean isJavaFile = javaFileMapping.containsKey(originalFile); + + if (isJavaFile) { + return new SketchInterval( + javaFileMapping.get(originalFile), + iproblem.getSourceStart(), + iproblem.getSourceEnd() + 1, + -1, // Is outside sketch code + -1 // Is outside sketch code + ); + } else { + return mapJavaToSketch( + iproblem.getSourceStart(), + iproblem.getSourceEnd() + 1 // make it exclusive + ); + } } @@ -213,6 +236,9 @@ public class PreprocSketch { public final List coreAndDefaultImports = new ArrayList<>(); public final List codeFolderImports = new ArrayList<>(); public final List otherProblems = new ArrayList<>(); + public List iproblems; + + public Map javaFileMapping; public PreprocSketch build() { return new PreprocSketch(this); @@ -247,6 +273,9 @@ public class PreprocSketch { otherProblems = b.otherProblems; + javaFileMapping = b.javaFileMapping; + iproblems = b.iproblems; + programImports = Collections.unmodifiableList(b.programImports); coreAndDefaultImports = Collections.unmodifiableList(b.coreAndDefaultImports); codeFolderImports = Collections.unmodifiableList(b.codeFolderImports);