Search in sources :

Example 1 with TextContext

use of jetbrick.template.parser.grammer.JetTemplateParser.TextContext in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitBlock.

@Override
public Code visitBlock(BlockContext ctx) {
    int size = ctx.getChildCount();
    BlockCode code = scopeCode.createBlockCode(size);
    if (size == 0)
        return code;
    for (int i = 0; i < size; i++) {
        ParseTree node = ctx.children.get(i);
        Code c = node.accept(this);
        if (node instanceof TextContext) {
            // 文本节点
            TextCode textCode = (TextCode) c;
            if (trimDirectiveLine || trimDirectiveComments) {
                ParseTree prev = (i > 0) ? ctx.children.get(i - 1) : null;
                ParseTree next = (i < size - 1) ? ctx.children.get(i + 1) : null;
                boolean trimLeft;
                boolean keepLeftNewLine = false;
                if (prev == null) {
                    trimLeft = !(ctx.getParent() instanceof TemplateContext);
                } else {
                    trimLeft = prev instanceof DirectiveContext;
                    if (trimLeft) {
                        // inline directive, 对于一个内联的 #if, #for 指令,后面有要求保留一个 NewLine
                        // @see https://github.com/subchen/jetbrick-template/issues/25
                        ParserRuleContext directive = (ParserRuleContext) ((DirectiveContext) prev).getChild(0);
                        if (directive instanceof If_directiveContext || directive instanceof For_directiveContext) {
                            if (directive.getStart().getLine() == directive.getStop().getLine()) {
                                // 保留一个 NewLine
                                keepLeftNewLine = true;
                            }
                        }
                    }
                }
                boolean trimRight;
                if (next == null) {
                    trimRight = !(ctx.getParent() instanceof TemplateContext);
                } else {
                    trimRight = (next instanceof DirectiveContext);
                }
                // trim 指令两边的注释
                if (trimDirectiveComments) {
                    textCode.trimComments(trimLeft, trimRight, commentsPrefix, commentsSuffix);
                }
                // trim 指令两边的空白内容
                if (trimDirectiveLine) {
                    textCode.trimEmptyLine(trimLeft, trimRight, keepLeftNewLine);
                }
                // trim 掉 #tag 和 #macro 指令最后一个多余的 '\n'
                if (next == null) {
                    if (ctx.getParent() instanceof Tag_directiveContext || ctx.getParent() instanceof Macro_directiveContext) {
                        textCode.trimLastNewLine();
                    }
                }
            }
            if (!textCode.isEmpty()) {
                // 如果有相同内容的Text,则从缓存中读取
                TextCode old = textCache.get(textCode.getText());
                if (old == null) {
                    old = textCode;
                    textCache.put(textCode.getText(), textCode);
                    // add text into field
                    tcc.addField(textCode.getId(), textCode.getText());
                }
                code.addLine(old.toString());
            }
        } else {
            code.addChild(c);
        }
    }
    return code;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) For_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.For_directiveContext) TemplateContext(jetbrick.template.parser.grammer.JetTemplateParser.TemplateContext) ScopeCode(jetbrick.template.parser.code.ScopeCode) BlockCode(jetbrick.template.parser.code.BlockCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) TemplateClassCode(jetbrick.template.parser.code.TemplateClassCode) TextCode(jetbrick.template.parser.code.TextCode) ForExpressionCode(jetbrick.template.parser.code.ForExpressionCode) SegmentCode(jetbrick.template.parser.code.SegmentCode) MacroCode(jetbrick.template.parser.code.MacroCode) Code(jetbrick.template.parser.code.Code) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) TagCode(jetbrick.template.parser.code.TagCode) BlockCode(jetbrick.template.parser.code.BlockCode) If_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.If_directiveContext) TextContext(jetbrick.template.parser.grammer.JetTemplateParser.TextContext) Macro_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Macro_directiveContext) DirectiveContext(jetbrick.template.parser.grammer.JetTemplateParser.DirectiveContext) TextCode(jetbrick.template.parser.code.TextCode) Tag_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Tag_directiveContext) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

BlockCode (jetbrick.template.parser.code.BlockCode)1 Code (jetbrick.template.parser.code.Code)1 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)1 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)1 MacroCode (jetbrick.template.parser.code.MacroCode)1 ScopeCode (jetbrick.template.parser.code.ScopeCode)1 SegmentCode (jetbrick.template.parser.code.SegmentCode)1 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)1 TagCode (jetbrick.template.parser.code.TagCode)1 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)1 TextCode (jetbrick.template.parser.code.TextCode)1 DirectiveContext (jetbrick.template.parser.grammer.JetTemplateParser.DirectiveContext)1 For_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.For_directiveContext)1 If_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.If_directiveContext)1 Macro_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Macro_directiveContext)1 Tag_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Tag_directiveContext)1 TemplateContext (jetbrick.template.parser.grammer.JetTemplateParser.TemplateContext)1 TextContext (jetbrick.template.parser.grammer.JetTemplateParser.TextContext)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1