From c8996bedf0a4a78dc1b5caba8649e1d7a761506a Mon Sep 17 00:00:00 2001 From: "Ray J. Greenwell" Date: Tue, 9 Sep 2025 16:03:31 -0700 Subject: [PATCH] Support alternate indent widths for code generation. --- .../presents/tools/GenDObjectTask.java | 2 +- .../presents/tools/GenStreamableTask.java | 2 +- .../threerings/presents/tools/GenTask.java | 38 +++++++++++++++++++ .../threerings/presents/tools/SourceFile.java | 14 +++++-- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/tools/src/main/java/com/threerings/presents/tools/GenDObjectTask.java b/tools/src/main/java/com/threerings/presents/tools/GenDObjectTask.java index 1c0040365..160b55245 100644 --- a/tools/src/main/java/com/threerings/presents/tools/GenDObjectTask.java +++ b/tools/src/main/java/com/threerings/presents/tools/GenDObjectTask.java @@ -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 diff --git a/tools/src/main/java/com/threerings/presents/tools/GenStreamableTask.java b/tools/src/main/java/com/threerings/presents/tools/GenStreamableTask.java index eb4a66af6..84f765667 100644 --- a/tools/src/main/java/com/threerings/presents/tools/GenStreamableTask.java +++ b/tools/src/main/java/com/threerings/presents/tools/GenStreamableTask.java @@ -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) { diff --git a/tools/src/main/java/com/threerings/presents/tools/GenTask.java b/tools/src/main/java/com/threerings/presents/tools/GenTask.java index 5db005b0d..aa482df56 100644 --- a/tools/src/main/java/com/threerings/presents/tools/GenTask.java +++ b/tools/src/main/java/com/threerings/presents/tools/GenTask.java @@ -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 _filesets = Lists.newArrayList(); @@ -267,4 +303,6 @@ public abstract class GenTask extends Task protected boolean _checking; protected Set _modifiedPaths = Sets.newHashSet(); + + protected static final int STANDARD_INDENT = 4; } diff --git a/tools/src/main/java/com/threerings/presents/tools/SourceFile.java b/tools/src/main/java/com/threerings/presents/tools/SourceFile.java index bc2d47145..4a4ccdd61 100644 --- a/tools/src/main/java/com/threerings/presents/tools/SourceFile.java +++ b/tools/src/main/java/com/threerings/presents/tools/SourceFile.java @@ -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 _lines; + protected String _indent; protected int _nstart = -1, _nend = -1; protected int _mstart = -1, _mend = -1;