Search in sources :

Example 1 with Edit

use of processing.mode.java.pdex.TextTransform.Edit in project processing by processing.

the class SourceUtils method replaceTypeConstructors.

public static List<Edit> replaceTypeConstructors(CharSequence source) {
    List<Edit> result = new ArrayList<>();
    Matcher matcher = TYPE_CONSTRUCTOR_REGEX.matcher(source);
    while (matcher.find()) {
        String match = matcher.group(1);
        int offset = matcher.start(1);
        int length = match.length();
        result.add(Edit.insert(offset, "PApplet."));
        String replace = "parse" + Character.toUpperCase(match.charAt(0)) + match.substring(1);
        result.add(Edit.replace(offset, length, replace));
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Edit(processing.mode.java.pdex.TextTransform.Edit)

Example 2 with Edit

use of processing.mode.java.pdex.TextTransform.Edit in project processing by processing.

the class SourceUtils method preprocessAST.

public static List<Edit> preprocessAST(CompilationUnit cu) {
    final List<Edit> edits = new ArrayList<>();
    // Walk the tree
    cu.accept(new ASTVisitor() {

        @Override
        public boolean visit(SimpleType node) {
            // replace "color" with "int"
            if ("color".equals(node.getName().toString())) {
                edits.add(Edit.replace(node.getStartPosition(), node.getLength(), "int"));
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(NumberLiteral node) {
            // add 'f' to floats
            String s = node.getToken().toLowerCase();
            if (FLOATING_POINT_LITERAL_VERIFIER.matcher(s).matches() && !s.endsWith("f") && !s.endsWith("d")) {
                edits.add(Edit.insert(node.getStartPosition() + node.getLength(), "f"));
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(MethodDeclaration node) {
            // add 'public' to methods with default visibility
            int accessModifiers = node.getModifiers() & ACCESS_MODIFIERS_MASK;
            if (accessModifiers == 0) {
                edits.add(Edit.insert(node.getStartPosition(), "public "));
            }
            return super.visit(node);
        }
    });
    return edits;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Edit(processing.mode.java.pdex.TextTransform.Edit) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 3 with Edit

use of processing.mode.java.pdex.TextTransform.Edit in project processing by processing.

the class SourceUtils method parseProgramImports.

public static List<Edit> parseProgramImports(CharSequence source, List<ImportStatement> outImports) {
    List<Edit> result = new ArrayList<>();
    Matcher matcher = IMPORT_REGEX.matcher(source);
    while (matcher.find()) {
        ImportStatement is = ImportStatement.parse(matcher.toMatchResult());
        outImports.add(is);
        int idx = matcher.start(1);
        int len = matcher.end(1) - idx;
        // Remove the import from the main program
        // Substitute with white spaces
        result.add(Edit.move(idx, len, 0));
        result.add(Edit.insert(0, "\n"));
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Edit(processing.mode.java.pdex.TextTransform.Edit)

Example 4 with Edit

use of processing.mode.java.pdex.TextTransform.Edit in project processing by processing.

the class SourceUtils method replaceColorRegex.

public static List<Edit> replaceColorRegex(CharSequence source) {
    final List<Edit> edits = new ArrayList<>();
    Matcher matcher = COLOR_TYPE_REGEX.matcher(source);
    while (matcher.find()) {
        int offset = matcher.start(1);
        edits.add(Edit.replace(offset, 5, "int"));
    }
    return edits;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Edit(processing.mode.java.pdex.TextTransform.Edit)

Example 5 with Edit

use of processing.mode.java.pdex.TextTransform.Edit in project processing by processing.

the class SourceUtils method replaceHexLiterals.

public static List<Edit> replaceHexLiterals(CharSequence source) {
    // Find all #[webcolor] and replace with 0xff[webcolor]
    // Should be 6 digits only.
    List<Edit> result = new ArrayList<>();
    Matcher matcher = HEX_LITERAL_REGEX.matcher(source);
    while (matcher.find()) {
        int offset = matcher.start(1);
        result.add(Edit.replace(offset, 1, "0xff"));
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Edit(processing.mode.java.pdex.TextTransform.Edit)

Aggregations

ArrayList (java.util.ArrayList)6 Edit (processing.mode.java.pdex.TextTransform.Edit)6 Matcher (java.util.regex.Matcher)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)1 SimpleType (org.eclipse.jdt.core.dom.SimpleType)1