Search in sources :

Example 1 with NodeWithVariables

use of com.github.javaparser.ast.nodeTypes.NodeWithVariables in project javaparser by javaparser.

the class LexicalPreservingPrinter method interpret.

private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) {
    LexicalDifferenceCalculator.CalculatedSyntaxModel calculatedSyntaxModel = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(csm, node);
    List<TokenTextElement> indentation = findIndentation(node);
    boolean pendingIndentation = false;
    for (CsmElement element : calculatedSyntaxModel.elements) {
        if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken) element).isNewLine())) {
            indentation.forEach(nodeText::addElement);
        }
        pendingIndentation = false;
        if (element instanceof LexicalDifferenceCalculator.CsmChild) {
            nodeText.addChild(((LexicalDifferenceCalculator.CsmChild) element).getChild());
        } else if (element instanceof CsmToken) {
            CsmToken csmToken = (CsmToken) element;
            nodeText.addToken(csmToken.getTokenType(), csmToken.getContent(node));
            if (csmToken.isNewLine()) {
                pendingIndentation = true;
            }
        } else if (element instanceof CsmMix) {
            CsmMix csmMix = (CsmMix) element;
            csmMix.getElements().forEach(e -> interpret(node, e, nodeText));
        } else {
            throw new UnsupportedOperationException(element.getClass().getSimpleName());
        }
    }
    // so they have to be handled in a special way
    if (node instanceof VariableDeclarator) {
        VariableDeclarator variableDeclarator = (VariableDeclarator) node;
        variableDeclarator.getParentNode().ifPresent(parent -> ((NodeWithVariables<?>) parent).getMaximumCommonType().ifPresent(mct -> {
            int extraArrayLevels = variableDeclarator.getType().getArrayLevel() - mct.getArrayLevel();
            for (int i = 0; i < extraArrayLevels; i++) {
                nodeText.addElement(new TokenTextElement(LBRACKET));
                nodeText.addElement(new TokenTextElement(RBRACKET));
            }
        }));
    }
    return nodeText;
}
Also used : java.util(java.util) AstObserver(com.github.javaparser.ast.observer.AstObserver) CsmMix(com.github.javaparser.printer.concretesyntaxmodel.CsmMix) TreeVisitor(com.github.javaparser.ast.visitor.TreeVisitor) Pair(com.github.javaparser.utils.Pair) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) DataKey(com.github.javaparser.ast.DataKey) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) Utils.assertNotNull(com.github.javaparser.utils.Utils.assertNotNull) Utils.decapitalize(com.github.javaparser.utils.Utils.decapitalize) PropagatingAstObserver(com.github.javaparser.ast.observer.PropagatingAstObserver) Method(java.lang.reflect.Method) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) TokenTypes.eolTokenKind(com.github.javaparser.TokenTypes.eolTokenKind) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) InvocationTargetException(java.lang.reflect.InvocationTargetException) GeneratedJavaParserConstants(com.github.javaparser.GeneratedJavaParserConstants) com.github.javaparser(com.github.javaparser) ParameterizedType(java.lang.reflect.ParameterizedType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) NodeWithVariables(com.github.javaparser.ast.nodeTypes.NodeWithVariables) ConcreteSyntaxModel(com.github.javaparser.printer.ConcreteSyntaxModel) Utils(com.github.javaparser.utils.Utils) Writer(java.io.Writer) Comment(com.github.javaparser.ast.comments.Comment) Comparator(java.util.Comparator) CsmMix(com.github.javaparser.printer.concretesyntaxmodel.CsmMix) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) NodeWithVariables(com.github.javaparser.ast.nodeTypes.NodeWithVariables)

Aggregations

com.github.javaparser (com.github.javaparser)1 GeneratedJavaParserConstants (com.github.javaparser.GeneratedJavaParserConstants)1 TokenTypes.eolTokenKind (com.github.javaparser.TokenTypes.eolTokenKind)1 DataKey (com.github.javaparser.ast.DataKey)1 Node (com.github.javaparser.ast.Node)1 NodeList (com.github.javaparser.ast.NodeList)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Comment (com.github.javaparser.ast.comments.Comment)1 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)1 NodeWithVariables (com.github.javaparser.ast.nodeTypes.NodeWithVariables)1 AstObserver (com.github.javaparser.ast.observer.AstObserver)1 ObservableProperty (com.github.javaparser.ast.observer.ObservableProperty)1 PropagatingAstObserver (com.github.javaparser.ast.observer.PropagatingAstObserver)1 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)1 TreeVisitor (com.github.javaparser.ast.visitor.TreeVisitor)1 ConcreteSyntaxModel (com.github.javaparser.printer.ConcreteSyntaxModel)1 CsmElement (com.github.javaparser.printer.concretesyntaxmodel.CsmElement)1 CsmMix (com.github.javaparser.printer.concretesyntaxmodel.CsmMix)1 CsmToken (com.github.javaparser.printer.concretesyntaxmodel.CsmToken)1 Pair (com.github.javaparser.utils.Pair)1