Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c5c7d5d
added formatteroption to store the options.
Jun 25, 2026
837d2b8
reuse direct string for indent, makes more sense.
Jun 25, 2026
ae67231
added some tests and completed implementation.
Jul 2, 2026
df0e819
changed names, added wrapping of method declaration block ("bracket")
Jul 2, 2026
a98dd00
minor refactors
Jul 2, 2026
bd8fe45
added wrap of name
Jul 3, 2026
7875398
minor refactor
Jul 3, 2026
9da58fd
re hieraching JVars.
Jul 11, 2026
7ee72e9
moved IJDeclaration implem from JVar to JFieldVar/JBlockVar
Jul 11, 2026
e63f2ff
renamed wrapping option elements, added samevar for field and block vars
Jul 13, 2026
2578aa4
added dimension for localvar declaration
Jul 13, 2026
77a99b7
minor fix doc
Jul 13, 2026
6c21737
reworked JForLoop to have better java compliance
Jul 14, 2026
f97fd73
refactor options to settings
Jul 15, 2026
9f28b88
doc update
Jul 15, 2026
ec828b3
adding wrap to catch clause type, reworking catch structure
Jul 15, 2026
c979807
fixed genericprint , added variables wrap options
Jul 17, 2026
d6c7b42
refactor internal to have a dedicated WriteContext handling writes
Jul 18, 2026
6e2b0fa
added formatter settings
Jul 18, 2026
cbf28ed
removed intermediate code, made call to actual new one
Jul 18, 2026
2141aa7
moved jformatter fields in the context.
Jul 19, 2026
5eda1a4
Merge branch 'master' of github.com:phax/jcodemodel into formatterOpt…
Jul 20, 2026
891fe2b
added array init + updated tests
Jul 20, 2026
89eea1d
fix array init generation
Jul 20, 2026
de74333
more array init
Jul 20, 2026
4902102
array init tests and format tests
Jul 20, 2026
6280e9a
unit test for the context layers
Jul 20, 2026
c282035
applying formatter
Jul 21, 2026
14e9591
more meddling with auto formatter
Jul 21, 2026
7386d88
applying formatter
Jul 21, 2026
7f07089
JArray.element to list
Jul 22, 2026
62651e7
NullWriter IOException back
Jul 22, 2026
a8b1df6
JForEachVar constr isFinal
Jul 22, 2026
5fc3184
wrap.wrapBeforeSep
Jul 22, 2026
0fc00e2
phelger spaces2 + formatter only contains jcm settings :P
Jul 22, 2026
fdd54ba
yes mo_ma
Jul 22, 2026
2836a53
JForEach aMods.isFinal ()
Jul 22, 2026
a1854a3
jforloop initVar not set to null + renamed to m_aInitVar
Jul 22, 2026
187c069
JForLoop runtimeException -> IllegalStateException
Jul 22, 2026
db938dc
m_aContextLayer
Jul 22, 2026
cd5fbf9
"formatterSettings"
Jul 22, 2026
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
418 changes: 275 additions & 143 deletions jcodemodel/meta/formatter/eclipse/jcodemodel-eclipse-cleanup.xml

Large diffs are not rendered by default.

519 changes: 78 additions & 441 deletions jcodemodel/meta/formatter/eclipse/jcodemodel-eclipse-formatter.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* (by often taking other {@link IJExpression}s as parameters. For example, you can build "5+2" by
* <code>JExpr.lit(5).add(JExpr.lit(2))</code>
*/
public interface IJExpression extends IJGenerable
public interface IJExpression extends IVariableInitializer
{
/**
* @return <code>-[this]" from "[this]</code>.
Expand Down
86 changes: 81 additions & 5 deletions jcodemodel/src/main/java/com/helger/jcodemodel/IJFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@

import org.jspecify.annotations.NonNull;

import com.helger.jcodemodel.writer.FormatterSettings;
import com.helger.jcodemodel.writer.settings.Wrap.ListWrapping;

/**
* Base interface for JFormatter.
*
Expand Down Expand Up @@ -72,15 +75,27 @@ public interface IJFormatter extends Closeable
* @return this for chaining
*/
@NonNull
IJFormatter indent ();
default IJFormatter indent ()
{
return indent (1);
}

@NonNull
IJFormatter indent (int nb);

/**
* Decrement the indentation level.
*
* @return this for chaining
*/
@NonNull
IJFormatter outdent ();
default IJFormatter outdent ()
{
return outdent (1);
}

@NonNull
IJFormatter outdent (int nb);

/**
* Print a new line into the stream
Expand All @@ -89,11 +104,16 @@ public interface IJFormatter extends Closeable
*/
@NonNull
IJFormatter newline ();

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

/**
* @return the size of the line written, so far.
*/
int currentLineSize ();

/**
* Print a char into the stream
Expand Down Expand Up @@ -121,6 +141,11 @@ default IJFormatter printCloseTypeArgs ()
@NonNull
IJFormatter print (@NonNull String sStr);

default IJFormatter println (@NonNull String sStr)
{
return print (sStr).print (getNewLine ());
}

/**
* Print a type name.
* <p>
Expand Down Expand Up @@ -153,6 +178,13 @@ default IJFormatter type (@NonNull final AbstractJType aType)
@NonNull
IJFormatter var (@NonNull JVar aVar);

default IJFormatter vars (@NonNull final Collection <? extends JVar> aList)
{
return vars (aList, null);
}

IJFormatter vars (@NonNull final Collection <? extends JVar> aList, ListWrapping wrapping);

/**
* Print an identifier
*
Expand Down Expand Up @@ -182,7 +214,13 @@ default IJFormatter type (@NonNull final AbstractJType aType)
* @return this for chaining
*/
@NonNull
IJFormatter generable (@NonNull final Collection <? extends IJGenerable> aList);
default IJFormatter generable (@NonNull final Collection <? extends IJGenerable> aList)
{
return generable (aList, ",", null);
}

@NonNull
IJFormatter generable (@NonNull final Collection <? extends IJGenerable> aList, String separator, ListWrapping wrapping);

/**
* Cause the {@link IJStatement} to generate source for itself
Expand All @@ -203,4 +241,42 @@ default IJFormatter type (@NonNull final AbstractJType aType)
*/
@NonNull
IJFormatter declaration (@NonNull IJDeclaration aObj);

FormatterSettings settings ();

public interface IContextCloser extends AutoCloseable
{
@Override
void close ();

IContextCloser persistOnClose (boolean b);

default IContextCloser persistOnClose ()
{
return persistOnClose (true);
}

/// shortcut to set the persist then close. Since the close is idempotent, this
/// call may have no result.
default void commit ()
{
persistOnClose ().close ();
}

/// shortcut to unset the persist then close. Since the close is idempotent,
/// this call may have no result.
default void rollback ()
{
persistOnClose (false).close ();
}

/// @return true after #close has been called at least once.
boolean isClosed ();

/// get the actual buffer's value
String value ();
}

/// create a new buffer to write data into.
IContextCloser addContextLayer ();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.helger.jcodemodel;

/// marker for classes that are allowed to be used as variable initializers.
public interface IVariableInitializer extends IJGenerable {

}
85 changes: 56 additions & 29 deletions jcodemodel/src/main/java/com/helger/jcodemodel/JBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import com.helger.annotation.Nonnegative;
import com.helger.base.enforce.ValueEnforcer;
import com.helger.jcodemodel.vars.JBlockVar;

/**
* A block of Java code, which may contain statements and local declarations.
Expand Down Expand Up @@ -219,8 +220,8 @@ public int pos ()
@Nonnegative
public int pos (@Nonnegative final int nNewPos)
{
ValueEnforcer.isTrue (nNewPos >= 0 && nNewPos <= m_aContentList.size (),
() -> "Illegal position provided: " + nNewPos);
ValueEnforcer.isTrue ( (nNewPos >= 0) && (nNewPos <= m_aContentList.size ()),
() -> "Illegal position provided: " + nNewPos);

final int nOldPos = m_nPos;
m_nPos = nNewPos;
Expand Down Expand Up @@ -255,7 +256,7 @@ public int size ()
* @return Newly generated {@link JVar}
*/
@NonNull
public JVar decl (final AbstractJType aType, @NonNull final String sName)
public JBlockVar decl (final AbstractJType aType, @NonNull final String sName)
{
return decl (JMod.NONE, aType, sName, null);
}
Expand All @@ -273,7 +274,7 @@ public JVar decl (final AbstractJType aType, @NonNull final String sName)
* @return Newly generated {@link JVar}
*/
@NonNull
public JVar decl (final int nMods, @NonNull final AbstractJType aType, @NonNull final String sName)
public JBlockVar decl (final int nMods, @NonNull final AbstractJType aType, @NonNull final String sName)
{
return decl (nMods, aType, sName, null);
}
Expand All @@ -291,7 +292,10 @@ public JVar decl (final int nMods, @NonNull final AbstractJType aType, @NonNull
* @return Newly generated {@link JVar}
*/
@NonNull
public JVar decl (@NonNull final AbstractJType aType, @NonNull final String sName, @Nullable final IJExpression aInit)
public JBlockVar decl (
@NonNull final AbstractJType aType,
@NonNull final String sName,
@Nullable final IVariableInitializer aInit)
{
return decl (JMod.NONE, aType, sName, aInit);
}
Expand All @@ -311,12 +315,13 @@ public JVar decl (@NonNull final AbstractJType aType, @NonNull final String sNam
* @return Newly generated {@link JVar}
*/
@NonNull
public JVar decl (final int nMods,
final AbstractJType aType,
@NonNull final String sName,
@Nullable final IJExpression aInit)
public JBlockVar decl (
final int nMods,
final AbstractJType aType,
@NonNull final String sName,
@Nullable final IVariableInitializer aInit)
{
final JVar v = new JVar (JMods.forVar (nMods), aType, sName, aInit);
final JBlockVar v = new JBlockVar (JMods.forVar (nMods), aType, sName, aInit);
internalInsert (v);
return v;
}
Expand Down Expand Up @@ -562,7 +567,9 @@ public JBlock addSingleLineComment ()
public JBlock addSingleLineComment (@Nullable final String sComment)
{
if (sComment != null)
{
internalInsert (new JSingleLineCommentStatement (sComment));
}
return this;
}

Expand Down Expand Up @@ -608,9 +615,10 @@ public JConditional _if (@NonNull final IJExpression aTestExpr, @NonNull final I
* @return Newly generated {@link JConditional} statement
*/
@NonNull
public JConditional _if (@NonNull final IJExpression aTestExpr,
@NonNull final IJStatement aThen,
@NonNull final IJStatement aElse)
public JConditional _if (
@NonNull final IJExpression aTestExpr,
@NonNull final IJStatement aThen,
@NonNull final IJStatement aElse)
{
final JConditional aCond = new JConditional (aTestExpr);
aCond._then ().add (aThen);
Expand Down Expand Up @@ -745,8 +753,12 @@ public JThrow _throw (@NonNull final AbstractJClass aThrowClass, @NonNull final
{
final JInvocation aCtor = JExpr._new (aThrowClass);
if (aParams != null)
{
for (final IJExpression aParam : aParams)
{
aCtor.arg (aParam);
}
}
return _throw (aCtor);
}

Expand Down Expand Up @@ -883,9 +895,10 @@ public JBlock block (final boolean bBracesRequired, final boolean bIndentRequire
* @return Newly generated enhanced For statement per j2se 1.5 specification
*/
@NonNull
public JForEach forEach (@NonNull final AbstractJType aVarType,
@NonNull final String sName,
@NonNull final IJExpression aCollection)
public JForEach forEach (
@NonNull final AbstractJType aVarType,
@NonNull final String sName,
@NonNull final IJExpression aCollection)
{
return forEach (0, aVarType, sName, aCollection);
}
Expand All @@ -904,10 +917,11 @@ public JForEach forEach (@NonNull final AbstractJType aVarType,
* @return Newly generated enhanced For statement per j2se 1.5 specification
*/
@NonNull
public JForEach forEach (final int nMods,
@NonNull final AbstractJType aVarType,
@NonNull final String sName,
@NonNull final IJExpression aCollection)
public JForEach forEach (
final int nMods,
@NonNull final AbstractJType aVarType,
@NonNull final String sName,
@NonNull final IJExpression aCollection)
{
return internalInsert (new JForEach (JMods.forVar (nMods), aVarType, sName, aCollection));
}
Expand Down Expand Up @@ -945,6 +959,7 @@ public IJStatement directStatement (@NonNull final String sSource)
return aStatement;
}

@Override
public void generate (@NonNull final IJFormatter f)
{
if (m_bVirtualBlock)
Expand All @@ -960,36 +975,48 @@ public void generate (@NonNull final IJFormatter f)
f.newline ();
}
if (m_bIndentRequired)
{
f.indent ();
}
generateBody (f);
if (m_bIndentRequired)
{
f.outdent ();
}
if (m_bBracesRequired)
{
f.print ('}');
}
}
}

protected void generateBody (@NonNull final IJFormatter f)
{
for (final IJObject aContentElement : m_aContentList)
{
if (aContentElement instanceof IJDeclaration)
f.declaration ((IJDeclaration) aContentElement);
if (aContentElement instanceof IJDeclaration ijd)
{
f.declaration (ijd);
}
else if (aContentElement instanceof IJStatement ijs)
{
f.statement (ijs);
}
else
if (aContentElement instanceof IJStatement)
f.statement ((IJStatement) aContentElement);
else
{
// For lambda expressions in JLambdaBlock
f.generable ((IJGenerable) aContentElement);
}
{
// For lambda expressions in JLambdaBlock
f.generable ((IJGenerable) aContentElement);
}
}
}

@Override
public void state (@NonNull final IJFormatter f)
{
f.generable (this);
if (m_bBracesRequired)
{
f.newline ();
}
}
}
Loading