Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public interface IJFormatter extends Closeable
*/
@NonNull
IJFormatter newline ();

/**
* @return the internal String used for new lines.
*/
String getNewLine();

/**
* Print a char into the stream
Expand Down
30 changes: 20 additions & 10 deletions jcodemodel/src/main/java/com/helger/jcodemodel/JTextBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ public boolean keepWhitespaces ()
*/
public @NonNull JTextBlock add (final @Nullable String line)
{
if (line != null)
if (line != null) {
formatLines (line).forEach (m_aLines::add);
}

return this;
}
Expand Down Expand Up @@ -207,6 +208,7 @@ public boolean keepWhitespaces ()
/**
* unmodifiable iterator over the internal lines
*/
@Override
public @NonNull Iterator <String> iterator ()
{
return Collections.unmodifiableList (m_aLines).iterator ();
Expand All @@ -217,9 +219,12 @@ public boolean keepWhitespaces ()
return m_aLines.stream ();
}

@Override
public void generate (@NonNull final IJFormatter f)
{
f.print (LIMITER).newline ();
StringBuilder sb = new StringBuilder();
String newLine = f.getNewLine();
sb.append(LIMITER).append(newLine);
final String indent = m_nIndentSize <= 0 || m_aLines.isEmpty () ? ""
: Character.toString (m_cIndentChar)
.repeat (m_nIndentSize);
Expand All @@ -244,8 +249,9 @@ public void generate (@NonNull final IJFormatter f)

for (String line : modifiedLines)
{
if (!firstLine)
f.newline ();
if (!firstLine) {
sb.append(newLine);
}

if (escapeFirstChar && !line.isEmpty ())
{
Expand All @@ -260,17 +266,18 @@ public void generate (@NonNull final IJFormatter f)
line = RegExHelper.stringReplacePattern (" $", line, "\\\\s");
line = RegExHelper.stringReplacePattern ("\t$", line, "\\\\t");
}
f.print (indent).print (line);
sb.append(indent).append(line);
firstLine = false;
lastEmpty = line.isEmpty ();
}
f.print (LIMITER);
sb.append(LIMITER);
if (!lastEmpty && m_nIndentSize > 0)
{
// if the last line is not empty, then the delimiter is not enough to enforce
// the indent. So we add a call after that.
f.print (".indent(").print (Integer.toString (m_nIndentSize)).print (")");
sb.append(".indent(").append (Integer.toString (m_nIndentSize)).append (")");
}
f.print(sb.toString());
}

/**
Expand All @@ -284,18 +291,21 @@ public void generate (@NonNull final IJFormatter f)
*/
static boolean requiresEscapeFirstChar (final boolean keepWhitespaces, final @NonNull List <String> lines)
{
if (!keepWhitespaces)
if (!keepWhitespaces) {
return false;
}

if (lines.isEmpty ())
if (lines.isEmpty ()) {
return false;
}

// no non-blank line that does not start with a space or tab
if (lines.stream ()
.filter (l -> !(l.isBlank () || l.startsWith (" ") || l.startsWith ("\t")))
.findAny ()
.isPresent ())
.isPresent ()) {
return false;
}

// if last line does not start with space/tab we don't need to escape, blank or
// not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ public JFormatter setJavaFeature (final int nJavaFeature)
m_nJavaFeature = nJavaFeature;
return this;
}

public String getNewLine()
{
return m_aPW.getNewLine();
}

public boolean isPrinting ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public SourcePrintWriter (@NonNull final Writer aWrappedWriter, @NonNull final S
super (aWrappedWriter);
m_sNewLine = sNewLine;
}

public String getNewLine()
{
return m_sNewLine;
}

private void _handleException (@NonNull final IOException ex, @NonNull final String sSource)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@
@Generated("com.helger.jcodemodel.JCodeModel")
public class TextBlocksExample {
public static final String EMPTY = """
""";
""";
public static final String ONE_LINE = """
a""";
a""";
public static final String TWO_LINES = """
a
b""";
a
b""";
public static final String TWO_DOUBLE_DQUOTES_LINES = """
""
"\"""";
""
"\"""";
public static final String TWO_TRIPLE_DQUOTES_LINES = """
""\"
""\"""";
""\"
""\"""";
public static final String FIVE_DQUOTES = """
""\""\"""";
""\""\"""";
public static final String TWO_FIVE_DQUOTES_LINES = """
""\"""
""\""\"""";
""\"""
""\""\"""";
public static final String ONE_LINE_ENDSPACE = """
a """;
a """;
public static final String ONE_LINE_ENDTAB = """
a """;
a """;
public static final String TWO_LINES_ENDTAB = """
a
b """;
a
b """;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
@Generated("com.helger.jcodemodel.JCodeModel")
public class TextBlocksKeepWhiteSpaces {
public static final String ONE_LINE_SPACES = """
\s a \s""";
\s a \s""";
public static final String ONE_LINE_TABS = """
\ta\t""";
\ta\t""";
public static final String SPACES_EMPTYLINE = """
\s
""";
\s
""";
public static final String THREE_LINES_SPACES = """
\sa\s
b\s
c \s""";
\sa\s
b\s
c \s""";
public static final String EMPTY_THEN_SPACED = """

\s a""";
\s a""";
public static final String TWO_EMPTY_LINES_2SPACES_3SPACES = """


\s\s
\s""";
\s\s
\s""";
}
2 changes: 1 addition & 1 deletion sh/cfg/tabspaces
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
echo "*.java filter=tabspace" > .git/info/attributes # tell git to apply the tabspace script on the *.java files
echo "*.xml filter=tabspace" >> .git/info/attributes # tell git to apply the tabspace script on the *.xml files
git config filter.tabspace.clean 'expand --tabs=2 --initial' # replace tabs with two spaces when commit of *.java files
git config filter.tabspace.smudge 'unexpand --tabs=2 --first-only' # replace double spaces with two tabs when checking a *.java file out
#git config filter.tabspace.smudge 'unexpand --tabs=2 --first-only' # replace double spaces with two tabs when checking a *.java file out