com.intellij.formatting.Indent Java Examples

The following examples show how to use com.intellij.formatting.Indent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CStyleCommentBlock.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected List<Block> buildChildren() {
  if (!isCommentFormattable.getValue()) {
    return Collections.emptyList();
  }

  return ContainerUtil.<LineInfo, Block>map(lines.getValue(), t -> {
    String text = t.myText;

    Indent indent = null;
    if (!isCommentFormattable.getValue()) {
      indent = null;
    }
    else if (text.startsWith("/*")) {
      indent = Indent.getNoneIndent();
    }
    else {
      indent = Indent.getSpaceIndent(1);
    }

    return new TextLineBlock(text, t.myTextRange, null, indent, null);
  });
}
 
Example #2
Source File: FusionIndentProcessor.java    From intellij-neos with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param node An ASTNode
 * @return The calculated indent for the given node
 */
public static Indent getIndent(ASTNode node) {
    IElementType type = node.getElementType();
    Indent indent = Indent.getNoneIndent();
    ASTNode parent = node.getTreeParent();
    if (parent == null) {
        return indent;
    }
    if (TYPES_WHICH_PROVOKE_AN_INDENT.contains(parent.getElementType())) {
        indent = Indent.getIndent(Indent.Type.NORMAL, false, true);
    }
    if (TYPES_WHICH_DO_NOT_NEED_AN_BEGINNING_INDENT.contains(type)) {
        indent = Indent.getNoneIndent();
    }
    if (TYPES_WHICH_CANNOT_GET_AN_INDENT.contains(type)) {
        indent = Indent.getAbsoluteNoneIndent();
    }
    return indent;
}
 
Example #3
Source File: FormatterBasedLineIndentInfoBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static List<Indent.Type> getIndentOnStartOffset(Block block, TextRange range, int startOffset) {
  List<Indent.Type> indentsOnStartOffset = new ArrayList<Indent.Type>();

  while (block != null && range.getStartOffset() == startOffset) {
    Indent.Type type = block.getIndent() != null ? block.getIndent().getType() : Indent.Type.CONTINUATION_WITHOUT_FIRST;
    indentsOnStartOffset.add(type);

    if (block instanceof AbstractBlock) {
      ((AbstractBlock)block).setBuildIndentsOnly(true);
    }
    List<Block> subBlocks = block.getSubBlocks();
    block = subBlocks.isEmpty() ? null : subBlocks.get(0);
  }

  return indentsOnStartOffset;
}
 
Example #4
Source File: ProtoFileBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    List<Block> blocks = new ArrayList<>();
    ASTNode child = myNode.getFirstChildNode();
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
            IElementType elementType = child.getElementType();
            if (ProtoParserDefinition.rule(ProtoParser.RULE_proto).equals(elementType)) {
                appendProtoBlocks(child, blocks);
            } else {
                // Comments are not part of root rule, we have to append them separately
                blocks.add(new LeafBlock(child, Alignment.createAlignment(), Indent.getNoneIndent(), settings));
            }
        }
        child = child.getTreeNext();
    }
    return blocks;
}
 
Example #5
Source File: FormatterBasedLineIndentInfoBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean hasOnlyNormalOrNoneIndents(List<Indent.Type> indents) {
  Indent.Type outerMostIndent = indents.get(0);
  if (outerMostIndent != Indent.Type.NONE && outerMostIndent != Indent.Type.NORMAL) {
    return false;
  }

  List<Indent.Type> innerIndents = indents.subList(1, indents.size());
  for (Indent.Type indent : innerIndents) {
    if (indent != Indent.Type.NONE && indent != Indent.Type.NORMAL
        && indent != Indent.Type.CONTINUATION_WITHOUT_FIRST) {
      //continuation without first here because it is CONTINUATION only if it's owner is not the first child
      return false;
    }
  }

  return true;
}
 
Example #6
Source File: FormatterBasedLineIndentInfoBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean hasNormalIndent(Block block) {
  final TextRange range = block.getTextRange();
  final int startOffset = range.getStartOffset();

  List<Indent.Type> allIndents = getIndentOnStartOffset(block, range, startOffset);

  if (hasOnlyNormalOrNoneIndents(allIndents)) {
    int normalIndents = ContainerUtil.filter(allIndents, new Condition<Indent.Type>() {
      @Override
      public boolean value(Indent.Type type) {
        return type == Indent.Type.NORMAL;
      }
    }).size();
    return normalIndents < 2;
  }

  return false;
}
 
Example #7
Source File: JSGraphQLEndpointBlock.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@Override
public Indent getIndent() {

	if (INDENT_EXCLUSIONS.contains(myNode.getElementType())) {
		return Indent.getNoneIndent();
	}

	if (parent != null) {
		final IElementType elementType = parent.myNode.getElementType();
		if (INDENT_PARENTS.contains(elementType)) {
			return Indent.getNormalIndent();
		}
	}

	// default is no indentation
	return Indent.getNoneIndent();

}
 
Example #8
Source File: CSharpIndentProcessor.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Nonnull
public Indent getChildIndent()
{
	IElementType elementType = myBlock.getNode().getElementType();
	if(elementType == CSharpStubElements.FILE ||
			elementType == CSharpElements.MODIFIER_LIST ||
			elementType == CSharpElements.IF_STATEMENT ||
			elementType == CSharpElements.TRY_STATEMENT ||
			elementType == CSharpStubElements.MODIFIER_LIST)
	{
		return Indent.getNoneIndent();
	}

	return Indent.getNormalIndent();
}
 
Example #9
Source File: BashBlockGenerator.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
public static List<Block> generateSubBlocks(ASTNode node,
                                            Alignment _myAlignment,
                                            Wrap _myWrap,
                                            CodeStyleSettings _mySettings,
                                            BashBlock block) {
    myWrap = _myWrap;
    mySettings = _mySettings;
    myAlignment = _myAlignment;

    // For other cases
    final List<Block> subBlocks = new ArrayList<Block>();
    ASTNode children[] = getBashChildren(node);
    ASTNode prevChildNode = null;
    for (ASTNode childNode : children) {
        if (canBeCorrectBlock(childNode)) {
            final Indent indent = BashIndentProcessor.getChildIndent(block, prevChildNode, childNode);
            subBlocks.add(new BashBlock(childNode, myAlignment, indent, myWrap, mySettings));
            prevChildNode = childNode;
        }
    }
    return subBlocks;
}
 
Example #10
Source File: BuckBlock.java    From Buck-IntelliJ-Plugin with Apache License 2.0 6 votes vote down vote up
public BuckBlock(@Nullable final BuckBlock parent,
                 @NotNull final ASTNode node,
                 @NotNull CodeStyleSettings settings,
                 @Nullable final Alignment alignment,
                 @NotNull final Indent indent,
                 @Nullable final Wrap wrap) {
  myParent = parent;
  myAlignment = alignment;
  myIndent = indent;
  myNode = node;
  myPsiElement = node.getPsi();
  myWrap = wrap;
  mySettings = settings;

  mySpacingBuilder = BuckFormattingModelBuilder.createSpacingBuilder(settings);

  if (myPsiElement instanceof BuckArrayElements ||
      myPsiElement instanceof BuckRuleBody ||
      myPsiElement instanceof BuckListElements ||
      myPsiElement instanceof BuckObjectElements) {
    myChildWrap = Wrap.createWrap(CommonCodeStyleSettings.WRAP_ALWAYS, true);
  } else {
    myChildWrap = null;
  }
}
 
Example #11
Source File: BashIndentProcessor.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates indent, based on code style, between parent block and child node
 *
 * @param parent        parent block
 * @param child         child node
 * @param prevChildNode previous child node
 * @return indent
 */
@NotNull
public static Indent getChildIndent(@NotNull final BashBlock parent, @Nullable final ASTNode prevChildNode, @NotNull final ASTNode child) {
    ASTNode node = parent.getNode();
    IElementType nodeType = node.getElementType();
    PsiElement psiElement = node.getPsi();

    // For Bash file
    if (psiElement instanceof BashFile) {
        return Indent.getNoneIndent();
    }

    if (BLOCKS.contains(nodeType)) {
        return indentForBlock(psiElement, child);
    }

    //subshell as function body
    ASTNode parentNode = node.getTreeParent();
    if (parentNode != null && parentNode.getElementType() == SUBSHELL_COMMAND) {
        if (parentNode.getTreeParent() != null && parentNode.getTreeParent().getElementType() == FUNCTION_DEF_COMMAND) {
            return Indent.getNormalIndent();
        }
    }

    return Indent.getNoneIndent();
}
 
Example #12
Source File: GraphQLBlockWrapper.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
protected Indent getChildIndent() {
    if(myNode.getPsi() instanceof JSStringTemplateExpression) {
        // enter in a top-level block inside template indents, e.g. queries etc.
        return Indent.getNormalIndent();
    }
    if(wrapped != null) {
        return wrapped.getChildAttributes(0).getChildIndent();
    }
    if(parent == null) {
        return Indent.getNoneIndent();
    }
    return Indent.getIndent(Indent.Type.NORMAL, false, false);
}
 
Example #13
Source File: JSGraphQLEndpointBlock.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
protected Indent getChildIndent() {
	if (parent == null) {
		return Indent.getNoneIndent();
	}
	if (INDENT_PARENTS.contains(myNode.getElementType())) {
		return Indent.getNormalIndent();
	}
	return Indent.getNoneIndent();
}
 
Example #14
Source File: GraphQLBlockWrapper.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public Indent getIndent() {
    if(myNode.getPsi() instanceof JSStringTemplateExpression) {
        // we're a GraphQL block for a JS string template expression, so return normal indent to indent queries inside the template string
        return Indent.getNormalIndent();
    }
    if(wrapped != null) {
        Indent indent = wrapped.getIndent();
        if(indent != null) {
            return indent;
        }
    }
    return Indent.getNoneIndent();
}
 
Example #15
Source File: GraphQLInjectedLanguageBlockBuilder.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public boolean addInjectedBlocks(List<? super Block> result, ASTNode injectionHost, Wrap wrap, Alignment alignment, Indent indent) {
    boolean added = super.addInjectedBlocks(result, injectionHost, wrap, alignment, indent);
    if(!added) {
        // 'if (places.size() != 1) {' guard in super
        // happens when the GraphQL is interrupted by JS/TS template fragments
        added = graphQLSupportingAddInjectedBlocks(result, injectionHost, wrap, alignment, indent);
    }
    return added;
}
 
Example #16
Source File: BuckFormattingModelBuilder.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(@NotNull PsiElement element,
                                   @NotNull CodeStyleSettings settings,
                                   @NotNull FormattingMode mode) {
  final BuckBlock block =
      new BuckBlock(null, element.getNode(), settings, null, Indent.getNoneIndent(), null);
  return FormattingModelProvider.createFormattingModelForPsiFile(
      element.getContainingFile(),
      block,
      settings);
}
 
Example #17
Source File: BuckBlock.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public ChildAttributes getChildAttributes(int newChildIndex) {
  if (hasElementType(myNode, BUCK_CONTAINERS)) {
    return new ChildAttributes(Indent.getNormalIndent(), null);
  } else if (myNode.getPsi() instanceof PsiFile) {
    return new ChildAttributes(Indent.getNoneIndent(), null);
  } else {
    return new ChildAttributes(null, null);
  }
}
 
Example #18
Source File: BashIndentProcessor.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Indent for common block
 *
 * @param psiBlock
 * @param child
 * @return
 */
private static Indent indentForBlock(PsiElement psiBlock, ASTNode child) {
    if (LEFT_CURLY.equals(child.getElementType()) || RIGHT_CURLY.equals(child.getElementType())) {
        return Indent.getNoneIndent();
    }

    return Indent.getNormalIndent();
}
 
Example #19
Source File: XQueryFormattingBlock.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public ChildAttributes getChildAttributes(int newChildIndex) {
    IElementType type = myNode.getElementType();
    Indent childIndent = calculateChildIndent(type, false);
    if (childIndent == null && newChildIndex > 0) {
        IElementType calculatedType = getIElementType(newChildIndex);
        childIndent = calculateChildIndent(calculatedType, true);
    }
    return new ChildAttributes(childIndent != null ? childIndent : Indent.getNoneIndent(), null);
}
 
Example #20
Source File: XQueryFormattingBlock.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
private Indent calculateChildIndent(IElementType type, boolean fromCalculatedType) {
    if (type == ENCLOSED_EXPRESSION || type == FUNCTION_DECL || (! fromCalculatedType && type == PARENTHESIZED_EXPR)
            || type == LET_BINDING || type == OP_ASSIGN || type == RETURN_CLAUSE
            || (! fromCalculatedType && type == TRY_CLAUSE) || (! fromCalculatedType && type == CATCH_CLAUSE)
            || type == CATCH_CLAUSE_EXPRESSION)
        return Indent.getNormalIndent();
    return null;
}
 
Example #21
Source File: JavaLikeLangLineIndentProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
protected Indent getIndentInBlock(@Nonnull Project project, @Nullable Language language, @Nonnull SemanticEditorPosition blockStartPosition) {
  if (language != null) {
    CommonCodeStyleSettings settings = CodeStyle.getSettings(blockStartPosition.getEditor()).getCommonSettings(language);
    if (settings.BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED) {
      return getDefaultIndentFromType(settings.METHOD_BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED ? NONE : null);
    }
  }
  return getDefaultIndentFromType(NORMAL);
}
 
Example #22
Source File: IndentCalculator.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static int indentToSize(@Nonnull Indent indent, @Nonnull CommonCodeStyleSettings.IndentOptions options) {
  if (indent.getType() == NORMAL) {
    return options.INDENT_SIZE;
  }
  else if (indent.getType() == CONTINUATION) {
    return options.CONTINUATION_INDENT_SIZE;
  }
  else if (indent.getType() == SPACES && indent instanceof IndentImpl) {
    return ((IndentImpl)indent).getSpaces();
  }
  return 0;
}
 
Example #23
Source File: BlockUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static DataLanguageBlockWrapper createAndAddBlock(List<DataLanguageBlockWrapper> list, Block block, @Nullable final Indent indent) {
  DataLanguageBlockWrapper wrapper = DataLanguageBlockWrapper.create(block, indent);
  if (wrapper != null) {
    list.add(wrapper);
  }
  return wrapper;
}
 
Example #24
Source File: SoyBlock.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@Override
public Indent getChildIndent() {
  PsiElement element = myNode.getPsi();
  if (element instanceof TagBlockElement) {
    return Indent.getNormalIndent();
  } else if (myNode.getPsi() instanceof TagElement) {
    return Indent.getContinuationWithoutFirstIndent();
  } else {
    return Indent.getNoneIndent();
  }
}
 
Example #25
Source File: BlockFactory.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
static Block createBlock(ASTNode node, Alignment alignment, Indent indent, CodeStyleSettings settings) {
    Factory factory = REGISTRY.get(node.getElementType());
    if (factory == null) {
        // If element type is unknown it is best to keep existing formatting
        return createLeaf(node, alignment, indent, settings);
    }
    return factory.create(node, alignment, indent, settings);
}
 
Example #26
Source File: ProtoFileBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
private void appendProtoBlocks(ASTNode protoRootNode, List<Block> blocks) {
    ASTNode child = protoRootNode.getFirstChildNode();
    Alignment alignment = Alignment.createAlignment();
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
            Block block = createBlock(child, alignment, Indent.getNoneIndent(), settings);
            blocks.add(block);
        }
        child = child.getTreeNext();
    }
}
 
Example #27
Source File: StatementBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    ASTNode child = getNode().getFirstChildNode();
    List<Block> result = new ArrayList<>();
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
            Block block = BlockFactory.createBlock(child, Alignment.createAlignment(), Indent.getNoneIndent(), settings);
            result.add(block);
        }
        child = child.getTreeNext();
    }
    return result;
}
 
Example #28
Source File: ParentBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    ASTNode child = getNode().getFirstChildNode();
    State state = State.BEFORE_LEFT_CURLY_BRACE;
    List<Block> result = new ArrayList<>();
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
            IElementType elementType = child.getElementType();
            if (LCURLY.equals(elementType)) {
                state = State.AFTER_LEFT_CURLY_BRACE;
                result.add(BlockFactory.createBlock(child, myAlignment, Indent.getNoneIndent(), settings));
            } else if (RCURLY.equals(elementType)) {
                result.add(BlockFactory.createBlock(child, myAlignment, Indent.getNoneIndent(), settings));
                state = State.AFTER_RIGHT_CURLY_BRACE;
            } else {
                switch (state) {
                    case BEFORE_LEFT_CURLY_BRACE:
                        Block block = BlockFactory.createBlock(child, myAlignment, Indent.getNoneIndent(), settings);
                        headerBlocks.add(block);
                        result.add(block);
                        break;
                    case AFTER_LEFT_CURLY_BRACE:
                        result.add(BlockFactory.createBlock(child, childAlignment, Indent.getNormalIndent(true), settings));
                        break;
                    case AFTER_RIGHT_CURLY_BRACE:
                        result.add(BlockFactory.createBlock(child, myAlignment, Indent.getNoneIndent(), settings));
                        break;
                    default:
                        throw new IllegalStateException(state.toString());
                }
            }
        }
        child = child.getTreeNext();
    }
    return result;
}
 
Example #29
Source File: CypherFormattingModelBuilder.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    CypherBlock block = new CypherBlock(element.getNode(), Alignment.createAlignment(),
            Indent.getNoneIndent(), Wrap.createWrap(WrapType.NONE, false),
            settings,
            CypherFormattingModelBuilder.createSpacingBuilder(settings)
    );
    return FormattingModelProvider.createFormattingModelForPsiFile(element.getContainingFile(), block, settings);
}
 
Example #30
Source File: CypherBlock.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
CypherBlock(@NotNull ASTNode node,
            @Nullable Alignment alignment,
            @Nullable Indent indent,
            @Nullable Wrap wrap,
            @NotNull CodeStyleSettings settings,
            @NotNull SpacingBuilder spacingBuilder) {
    this.node = node;
    this.codeStyleSettings = settings;
    this.wrap = wrap;
    this.indent = indent;
    this.alignment = alignment;
    this.spacingBuilder = spacingBuilder;
}