On 3/9/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

Anyhow with the proposed VelocityWhitespaceGobbleStructuredTemplates
you could achieve your desired output with either:

import com.myco.MyClass;
public Class AnotherClass {
#foreach( $t in $types )
  #if( $t.name.contains("_") )
      #text( "   Integer $t.name = 0;" )
      #if( $t.type == "static" )
           #text( "   // ignore static type $t.name" )
      #end
  #end
#end
}



But this requires that all output is encapsulated in #text() which pretty
much eliminates the ability to copy/past text from other sources such as
working java programs.



or embedding the directives with a alternate indentation level to
optically make it stand out from the schmoo:

import com.myco.MyClass;
public Class AnotherClass {
#foreach( $t in $types )
     #if( $t.name.contains("_") )
   Integer $t.name = 0;
         #if ($t.type == "static")
   // ignore static type $t.name
         #end
     #end
#end
}



This obscures the indenting of the template, making it difficult to
maintain, at least for those of us who work better with nicely formatted
files.



Your #setindent directive example would be hard for a parser to
understand.
More standard would be #setindent("   ").



Or perhaps #setindent(4) ## numeric value indicates amount of indent which
allows something like
#set ($indent = $indent +4)
#setindent($indent)




But creating complex Java code
with many indented blocks would clutter the template with these directives
and not contribute to readability compared to the two above example
approaches.
I would therefore vote against the proposal of the *SetindentDirective,
which
could be also done using:
a) code formatter as Jalopy


The same argument can be made for XML and HTML files, so why are we
discussing whitespace at all?  What about the case where the template is
generating sample Java code to be included in a <code>...</code> block in an
HTML document to be sent to a browser.  There is no chance to run this
through Jalopy.

We currently use both Jalopy and/or the Eclipse JDT code formatter to
reformat files that are generated for application code, but these have
problems from time to time and Jalopy aborts without formatting.  It is
easier to view the generated file if it has some reasonable indentation.



b) the the VelocityWhitespaceGobbleAll and an custom #setindent()
directive
  working together with a modified FileWriter



When you say "custom #setindent()" does this say that it is not a standard
part of Velocity?  I think this should be standard.



Currently I use the ugly workaround in my macros:

#macro( ClassMacro $indent $name $types )
${indent}public Class $name {
#**#foreach( $t in $types )##
#*  *#if( $t.name.contains("_") )##
${indent}  Integer $t.name = 0;
#*    *#if ($t.type == "static")##
${indent}  // ignore static type $t.name
#*    *#end##
#*  *#end##
#**#end##
${indent}}


As you say, "ugly".  Hopefully VelocityWhitespaceGobbleAll  would eliminate
the nead for the velocity comments #*  *# at beginning of every line.  It
would be nice if these tricks were not needed, but the ${indent} would be
tolerable if all the leading whitespace could be discarded and there was an
easy way to populate $indent with desired number of blanks.

import com.myco.MyClass;
public Class AnotherClass {
$set ($indent = $leadingBlanks.substring(0,$indentAmount))
#foreach( $t in $types )
    #if( $t.name.contains("_") )
        ${indent} Integer $t.name = 0;
        #if ($t.type == "static")
            ${indent} // ignore static type $t.name
        #end
    #end
#end
}

I think this might be equivalent to your suggestion to combine
VelocityWhitespaceGobbleAll  with a new #setindent(amount) directive.  If
Velocity is discarding all leading whitespace, and then injecting the amount
specified by #setindent() a lot of the formatting issue would e resolved and
in a lot of cases there would be no need for post processing the generated
output.

Michael

Reply via email to