Java源码示例:com.intellij.formatting.Block
示例1
@Nullable
@Override
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
if (!myFormattingInfo.getCsvCodeStyleSettings().TABULARIZE || myFormattingInfo.getCsvCodeStyleSettings().WHITE_SPACES_OUTSIDE_QUOTES || child1 == null) {
return null;
}
int spaces = 0;
CsvBlockElement block1 = (CsvBlockElement) child1;
CsvBlockElement block2 = (CsvBlockElement) child2;
if ((block1.getElementType() == CsvTypes.QUOTE && myFormattingInfo.getCsvCodeStyleSettings().LEADING_WHITE_SPACES) ||
(block2.getElementType() == CsvTypes.QUOTE && !myFormattingInfo.getCsvCodeStyleSettings().LEADING_WHITE_SPACES)) {
spaces = getColumnInfo().getMaxLength() - getTextLength();
} else {
return myFormattingInfo.getSpacingBuilder().getSpacing(this, child1, child2);
}
return Spacing.createSpacing(spaces, spaces, 0, true, 0);
}
示例2
@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;
}
示例3
@Nullable
@Override
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
if (child1 == null) {
return StatementBlock.NONE;
}
if (child2 instanceof ASTBlock) {
ASTBlock block = (ASTBlock) child2;
IElementType elementType = block.getNode().getElementType();
// Do not move trailing comments to new line.
if (LINE_COMMENT.equals(elementType)
|| COMMENT.equals(elementType)) {
return SPACE_OR_NEW_LINE;
}
}
return StatementBlock.NEW_LINE;
}
示例4
@Nullable
@Override
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
if (child2 instanceof ASTBlock) {
ASTBlock block = (ASTBlock) child2;
// Do not move semicolon after '}' to new line.
IElementType elementType = block.getNode().getElementType();
if (SEMICOLON.equals(elementType)) {
return NONE;
}
// Do not move trailing comments to new line.
if (LINE_COMMENT.equals(elementType)
|| COMMENT.equals(elementType)) {
return SPACE_OR_NEW_LINE;
}
}
if (headerBlocks.contains(child1)) {
return super.getSpacing(child1, child2);
}
return NEW_LINE;
}
示例5
@NotNull
@Override
public List<Block> getSubBlocks() {
if (subBlocks == null) {
Predicate<ASTNode> isWhitespaceOrEmpty = CypherBlock::isWhitespaceOrEmpty;
AlignmentStrategy.AlignmentPerTypeStrategy strategy = createStrategy(getNode());
subBlocks = new ArrayList<>();
for (ASTNode subNode = node.getFirstChildNode(); subNode != null; subNode = subNode.getTreeNext()) {
if (isWhitespaceOrEmpty.test(subNode)) {
continue;
}
Alignment alignment = strategy != null
? strategy.getAlignment(getNode().getElementType(), subNode.getElementType())
: null;
CypherBlock block = makeSubBlock(subNode, alignment);
subBlocks.add(block);
}
}
return subBlocks;
}
示例6
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
if(element instanceof JSFile || element.getContainingFile() instanceof JSFile) {
final JSFile file = (JSFile)(element instanceof JSFile ? element : element.getContainingFile());
file.putUserData(WANT_DEFAULT_FORMATTER_KEY, true);
try {
final FormattingModelBuilder formattingModelBuilder = LanguageFormatting.INSTANCE.forContext(file.getLanguage(), element);
if (formattingModelBuilder != null) {
final FormattingModel model = formattingModelBuilder.createModel(element, settings);
final Block rootBlock = model.getRootBlock();
return new DelegatingFormattingModel(model, new GraphQLBlockWrapper(rootBlock, null, element.getNode(), rootBlock.getWrap(), rootBlock.getAlignment(), createSpaceBuilder(settings, element), settings));
}
} finally {
file.putUserData(WANT_DEFAULT_FORMATTER_KEY, null);
}
}
throw new IllegalArgumentException("Unsupported element '" + element + "'. It must be an element in a JSFile with its own default formatter to support injected GraphQL formatting");
}
示例7
@Override
protected List<Block> buildChildren() {
List<Block> blocks = new ArrayList<>();
ASTNode child = myNode.getFirstChildNode();
while (child != null) {
if (!TokenType.WHITE_SPACE.equals(child.getElementType()) && !TokenType.BAD_CHARACTER.equals(child.getElementType())) {
if (!child.getTextRange().isEmpty()) {
JSGraphQLEndpointBlock block = new JSGraphQLEndpointBlock(this, child, null, null, spacingBuilder);
blocks.add(block);
}
}
child = child.getTreeNext();
}
return blocks;
}
示例8
public static List<Block> generateSubBlocks(ASTNode node,
Alignment _myAlignment,
Wrap _myWrap,
CodeStyleSettings _mySettings,
BashBlock block) {
myWrap = _myWrap;
mySettings = _mySettings;
myAlignment = _myAlignment;
// For other cases
final List<Block> subBlocks = new ArrayList<Block>();
ASTNode children[] = getBashChildren(node);
ASTNode prevChildNode = null;
for (ASTNode childNode : children) {
if (canBeCorrectBlock(childNode)) {
final Indent indent = BashIndentProcessor.getChildIndent(block, prevChildNode, childNode);
subBlocks.add(new BashBlock(childNode, myAlignment, indent, myWrap, mySettings));
prevChildNode = childNode;
}
}
return subBlocks;
}
示例9
@Nullable
private IElementType getIElementType(int newChildIndex) {
Block block = getSubBlocks().get(newChildIndex - 1);
while (block instanceof XQueryFormattingBlock && ! block.getSubBlocks().isEmpty()) {
List<Block> subBlocks = block.getSubBlocks();
Block childBlock = subBlocks.get(subBlocks.size() - 1);
if (! (childBlock instanceof XQueryFormattingBlock)) break;
else {
ASTNode node = ((XQueryFormattingBlock) childBlock).getNode();
PsiElement psi = node.getPsi();
IElementType elementType = node.getElementType();
if (elementType instanceof XQueryTokenType) break;
if (psi instanceof LeafPsiElement || psi instanceof XQueryFunctionName || psi instanceof
XQueryVarName || psi instanceof XQueryNamespacePrefix)
break;
}
block = childBlock;
}
return block instanceof XQueryFormattingBlock ? ((XQueryFormattingBlock) block).getNode().getElementType() :
null;
}
示例10
@Deprecated
public DocumentBasedFormattingModel(final Block rootBlock,
@Nonnull final Document document,
final Project project,
final CodeStyleSettings settings,
final FileType fileType,
final PsiFile file) {
myRootBlock = rootBlock;
myDocument = document;
myProject = project;
mySettings = settings;
myFileType = fileType;
myFile = file;
myDocumentModel = new FormattingDocumentModelImpl(document,file);
myOriginalFormattingModel = null;
}
示例11
private void popUntilTopBlockStartsNewLine() {
popUntilTopBlockStartOffsetGreaterOrEqual(myCurrentLineStartOffset);
if (myStack.isEmpty()) return;
Block block = myStack.peek();
while (block != null && !isStartingNewLine(block)) {
myCurrentDocumentLine++;
if (myCurrentDocumentLine >= myTotalLines) {
myStack.clear();
break;
}
myCurrentLineStartOffset = myDocument.getLineStartOffset(myCurrentDocumentLine);
popUntilTopBlockStartOffsetGreaterOrEqual(myCurrentLineStartOffset);
block = myStack.isEmpty() ? null : myStack.peek();
}
}
示例12
@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;
}
示例13
public List<LineIndentInfo> build() {
List<Block> newLineBlocks = getBlocksStartingNewLine();
return ContainerUtil.map(newLineBlocks, new Function<Block, LineIndentInfo>() {
@Override
public LineIndentInfo fun(Block newLineBlock) {
int blockStartOffset = newLineBlock.getTextRange().getStartOffset();
int line = myDocument.getLineNumber(blockStartOffset);
int lineStartOffset = myDocument.getLineStartOffset(line);
if (rangeHasTabs(lineStartOffset, blockStartOffset)) {
return LineIndentInfo.LINE_WITH_TABS;
}
if (hasNormalIndent(newLineBlock)) {
return LineIndentInfo.newNormalIndent(blockStartOffset - lineStartOffset);
}
else {
return LineIndentInfo.LINE_WITH_NOT_COUNTABLE_INDENT;
}
}
});
}
示例14
private static boolean hasNormalIndent(Block block) {
final TextRange range = block.getTextRange();
final int startOffset = range.getStartOffset();
List<Indent.Type> allIndents = getIndentOnStartOffset(block, range, startOffset);
if (hasOnlyNormalOrNoneIndents(allIndents)) {
int normalIndents = ContainerUtil.filter(allIndents, new Condition<Indent.Type>() {
@Override
public boolean value(Indent.Type type) {
return type == Indent.Type.NORMAL;
}
}).size();
return normalIndents < 2;
}
return false;
}
示例15
private static List<Indent.Type> getIndentOnStartOffset(Block block, TextRange range, int startOffset) {
List<Indent.Type> indentsOnStartOffset = new ArrayList<Indent.Type>();
while (block != null && range.getStartOffset() == startOffset) {
Indent.Type type = block.getIndent() != null ? block.getIndent().getType() : Indent.Type.CONTINUATION_WITHOUT_FIRST;
indentsOnStartOffset.add(type);
if (block instanceof AbstractBlock) {
((AbstractBlock)block).setBuildIndentsOnly(true);
}
List<Block> subBlocks = block.getSubBlocks();
block = subBlocks.isEmpty() ? null : subBlocks.get(0);
}
return indentsOnStartOffset;
}
示例16
@Nonnull
private List<Block> getBlocksStartingNewLine() {
NewLineBlocksIterator newLineBlocksIterator = new NewLineBlocksIterator(myRootBlock, myDocument);
List<Block> newLineBlocks = new ArrayList<Block>();
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;
}
示例17
public static List<DataLanguageBlockWrapper> buildChildWrappers(@Nonnull final Block parent) {
assert !(parent instanceof DataLanguageBlockWrapper) : parent.getClass();
List<Block> children = parent.getSubBlocks();
if (children.size() == 0) return Collections.emptyList();
ArrayList<DataLanguageBlockWrapper> result = new ArrayList<DataLanguageBlockWrapper>(children.size());
DataLanguageBlockWrapper prevWrapper = null;
for (Block child : children) {
DataLanguageBlockWrapper currWrapper = createAndAddBlock(result, child, null);
if(currWrapper != null && prevWrapper != null) {
Spacing spacing = parent.getSpacing(prevWrapper.getOriginal(), currWrapper.getOriginal());
prevWrapper.setRightHandSpacing(currWrapper, spacing);
}
prevWrapper = currWrapper;
}
return result;
}
示例18
private static void splitByRightBoundAndCollectBlocks(@Nonnull List<Block> blocks,
@Nonnull List<DataLanguageBlockWrapper> before,
@Nonnull List<DataLanguageBlockWrapper> after,
@Nonnull TextRange bounds) {
for (Block block : blocks) {
final TextRange textRange = block.getTextRange();
if (bounds.contains(textRange)) {
createAndAddBlock(before, block, null);
}
else if (bounds.getEndOffset() <= textRange.getStartOffset()) {
createAndAddBlock(after, block, null);
}
else {
//assert block.getSubBlocks().size() != 0 : "Block " + block.getTextRange() + " doesn't contain subblocks!";
splitByRightBoundAndCollectBlocks(block.getSubBlocks(), before, after, bounds);
}
}
}
示例19
private int calcRightMargin(Block rootBlock) {
Language language = null;
if (rootBlock instanceof ASTBlock) {
ASTNode node = ((ASTBlock)rootBlock).getNode();
if (node != null) {
PsiElement psiElement = node.getPsi();
if (psiElement.isValid()) {
PsiFile psiFile = psiElement.getContainingFile();
if (psiFile != null) {
language = psiFile.getViewProvider().getBaseLanguage();
}
}
}
}
return mySettings.getRightMargin(language);
}
示例20
@Override
protected List<Block> buildChildren() {
if (!isCommentFormattable.getValue()) {
return Collections.emptyList();
}
return ContainerUtil.<LineInfo, Block>map(lines.getValue(), t -> {
String text = t.myText;
Indent indent = null;
if (!isCommentFormattable.getValue()) {
indent = null;
}
else if (text.startsWith("/*")) {
indent = Indent.getNoneIndent();
}
else {
indent = Indent.getSpaceIndent(1);
}
return new TextLineBlock(text, t.myTextRange, null, indent, null);
});
}
示例21
@Override
public Spacing getSpacing(Block child1, Block child2) {
if (getNode().getElementType() == SoyTypes.LITERAL_STATEMENT) {
// No custom spacing inside literal statements whatsoever.
return null;
}
Spacing spacing = super.getSpacing(child1, child2);
return spacing != null ? spacing : SoySpacing.getSpacing(getSettings().getCommonSettings(
SoyLanguage.INSTANCE), this, child1, child2);
}
示例22
private boolean isDirectXMLTagChild() {
BlockWithParent parent = getParent();
if (parent == null) {
return false;
}
BlockWithParent grandParent = getParent().getParent();
return isSynthetic(parent) && isXMLTagBlock(grandParent)
&& ((Block) grandParent).getTextRange().getStartOffset() == ((Block) parent).getTextRange()
.getStartOffset();
}
示例23
public static boolean isQuotedField(@Nullable CsvBlock block) {
if (block != null && block.getNode().getElementType() == CsvTypes.FIELD) {
List<Block> subBlocks = block.buildChildren();
if (subBlocks.size() > 0) {
AbstractBlock abstractBlock = (AbstractBlock) subBlocks.get(0);
return abstractBlock.getNode().getElementType() == CsvTypes.QUOTE;
}
}
return false;
}
示例24
@Override
protected List<Block> buildChildren() {
ASTNode node = this.getNode().getFirstChildNode();
List<Block> blocks = new ArrayList<>();
while (node != null) {
if (node.getElementType() != TokenType.WHITE_SPACE) {
CsvBlockElement block = new CsvBlockElement(node, myFormattingInfo, this);
blocks.add(block);
}
node = node.getTreeNext();
}
return blocks;
}
示例25
@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;
}
示例26
@Nullable
@Override
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
Spacing spacing = spacingBuilder.getSpacing(this, child1, child2);
if (spacing == null) {
return SPACE;
}
return spacing;
}
示例27
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();
}
}
示例28
static Block createBlock(ASTNode node, Alignment alignment, Indent indent, CodeStyleSettings settings) {
Factory factory = REGISTRY.get(node.getElementType());
if (factory == null) {
// If element type is unknown it is best to keep existing formatting
return createLeaf(node, alignment, indent, settings);
}
return factory.create(node, alignment, indent, settings);
}
示例29
@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;
}
示例30
@Nullable
@Override
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
//todo Add more complex logic on spacing, if needed
return spacingBuilder.getSpacing(this, child1, child2);
}