diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java index 0dda8cb5b21..f78aa877297 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java @@ -41,6 +41,31 @@ * case, this check should just ensure that a minimal set of indentation * rules is followed. *

+ *

+ * Basic offset indentation is used for indentation inside code blocks. + * For any lines that span more than 1, line wrapping indentation is used for those lines + * after the first. Brace adjustment, case, and throws indentations are all used only if + * those specific identifiers start the line. If, for example, a brace is used in the + * middle of the line, its indentation will not take effect. All indentations have an + * accumulative/recursive effect when they are triggered. If during a line wrapping, another + * code block is found and it doesn't end on that same line, then the subsequent lines + * afterwards, in that new code block, are increased on top of the line wrap and any + * indentations above it. + *

+ *

+ * Example: + *

+ *
+ * if ((condition1 && condition2)
+ *         || (condition3 && condition4)    // line wrap with bigger indentation
+ *         ||!(condition5 && condition6)) { // line wrap with bigger indentation
+ *   field.doSomething()                    // basic offset
+ *       .doSomething()                     // line wrap
+ *       .doSomething( c -> {               // line wrap
+ *         return c.doSome();               // basic offset
+ *       });
+ * }
+ * 
* *

- * To configure the check: + * To configure the check for default behavior: *

*
  * <module name="Indentation"/>
  * 
*

+ * Example of Compliant code for default configuration (in comment name of property + * that controls indentations): + *

+ *
+ * class Test {
+ *    String field;               // basicOffset
+ *    int[] arr = {               // basicOffset
+ *        5,                      // arrayInitIndent
+ *        6 };                    // arrayInitIndent
+ *    void bar() throws Exception // basicOffset
+ *    {                           // braceAdjustment
+ *        foo();                  // basicOffset
+ *    }                           // braceAdjustment
+ *    void foo() {                // basicOffset
+ *        if ((cond1 && cond2)    // basicOffset
+ *                  || (cond3 && cond4)    // lineWrappingIndentation, forceStrictCondition
+ *                  ||!(cond5 && cond6)) { // lineWrappingIndentation, forceStrictCondition
+ *            field.doSomething()          // basicOffset
+ *                .doSomething()           // lineWrappingIndentation and forceStrictCondition
+ *                .doSomething( c -> {     // lineWrappingIndentation and forceStrictCondition
+ *                    return c.doSome();   // basicOffset
+ *                });
+ *        }
+ *    }
+ *    void fooCase()                // basicOffset
+ *        throws Exception {        // throwsIndent
+ *        switch (field) {          // basicOffset
+ *            case "value" : bar(); // caseIndent
+ *        }
+ *    }
+ * }
+ * 
+ *

* To configure the check to enforce the indentation style recommended by Oracle: *

*
@@ -94,6 +152,17 @@
  * </module>
  * 
*

+ * Example of Compliant code for default configuration (in comment name of property that controls + * indentation): + *

+ *
+ * void fooCase() {          // basicOffset
+ *     switch (field) {      // basicOffset
+ *     case "value" : bar(); // caseIndent
+ *     }
+ * }
+ * 
+ *

* To configure the Check to enforce strict condition in line-wrapping validation. *

*
@@ -102,18 +171,49 @@
  * </module>
  * 
*

- * Such config doesn't allow next cases: + * Such config doesn't allow next cases even code is aligned further to the right for better + * reading: *

*
  * void foo(String aFooString,
- *         int aFooInt) {} // indent:8 ; expected: 4; warn, because 8 != 4
+ *         int aFooInt) { // indent:8 ; expected: 4; violation, because 8 != 4
+ *     if (cond1
+ *         || cond2) {
+ *         field.doSomething()
+ *             .doSomething();
+ *     }
+ *     if ((cond1 && cond2)
+ *               || (cond3 && cond4)    // violation
+ *               ||!(cond5 && cond6)) { // violation
+ *         field.doSomething()
+ *              .doSomething()          // violation
+ *              .doSomething( c -> {    // violation
+ *                  return c.doSome();
+ *             });
+ *     }
+ * }
  * 
*

* But if forceStrictCondition = false, this code is valid: *

*
  * void foo(String aFooString,
- *         int aFooInt) {} // indent:8 ; expected: > 4; ok, because 8 > 4
+ *         int aFooInt) { // indent:8 ; expected: > 4; ok, because 8 > 4
+ *     if (cond1
+ *         || cond2) {
+ *         field.doSomething()
+ *             .doSomething();
+ *     }
+ *     if ((cond1 && cond2)
+ *               || (cond3 && cond4)
+ *               ||!(cond5 && cond6)) {
+ *         field.doSomething()
+ *              .doSomething()
+ *              .doSomething( c -> {
+ *                  return c.doSome();
+ *             });
+ *     }
+ * }
  * 
* * @noinspection ThisEscapedInObjectConstruction diff --git a/src/xdocs/config_misc.xml b/src/xdocs/config_misc.xml index 734d8d96f05..6cd0a52ffa1 100644 --- a/src/xdocs/config_misc.xml +++ b/src/xdocs/config_misc.xml @@ -1084,6 +1084,30 @@ String unitAbbrev = "\u03bc\u03bc\u03bc"; times it is practical experience. In any case, this check should just ensure that a minimal set of indentation rules is followed.

+

+ Basic offset indentation is used for indentation inside code blocks. + For any lines that span more than 1, line wrapping indentation is used for those + lines after the first. + Brace adjustment, case, and throws indentations are all used only if those specific + identifiers start the line. If, for example, a brace is used in the middle of the line, + its indentation will not take effect. + All indentations have an accumulative/recursive effect when they are triggered. If + during a line wrapping, another code block is found and it doesn't end on that same + line, then the subsequent lines afterwards, in that new code block, are increased on + top of the line wrap and any indentations above it. +

+

Example:

+ +if ((condition1 && condition2) + || (condition3 && condition4) // line wrap with bigger indentation + ||!(condition5 && condition6)) { // line wrap with bigger indentation + field.doSomething() // basic offset + .doSomething() // line wrap + .doSomething( c -> { // line wrap + return c.doSome(); // basic offset + }); +} + @@ -1157,12 +1181,44 @@ String unitAbbrev = "\u03bc\u03bc\u03bc";

- To configure the check: + To configure the check for default behavior:

<module name="Indentation"/> - +

+ Example of Compliant code for default configuration + (in comment name of property that controls indentations): +

+ +class Test { + String field; // basicOffset + int[] arr = { // basicOffset + 5, // arrayInitIndent + 6 }; // arrayInitIndent + void bar() throws Exception // basicOffset + { // braceAdjustment + foo(); // basicOffset + } // braceAdjustment + void foo() { // basicOffset + if ((cond1 && cond2) // basicOffset + || (cond3 && cond4) // lineWrappingIndentation, forceStrictCondition + ||!(cond5 && cond6)) { // lineWrappingIndentation, forceStrictCondition + field.doSomething() // basicOffset + .doSomething() // lineWrappingIndentation and forceStrictCondition + .doSomething( c -> { // lineWrappingIndentation and forceStrictCondition + return c.doSome(); // basicOffset + }); + } + } + void fooCase() // basicOffset + throws Exception { // throwsIndent + switch (field) { // basicOffset + case "value" : bar(); // caseIndent + } + } +} +

To configure the check to enforce the indentation style recommended by Oracle: @@ -1172,6 +1228,17 @@ String unitAbbrev = "\u03bc\u03bc\u03bc"; <module name="Indentation"> <property name="caseIndent" value="0"/> </module> + +

+ Example of Compliant code for default configuration + (in comment name of property that controls indentation): +

+ +void fooCase() { // basicOffset + switch (field) { // basicOffset + case "value" : bar(); // caseIndent + } +}

To configure the Check to enforce strict condition in line-wrapping validation. @@ -1182,18 +1249,49 @@ String unitAbbrev = "\u03bc\u03bc\u03bc"; </module>

- Such config doesn't allow next cases: + Such config doesn't allow next cases + even code is aligned further to the right for better reading:

void foo(String aFooString, - int aFooInt) {} // indent:8 ; expected: 4; warn, because 8 != 4 + int aFooInt) { // indent:8 ; expected: 4; violation, because 8 != 4 + if (cond1 + || cond2) { + field.doSomething() + .doSomething(); + } + if ((cond1 && cond2) + || (cond3 && cond4) // violation + ||!(cond5 && cond6)) { // violation + field.doSomething() + .doSomething() // violation + .doSomething( c -> { // violation + return c.doSome(); + }); + } +}

But if forceStrictCondition = false, this code is valid:

void foo(String aFooString, - int aFooInt) {} // indent:8 ; expected: > 4; ok, because 8 > 4 + int aFooInt) { // indent:8 ; expected: > 4; ok, because 8 > 4 + if (cond1 + || cond2) { + field.doSomething() + .doSomething(); + } + if ((cond1 && cond2) + || (cond3 && cond4) + ||!(cond5 && cond6)) { + field.doSomething() + .doSomething() + .doSomething( c -> { + return c.doSome(); + }); + } +}