- [#6234] Add newline configuration to code generator - [#8138] Add indentation configuration to code generator
This commit is contained in:
parent
777c6b039f
commit
83bfd238b6
@ -124,6 +124,8 @@ abstract class AbstractGenerator implements Generator {
|
||||
boolean generateEmptyCatalogs = false;
|
||||
boolean generateEmptySchemas = false;
|
||||
boolean generatePrimaryKeyTypes = false;
|
||||
String generateNewline = "\n";
|
||||
String generateIndentation;
|
||||
|
||||
protected GeneratorStrategyWrapper strategy;
|
||||
protected String targetEncoding = "UTF-8";
|
||||
@ -893,6 +895,26 @@ abstract class AbstractGenerator implements Generator {
|
||||
this.generatePrimaryKeyTypes = generatePrimaryKeyTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateNewline() {
|
||||
return generateNewline;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGenerateNewline(String newline) {
|
||||
this.generateNewline = newline;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateIndentation() {
|
||||
return generateIndentation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGenerateIndentation(String indentation) {
|
||||
this.generateIndentation = indentation;
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
@Override
|
||||
|
||||
@ -718,6 +718,10 @@ public class GenerationTool {
|
||||
generator.setGenerateEmptySchemas(g.getGenerate().isEmptySchemas());
|
||||
if (g.getGenerate().isPrimaryKeyTypes() != null)
|
||||
generator.setGeneratePrimaryKeyTypes(g.getGenerate().isPrimaryKeyTypes());
|
||||
if (g.getGenerate().getNewline() != null)
|
||||
generator.setGenerateNewline(g.getGenerate().getNewline());
|
||||
if (g.getGenerate().getIndentation() != null)
|
||||
generator.setGenerateIndentation(g.getGenerate().getIndentation());
|
||||
|
||||
|
||||
// [#3669] Optional Database element
|
||||
|
||||
@ -763,6 +763,26 @@ public interface Generator {
|
||||
*/
|
||||
void setGeneratePrimaryKeyTypes(boolean generatePrimaryKeyTypes);
|
||||
|
||||
/**
|
||||
* The newline character(s) to be used in generated code.
|
||||
*/
|
||||
String generateNewline();
|
||||
|
||||
/**
|
||||
* The newline character(s) to be used in generated code.
|
||||
*/
|
||||
void setGenerateNewline(String newline);
|
||||
|
||||
/**
|
||||
* The indentation character(s) to be used in generated code.
|
||||
*/
|
||||
String generateIndentation();
|
||||
|
||||
/**
|
||||
* The indentation character(s) to be used in generated code.
|
||||
*/
|
||||
void setGenerateIndentation(String indentation);
|
||||
|
||||
/**
|
||||
* The target directory
|
||||
*/
|
||||
|
||||
@ -81,8 +81,9 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
|
||||
private final String encoding;
|
||||
private final StringBuilder sb;
|
||||
private int indentTabs;
|
||||
private String tabString = " ";
|
||||
private boolean newline = true;
|
||||
private String tabString = " ";
|
||||
private String newlineString = "\n";
|
||||
private boolean newline = true;
|
||||
|
||||
protected GeneratorWriter(File file) {
|
||||
this(file, null);
|
||||
@ -97,7 +98,11 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
|
||||
}
|
||||
|
||||
public void tabString(String string) {
|
||||
this.tabString = string;
|
||||
this.tabString = string.replace("\\t", "\t");
|
||||
}
|
||||
|
||||
public void newlineString(String string) {
|
||||
this.newlineString = string.replace("\\n", "\n").replace("\\r", "\r");
|
||||
}
|
||||
|
||||
public File file() {
|
||||
@ -124,7 +129,7 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public W print(String string, Object... args) {
|
||||
string = string.replaceAll("\t", tabString);
|
||||
string = string.replace("\n", newlineString).replace("\t", tabString);
|
||||
|
||||
if (newline && indentTabs > 0) {
|
||||
for (int i = 0; i < indentTabs; i++)
|
||||
@ -205,7 +210,7 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
|
||||
|
||||
// Don't add empty lines at the beginning of files
|
||||
if (sb.length() > 0) {
|
||||
sb.append("\n");
|
||||
sb.append(newlineString);
|
||||
newline = true;
|
||||
}
|
||||
|
||||
|
||||
@ -4731,7 +4731,7 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
}
|
||||
|
||||
StringBuilder sb1 = new StringBuilder();
|
||||
String glue1 = "\n";
|
||||
String glue1 = generateNewline();
|
||||
|
||||
for (UniqueKeyDefinition uk : table.getUniqueKeys()) {
|
||||
|
||||
@ -4756,7 +4756,7 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
|
||||
sb1.append(scala ? ")" : "}").append(")");
|
||||
|
||||
glue1 = ",\n";
|
||||
glue1 = "," + generateNewline();
|
||||
}
|
||||
}
|
||||
|
||||
@ -4769,7 +4769,7 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
|
||||
if (StringUtils.isBlank(generateJPAVersion()) || "2.1".compareTo(generateJPAVersion()) <= 0) {
|
||||
StringBuilder sb2 = new StringBuilder();
|
||||
String glue2 = "\n";
|
||||
String glue2 = generateNewline();
|
||||
|
||||
for (IndexDefinition index : table.getIndexes()) {
|
||||
sb2.append(glue2);
|
||||
@ -4797,7 +4797,7 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
}
|
||||
|
||||
sb2.append("\")");
|
||||
glue2 = ",\n";
|
||||
glue2 = "," + generateNewline();
|
||||
}
|
||||
|
||||
if (sb2.length() > 0) {
|
||||
@ -6375,7 +6375,14 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
// [#3880] Users may need to call this method
|
||||
protected JavaWriter newJavaWriter(File file) {
|
||||
file = fixSuffix(file);
|
||||
return new JavaWriter(file, generateFullyQualifiedTypes(), targetEncoding, generateJavadoc());
|
||||
JavaWriter result = new JavaWriter(file, generateFullyQualifiedTypes(), targetEncoding, generateJavadoc());
|
||||
|
||||
if (generateIndentation != null)
|
||||
result.tabString(generateIndentation);
|
||||
if (generateNewline != null)
|
||||
result.newlineString(generateNewline);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected File getFile(Definition definition) {
|
||||
|
||||
@ -17871,6 +17871,51 @@ public class CaseInsensitiveOrderProvider implements Comparator<Definition> {
|
||||
|
||||
</html></content>
|
||||
</section>
|
||||
|
||||
<section id="codegen-whitespace">
|
||||
<title>Whitespace (newlines and indentation)</title>
|
||||
<content><html>
|
||||
<p>
|
||||
By default, jOOQ's code generator produces unix newline characters (\n) and 4 space indentation (Java) or 2 space indentation (Scala). This can be overridden by using the following configuration flags:
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>XML configuration (standalone and Maven)</strong>
|
||||
</p>
|
||||
|
||||
</html><xml><![CDATA[<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-{codegen-xsd-version}.xsd">
|
||||
<generator>
|
||||
<generate>
|
||||
<indentation> </indentation>
|
||||
<newline>\n</newline>
|
||||
</generate>
|
||||
</generator>
|
||||
</configuration>]]></xml><html>
|
||||
|
||||
<p>
|
||||
<strong>Programmatic configuration</strong>
|
||||
</p>
|
||||
|
||||
</html><java><![CDATA[configuration
|
||||
.withGenerator(new Generator(
|
||||
.withGenerate(new Generate()
|
||||
.withIndentation(" ")
|
||||
.withNewline("\n"))));]]></java><html>
|
||||
|
||||
<p>
|
||||
<strong>Gradle configuration</strong>
|
||||
</p>
|
||||
|
||||
</html><java><![CDATA[myConfigurationName(sourceSets.main) {
|
||||
generator {
|
||||
generate {
|
||||
indentation = ' '
|
||||
newline = '\n'
|
||||
}
|
||||
}
|
||||
}]]></java><html>
|
||||
</html></content>
|
||||
</section>
|
||||
</sections>
|
||||
</section>
|
||||
|
||||
|
||||
@ -163,6 +163,11 @@ public class Generate implements Serializable
|
||||
protected Boolean javaTimeTypes = false;
|
||||
@XmlElement(defaultValue = "false")
|
||||
protected Boolean primaryKeyTypes = false;
|
||||
@XmlElement(defaultValue = "\\n")
|
||||
@XmlJavaTypeAdapter(StringAdapter.class)
|
||||
protected String newline = "\\n";
|
||||
@XmlJavaTypeAdapter(StringAdapter.class)
|
||||
protected String indentation;
|
||||
|
||||
/**
|
||||
* Generate index information.
|
||||
@ -1707,6 +1712,54 @@ public class Generate implements Serializable
|
||||
this.primaryKeyTypes = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The newline characters to be used in generated code. Whitespace characters can be used, e.g. \n, \r\n
|
||||
*
|
||||
* @return
|
||||
* possible object is
|
||||
* {@link String }
|
||||
*
|
||||
*/
|
||||
public String getNewline() {
|
||||
return newline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the newline property.
|
||||
*
|
||||
* @param value
|
||||
* allowed object is
|
||||
* {@link String }
|
||||
*
|
||||
*/
|
||||
public void setNewline(String value) {
|
||||
this.newline = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The indentation characters to be used in generated code. If unspecified, an idiomatic default indentation of the language will be used (4 spaces in Java, 2 spaces in Scala). Whitespace characters can be used, e.g. \t
|
||||
*
|
||||
* @return
|
||||
* possible object is
|
||||
* {@link String }
|
||||
*
|
||||
*/
|
||||
public String getIndentation() {
|
||||
return indentation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the indentation property.
|
||||
*
|
||||
* @param value
|
||||
* allowed object is
|
||||
* {@link String }
|
||||
*
|
||||
*/
|
||||
public void setIndentation(String value) {
|
||||
this.indentation = value;
|
||||
}
|
||||
|
||||
public Generate withIndexes(Boolean value) {
|
||||
setIndexes(value);
|
||||
return this;
|
||||
@ -2022,6 +2075,16 @@ public class Generate implements Serializable
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generate withNewline(String value) {
|
||||
setNewline(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generate withIndentation(String value) {
|
||||
setIndentation(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@ -2340,6 +2403,16 @@ public class Generate implements Serializable
|
||||
sb.append(primaryKeyTypes);
|
||||
sb.append("</primaryKeyTypes>");
|
||||
}
|
||||
if (newline!= null) {
|
||||
sb.append("<newline>");
|
||||
sb.append(newline);
|
||||
sb.append("</newline>");
|
||||
}
|
||||
if (indentation!= null) {
|
||||
sb.append("<indentation>");
|
||||
sb.append(indentation);
|
||||
sb.append("</indentation>");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@ -2922,6 +2995,24 @@ public class Generate implements Serializable
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (newline == null) {
|
||||
if (other.newline!= null) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!newline.equals(other.newline)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (indentation == null) {
|
||||
if (other.indentation!= null) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!indentation.equals(other.indentation)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -2992,6 +3083,8 @@ public class Generate implements Serializable
|
||||
result = ((prime*result)+((emptySchemas == null)? 0 :emptySchemas.hashCode()));
|
||||
result = ((prime*result)+((javaTimeTypes == null)? 0 :javaTimeTypes.hashCode()));
|
||||
result = ((prime*result)+((primaryKeyTypes == null)? 0 :primaryKeyTypes.hashCode()));
|
||||
result = ((prime*result)+((newline == null)? 0 :newline.hashCode()));
|
||||
result = ((prime*result)+((indentation == null)? 0 :indentation.hashCode()));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@ -1182,6 +1182,14 @@ This flag is ignored in the commercial Java 6 distribution of jOOQ 3.9+ ]]></jxb
|
||||
<element name="primaryKeyTypes" type="boolean" default="false" minOccurs="0" maxOccurs="1">
|
||||
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether wrapper types should be generated for primary key columns, and for their referencing foreign keys.]]></jxb:javadoc></jxb:property></appinfo></annotation>
|
||||
</element>
|
||||
|
||||
<element name="newline" type="string" default="\n" minOccurs="0" maxOccurs="1">
|
||||
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The newline characters to be used in generated code. Whitespace characters can be used, e.g. \n, \r\n]]></jxb:javadoc></jxb:property></appinfo></annotation>
|
||||
</element>
|
||||
|
||||
<element name="indentation" type="string" minOccurs="0" maxOccurs="1">
|
||||
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The indentation characters to be used in generated code. If unspecified, an idiomatic default indentation of the language will be used (4 spaces in Java, 2 spaces in Scala). Whitespace characters can be used, e.g. \t]]></jxb:javadoc></jxb:property></appinfo></annotation>
|
||||
</element>
|
||||
</all>
|
||||
</complexType>
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user