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();
}
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;
}
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;
}
}
}
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;
}
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);
}
Aggregations