com.intellij.psi.formatter.FormatterUtil Java Examples

The following examples show how to use com.intellij.psi.formatter.FormatterUtil. 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: 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 #2
Source File: HaxeWrappingProcessor.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private static Wrap createChildWrap(ASTNode child, int parentWrap, boolean newLineAfterLBrace, boolean newLineBeforeRBrace) {
  IElementType childType = child.getElementType();
  if (childType != PLPAREN && childType != PRPAREN) {
    if (FormatterUtil.isPrecededBy(child, PLBRACK)) {
      if (newLineAfterLBrace) {
        return Wrap.createChildWrap(Wrap.createWrap(parentWrap, true), WrapType.ALWAYS, true);
      }
      else {
        return Wrap.createWrap(WrapType.NONE, true);
      }
    }
    return Wrap.createWrap(WrappingUtil.getWrapType(parentWrap), true);
  }
  if (childType == PRBRACK && newLineBeforeRBrace) {
    return Wrap.createWrap(WrapType.ALWAYS, true);
  }
  return Wrap.createWrap(WrapType.NONE, true);
}
 
Example #3
Source File: TemplateLanguageBlock.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected List<Block> buildChildren() {
  myChildrenBuilt = true;
  if (isLeaf()) {
    return EMPTY;
  }
  final ArrayList<TemplateLanguageBlock> tlChildren = new ArrayList<>(5);
  for (ASTNode childNode = getNode().getFirstChildNode(); childNode != null; childNode = childNode.getTreeNext()) {
    if (FormatterUtil.containsWhiteSpacesOnly(childNode)) continue;
    if (shouldBuildBlockFor(childNode)) {
      final TemplateLanguageBlock childBlock = myBlockFactory
              .createTemplateLanguageBlock(childNode, createChildWrap(childNode), createChildAlignment(childNode), null, mySettings);
      childBlock.setParent(this);
      tlChildren.add(childBlock);
    }
  }
  final List<Block> children = (List<Block>)(myForeignChildren == null ? tlChildren : BlockUtil.mergeBlocks(tlChildren, myForeignChildren));
  //BlockUtil.printBlocks(getTextRange(), children);
  return BlockUtil.setParent(children, this);
}
 
Example #4
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 #5
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 #6
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 #7
Source File: HaxeBlock.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
  myChildrenBuilt = true;
  if (isLeaf()) {
    return EMPTY;
  }
  final ArrayList<Block> tlChildren = new ArrayList<Block>();
  for (ASTNode childNode = getNode().getFirstChildNode(); childNode != null; childNode = childNode.getTreeNext()) {
    if (FormatterUtil.containsWhiteSpacesOnly(childNode)) continue;
    final HaxeBlock childBlock = new HaxeBlock(childNode, createChildWrap(childNode), createChildAlignment(childNode), mySettings);
    childBlock.setParent(this);
    tlChildren.add(childBlock);
  }
  return tlChildren;
}
 
Example #8
Source File: EnterHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private PsiComment createComment(final CharSequence buffer, final CodeInsightSettings settings) throws IncorrectOperationException {
  myDocument.insertString(myOffset, buffer);

  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
  CodeStyleManager.getInstance(getProject()).adjustLineIndent(myFile, myOffset + buffer.length() - 2);

  PsiComment comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class);

  comment = createJavaDocStub(settings, comment, getProject());
  if (comment == null) {
    return null;
  }

  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
  final Ref<PsiComment> commentRef = Ref.create(comment);
  codeStyleManager.runWithDocCommentFormattingDisabled(myFile, () -> formatComment(commentRef, codeStyleManager));
  comment = commentRef.get();

  PsiElement next = comment.getNextSibling();
  if (next == null && comment.getParent().getClass() == comment.getClass()) {
    next = comment.getParent().getNextSibling(); // expanding chameleon comment produces comment under comment
  }
  if (next != null) {
    next = myFile.findElementAt(next.getTextRange().getStartOffset()); // maybe switch to another tree
  }
  if (next != null && (!FormatterUtil.containsWhiteSpacesOnly(next.getNode()) || !next.getText().contains(LINE_SEPARATOR))) {
    int lineBreakOffset = comment.getTextRange().getEndOffset();
    myDocument.insertString(lineBreakOffset, LINE_SEPARATOR);
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    codeStyleManager.adjustLineIndent(myFile, lineBreakOffset + 1);
    comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class);
  }
  return comment;
}
 
Example #9
Source File: AbstractBlock.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isIncomplete() {
  if (myIncomplete == null) {
    myIncomplete = FormatterUtil.isIncomplete(getNode());
  }
  return myIncomplete;
}
 
Example #10
Source File: CodeStyleManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void reformatText(@Nonnull PsiFile file, @Nonnull FormatTextRanges ranges, @Nullable Editor editor) throws IncorrectOperationException {
  if (ranges.isEmpty()) {
    return;
  }
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();

  CheckUtil.checkWritable(file);
  if (!SourceTreeToPsiMap.hasTreeElement(file)) {
    return;
  }

  ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(file);
  transformAllChildren(treeElement);

  LOG.assertTrue(file.isValid(), "File name: " + file.getName() + " , class: " + file.getClass().getSimpleName());

  if (editor == null) {
    editor = PsiUtilBase.findEditor(file);
  }

  CaretPositionKeeper caretKeeper = null;
  if (editor != null) {
    caretKeeper = new CaretPositionKeeper(editor, getSettings(file), file.getLanguage());
  }

  if (FormatterUtil.isFormatterCalledExplicitly()) {
    removeEndingWhiteSpaceFromEachRange(file, ranges);
  }

  formatRanges(file, ranges, ExternalFormatProcessor.useExternalFormatter(file) ? null  // do nothing, delegate the external formatting activity to post-processor
                                                                                : () -> {
                                                                                  final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(file), file.getLanguage());
                                                                                  codeFormatter.processText(file, ranges, true);
                                                                                });

  if (caretKeeper != null) {
    caretKeeper.restoreCaretPosition();
  }
}
 
Example #11
Source File: FormatterImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void setProgressTask(@Nonnull FormattingProgressTask progressIndicator) {
  if (!FormatterUtil.isFormatterCalledExplicitly()) {
    return;
  }
  myProgressTask.set(progressIndicator);
}
 
Example #12
Source File: CypherBlock.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 4 votes vote down vote up
private static boolean isReturnBodyKeywords(ASTNode node) {
    return FormatterUtil.isOneOf(node,
            CypherTypes.LIMIT,
            CypherTypes.SKIP,
            CypherTypes.ORDER);
}
 
Example #13
Source File: HaxeWrappingProcessor.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
Wrap createChildWrap(ASTNode child, Wrap defaultWrap, Wrap childWrap) {
  final IElementType childType = child.getElementType();
  final IElementType elementType = myNode.getElementType();
  if (childType == OCOMMA || childType == OSEMI) return defaultWrap;

  //
  // Function definition/call
  //
  if (elementType == PARAMETER_LIST || elementType == EXPRESSION_LIST) {
    final ASTNode parent = myNode.getTreeParent();
    if (parent == null) {
      return defaultWrap;
    }
    final IElementType parentType = parent.getElementType();
    if (parentType == CALL_EXPRESSION &&
        mySettings.CALL_PARAMETERS_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
      if (myNode.getFirstChildNode() == child) {
        return createWrap(mySettings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE);
      }
      if (!mySettings.PREFER_PARAMETERS_WRAP && childWrap != null) {
        return Wrap.createChildWrap(childWrap, WrappingUtil.getWrapType(mySettings.CALL_PARAMETERS_WRAP), true);
      }
      return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.CALL_PARAMETERS_WRAP), true);
    }
    if (FUNCTION_DEFINITION.contains(parentType) &&
        mySettings.METHOD_PARAMETERS_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
      if (myNode.getFirstChildNode() == child) {
        return createWrap(mySettings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE);
      }
      if (childType == PRPAREN) {
        return createWrap(mySettings.METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE);
      }
      return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.METHOD_PARAMETERS_WRAP), true);
    }
  }

  if (elementType == CALL_EXPRESSION) {
    if (mySettings.CALL_PARAMETERS_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
      if (childType == PRPAREN) {
        return createWrap(mySettings.CALL_PARAMETERS_RPAREN_ON_NEXT_LINE);
      }
    }
  }

  //
  // If
  //
  if (elementType == IF_STATEMENT && childType == KELSE) {
    return createWrap(mySettings.ELSE_ON_NEW_LINE);
  }

  //
  //Binary expressions
  //
  if (BINARY_EXPRESSIONS.contains(elementType) && mySettings.BINARY_OPERATION_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
    if ((mySettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE && BINARY_OPERATORS.contains(childType)) ||
        (!mySettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE && isRightOperand(child))) {
      return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.BINARY_OPERATION_WRAP), true);
    }
  }

  //
  // Assignment
  //
  if (elementType == ASSIGN_EXPRESSION && mySettings.ASSIGNMENT_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
    if (childType != ASSIGN_OPERATION) {
      if (FormatterUtil.isPrecededBy(child, ASSIGN_OPERATION) &&
          mySettings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE) {
        return Wrap.createWrap(WrapType.NONE, true);
      }
      return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.ASSIGNMENT_WRAP), true);
    }
    else if (mySettings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE) {
      return Wrap.createWrap(WrapType.NORMAL, true);
    }
  }

  //
  // Ternary expressions
  //
  if (elementType == TERNARY_EXPRESSION) {
    if (myNode.getFirstChildNode() != child) {
      if (mySettings.TERNARY_OPERATION_SIGNS_ON_NEXT_LINE) {
        if (!FormatterUtil.isPrecededBy(child, OQUEST) &&
            !FormatterUtil.isPrecededBy(child, OCOLON)) {
          return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.TERNARY_OPERATION_WRAP), true);
        }
      }
      else if (childType != OQUEST && childType != OCOLON) {
        return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.TERNARY_OPERATION_WRAP), true);
      }
    }
    return Wrap.createWrap(WrapType.NONE, true);
  }
  return defaultWrap;
}
 
Example #14
Source File: XQueryFormattingBlock.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
private IElementType getTypeOfPreviousElement(ASTNode myNode) {
    ASTNode prevSibling = FormatterUtil.getPreviousNonWhitespaceSibling(myNode);
    IElementType prevType = prevSibling != null ? prevSibling.getElementType() : null;
    return prevType;
}