Search in sources :

Example 1 with ASTBlock

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

the class BlockUtil method printBlocks.

static void printBlocks(@Nullable TextRange textRange, @NotNull List<Block> list) {
    StringBuilder sb = new StringBuilder(String.valueOf(textRange)).append(": ");
    for (Block block : list) {
        ASTNode node = block instanceof ASTBlock ? ((ASTBlock) block).getNode() : null;
        TextRange r = block.getTextRange();
        //.append(" ").append(((BlockWithParent)block).getParent() != null)
        sb.append(" [").append(node != null ? node.getElementType() : null).append(r).append(block.getIndent()).append(block.getAlignment()).append("] ");
    }
    System.out.println(sb);
}
Also used : ASTBlock(com.intellij.formatting.ASTBlock) ASTNode(com.intellij.lang.ASTNode) Block(com.intellij.formatting.Block) ASTBlock(com.intellij.formatting.ASTBlock) TextRange(com.intellij.openapi.util.TextRange)

Example 2 with ASTBlock

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

the class PsiViewerDialog method initMap.

private void initMap(BlockTreeNode rootBlockNode, PsiElement psiEl) {
    JBTreeTraverser<BlockTreeNode> traverser = new JBTreeTraverser<>(o -> JBIterable.of(o.getChildren()));
    for (BlockTreeNode block : traverser.withRoot(rootBlockNode)) {
        PsiElement currentElem = null;
        if (block.getBlock() instanceof ASTBlock) {
            ASTNode node = ((ASTBlock) block.getBlock()).getNode();
            if (node != null) {
                currentElem = node.getPsi();
            }
        }
        if (currentElem == null) {
            currentElem = InjectedLanguageUtil.findElementAtNoCommit(psiEl.getContainingFile(), block.getBlock().getTextRange().getStartOffset());
        }
        myPsiToBlockMap.put(currentElem, block);
        //nested PSI elements with same ranges will be mapped to one blockNode
        //    assert currentElem != null;      //for Scala-language plugin etc it can be null, because formatterBlocks is not instance of ASTBlock
        TextRange curTextRange = currentElem.getTextRange();
        PsiElement parentElem = currentElem.getParent();
        while (parentElem != null && parentElem.getTextRange().equals(curTextRange)) {
            myPsiToBlockMap.put(parentElem, block);
            parentElem = parentElem.getParent();
        }
    }
}
Also used : BlockTreeNode(com.intellij.internal.psiView.formattingblocks.BlockTreeNode) ASTBlock(com.intellij.formatting.ASTBlock) ASTNode(com.intellij.lang.ASTNode) JBTreeTraverser(com.intellij.util.containers.JBTreeTraverser) TextRange(com.intellij.openapi.util.TextRange)

Example 3 with ASTBlock

use of com.intellij.formatting.ASTBlock 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 4 with ASTBlock

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

the class FormatterBasedLineIndentInfoBuilder method getBlocksStartingNewLine.

@NotNull
private List<Block> getBlocksStartingNewLine() {
    NewLineBlocksIterator newLineBlocksIterator = new NewLineBlocksIterator(myRootBlock, myDocument, myProgressIndicator);
    List<Block> newLineBlocks = new ArrayList<>();
    int currentLine = 0;
    while (newLineBlocksIterator.hasNext() && currentLine < MAX_NEW_LINE_BLOCKS_TO_PROCESS) {
        Block next = newLineBlocksIterator.next();
        if (next instanceof ASTBlock && ((ASTBlock) next).getNode() instanceof PsiComment) {
            continue;
        }
        newLineBlocks.add(next);
        currentLine++;
    }
    return newLineBlocks;
}
Also used : PsiComment(com.intellij.psi.PsiComment) ASTBlock(com.intellij.formatting.ASTBlock) ArrayList(java.util.ArrayList) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) ASTBlock(com.intellij.formatting.ASTBlock) Block(com.intellij.formatting.Block) NewLineBlocksIterator(com.intellij.psi.formatter.common.NewLineBlocksIterator) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ASTBlock (com.intellij.formatting.ASTBlock)4 Block (com.intellij.formatting.Block)3 BlockTreeNode (com.intellij.internal.psiView.formattingblocks.BlockTreeNode)2 ASTNode (com.intellij.lang.ASTNode)2 TextRange (com.intellij.openapi.util.TextRange)2 BlockTreeBuilder (com.intellij.internal.psiView.formattingblocks.BlockTreeBuilder)1 BlockTreeStructure (com.intellij.internal.psiView.formattingblocks.BlockTreeStructure)1 PsiComment (com.intellij.psi.PsiComment)1 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)1 NewLineBlocksIterator (com.intellij.psi.formatter.common.NewLineBlocksIterator)1 JBTreeTraverser (com.intellij.util.containers.JBTreeTraverser)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1