Search in sources :

Example 1 with Block

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

the class IndentOptionsDetectorImpl method calcLineIndentInfo.

@Nullable
private List<LineIndentInfo> calcLineIndentInfo(@Nullable ProgressIndicator indicator) {
    if (myDocument == null || myDocument.getLineCount() < 3 || isFileBigToDetect()) {
        return null;
    }
    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myProject);
    FormattingModelBuilder modelBuilder = LanguageFormatting.INSTANCE.forContext(myFile);
    if (modelBuilder == null)
        return null;
    FormattingModel model = modelBuilder.createModel(myFile, settings);
    Block rootBlock = model.getRootBlock();
    return new FormatterBasedLineIndentInfoBuilder(myDocument, rootBlock, indicator).build();
}
Also used : FormattingModel(com.intellij.formatting.FormattingModel) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) Block(com.intellij.formatting.Block) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Block

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

the class NewLineBlocksIterator method next.

@Override
public Block next() {
    popUntilTopBlockStartsNewLine();
    Block current = myStack.peek();
    TextRange currentBlockRange = current.getTextRange();
    myCurrentDocumentLine = myDocument.getLineNumber(currentBlockRange.getStartOffset());
    myCurrentDocumentLine++;
    if (myCurrentDocumentLine < myTotalLines) {
        myCurrentLineStartOffset = myDocument.getLineStartOffset(myCurrentDocumentLine);
        if (currentBlockRange.getEndOffset() < myCurrentLineStartOffset) {
            myStack.pop();
        } else {
            pushAll(current);
        }
    }
    return current;
}
Also used : Block(com.intellij.formatting.Block) TextRange(com.intellij.openapi.util.TextRange)

Example 3 with Block

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

the class NewLineBlocksIterator method popUntilTopBlockStartOffsetGreaterOrEqual.

private void popUntilTopBlockStartOffsetGreaterOrEqual(final int lineStartOffset) {
    while (!myStack.isEmpty()) {
        checkCancelled();
        Block current = myStack.peek();
        TextRange range = current.getTextRange();
        if (range.getStartOffset() < lineStartOffset) {
            myStack.pop();
            if (range.getEndOffset() > lineStartOffset) {
                pushAll(current);
            }
        } else {
            break;
        }
    }
}
Also used : Block(com.intellij.formatting.Block) TextRange(com.intellij.openapi.util.TextRange)

Example 4 with Block

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

the class BlockUtil method mergeBlocks.

public static List<Block> mergeBlocks(@NotNull List<TemplateLanguageBlock> tlBlocks, @NotNull List<DataLanguageBlockWrapper> foreignBlocks) {
    ArrayList<Block> result = new ArrayList<>(tlBlocks.size() + foreignBlocks.size());
    int vInd = 0;
    int fInd = 0;
    while (vInd < tlBlocks.size() && fInd < foreignBlocks.size()) {
        final TemplateLanguageBlock v = tlBlocks.get(vInd);
        final DataLanguageBlockWrapper f = foreignBlocks.get(fInd);
        final TextRange vRange = v.getTextRange();
        final TextRange fRange = f.getTextRange();
        if (vRange.getStartOffset() >= fRange.getEndOffset()) {
            // add leading foreign blocks
            result.add(f);
            fInd++;
        } else if (vRange.getEndOffset() <= fRange.getStartOffset()) {
            // add leading TL blocks
            result.add(v);
            vInd++;
        } else if (vRange.getStartOffset() < fRange.getStartOffset() || vRange.getStartOffset() == fRange.getStartOffset() && vRange.getEndOffset() >= fRange.getEndOffset()) {
            // add including TL blocks and split intersecting foreign blocks
            result.add(v);
            while (fInd < foreignBlocks.size() && vRange.contains(foreignBlocks.get(fInd).getTextRange())) {
                v.addForeignChild(foreignBlocks.get(fInd++));
            }
            if (fInd < foreignBlocks.size()) {
                final DataLanguageBlockWrapper notContainedF = foreignBlocks.get(fInd);
                if (vRange.intersectsStrict(notContainedF.getTextRange())) {
                    Pair<List<DataLanguageBlockWrapper>, List<DataLanguageBlockWrapper>> splitBlocks = splitBlocksByRightBound(notContainedF.getOriginal(), vRange);
                    v.addForeignChildren(splitBlocks.getFirst());
                    foreignBlocks.remove(fInd);
                    if (splitBlocks.getSecond().size() > 0) {
                        foreignBlocks.addAll(fInd, splitBlocks.getSecond());
                    }
                }
            }
            vInd++;
        } else if (vRange.getStartOffset() > fRange.getStartOffset() || vRange.getStartOffset() == fRange.getStartOffset() && vRange.getEndOffset() < fRange.getEndOffset()) {
            // add including foreign blocks or split them if needed
            int lastContainedTlInd = vInd;
            while (lastContainedTlInd < tlBlocks.size() && fRange.intersectsStrict(tlBlocks.get(lastContainedTlInd).getTextRange())) {
                lastContainedTlInd++;
            }
            if (fRange.contains(tlBlocks.get(lastContainedTlInd - 1).getTextRange())) {
                result.add(f);
                fInd++;
                while (vInd < lastContainedTlInd) {
                    f.addTlChild(tlBlocks.get(vInd++));
                }
            } else {
                foreignBlocks.remove(fInd);
                foreignBlocks.addAll(fInd, buildChildWrappers(f.getOriginal()));
            }
        }
    }
    while (vInd < tlBlocks.size()) {
        result.add(tlBlocks.get(vInd++));
    }
    while (fInd < foreignBlocks.size()) {
        result.add(foreignBlocks.get(fInd++));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Block(com.intellij.formatting.Block) ASTBlock(com.intellij.formatting.ASTBlock) TextRange(com.intellij.openapi.util.TextRange) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with Block

use of com.intellij.formatting.Block 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)

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