private void processHeadCommentsAndWhiteSpaces(@NotNull List<Block> result) { while (myCurrentChild != null) { if (StdTokenSets.COMMENT_BIT_SET.contains(myCurrentChild.getElementType()) || myCurrentChild.getElementType() == JavaDocElementType.DOC_COMMENT) { Block commentBlock = createJavaBlock( myCurrentChild, mySettings, myJavaSettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy() ); result.add(commentBlock); myCurrentIndent = Indent.getNoneIndent(); } else if (!FormatterUtil.containsWhiteSpacesOnly(myCurrentChild)) { break; } myCurrentOffset += myCurrentChild.getTextLength(); myCurrentChild = myCurrentChild.getTreeNext(); } }
private static int calcNewState(final ASTNode child, final int state) { if (state == BEFORE_FIRST) { if (child.getElementType() == JavaTokenType.ELSE_KEYWORD) { return AFTER_ELSE; } if (StdTokenSets.COMMENT_BIT_SET.contains(child.getElementType())) { return BEFORE_FIRST; } if (child.getElementType() == JavaElementType.CATCH_SECTION) { return BEFORE_FIRST; } } else if (state == BEFORE_BLOCK){ if (child.getElementType() == JavaTokenType.ELSE_KEYWORD) { return AFTER_ELSE; } if (child.getElementType() == JavaElementType.BLOCK_STATEMENT) { return BEFORE_FIRST; } if (child.getElementType() == JavaElementType.CODE_BLOCK) { return BEFORE_FIRST; } } return BEFORE_BLOCK; }
private static int calcNewState(final ASTNode child, int state) { switch (state) { case BEFORE_FIRST: { if (StdTokenSets.COMMENT_BIT_SET.contains(child.getElementType())) { return BEFORE_FIRST; } else if (isLBrace(child)) { return INSIDE_BODY; } else { return BEFORE_LBRACE; } } case BEFORE_LBRACE: { if (isLBrace(child)) { return INSIDE_BODY; } else { return BEFORE_LBRACE; } } } return INSIDE_BODY; }
private void processHeadCommentsAndWhiteSpaces(@NotNull List<Block> result) { while(myCurrentChild != null) { if(StdTokenSets.COMMENT_BIT_SET.contains(myCurrentChild.getElementType()) || myCurrentChild.getElementType() == JavaDocElementType.DOC_COMMENT) { Block commentBlock = createJavaBlock(myCurrentChild, mySettings, myJavaSettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy()); result.add(commentBlock); myCurrentIndent = Indent.getNoneIndent(); } else if(!FormatterUtil.containsWhiteSpacesOnly(myCurrentChild)) { break; } myCurrentOffset += myCurrentChild.getTextLength(); myCurrentChild = myCurrentChild.getTreeNext(); } }
/** * Serves for processing composite field definitions as a single formatting block. * <p/> * <code>'Composite field definition'</code> looks like {@code 'int i1, i2 = 2'}. It produces two nodes of type * {@link JavaElementType#FIELD} - {@code 'int i1'} and {@code 'i2 = 2'}. This method returns the second node if the first one * is given (the given node is returned for <code>'single'</code> fields). * * @param child child field node to check * @return last child field node at the field group identified by the given node if any; given child otherwise */ @NotNull public static ASTNode findLastFieldInGroup(@NotNull final ASTNode child) { PsiElement psi = child.getPsi(); if (psi == null) { return child; } final PsiTypeElement typeElement = ((PsiVariable)psi).getTypeElement(); if (typeElement == null) return child; ASTNode lastChildNode = child.getLastChildNode(); if (lastChildNode == null) return child; if (lastChildNode.getElementType() == JavaTokenType.SEMICOLON) return child; ASTNode currentResult = child; ASTNode currentNode = child.getTreeNext(); while (currentNode != null) { if (currentNode.getElementType() == TokenType.WHITE_SPACE || currentNode.getElementType() == JavaTokenType.COMMA || StdTokenSets.COMMENT_BIT_SET.contains(currentNode.getElementType())) { } else if (currentNode.getElementType() == JavaElementType.FIELD) { if (compoundFieldPart(currentNode)) { currentResult = currentNode; } else { return currentResult; } } else { return currentResult; } currentNode = currentNode.getTreeNext(); } return currentResult; }
private Indent calcIndent(final ASTNode child, final int state) { if (state == AFTER_ELSE && child.getElementType() == JavaElementType.IF_STATEMENT) { if (mySettings.SPECIAL_ELSE_IF_TREATMENT) { return Indent.getNoneIndent(); } else { return getCodeBlockInternalIndent(1); } } if (isSimpleStatement(child)){ return createNormalIndent(1); } if (child.getElementType() == JavaTokenType.ELSE_KEYWORD) return Indent.getNoneIndent(); if (state == BEFORE_FIRST || child.getElementType() == JavaTokenType.WHILE_KEYWORD) { return Indent.getNoneIndent(); } else { if (isPartOfCodeBlock(child)) { return getCodeBlockExternalIndent(); } else if (isSimpleStatement(child) || StdTokenSets.COMMENT_BIT_SET.contains(child.getElementType())){ return getCodeBlockInternalIndent(1); } else if (isNodeParentMethod(child) && (child.getElementType() == JavaElementType.TYPE || child.getElementType() == JavaTokenType.IDENTIFIER || child.getElementType() == JavaElementType.THROWS_LIST || child.getElementType() == JavaElementType.TYPE_PARAMETER_LIST)) { return Indent.getNoneIndent(); } else { return Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS); } } }
@Override public TokenSet getCommentTokenSet(@NotNull final PsiFile file) { final JspFile jspFile = JspPsiUtil.getJspFile(file); TokenSet commentTokens = TokenSet.orSet(JavaIndexPatternBuilder.XML_COMMENT_BIT_SET, StdTokenSets.COMMENT_BIT_SET); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(jspFile.getViewProvider().getTemplateDataLanguage()); if (parserDefinition != null) { commentTokens = TokenSet.orSet(commentTokens, parserDefinition.getCommentTokens()); } return commentTokens; }
@Override @Nullable public TokenSet getCommentTokenSet(@NotNull final PsiFile file) { if (file instanceof PsiJavaFile && !(file instanceof ServerPageFile)) { return TokenSet.orSet(StdTokenSets.COMMENT_BIT_SET, XML_COMMENT_BIT_SET, JavaDocTokenType.ALL_JAVADOC_TOKENS, XML_DATA_CHARS); } return null; }
private static List<PsiField> getFieldsInSameDeclaration(final PsiField variable) { List<PsiField> result = new ArrayList<PsiField>(); ASTNode node = variable.getNode(); if (node != null) { while (true) { ASTNode comma = TreeUtil.skipElements(node.getTreeNext(), StdTokenSets.WHITE_SPACE_OR_COMMENT_BIT_SET); if (comma == null || comma.getElementType() != JavaTokenType.COMMA) break; ASTNode nextField = TreeUtil.skipElements(comma.getTreeNext(), StdTokenSets.WHITE_SPACE_OR_COMMENT_BIT_SET); if (nextField == null || nextField.getElementType() != JavaElementType.FIELD) break; result.add((PsiField) nextField.getPsi()); node = nextField; } } return result; }
private Indent calcIndent(final ASTNode child, final int state) { if (state == AFTER_ELSE && child.getElementType() == JavaElementType.IF_STATEMENT) { if (mySettings.SPECIAL_ELSE_IF_TREATMENT) { return Indent.getNoneIndent(); } else { return getCodeBlockInternalIndent(1); } } if (isSimpleStatement(child)){ return createNormalIndent(1); } if (child.getElementType() == JavaTokenType.ELSE_KEYWORD) return Indent.getNoneIndent(); if (state == BEFORE_FIRST || child.getElementType() == JavaTokenType.WHILE_KEYWORD) { return Indent.getNoneIndent(); } else { if (isPartOfCodeBlock(child)) { return getCodeBlockExternalIndent(); } else if (isSimpleStatement(child) || StdTokenSets.COMMENT_BIT_SET.contains(child.getElementType())){ return getCodeBlockInternalIndent(1); } else { return Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS); } } }
@Override public TokenSet getCommentTokenSet(final PsiFile file) { final JspFile jspFile = JspPsiUtil.getJspFile(file); TokenSet commentTokens = TokenSet.orSet(JavaIndexPatternBuilder.XML_COMMENT_BIT_SET, StdTokenSets.COMMENT_BIT_SET); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(jspFile.getViewProvider().getTemplateDataLanguage()); if (parserDefinition != null) { commentTokens = TokenSet.orSet(commentTokens, parserDefinition.getCommentTokens()); } return commentTokens; }
@Override @Nullable public TokenSet getCommentTokenSet(final PsiFile file) { if (file instanceof PsiJavaFile && !(file instanceof JspFile)) { return TokenSet.orSet(StdTokenSets.COMMENT_BIT_SET, XML_COMMENT_BIT_SET, JavaDocTokenType.ALL_JAVADOC_TOKENS, XML_DATA_CHARS); } return null; }
private static int calcNewState(final ASTNode child, final int state) { if(state == BEFORE_FIRST) { if(child.getElementType() == JavaTokenType.ELSE_KEYWORD) { return AFTER_ELSE; } if(StdTokenSets.COMMENT_BIT_SET.contains(child.getElementType())) { return BEFORE_FIRST; } if(child.getElementType() == JavaElementType.CATCH_SECTION) { return BEFORE_FIRST; } } else if(state == BEFORE_BLOCK) { if(child.getElementType() == JavaTokenType.ELSE_KEYWORD) { return AFTER_ELSE; } if(child.getElementType() == JavaElementType.BLOCK_STATEMENT) { return BEFORE_FIRST; } if(child.getElementType() == JavaElementType.CODE_BLOCK) { return BEFORE_FIRST; } } return BEFORE_BLOCK; }
private static int calcNewState(final ASTNode child, int state) { switch(state) { case BEFORE_FIRST: { if(StdTokenSets.COMMENT_BIT_SET.contains(child.getElementType())) { return BEFORE_FIRST; } else if(child.getElementType() == JavaTokenType.LBRACE) { return INSIDE_BODY; } else { return BEFORE_LBRACE; } } case BEFORE_LBRACE: { if(child.getElementType() == JavaTokenType.LBRACE) { return INSIDE_BODY; } else { return BEFORE_LBRACE; } } } return INSIDE_BODY; }
@Override @Nullable public TokenSet getCommentTokenSet(final PsiFile file) { if (file instanceof PsiJavaFile /*&& !(file instanceof JspFile)*/) { return TokenSet.orSet(StdTokenSets.COMMENT_BIT_SET, XML_COMMENT_BIT_SET, JavaDocTokenType.ALL_JAVADOC_TOKENS, XML_DATA_CHARS); } return null; }
private static boolean mayContainClassesInside(PsiElement psiElement, final CharSequence fileBuffer) { PsiFile psiFile = psiElement.getContainingFile(); boolean mayHaveClassesInside = false; if (psiFile instanceof PsiJavaFileImpl) { Lexer originalLexer = JavaParserDefinition.createLexer(((PsiJavaFileImpl)psiFile).getLanguageLevel()); FilterLexer lexer = new FilterLexer(originalLexer, new FilterLexer.SetFilter(StdTokenSets.WHITE_SPACE_OR_COMMENT_BIT_SET)); final TextRange range = psiElement.getTextRange(); lexer.start(fileBuffer, range.getStartOffset(), range.getEndOffset()); boolean isInNewExpression = false; boolean isRightAfterNewExpression = false; int angleLevel = 0; int parenLevel = 0; do { IElementType tokenType = lexer.getTokenType(); if (tokenType == null) break; if (tokenType == JavaTokenType.NEW_KEYWORD) { isInNewExpression = true; } else if (tokenType == JavaTokenType.LPARENTH) { if (isInNewExpression) parenLevel++; } else if (tokenType == JavaTokenType.LT) { if (isInNewExpression) angleLevel++; } else if (tokenType == JavaTokenType.GT) { if (isInNewExpression) angleLevel--; } else if (tokenType == JavaTokenType.RPARENTH) { if (isInNewExpression) { parenLevel--; if (parenLevel == 0) { isRightAfterNewExpression = true; } } } else if (tokenType == JavaTokenType.LBRACE) { if (isInNewExpression || isRightAfterNewExpression) { mayHaveClassesInside = true; } } else if (tokenType == JavaTokenType.LBRACKET) { if (parenLevel == 0 && angleLevel == 0) isInNewExpression = false; } else if (tokenType == JavaTokenType.INTERFACE_KEYWORD || tokenType == JavaTokenType.CLASS_KEYWORD || tokenType == JavaTokenType.ENUM_KEYWORD) { mayHaveClassesInside = true; } if (isInNewExpression && isRightAfterNewExpression) { isInNewExpression = false; } else { isRightAfterNewExpression = false; } lexer.advance(); } while (!mayHaveClassesInside); } return mayHaveClassesInside; }
@Override protected List<Block> buildChildren() { ASTNode child = myNode.getFirstChildNode(); int offset = myStartOffset != -1 ? myStartOffset : child != null ? child.getTextRange().getStartOffset():0; final ArrayList<Block> result = new ArrayList<Block>(); Indent indent = null; while (child != null) { if (StdTokenSets.COMMENT_BIT_SET.contains(child.getElementType()) || child.getElementType() == JavaDocElementType.DOC_COMMENT) { result.add(createJavaBlock(child, mySettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy())); indent = Indent.getNoneIndent(); } else if (!FormatterUtil.containsWhiteSpacesOnly(child)) { break; } offset += child.getTextLength(); child = child.getTreeNext(); } myReservedAlignment = createChildAlignment(); myReservedAlignment2 = createChildAlignment2(myReservedAlignment); Wrap childWrap = createChildWrap(); while (child != null) { if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0){ final ASTNode astNode = child; AlignmentStrategy alignmentStrategyToUse = ALIGN_IN_COLUMNS_ELEMENT_TYPES.contains(myNode.getElementType()) ? myAlignmentStrategy : AlignmentStrategy.wrap(chooseAlignment(myReservedAlignment, myReservedAlignment2, child)); child = processChild(result, astNode, alignmentStrategyToUse, childWrap, indent, offset); if (astNode != child && child != null) { offset = child.getTextRange().getStartOffset(); } if (indent != null && !(myNode.getPsi() instanceof PsiFile) && child != null && child.getElementType() != JavaElementType.MODIFIER_LIST) { indent = Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS); } } if (child != null) { offset += child.getTextLength(); child = child.getTreeNext(); } } return result; }
/** * Serves for processing composite field definitions as a single formatting block. * <p> * {@code 'Composite field definition'} looks like {@code 'int i1, i2 = 2'}. It produces two nodes of type * {@link JavaElementType#FIELD} - {@code 'int i1'} and {@code 'i2 = 2'}. This method returns the second node if the first one * is given (the given node is returned for {@code 'single'} fields). * * @param child child field node to check * @return last child field node at the field group identified by the given node if any; given child otherwise */ @NotNull public static ASTNode findLastFieldInGroup(@NotNull final ASTNode child) { PsiElement psi = child.getPsi(); if(psi == null) { return child; } final PsiTypeElement typeElement = ((PsiVariable) psi).getTypeElement(); if(typeElement == null) { return child; } ASTNode lastChildNode = child.getLastChildNode(); if(lastChildNode == null) { return child; } if(lastChildNode.getElementType() == JavaTokenType.SEMICOLON) { return child; } ASTNode currentResult = child; ASTNode currentNode = child.getTreeNext(); while(currentNode != null) { if(currentNode.getElementType() == TokenType.WHITE_SPACE || currentNode.getElementType() == JavaTokenType.COMMA || StdTokenSets.COMMENT_BIT_SET.contains(currentNode.getElementType())) { } else if(currentNode.getElementType() == JavaElementType.FIELD) { if(compoundFieldPart(currentNode)) { currentResult = currentNode; } else { return currentResult; } } else { return currentResult; } currentNode = currentNode.getTreeNext(); } return currentResult; }
private Indent calcIndent(final ASTNode child, final int state) { if(state == AFTER_ELSE && child.getElementType() == JavaElementType.IF_STATEMENT) { if(mySettings.SPECIAL_ELSE_IF_TREATMENT) { return Indent.getNoneIndent(); } else { return getCodeBlockInternalIndent(1); } } if(isSimpleStatement(child)) { return createNormalIndent(1); } if(child.getElementType() == JavaTokenType.ELSE_KEYWORD) { return Indent.getNoneIndent(); } if(state == BEFORE_FIRST || child.getElementType() == JavaTokenType.WHILE_KEYWORD) { return Indent.getNoneIndent(); } else { if(isPartOfCodeBlock(child)) { return getCodeBlockExternalIndent(); } else if(isSimpleStatement(child) || StdTokenSets.COMMENT_BIT_SET.contains(child.getElementType())) { return getCodeBlockInternalIndent(1); } else if(isNodeParentMethod(child) && (child.getElementType() == JavaElementType.TYPE || child.getElementType() == JavaTokenType.IDENTIFIER || child.getElementType() == JavaElementType .THROWS_LIST || child.getElementType() == JavaElementType.TYPE_PARAMETER_LIST)) { return Indent.getNoneIndent(); } else { return Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS); } } }
private void doInit(ASTNode child, CommonCodeStyleSettings settings, JavaCodeStyleSettings javaSettings) { if(isErrorElement(child)) { myResult = Spacing.getReadOnlySpacing(); return; } init(child); mySettings = settings; myJavaSettings = javaSettings; if(myChild1 == null) { // Given node corresponds to the first document block. if(FormatterUtil.isFormatterCalledExplicitly()) { createSpaceInCode(false); } return; } final PsiElement myChild1Psi1 = myChild1.getPsi(); final PsiElement myChild1Psi2 = myChild2.getPsi(); if(myChild1Psi1 == null || myChild1Psi1.getLanguage() != JavaLanguage.INSTANCE || myChild1Psi2 == null || myChild1Psi2.getLanguage() != JavaLanguage.INSTANCE) { return; } if(myChild2 != null && StdTokenSets.COMMENT_BIT_SET.contains(myChild2.getElementType())) { if(myChild2.getElementType() == JavaTokenType.C_STYLE_COMMENT) { myResult = Spacing.getReadOnlySpacing(); } else if(mySettings.KEEP_FIRST_COLUMN_COMMENT) { myResult = Spacing.createKeepingFirstColumnSpacing(0, Integer.MAX_VALUE, true, mySettings.KEEP_BLANK_LINES_IN_CODE); } else { myResult = Spacing.createSpacing(0, Integer.MAX_VALUE, 0, true, mySettings.KEEP_BLANK_LINES_IN_CODE); } } else if(myParent != null) { myParent.accept(this); if(myResult == null) { final ASTNode prev = getPrevElementType(myChild2); if(prev != null && prev.getElementType() == JavaTokenType.END_OF_LINE_COMMENT) { myResult = Spacing.createSpacing(0, 0, 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } else if(!canStickChildrenTogether(myChild1, myChild2)) { myResult = Spacing.createSpacing(1, Integer.MIN_VALUE, 0, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } else if(myChild1.getElementType() == JavaTokenType.C_STYLE_COMMENT) { myResult = null; } else if(!shouldKeepSpace(myParent)) { myResult = Spacing.createSpacing(0, 0, 0, true, mySettings.KEEP_BLANK_LINES_IN_CODE); } } } }
@Override public void visitMethod(PsiMethod method) { if(myChild1.getElementType() == JavaDocElementType.DOC_COMMENT) { myResult = Spacing.createSpacing(0, 0, 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_DECLARATIONS); return; } if(myRole2 == ChildRole.PARAMETER_LIST) { createSpaceInCode(mySettings.SPACE_BEFORE_METHOD_PARENTHESES); } else if(myRole1 == ChildRole.PARAMETER_LIST && myRole2 == ChildRole.THROWS_LIST || myRole1 == ChildRole.TYPE_PARAMETER_LIST) { createSpaceInCode(true); } else if(myRole2 == ChildRole.METHOD_BODY) { myResult = getSpaceBeforeMethodLBrace(method); } else if(myRole1 == ChildRole.MODIFIER_LIST) { processModifierList(); } else if(StdTokenSets.COMMENT_BIT_SET.contains(myChild1.getElementType()) && (myRole2 == ChildRole.MODIFIER_LIST || myRole2 == ChildRole.TYPE_REFERENCE)) { myResult = Spacing.createSpacing(0, 0, 1, mySettings.KEEP_LINE_BREAKS, 0); } else if(myRole2 == ChildRole.DEFAULT_KEYWORD || myRole2 == ChildRole.ANNOTATION_DEFAULT_VALUE) { createSpaceInCode(true); } else if(myChild2.getElementType() == JavaTokenType.SEMICOLON) { createSpaceInCode(false); } else if(myRole1 == ChildRole.TYPE) { createSpaceInCode(true); } }