From bceb495731b969afa6430be8d005c9d22c162757 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 6 Feb 2022 19:18:50 -0500 Subject: [PATCH] cleaning syntax, spaces, and warnings in search of bug --- .../processing/mode/java/ErrorChecker.java | 92 +++++++++---------- .../processing/mode/java/PreprocService.java | 26 ++---- 2 files changed, 54 insertions(+), 64 deletions(-) diff --git a/java/src/processing/mode/java/ErrorChecker.java b/java/src/processing/mode/java/ErrorChecker.java index 6cdd0ed68..34f9b9057 100644 --- a/java/src/processing/mode/java/ErrorChecker.java +++ b/java/src/processing/mode/java/ErrorChecker.java @@ -29,7 +29,8 @@ import processing.app.Problem; class ErrorChecker { - // Delay delivering error check result after last sketch change #2677 + // Delay delivering error check result after last sketch change + // https://github.com/processing/processing/issues/2677 private final static long DELAY_BEFORE_UPDATE = 650; final private ScheduledExecutorService scheduler; @@ -81,7 +82,6 @@ class ErrorChecker { private void handleSketchProblems(PreprocSketch ps) { - Map suggCache = JavaMode.importSuggestEnabled ? new HashMap<>() : Collections.emptyMap(); @@ -108,26 +108,25 @@ class ErrorChecker { AtomicReference searchClassPath = new AtomicReference<>(null); List cuProblems = iproblems.stream() - // Filter Warnings if they are not enabled - .filter(iproblem -> !(iproblem.isWarning() && !JavaMode.warningsEnabled)) - .filter(iproblem -> !(isIgnorableProblem(iproblem))) - // Transform into our Problems - .map(iproblem -> { - JavaProblem p = convertIProblem(iproblem, ps); + // Filter Warnings if they are not enabled + .filter(iproblem -> !(iproblem.isWarning() && !JavaMode.warningsEnabled)) + .filter(iproblem -> !(isIgnorableProblem(iproblem))) + // Transform into our Problems + .map(iproblem -> { + JavaProblem p = convertIProblem(iproblem, ps); - // Handle import suggestions - if (p != null && JavaMode.importSuggestEnabled && isUndefinedTypeProblem(iproblem)) { - ClassPath cp = searchClassPath.updateAndGet(prev -> prev != null ? - prev : new ClassPathFactory().createFromPaths(ps.searchClassPathArray)); - String[] s = suggCache.computeIfAbsent(iproblem.getArguments()[0], - name -> getImportSuggestions(cp, name)); - p.setImportSuggestions(s); - } - - return p; - }) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // Handle import suggestions + if (p != null && JavaMode.importSuggestEnabled && isUndefinedTypeProblem(iproblem)) { + ClassPath cp = searchClassPath.updateAndGet(prev -> prev != null ? + prev : new ClassPathFactory().createFromPaths(ps.searchClassPathArray)); + String[] s = suggCache.computeIfAbsent(iproblem.getArguments()[0], + name -> getImportSuggestions(cp, name)); + p.setImportSuggestions(s); + } + return p; + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); problems.addAll(cuProblems); } @@ -135,7 +134,8 @@ class ErrorChecker { if (scheduledUiUpdate != null) { scheduledUiUpdate.cancel(true); } - // Update UI after a delay. See #2677 + // Update the UI after a delay + // https://github.com/processing/processing/issues/2677 long delay = nextUiUpdate - System.currentTimeMillis(); Runnable uiUpdater = () -> { if (nextUiUpdate > 0 && System.currentTimeMillis() >= nextUiUpdate) { @@ -166,9 +166,8 @@ class ErrorChecker { // also produced and is preferred over this one. // (Syntax error, insert ":: IdentifierOrNew" to complete Expression) // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=405780 - boolean ignorable = message.contains( - "Syntax error, insert \":: IdentifierOrNew\"" - ); + boolean ignorable = + message.contains("Syntax error, insert \":: IdentifierOrNew\""); // It's ok if the file names do not line up during preprocessing. ignorable |= message.contains("must be defined in its own file"); @@ -238,7 +237,6 @@ class ErrorChecker { problems.add(problem); } - // Go through iproblems and look for problems involving curly quotes List problems2 = new ArrayList<>(0); IProblem[] iproblems = ps.compilationUnit.getProblems(); @@ -330,31 +328,31 @@ class ErrorChecker { static public String[] getImportSuggestions(ClassPath cp, String className) { className = className.replace("[", "\\[").replace("]", "\\]"); - RegExpResourceFilter regf = new RegExpResourceFilter( + RegExpResourceFilter filter = new RegExpResourceFilter( Pattern.compile(".*"), Pattern.compile("(.*\\$)?" + className + "\\.class", Pattern.CASE_INSENSITIVE)); - String[] resources = cp.findResources("", regf); + String[] resources = cp.findResources("", filter); return Arrays.stream(resources) - // remove ".class" suffix - .map(res -> res.substring(0, res.length() - 6)) - // replace path separators with dots - .map(res -> res.replace('/', '.')) - // replace inner class separators with dots - .map(res -> res.replace('$', '.')) - // sort, prioritize clases from java. package - .map(res -> res.startsWith("classes.") ? res.substring(8) : res) - .sorted((o1, o2) -> { - // put java.* first, should be prioritized more - boolean o1StartsWithJava = o1.startsWith("java"); - boolean o2StartsWithJava = o2.startsWith("java"); - if (o1StartsWithJava != o2StartsWithJava) { - if (o1StartsWithJava) return -1; - return 1; - } - return o1.compareTo(o2); - }) - .toArray(String[]::new); + // remove ".class" suffix + .map(res -> res.substring(0, res.length() - 6)) + // replace path separators with dots + .map(res -> res.replace('/', '.')) + // replace inner class separators with dots + .map(res -> res.replace('$', '.')) + // sort, prioritize classes from java. package + .map(res -> res.startsWith("classes.") ? res.substring(8) : res) + .sorted((o1, o2) -> { + // put java.* first, should be prioritized more + boolean o1StartsWithJava = o1.startsWith("java"); + boolean o2StartsWithJava = o2.startsWith("java"); + if (o1StartsWithJava != o2StartsWithJava) { + if (o1StartsWithJava) return -1; + return 1; + } + return o1.compareTo(o2); + }) + .toArray(String[]::new); } } diff --git a/java/src/processing/mode/java/PreprocService.java b/java/src/processing/mode/java/PreprocService.java index 777896ec1..472972c56 100644 --- a/java/src/processing/mode/java/PreprocService.java +++ b/java/src/processing/mode/java/PreprocService.java @@ -358,11 +358,7 @@ public class PreprocService { tabStartsList.append(workBuffer.length()); tabLineStarts.add(numLines); - StringBuilder newPiece = new StringBuilder(); - newPiece.append(getSketchTabContents(sc)); - newPiece.append('\n'); - - String newPieceBuilt = newPiece.toString(); + String newPieceBuilt = getSketchTabContents(sc) + '\n'; numLines += SourceUtil.getCount(newPieceBuilt, "\n"); workBuffer.append(newPieceBuilt); } else if (sc.isExtension("java")) { @@ -657,9 +653,9 @@ public class PreprocService { // Return return new CompileResults( - astRequester.getMainCompilationUnit(), - astRequester.getProblems(), - javaFileMapping + astRequester.getMainCompilationUnit(), + astRequester.getProblems(), + javaFileMapping ); } @@ -758,10 +754,7 @@ public class PreprocService { if (source.equals(mainSource)) { mainCompilationUnit = ast; } - - for (IProblem problem : ast.getProblems()) { - problems.add(problem); - } + Collections.addAll(problems, ast.getProblems()); } /** @@ -787,7 +780,7 @@ public class PreprocService { /** * Data structure holding the results of compilation. */ - private class CompileResults { + static private class CompileResults { private final CompilationUnit compilationUnit; private final List problems; private final Map javaFileMapping; @@ -845,9 +838,9 @@ public class PreprocService { /** * SketchCode (tab of sketch) which is a ".java" tab. */ - private class JavaSketchCode { - private SketchCode sketchCode; - private int tabIndex; + static private class JavaSketchCode { + private final SketchCode sketchCode; + private final int tabIndex; /** * Create a new record of a ".java" tab inside a sketch. @@ -877,7 +870,6 @@ public class PreprocService { public int getTabIndex() { return tabIndex; } - } /// IMPORTS -----------------------------------------------------------------