You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2016/09/11 23:12:58 UTC

svn commit: r1760282 - in /velocity/engine/trunk/velocity-engine-core/src: main/java/org/apache/velocity/runtime/directive/ main/java/org/apache/velocity/runtime/parser/node/ main/parser/ test/java/org/apache/velocity/test/ test/java/org/apache/velocit...

Author: cbrisson
Date: Sun Sep 11 23:12:57 2016
New Revision: 1760282

URL: http://svn.apache.org/viewvc?rev=1760282&view=rev
Log:
fix a few marginal indentatino bugs

Modified:
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
    velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/SpaceGobblingTestCase.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity667TestCase.java
    velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_smart.vtl.NONE
    velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_structured.vtl.NONE
    velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/macro.vtl.NONE
    velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.BC
    velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.LINES
    velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.NONE
    velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.STRUCTURED
    velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/ugly1.vtl

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java Sun Sep 11 23:12:57 2016
@@ -28,9 +28,11 @@ import org.apache.velocity.exception.Tem
 import org.apache.velocity.exception.VelocityException;
 import org.apache.velocity.runtime.Renderable;
 import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.runtime.RuntimeConstants.SpaceGobbling;
 import org.apache.velocity.runtime.RuntimeServices;
 import org.apache.velocity.runtime.parser.ParserTreeConstants;
 import org.apache.velocity.runtime.parser.Token;
+import org.apache.velocity.runtime.parser.node.ASTDirective;
 import org.apache.velocity.runtime.parser.node.Node;
 import org.apache.velocity.util.StringUtils;
 
@@ -174,28 +176,56 @@ public class RuntimeMacro extends Direct
 
     /**
      * It is probably quite rare that we need to render the macro literal
-     * so do it only on-demand and then cache the value. This tactic helps to
-     * reduce memory usage a bit.
+     * but since we won't keep the tokens in memory, we need to calculate it
+     * at parsing time.
      */
     private String getLiteral()
     {
+        SpaceGobbling spaceGobbling = rsvc.getSpaceGobbling();
+        ASTDirective directive = (ASTDirective)node;
+
+        String morePrefix = directive.getMorePrefix();
+
         if (literal == null)
         {
             StringBuilder buffer = new StringBuilder();
             Token t = node.getFirstToken();
 
+            /* avoid outputting twice the prefix and the 'MORE' prefix,
+             * but still display the prefix in the cases where the ASTDirective would hide it */
+            int pos = -1;
             while (t != null && t != node.getLastToken())
             {
-                buffer.append(t.image);
+                if (pos == -1) pos = t.image.lastIndexOf('#');
+                if (pos != -1)
+                {
+                    buffer.append(t.image.substring(pos));
+                    pos = 0;
+                }
+                else if (morePrefix.length() == 0 && spaceGobbling.compareTo(SpaceGobbling.LINES) >= 0)
+                {
+                    buffer.append(t.image);
+                }
                 t = t.next;
             }
 
             if (t != null)
             {
-                buffer.append(t.image);
+                if (pos == -1) pos = t.image.lastIndexOf('#');
+                if (pos != -1)
+                {
+                    buffer.append(t.image.substring(pos));
+                }
             }
 
             literal = buffer.toString();
+            /* avoid outputting twice the postfix, but still display it in the cases
+             * where the ASTDirective would hide it */
+            String postfix = directive.getPostfix();
+            if ((morePrefix.length() > 0 || spaceGobbling == SpaceGobbling.NONE) && literal.endsWith(postfix))
+            {
+                literal = literal.substring(0, literal.length() - postfix.length());
+            }
         }
         return literal;
     }

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java Sun Sep 11 23:12:57 2016
@@ -256,11 +256,6 @@ public class ASTDirective extends Simple
         this.postfix = postfix;
     }
 
-    public int getDirectiveType()
-    {
-        return directive.getType();
-    }
-
     /**
      * get indentation postfix
      * @return indentation prefix
@@ -271,6 +266,20 @@ public class ASTDirective extends Simple
     }
 
     /**
+     * more prefix getter
+     * @return more prefix
+     */
+    public String getMorePrefix()
+    {
+        return morePrefix;
+    }
+
+    public int getDirectiveType()
+    {
+        return directive.getType();
+    }
+
+    /**
      * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
      */
     public boolean render( InternalContextAdapter context, Writer writer)

Modified: velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt Sun Sep 11 23:12:57 2016
@@ -740,7 +740,11 @@ MORE :
             if ( debugPrint )
                 System.out.print( "$  : going to " + PRE_REFERENCE );
 
-            stateStackPush();
+            /* do not push PRE states */
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            {
+                stateStackPush();
+            }
             SwitchTo(PRE_REFERENCE);
         }
     }
@@ -765,7 +769,11 @@ MORE :
             if ( debugPrint )
                 System.out.print( "$!  : going to " + PRE_REFERENCE );
 
-            stateStackPush();
+            /* do not push PRE states */
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            {
+                stateStackPush();
+            }
             SwitchTo(PRE_REFERENCE);
         }
     }
@@ -775,7 +783,11 @@ MORE :
        if (!inComment)
        {
            inComment = true;
-           stateStackPush();
+            /* do not push PRE states */
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            {
+                stateStackPush();
+            }
            SwitchTo( IN_TEXTBLOCK );
        }
     }
@@ -784,10 +796,14 @@ MORE :
     {
     	if (!inComment)
     	{
-	        input_stream.backup(1);
-	        inComment = true;
+	    input_stream.backup(1);
+	    inComment = true;
+            /* do not push PRE states */
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            {
 	        stateStackPush();
-	        SwitchTo( IN_FORMAL_COMMENT);
+            }
+	    SwitchTo( IN_FORMAL_COMMENT);
     	}
     }
 
@@ -795,9 +811,13 @@ MORE :
     {
     	if (!inComment)
     	{
-	        inComment=true;
+	    inComment=true;
+            /* do not push PRE states */
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            {
 	        stateStackPush();
-	        SwitchTo( IN_MULTI_LINE_COMMENT );
+            }
+	    SwitchTo( IN_MULTI_LINE_COMMENT );
     	}
     }
 
@@ -821,7 +841,11 @@ MORE :
             if ( debugPrint )
                 System.out.print("# :  going to " + DIRECTIVE );
 
-            stateStackPush();
+            /* do not push PRE states */
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            {
+                stateStackPush();
+            }
             SwitchTo(PRE_DIRECTIVE);
         }
   }
@@ -1595,6 +1619,12 @@ boolean Directive() :
          <SINGLE_LINE_COMMENT_START> [<SINGLE_LINE_COMMENT>]
       )
     )* (<WHITESPACE> | <NEWLINE>)* <RPAREN>
+     )
+    |
+    {
+        token_source.stateStackPop();
+    }
+    )
      [
        LOOKAHEAD(2) ( [ ( t = <WHITESPACE> ) ] ( u = <NEWLINE> ) )
        {
@@ -1611,12 +1641,6 @@ boolean Directive() :
            t = u = null;
        }
      ]
-     )
-    |
-    {
-        token_source.stateStackPop();
-    }
-    )
     {
         if (d != null)
         {

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/SpaceGobblingTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/SpaceGobblingTestCase.java?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/SpaceGobblingTestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/SpaceGobblingTestCase.java Sun Sep 11 23:12:57 2016
@@ -123,7 +123,7 @@ public class SpaceGobblingTestCase exten
         writer.flush();
         writer.close();
 
-        if (false)//!isMatch(RESULT_DIR, COMPARE_DIR, templateFile, mode.toString(), mode.toString()))
+        if (!isMatch(RESULT_DIR, COMPARE_DIR, templateFile, mode.toString(), mode.toString()))
         {
             String result = getFileContents(RESULT_DIR, templateFile, mode.toString());
             String compare = getFileContents(COMPARE_DIR, templateFile, mode.toString());

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity667TestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity667TestCase.java?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity667TestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity667TestCase.java Sun Sep 11 23:12:57 2016
@@ -33,7 +33,7 @@ public class Velocity667TestCase extends
   
     public void test667()
     {
-          assertEvalExceptionAt("#macro", 1, 1);
-          assertEvalExceptionAt("#macro #macro", 1, 1);
+          assertEvalExceptionAt("#macro", 1, 6);
+          assertEvalExceptionAt("#macro #macro", 1, 7);
     }
 }

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_smart.vtl.NONE
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_smart.vtl.NONE?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_smart.vtl.NONE (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_smart.vtl.NONE Sun Sep 11 23:12:57 2016
@@ -6,7 +6,8 @@
       <td>
     
         first cell
-          </td>
+    
+      </td>
   
       <td>
     

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_structured.vtl.NONE
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_structured.vtl.NONE?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_structured.vtl.NONE (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/foreach_structured.vtl.NONE Sun Sep 11 23:12:57 2016
@@ -6,7 +6,8 @@
           <td>
             
               first cell
-                      </td>
+            
+          </td>
         
           <td>
             

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/macro.vtl.NONE
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/macro.vtl.NONE?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/macro.vtl.NONE (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/macro.vtl.NONE Sun Sep 11 23:12:57 2016
@@ -22,6 +22,7 @@ line4
   
 block foo
 
+
   </b>
 </a>
 
@@ -39,5 +40,6 @@ line4
       
 block foo
 
-      </b>
+    
+  </b>
 </a>

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.BC
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.BC?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.BC (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.BC Sun Sep 11 23:12:57 2016
@@ -10,6 +10,8 @@
  #$foo$
  #$foo#
  #$foo 
+ $#foo()$
+ $#foo()#
  $#@foo()hop#end$
  $#@foo()hop#end#
  $#@foo()hop#end 

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.LINES
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.LINES?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.LINES (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.LINES Sun Sep 11 23:12:57 2016
@@ -10,6 +10,8 @@
  #$foo$
  #$foo#
  #$foo 
+ $#foo()$
+ $#foo()#
  $#@foo()hop#end$
  $#@foo()hop#end#
  $#@foo()hop#end 

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.NONE
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.NONE?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.NONE (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.NONE Sun Sep 11 23:12:57 2016
@@ -10,6 +10,8 @@
  #$foo$
  #$foo#
  #$foo 
+ $#foo()$
+ $#foo()#
  $#@foo()hop#end$
  $#@foo()hop#end#
  $#@foo()hop#end 

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.STRUCTURED
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.STRUCTURED?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.STRUCTURED (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/compare/ugly1.vtl.STRUCTURED Sun Sep 11 23:12:57 2016
@@ -10,6 +10,8 @@
  #$foo$
  #$foo#
  #$foo 
+ $#foo()$
+ $#foo()#
  $#@foo()hop#end$
  $#@foo()hop#end#
  $#@foo()hop#end 

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/ugly1.vtl
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/ugly1.vtl?rev=1760282&r1=1760281&r2=1760282&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/ugly1.vtl (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/gobbling/ugly1.vtl Sun Sep 11 23:12:57 2016
@@ -10,6 +10,8 @@
  #$foo$
  #$foo#
  #$foo 
+ $#foo()$
+ $#foo()#
  $#@foo()hop#end$
  $#@foo()hop#end#
  $#@foo()hop#end