Class Indent

java.lang.Object
com.intellij.formatting.Indent
Direct Known Subclasses:
IndentImpl

public abstract class Indent extends Object
The indent setting for a formatting model block. Indicates how the block is indented relative to its parent block.

Relative indents

Number of factory methods of this class use 'indent relative to direct parent' flag. It specified anchor parent block to use to apply indent.

Consider the following situation:

     return a == 0
 && (b == 0
 || c == 0);
 

Here is the following blocks hierarchy (going from child to parent):

  • '|| c == 0`;
  • 'b == 0 || c == 0';
  • '(b == 0 || c == 0)';
  • 'a == 0 && (b == 0 || c == 0)';
  • 'return a == 0 && (b == 0 || c == 0)';

By default formatter applies block indent to the first block ancestor (direct or indirect) that starts on a new line. That means that such an ancestor for both blocks '|| c == 0' and '&& (b == 0 || c == 0)' is 'return a == 0 && (b == 0 || c == 0)'. That means that the code above is formatted as follows:

    return a == 0
        && (b == 0
        || c == 0);
 

In contrast, it's possible to specify that direct parent block that starts on a line before target child block is used as an anchor. Initial formatting example illustrates such approach.

Enforcing indent to children

It's possible to configure indent to enforce parent block indent to its children that start new line. Consider the following situation:

   foo("test", new Runnable() {
           public void run() {
           }
       },
       new Runnable() {
           public void run() {
           }
       }
   );
 
We want the first 'new Runnable() {...}' block here to be indented to the method expression list element. However, formatter uses indents only if the block starts new line. Here the block doesn't start new line ('new Runnable() ...'), hence we need to define 'enforce indent to children' flag in order to instruct formatter to apply parent indent to the sub-blocks.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Indent
    Returns the "absolute label" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Label indent" setting from the leftmost column in the document.
    static Indent
    Returns the "absolute none" indent instance, indicating that the block will be placed at the leftmost column in the document.
    static Indent
    Returns the "continuation" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Continuation indent" setting relative to its parent block.
    static Indent
    getContinuationIndent(boolean relativeToDirectParent)
    Returns the "continuation" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Continuation indent" setting relative to its parent block and given 'relative to direct parent' flag.
    static Indent
    Returns the "continuation without first" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Continuation indent" setting relative to its parent block, unless this block is the first of the children of its parent having the same indent type.
    static Indent
    getContinuationWithoutFirstIndent(boolean relativeToDirectParent)
    Returns the "continuation without first" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Continuation indent" setting relative to its parent block, unless this block is the first of the children of its parent having the same indent type.
    static Indent
    getIndent(@NotNull Indent.Type type, boolean relativeToDirectParent, boolean enforceIndentToChildren)
    Base factory method for Indent objects construction, i.e.
    static Indent
    getIndent(@NotNull Indent.Type type, int spaces, boolean relativeToDirectParent, boolean enforceIndentToChildren)
    Base factory method for Indent objects construction, i.e.
    static Indent
    Returns the "label" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Label indent" setting relative to its parent block.
    static Indent
    Returns the standard "empty indent" instance, indicating that the block is not indented relative to its parent block.
    static Indent
    Returns an instance of a regular indent, with the width specified in "Project Code Style | General | Indent".
    static Indent
    getNormalIndent(boolean relativeToDirectParent)
    Returns an instance of a regular indent, with the width specified in "Project Code Style | General | Indent" and given 'relative to direct parent' flag
    static Indent
     
    static Indent
    getSpaceIndent(int spaces)
    Returns an indent with the specified width.
    static Indent
    getSpaceIndent(int spaces, boolean relativeToDirectParent)
    Returns an indent with the specified width and given 'relative to direct parent' flag.
    abstract Indent.Type
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Indent

      public Indent()
  • Method Details

    • getType

      public abstract Indent.Type getType()
    • getNormalIndent

      public static Indent getNormalIndent()
      Returns an instance of a regular indent, with the width specified in "Project Code Style | General | Indent".

      Note: returned indent is not set to be 'relative' to it's direct parent block

      Returns:
      the indent instance.
      See Also:
    • getNormalIndent

      public static Indent getNormalIndent(boolean relativeToDirectParent)
      Returns an instance of a regular indent, with the width specified in "Project Code Style | General | Indent" and given 'relative to direct parent' flag
      Parameters:
      relativeToDirectParent - flag the indicates if current indent object anchors direct block parent (feel free to get more information about that at class-level javadoc)
      Returns:
      newly created indent instance configured in accordance with the given parameter
    • getNoneIndent

      public static Indent getNoneIndent()
      Returns the standard "empty indent" instance, indicating that the block is not indented relative to its parent block.
      Returns:
      the empty indent instance.
    • getAbsoluteNoneIndent

      public static Indent getAbsoluteNoneIndent()
      Returns the "absolute none" indent instance, indicating that the block will be placed at the leftmost column in the document.
      Returns:
      the indent instance.
    • getAbsoluteLabelIndent

      public static Indent getAbsoluteLabelIndent()
      Returns the "absolute label" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Label indent" setting from the leftmost column in the document.
      Returns:
      the indent instance.
    • getLabelIndent

      public static Indent getLabelIndent()
      Returns the "label" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Label indent" setting relative to its parent block.
      Returns:
      the indent instance.
    • getContinuationIndent

      public static Indent getContinuationIndent()
      Returns the "continuation" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Continuation indent" setting relative to its parent block.

      Note: returned indent is not set to be 'relative' to it's direct parent block

      Returns:
      the indent instance.
      See Also:
    • getContinuationIndent

      public static Indent getContinuationIndent(boolean relativeToDirectParent)
      Returns the "continuation" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Continuation indent" setting relative to its parent block and given 'relative to direct parent' flag.
      Parameters:
      relativeToDirectParent - flag the indicates if current indent object anchors direct block parent (feel free to get more information about that at class-level javadoc)
      Returns:
      newly created indent instance configured in accordance with the given parameter
    • getContinuationWithoutFirstIndent

      public static Indent getContinuationWithoutFirstIndent()
      Returns the "continuation without first" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Continuation indent" setting relative to its parent block, unless this block is the first of the children of its parent having the same indent type. This is used for things like parameter lists, where the first parameter does not have any indent and the remaining parameters are indented by the continuation indent.

      Note: returned indent is not set to be 'relative' to it's direct parent block

      Returns:
      the indent instance.
      See Also:
    • getContinuationWithoutFirstIndent

      public static Indent getContinuationWithoutFirstIndent(boolean relativeToDirectParent)
      Returns the "continuation without first" indent instance, indicating that the block will be indented by the number of spaces indicated in the "Project Code Style | General | Continuation indent" setting relative to its parent block, unless this block is the first of the children of its parent having the same indent type. This is used for things like parameter lists, where the first parameter does not have any indent and the remaining parameters are indented by the continuation indent and given 'relative to direct parent' flag.
      Parameters:
      relativeToDirectParent - flag the indicates if current indent object anchors direct block parent (feel free to get more information about that at class-level javadoc)
      Returns:
      newly created indent instance configured in accordance with the given parameter
    • getSpaceIndent

      public static Indent getSpaceIndent(int spaces)
      Returns an indent with the specified width.

      Note: returned indent is not set to be 'relative' to it's direct parent block

      Parameters:
      spaces - the number of spaces in the indent.
      Returns:
      the indent instance.
      See Also:
    • getSpaceIndent

      public static Indent getSpaceIndent(int spaces, boolean relativeToDirectParent)
      Returns an indent with the specified width and given 'relative to direct parent' flag.
      Parameters:
      spaces - the number of spaces in the indent
      relativeToDirectParent - flag the indicates if current indent object anchors direct block parent (feel free to get more information about that at class-level javadoc)
      Returns:
      newly created indent instance configured in accordance with the given parameter
    • getIndent

      public static Indent getIndent(@NotNull @NotNull Indent.Type type, boolean relativeToDirectParent, boolean enforceIndentToChildren)
      Base factory method for Indent objects construction, i.e. all other methods may be expressed in terms of this method.
      Parameters:
      type - indent type
      relativeToDirectParent - flag the indicates if current indent object anchors direct block parent (feel free to get more information about that at class-level javadoc)
      enforceIndentToChildren - flag the indicates if current indent object should be enforced for multiline block children (feel free to get more information about that at class-level javadoc)
      Returns:
      newly created indent configured in accordance with the given arguments
    • getIndent

      public static Indent getIndent(@NotNull @NotNull Indent.Type type, int spaces, boolean relativeToDirectParent, boolean enforceIndentToChildren)
      Base factory method for Indent objects construction, i.e. all other methods may be expressed in terms of this method.
      Parameters:
      type - indent type
      spaces - the number of spaces in the indent
      relativeToDirectParent - flag the indicates if current indent object anchors direct block parent (feel free to get more information about that at class-level javadoc)
      enforceIndentToChildren - flag the indicates if current indent object should be enforced for multiline block children (feel free to get more information about that at class-level javadoc)
      Returns:
      newly created indent configured in accordance with the given arguments
    • getSmartIndent

      public static Indent getSmartIndent(Indent.Type type)