com.intellij.formatting.Alignment Java Examples

The following examples show how to use com.intellij.formatting.Alignment. 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: 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 #2
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 #3
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 #4
Source File: CypherBlock.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public List<Block> getSubBlocks() {
    if (subBlocks == null) {
        Predicate<ASTNode> isWhitespaceOrEmpty = CypherBlock::isWhitespaceOrEmpty;
        AlignmentStrategy.AlignmentPerTypeStrategy strategy = createStrategy(getNode());

        subBlocks = new ArrayList<>();
        for (ASTNode subNode = node.getFirstChildNode(); subNode != null; subNode = subNode.getTreeNext()) {
            if (isWhitespaceOrEmpty.test(subNode)) {
                continue;
            }

            Alignment alignment = strategy != null
                        ? strategy.getAlignment(getNode().getElementType(), subNode.getElementType())
                        : null;

            CypherBlock block = makeSubBlock(subNode, alignment);
            subBlocks.add(block);
        }
    }
    return subBlocks;
}
 
Example #5
Source File: SoyFormattingModelBuilder.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@Override
public TemplateLanguageBlock createTemplateLanguageBlock(
    @NotNull ASTNode node,
    @Nullable Wrap wrap,
    @Nullable Alignment alignment,
    @Nullable List<DataLanguageBlockWrapper> foreignChildren,
    @NotNull CodeStyleSettings codeStyleSettings) {
  final FormattingDocumentModelImpl documentModel =
      FormattingDocumentModelImpl.createOn(node.getPsi().getContainingFile());
  if (node.getPsi() instanceof TagElement) {
    return new SoyTagBlock(
        this,
        codeStyleSettings,
        node,
        foreignChildren,
        new HtmlPolicy(codeStyleSettings, documentModel));
  } else if(node.getPsi() instanceof TagBlockElement) {
    return new SoyTagBlockBlock(
        this,
        codeStyleSettings,
        node,
        foreignChildren,
        new HtmlPolicy(codeStyleSettings, documentModel));
  } else if (node.getPsi() instanceof SoyStatementList) {
    return new SoyStatementListBlock(
        this,
        codeStyleSettings,
        node,
        foreignChildren,
        new HtmlPolicy(codeStyleSettings, documentModel));
  } else {
    return new SoyBlock(
      this,
      codeStyleSettings,
      node,
      foreignChildren,
      new HtmlPolicy(codeStyleSettings, documentModel));
  }
}
 
Example #6
Source File: AlignmentStrategy.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Alignment getAlignment(@Nullable IElementType parentType, @Nullable IElementType childType) {
  if (myParentType != null && parentType != null && myParentType != parentType) {
    return null;
  }
  return myAlignments.get(childType);
}
 
Example #7
Source File: AlignmentStrategy.java    From consulo with Apache License 2.0 5 votes vote down vote up
AlignmentPerTypeStrategy(Collection<IElementType> targetElementTypes,
                         IElementType parentType,
                         boolean allowBackwardShift,
                         Alignment.Anchor anchor)
{
  myParentType = parentType;
  myAllowBackwardShift = allowBackwardShift;
  for (IElementType elementType : targetElementTypes) {
    myAlignments.put(elementType, Alignment.createAlignment(myAllowBackwardShift, anchor));
  }
}
 
Example #8
Source File: HaxeAlignmentProcessor.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Nullable
public Alignment createChildAlignment() {
  IElementType elementType = myNode.getElementType();
  ASTNode parent = myNode.getTreeParent();
  IElementType parentType = parent == null ? null : parent.getElementType();

  if (BINARY_EXPRESSIONS.contains(elementType) && mySettings.ALIGN_MULTILINE_BINARY_OPERATION) {
    return myBaseAlignment;
  }

  if (elementType == TERNARY_EXPRESSION && mySettings.ALIGN_MULTILINE_TERNARY_OPERATION) {
    return myBaseAlignment;
  }

  if (elementType == PARAMETER_LIST || elementType == EXPRESSION_LIST) {
    boolean doAlign = false;
    if (FUNCTION_DEFINITION.contains(parentType)) {
      doAlign = mySettings.ALIGN_MULTILINE_PARAMETERS;
    }
    else if (parentType == CALL_EXPRESSION) {
      doAlign = mySettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS;
    }
    if (doAlign) {
      return myBaseAlignment;
    }
  }

  return null;
}
 
Example #9
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 #10
Source File: GraphQLBlockWrapper.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
protected GraphQLBlockWrapper(Block wrapped, @Nullable GraphQLBlockWrapper parent, @NotNull ASTNode node, @Nullable Wrap wrap, @Nullable Alignment alignment, SpacingBuilder spacingBuilder, CodeStyleSettings settings) {
    super(node, wrap, alignment);
    this.wrapped = wrapped;
    this.parent = parent;
    this.spacingBuilder = spacingBuilder;
    this.settings = settings;
}
 
Example #11
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;
}
 
Example #12
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 #13
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 #14
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 #15
Source File: FormattingModelBuilder.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(ProtoLanguage.INSTANCE);
    ASTNode fileNode = containingFile.getNode();
    Wrap wrap = Wrap.createWrap(WrapType.NONE, false);
    Alignment alignment = Alignment.createAlignment();
    ProtoFileBlock block = new ProtoFileBlock(fileNode, wrap, alignment, settings);
    return FormattingModelProvider.createFormattingModelForPsiFile(containingFile, block, settings);
}
 
Example #16
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 #17
Source File: HaxeAlignmentProcessor.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
public HaxeAlignmentProcessor(ASTNode node, CommonCodeStyleSettings settings) {
  myNode = node;
  mySettings = settings;
  myBaseAlignment = Alignment.createAlignment();
}
 
Example #18
Source File: AlignmentStrategy.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void renewAlignment(IElementType elementType) {
  myAlignments.put(elementType, Alignment.createAlignment(myAllowBackwardShift));
}
 
Example #19
Source File: FluidFormattingModelBuilder.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
@Override
protected Block createTemplateLanguageBlock(ASTNode node, CodeStyleSettings settings, XmlFormattingPolicy xmlFormattingPolicy, Indent indent, @Nullable Alignment alignment, @Nullable Wrap wrap) {
    PsiElement nodePsi = node.getPsi();

    return (nodePsi instanceof FluidStatement && hasInjection(nodePsi) ? new FluidBlockWithInjection(this, node, wrap, alignment, settings, xmlFormattingPolicy, indent) : new FluidBlock(this, node, wrap, alignment, settings, xmlFormattingPolicy, indent));
}
 
Example #20
Source File: SoyBlock.java    From bamboo-soy with Apache License 2.0 4 votes vote down vote up
@Override
public Alignment getAlignment() {
  return null;
}
 
Example #21
Source File: AlignmentStrategy.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public Alignment getAlignment(@Nullable IElementType parentType, @Nullable IElementType childType) {
  return (myFilterElementTypes.contains(childType) ^ myIgnoreFilterTypes) ? myAlignment : null;
}
 
Example #22
Source File: AlignmentStrategy.java    From consulo with Apache License 2.0 4 votes vote down vote up
private SharedAlignmentStrategy(Alignment alignment, boolean ignoreFilterTypes, IElementType... disabledElementTypes) {
  myAlignment = alignment;
  myIgnoreFilterTypes = ignoreFilterTypes;
  myFilterElementTypes.addAll(asList(disabledElementTypes));
}
 
Example #23
Source File: TemplateLanguageBlockFactory.java    From consulo with Apache License 2.0 4 votes vote down vote up
TemplateLanguageBlock createTemplateLanguageBlock(@Nonnull ASTNode node,
@Nullable Wrap wrap,
@Nullable Alignment alignment,
@Nullable List<DataLanguageBlockWrapper> foreignChildren,
@Nonnull CodeStyleSettings codeStyleSettings);
 
Example #24
Source File: XQueryFormattingBlock.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
public XQueryFormattingBlock(@NotNull ASTNode node, @Nullable Wrap wrap, @Nullable Alignment alignment,
                             @NotNull CommonCodeStyleSettings settings, @NotNull SpacingBuilder spacingBuilder) {
    super(node, wrap, alignment);
    this.spacingBuilder = spacingBuilder;
    this.settings = settings;
}
 
Example #25
Source File: ParentBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 4 votes vote down vote up
ParentBlock(@NotNull ASTNode node, @Nullable Alignment alignment,
            Indent indent, CodeStyleSettings settings) {
    super(node, alignment, indent, settings);
    childAlignment = Alignment.createAlignment();
}
 
Example #26
Source File: CSharpDisabledFormattingBlock.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public Alignment getAlignment()
{
	return null;
}
 
Example #27
Source File: BuckBlock.java    From Buck-IntelliJ-Plugin with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public Alignment getAlignment() {
  return myAlignment;
}
 
Example #28
Source File: LeafBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 4 votes vote down vote up
LeafBlock(ASTNode node, Alignment alignment, Indent indent, CodeStyleSettings settings) {
    this.node = node;
    this.alignment = alignment;
    myIndent = indent;
}
 
Example #29
Source File: LeafBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public Alignment getAlignment() {
    return alignment;
}
 
Example #30
Source File: BlockFactory.java    From protobuf-jetbrains-plugin with Apache License 2.0 4 votes vote down vote up
@NotNull
private static LeafBlock createLeaf(ASTNode node, Alignment alignment, Indent indent, CodeStyleSettings settings) {
    return new LeafBlock(node, alignment, indent, settings);
}