Search in sources :

Example 6 with Block

use of com.intellij.formatting.Block in project intellij-community by JetBrains.

the class PsiViewerDialog method doOKAction.

@Override
protected void doOKAction() {
    if (myBlockTreeBuilder != null) {
        Disposer.dispose(myBlockTreeBuilder);
    }
    final String text = myEditor.getDocument().getText();
    myEditor.getSelectionModel().removeSelection();
    myLastParsedText = text;
    myLastParsedTextHashCode = text.hashCode();
    myNewDocumentHashCode = myLastParsedTextHashCode;
    PsiElement rootElement = parseText(text);
    focusTree();
    ViewerTreeStructure structure = getTreeStructure();
    structure.setRootPsiElement(rootElement);
    myPsiTreeBuilder.queueUpdate();
    myPsiTree.setRootVisible(true);
    myPsiTree.expandRow(0);
    myPsiTree.setRootVisible(false);
    if (!myShowBlocksCheckBox.isSelected()) {
        return;
    }
    Block rootBlock = rootElement == null ? null : buildBlocks(rootElement);
    if (rootBlock == null) {
        myBlockTreeBuilder = null;
        myBlockTree.setRootVisible(false);
        myBlockTree.setVisible(false);
        return;
    }
    myBlockTree.setVisible(true);
    BlockTreeStructure blockTreeStructure = new BlockTreeStructure();
    BlockTreeNode rootNode = new BlockTreeNode(rootBlock, null);
    blockTreeStructure.setRoot(rootNode);
    myBlockTreeBuilder = new BlockTreeBuilder(myBlockTree, blockTreeStructure);
    myPsiToBlockMap = new HashMap<>();
    final PsiElement psiFile = (getTreeStructure()).getRootPsiElement();
    initMap(rootNode, psiFile);
    PsiElement rootPsi = rootNode.getBlock() instanceof ASTBlock ? ((ASTBlock) rootNode.getBlock()).getNode().getPsi() : rootElement;
    BlockTreeNode blockNode = myPsiToBlockMap.get(rootPsi);
    if (blockNode == null) {
        LOG.error(LogMessageEx.createEvent("PsiViewer: rootNode not found", "Current language: " + rootElement.getContainingFile().getLanguage(), AttachmentFactory.createAttachment(rootElement.getContainingFile().getOriginalFile().getVirtualFile())));
        blockNode = findBlockNode(rootPsi);
    }
    blockTreeStructure.setRoot(blockNode);
    myBlockTree.addTreeSelectionListener(new MyBlockTreeSelectionListener());
    myBlockTree.setRootVisible(true);
    myBlockTree.expandRow(0);
    myBlockTreeBuilder.queueUpdate();
}
Also used : BlockTreeBuilder(com.intellij.internal.psiView.formattingblocks.BlockTreeBuilder) BlockTreeNode(com.intellij.internal.psiView.formattingblocks.BlockTreeNode) ASTBlock(com.intellij.formatting.ASTBlock) BlockTreeStructure(com.intellij.internal.psiView.formattingblocks.BlockTreeStructure) Block(com.intellij.formatting.Block) ASTBlock(com.intellij.formatting.ASTBlock)

Example 7 with Block

use of com.intellij.formatting.Block in project intellij-community by JetBrains.

the class ExtendsListBlock method buildChildren.

@Override
protected List<Block> buildChildren() {
    final ArrayList<Block> result = new ArrayList<>();
    ArrayList<Block> elementsExceptKeyword = new ArrayList<>();
    myChildAlignment = createChildAlignment();
    myChildIndent = Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS);
    myUseChildAttributes = true;
    Wrap childWrap = createChildWrap();
    ASTNode child = myNode.getFirstChildNode();
    Alignment alignment = alignList() ? Alignment.createAlignment() : null;
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) {
            IElementType elementType = child.getElementType();
            if (ElementType.KEYWORD_BIT_SET.contains(elementType)) {
                if (!elementsExceptKeyword.isEmpty()) {
                    result.add(new SyntheticCodeBlock(elementsExceptKeyword, null, mySettings, myJavaSettings, Indent.getNoneIndent(), null));
                    elementsExceptKeyword = new ArrayList<>();
                }
                Indent indent = mySettings.ALIGN_THROWS_KEYWORD && elementType == JavaTokenType.THROWS_KEYWORD ? Indent.getNoneIndent() : myChildIndent;
                result.add(createJavaBlock(child, mySettings, myJavaSettings, indent, arrangeChildWrap(child, childWrap), alignment));
            } else {
                Alignment candidate = myAlignmentStrategy.getAlignment(elementType);
                if (candidate != null) {
                    alignment = myChildAlignment = candidate;
                }
                processChild(elementsExceptKeyword, child, myChildAlignment, childWrap, myChildIndent);
            }
        }
        child = child.getTreeNext();
    }
    if (!elementsExceptKeyword.isEmpty()) {
        result.add(new SyntheticCodeBlock(elementsExceptKeyword, alignment, mySettings, myJavaSettings, Indent.getNoneIndent(), null));
    }
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Alignment(com.intellij.formatting.Alignment) Indent(com.intellij.formatting.Indent) Wrap(com.intellij.formatting.Wrap) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) Block(com.intellij.formatting.Block)

Example 8 with Block

use of com.intellij.formatting.Block in project intellij-community by JetBrains.

the class CallChunkBlockBuilder method createJavaBlocks.

@NotNull
private List<Block> createJavaBlocks(@NotNull final List<ASTNode> subNodes) {
    final ArrayList<Block> result = new ArrayList<>();
    for (ASTNode node : subNodes) {
        Indent indent = Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS);
        result.add(newJavaBlock(node, mySettings, myJavaSettings, indent, null, AlignmentStrategy.getNullStrategy()));
    }
    return result;
}
Also used : Indent(com.intellij.formatting.Indent) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) AbstractJavaBlock.newJavaBlock(com.intellij.psi.formatter.java.AbstractJavaBlock.newJavaBlock) Block(com.intellij.formatting.Block) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with Block

use of com.intellij.formatting.Block in project intellij-community by JetBrains.

the class CallChunkBlockBuilder method create.

@NotNull
public Block create(@NotNull final List<ASTNode> subNodes, final Wrap wrap, @Nullable final Alignment alignment) {
    final ArrayList<Block> subBlocks = new ArrayList<>();
    final ASTNode firstNode = subNodes.get(0);
    if (firstNode.getElementType() == JavaTokenType.DOT) {
        AlignmentStrategy strategy = AlignmentStrategy.getNullStrategy();
        Block block = newJavaBlock(firstNode, mySettings, myJavaSettings, Indent.getNoneIndent(), null, strategy);
        subBlocks.add(block);
        subNodes.remove(0);
        if (!subNodes.isEmpty()) {
            subBlocks.add(create(subNodes, wrap, null));
        }
        return new SyntheticCodeBlock(subBlocks, alignment, mySettings, myJavaSettings, Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS), wrap);
    }
    return new SyntheticCodeBlock(createJavaBlocks(subNodes), alignment, mySettings, myJavaSettings, Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS), null);
}
Also used : ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) AbstractJavaBlock.newJavaBlock(com.intellij.psi.formatter.java.AbstractJavaBlock.newJavaBlock) Block(com.intellij.formatting.Block) AlignmentStrategy(com.intellij.formatting.alignment.AlignmentStrategy) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with Block

use of com.intellij.formatting.Block in project intellij-community by JetBrains.

the class ChainedCallChunk method buildBlocksFrom.

private List<Block> buildBlocksFrom(List<ASTNode> nodes) {
    List<ChainedCallChunk> methodCall = splitMethodCallOnChunksByDots(nodes);
    Wrap wrap = null;
    Alignment chainedCallsAlignment = null;
    List<Block> blocks = new ArrayList<>();
    for (int i = 0; i < methodCall.size(); i++) {
        ChainedCallChunk currentCallChunk = methodCall.get(i);
        if (isMethodCall(currentCallChunk) || isComment(currentCallChunk)) {
            if (wrap == null) {
                wrap = createCallChunkWrap(i, methodCall);
            }
            if (chainedCallsAlignment == null) {
                chainedCallsAlignment = createCallChunkAlignment(i, methodCall);
            }
        } else {
            wrap = null;
            chainedCallsAlignment = null;
        }
        CallChunkBlockBuilder builder = new CallChunkBlockBuilder(mySettings, myJavaSettings);
        blocks.add(builder.create(currentCallChunk.nodes, wrap, chainedCallsAlignment));
    }
    return blocks;
}
Also used : Alignment(com.intellij.formatting.Alignment) Wrap(com.intellij.formatting.Wrap) ArrayList(java.util.ArrayList) Block(com.intellij.formatting.Block)

Aggregations

Block (com.intellij.formatting.Block)27 ArrayList (java.util.ArrayList)14 ASTNode (com.intellij.lang.ASTNode)9 NotNull (org.jetbrains.annotations.NotNull)9 ASTBlock (com.intellij.formatting.ASTBlock)8 Indent (com.intellij.formatting.Indent)6 TextRange (com.intellij.openapi.util.TextRange)6 Alignment (com.intellij.formatting.Alignment)5 Wrap (com.intellij.formatting.Wrap)4 FormattingModel (com.intellij.formatting.FormattingModel)3 FormattingModelBuilder (com.intellij.formatting.FormattingModelBuilder)3 Document (com.intellij.openapi.editor.Document)3 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)3 NewLineBlocksIterator (com.intellij.psi.formatter.common.NewLineBlocksIterator)3 IElementType (com.intellij.psi.tree.IElementType)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiComment (com.intellij.psi.PsiComment)2 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)2 AbstractJavaBlock.newJavaBlock (com.intellij.psi.formatter.java.AbstractJavaBlock.newJavaBlock)2 IndentInheritingBlock (com.intellij.xml.template.formatter.IndentInheritingBlock)2