Support alternate indent widths for code generation.
This commit is contained in:
@@ -62,7 +62,7 @@ public class GenDObjectTask extends GenTask
|
||||
}
|
||||
|
||||
// slurp our source file into newline separated strings
|
||||
SourceFile sfile = new SourceFile();
|
||||
SourceFile sfile = new SourceFile(_indentWidth);
|
||||
sfile.readFrom(source);
|
||||
|
||||
// generate our fields section and our methods section
|
||||
|
||||
@@ -117,7 +117,7 @@ public class GenStreamableTask extends GenTask
|
||||
readbuf.append(READ_CLOSE);
|
||||
writebuf.append(WRITE_CLOSE);
|
||||
|
||||
SourceFile sfile = new SourceFile();
|
||||
SourceFile sfile = new SourceFile(_indentWidth);
|
||||
try {
|
||||
sfile.readFrom(source);
|
||||
} catch (IOException ioe) {
|
||||
|
||||
@@ -7,12 +7,14 @@ package com.threerings.presents.tools;
|
||||
|
||||
import static com.google.common.base.Charsets.UTF_8;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -91,6 +93,14 @@ public abstract class GenTask extends Task
|
||||
_checking = checking;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure an alternate indent width to the normal 4-space indent.
|
||||
*/
|
||||
public void setIndentWidth (int width)
|
||||
{
|
||||
_indentWidth = width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the actual work of the task.
|
||||
*/
|
||||
@@ -203,6 +213,7 @@ public abstract class GenTask extends Task
|
||||
{
|
||||
Reader reader =
|
||||
new InputStreamReader(getClass().getClassLoader().getResourceAsStream(template), UTF_8);
|
||||
reader = reindent(reader);
|
||||
return convertEols(Mustache.compiler().escapeHTML(false).compile(reader).execute(data));
|
||||
}
|
||||
|
||||
@@ -254,8 +265,33 @@ public abstract class GenTask extends Task
|
||||
return str.replace("\n", EOL);
|
||||
}
|
||||
|
||||
protected Reader reindent (Reader reader)
|
||||
throws IOException
|
||||
{
|
||||
if (_indentWidth == STANDARD_INDENT) return reader;
|
||||
|
||||
StringBuilder buf = new StringBuilder();
|
||||
try (BufferedReader br = new BufferedReader(reader)) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
int spaces = 0, len = line.length();
|
||||
while (spaces < len && line.charAt(spaces) == ' ') spaces++;
|
||||
if (spaces > 0) {
|
||||
int newSpaces = ((spaces / STANDARD_INDENT) * _indentWidth) +
|
||||
((spaces % STANDARD_INDENT) % _indentWidth);
|
||||
for (int ii = 0; ii < newSpaces; ++ii) buf.append(' ');
|
||||
}
|
||||
buf.append(line, spaces, len);
|
||||
buf.append('\n');
|
||||
}
|
||||
}
|
||||
return new StringReader(buf.toString());
|
||||
}
|
||||
|
||||
protected static String EOL = System.getProperty("line.separator");
|
||||
|
||||
protected int _indentWidth = STANDARD_INDENT;
|
||||
|
||||
/** A list of filesets that contain java source to be processed. */
|
||||
protected List<FileSet> _filesets = Lists.newArrayList();
|
||||
|
||||
@@ -267,4 +303,6 @@ public abstract class GenTask extends Task
|
||||
|
||||
protected boolean _checking;
|
||||
protected Set<String> _modifiedPaths = Sets.newHashSet();
|
||||
|
||||
protected static final int STANDARD_INDENT = 4;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,11 @@ import com.samskivert.util.StringUtil;
|
||||
*/
|
||||
public class SourceFile
|
||||
{
|
||||
public SourceFile (int indent)
|
||||
{
|
||||
_indent = StringUtil.spaces(indent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the code from the supplied source file.
|
||||
*/
|
||||
@@ -132,9 +137,9 @@ public class SourceFile
|
||||
if (!StringUtil.isBlank(prev) && !prev.equals("{")) {
|
||||
bout.newLine();
|
||||
}
|
||||
writeln(bout, " " + FIELDS_START);
|
||||
writeln(bout, _indent + FIELDS_START);
|
||||
bout.write(fsection);
|
||||
writeln(bout, " " + FIELDS_END);
|
||||
writeln(bout, _indent + FIELDS_END);
|
||||
if (!StringUtil.isBlank(safeGetLine(_nend))) {
|
||||
bout.newLine();
|
||||
}
|
||||
@@ -150,9 +155,9 @@ public class SourceFile
|
||||
if (!StringUtil.isBlank(safeGetLine(_mstart-1))) {
|
||||
bout.newLine();
|
||||
}
|
||||
writeln(bout, " " + METHODS_START);
|
||||
writeln(bout, _indent + METHODS_START);
|
||||
bout.write(msection);
|
||||
writeln(bout, " " + METHODS_END);
|
||||
writeln(bout, _indent + METHODS_END);
|
||||
String next = safeGetLine(_mend);
|
||||
if (!StringUtil.isBlank(next) && !next.equals("}")) {
|
||||
bout.newLine();
|
||||
@@ -246,6 +251,7 @@ public class SourceFile
|
||||
}
|
||||
|
||||
protected List<String> _lines;
|
||||
protected String _indent;
|
||||
|
||||
protected int _nstart = -1, _nend = -1;
|
||||
protected int _mstart = -1, _mend = -1;
|
||||
|
||||
Reference in New Issue
Block a user