move the one function from SyntaxUtil into SourceUtils, drop its "s"

This commit is contained in:
Ben Fry
2020-01-28 19:35:15 -05:00
parent 27e6ee6c41
commit ef06746e57
10 changed files with 49 additions and 76 deletions

View File

@@ -1740,10 +1740,10 @@ public class CompletionGenerator {
// If the parsed code contains pde enhancements, take 'em out.
// TODO: test this
TextTransform transform = new TextTransform(pdePhrase);
transform.addAll(SourceUtils.replaceTypeConstructors(pdePhrase));
transform.addAll(SourceUtils.replaceHexLiterals(pdePhrase));
transform.addAll(SourceUtils.replaceColorRegex(pdePhrase));
transform.addAll(SourceUtils.fixFloatsRegex(pdePhrase));
transform.addAll(SourceUtil.replaceTypeConstructors(pdePhrase));
transform.addAll(SourceUtil.replaceHexLiterals(pdePhrase));
transform.addAll(SourceUtil.replaceColorRegex(pdePhrase));
transform.addAll(SourceUtil.fixFloatsRegex(pdePhrase));
String phrase = transform.apply();
//After typing 'arg.' all members of arg type are to be listed. This one is a flag for it

View File

@@ -266,7 +266,7 @@ class ErrorChecker {
int tabStartOffset = ps.tabStartOffsets[tabIndex];
int tabEndOffset = (tabIndex < ps.tabStartOffsets.length - 1) ?
ps.tabStartOffsets[tabIndex + 1] : ps.scrubbedPdeCode.length();
int[] braceResult = SourceUtils.checkForMissingBraces(ps.scrubbedPdeCode, tabStartOffset, tabEndOffset);
int[] braceResult = SourceUtil.checkForMissingBraces(ps.scrubbedPdeCode, tabStartOffset, tabEndOffset);
if (braceResult[0] != 0) {
JavaProblem problem =
new JavaProblem(braceResult[0] < 0

View File

@@ -86,7 +86,7 @@ public class ImportStatement {
* @return Newly parsed import statement information.
*/
public static ImportStatement parse(String importString) {
Matcher matcher = SourceUtils.IMPORT_REGEX_NO_KEYWORD.matcher(importString);
Matcher matcher = SourceUtil.IMPORT_REGEX_NO_KEYWORD.matcher(importString);
if (!matcher.find()) return null;
return parse(matcher.toMatchResult());

View File

@@ -1649,7 +1649,7 @@ public class JavaEditor extends Editor {
if (sc.isExtension("pde")) {
String tabCode = sc.getProgram();
List<ImportStatement> imports = SourceUtils.parseProgramImports(tabCode);
List<ImportStatement> imports = SourceUtil.parseProgramImports(tabCode);
if (!imports.isEmpty()) {
ArrayList<String> importHeaders = new ArrayList<>();

View File

@@ -54,7 +54,6 @@ import processing.app.Util;
import processing.mode.java.TextTransform.OffsetMapper;
import processing.mode.java.preproc.PdePreprocessor;
import processing.mode.java.preproc.PreprocessorResult;
import processing.mode.java.preproc.SyntaxUtil;
import processing.data.IntList;
import processing.data.StringList;
@@ -332,8 +331,7 @@ public class PreprocService {
* @return The newly generated preprocessed sketch.
*/
private PreprocSketch preprocessSketch(PreprocSketch prevResult) {
boolean firstCheck = prevResult == null;
boolean firstCheck = (prevResult == null);
PreprocSketch.Builder result = new PreprocSketch.Builder();
@@ -368,7 +366,7 @@ public class PreprocService {
newPiece.append('\n');
String newPieceBuilt = newPiece.toString();
numLines += SyntaxUtil.getCount(newPieceBuilt, "\n");
numLines += SourceUtil.getCount(newPieceBuilt, "\n");
workBuffer.append(newPieceBuilt);
}
}
@@ -380,7 +378,8 @@ public class PreprocService {
boolean reloadLibraries = firstCheck || librariesChanged.getAndSet(false);
// Core and default imports
PdePreprocessor preProcessor = editor.createPreprocessor(editor.getSketch().getName());
PdePreprocessor preProcessor =
editor.createPreprocessor(editor.getSketch().getName());
if (coreAndDefaultImports == null) {
coreAndDefaultImports = buildCoreAndDefaultImports(preProcessor);
}
@@ -395,7 +394,7 @@ public class PreprocService {
// TODO: convert unicode escapes to chars
SourceUtils.scrubCommentsAndStrings(workBuffer);
SourceUtil.scrubCommentsAndStrings(workBuffer);
result.scrubbedPdeCode = workBuffer.toString();
@@ -474,7 +473,7 @@ public class PreprocService {
// Prepare advanced transforms which operate on AST
TextTransform toCompilable = new TextTransform(parsableStage);
toCompilable.addAll(SourceUtils.preprocessAST(parsableCU));
toCompilable.addAll(SourceUtil.preprocessAST(parsableCU));
// Transform code to compilable state
String compilableStage = toCompilable.apply();

View File

@@ -15,7 +15,7 @@ import java.util.regex.Pattern;
import processing.mode.java.TextTransform.Edit;
import processing.mode.java.preproc.PdePreprocessor;
public class SourceUtils {
public class SourceUtil {
public static final Pattern IMPORT_REGEX =
@@ -368,4 +368,30 @@ public class SourceUtils {
}
return new int[] {depth, lineNumber - 1, end - lineStart - 2, end - start - 2};
}
/**
* Determine how many times a string appears in another.
*
* @param body The string in which occurrences should be counted.
* @param search The string to look for.
* @return The number of times search appears in body.
*/
public static int getCount(String body, String search) {
int count = 0;
if (search.length() == 1) {
for (int i = 0; i < body.length(); i++) {
if (body.charAt(i) == search.charAt(0)) {
count++;
}
}
} else {
for (int i = 0; i < body.length(); i++) {
if (body.substring(i).startsWith(search)) {
count++;
}
}
}
return count;
}
}

View File

@@ -32,6 +32,7 @@ import org.antlr.v4.runtime.tree.ParseTree;
import processing.app.Preferences;
import processing.core.PApplet;
import processing.mode.java.ImportStatement;
import processing.mode.java.SourceUtil;
import processing.mode.java.TextTransform;
import processing.mode.java.preproc.PdePreprocessor.Mode;
import processing.mode.java.preproc.issue.PdePreprocessIssue;
@@ -1218,7 +1219,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
));
}
rewriteResultBuilder.addOffset(SyntaxUtil.getCount(newCode, "\n"));
rewriteResultBuilder.addOffset(SourceUtil.getCount(newCode, "\n"));
}
}

View File

@@ -1,53 +0,0 @@
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-19 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.mode.java.preproc;
/**
* Convenience functions useful for working on syntax checking for source.
*/
public class SyntaxUtil {
/**
* Determine how many times a string appears in another.
*
* @param body The string in which occurrences should be counted.
* @param search The string to look for.
* @return The number of times search appears in body.
*/
public static int getCount(String body, String search) {
int count = 0;
if (search.length() == 1) {
for (int i = 0; i < body.length(); i++) {
if (body.charAt(i) == search.charAt(0)) {
count++;
}
}
} else {
for (int i = 0; i < body.length(); i++) {
if (body.substring(i).startsWith(search)) {
count++;
}
}
}
return count;
}
}

View File

@@ -23,7 +23,7 @@ package processing.mode.java.preproc.issue;
import java.util.Optional;
import processing.mode.java.preproc.SyntaxUtil;
import processing.mode.java.SourceUtil;
/**
@@ -58,7 +58,7 @@ public class IssueLocationFactory {
// Determine if the issue should be relocated
boolean shouldAttributeToPrior = simplification.getAttributeToPriorToken();
shouldAttributeToPrior = shouldAttributeToPrior && originalLine != 0;
if (!shouldAttributeToPrior) {
return new IssueLocation(originalLine, originalOffset);
}
@@ -82,7 +82,7 @@ public class IssueLocationFactory {
if (foundStartOfMatchMaybe.isPresent()) {
startOfMatch = priorCode.length() - foundStartOfMatchMaybe.get();
String contentsOfMatch = priorCode.substring(startOfMatch);
linesOffset = SyntaxUtil.getCount(contentsOfMatch, "\n");
linesOffset = SourceUtil.getCount(contentsOfMatch, "\n");
} else {
startOfMatch = priorCode.length();
linesOffset = 0;

View File

@@ -24,7 +24,7 @@ package processing.mode.java.preproc.issue;
import processing.app.Language;
import processing.app.Platform;
import processing.mode.java.preproc.SyntaxUtil;
import processing.mode.java.SourceUtil;
import java.util.ArrayList;
import java.util.List;
@@ -273,7 +273,7 @@ public class PreprocessIssueMessageSimplifier {
messageContent = messageContent.replace(filter.get(), "");
}
int count = SyntaxUtil.getCount(messageContent, token);
int count = SourceUtil.getCount(messageContent, token);
if (count % 2 == 0) {
return Optional.empty();
@@ -342,8 +342,8 @@ public class PreprocessIssueMessageSimplifier {
public Optional<IssueMessageSimplification> simplify(String message) {
String messageContent = getOffendingArea(message);
int count1 = SyntaxUtil.getCount(messageContent, token1);
int count2 = SyntaxUtil.getCount(messageContent, token2);
int count1 = SourceUtil.getCount(messageContent, token1);
int count2 = SourceUtil.getCount(messageContent, token2);
if (count1 == count2) {
return Optional.empty();