private static PsiElement adjustSectionElement(PsiFile file, PsiElement element){ PsiElement psiElement = element; if(file instanceof PsiJavaFile) { if (psiElement instanceof PsiJavaToken) { if (((PsiJavaToken) psiElement).getTokenType() == ElementType.SEMICOLON) { psiElement = psiElement.getPrevSibling(); if (psiElement instanceof PsiLiteralExpression) { return psiElement; } } else if (((PsiJavaToken) psiElement).getTokenType() == ElementType.STRING_LITERAL) { return psiElement; } } }else if(file instanceof XmlFile) { if(psiElement instanceof XmlToken){ psiElement = psiElement.getParent(); if(psiElement instanceof XmlAttributeValue) { return psiElement; } } } return null; }
@Override public int getEdgePosition(final List<IElementType> tokens, final boolean atStreamEdge, final TokenTextGetter getter) { if (tokens.size() == 0) return 0; int result = 0; for (int idx = 0; idx < tokens.size(); idx++) { final IElementType tokenType = tokens.get(idx); if (ElementType.JAVA_WHITESPACE_BIT_SET.contains(tokenType)) { if (StringUtil.containsLineBreak(getter.get(idx))) break; } else if (ElementType.JAVA_PLAIN_COMMENT_BIT_SET.contains(tokenType)) { result = idx + 1; } else break; } return result; }
@Override public ASTNode findChildByRole(int role) { LOG.assertTrue(ChildRole.isUnique(role)); switch (role) { default: return null; case ChildRole.LOPERAND: return getFirstChildNode(); case ChildRole.ROPERAND: return ElementType.EXPRESSION_BIT_SET.contains(getLastChildNode().getElementType()) ? getLastChildNode() : null; case ChildRole.OPERATION_SIGN: return findChildByType(OUR_OPERATIONS_BIT_SET); } }
@Override public int getChildRole(ASTNode child) { LOG.assertTrue(child.getTreeParent() == this); if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())){ int role = getChildRole(child, ChildRole.CONDITION); if (role != ChildRoleBase.NONE) return role; role = getChildRole(child, ChildRole.THEN_EXPRESSION); if (role != ChildRoleBase.NONE) return role; role = getChildRole(child, ChildRole.ELSE_EXPRESSION); return role; } if (child.getElementType() == JavaTokenType.QUEST){ return ChildRole.QUEST; } if (child.getElementType() == JavaTokenType.COLON){ return ChildRole.COLON; } return ChildRoleBase.NONE; }
private static int stripAnnotationsFromModifierList(@NotNull PsiElement element) { TextRange textRange = element.getTextRange(); if (textRange == null) return 0; PsiAnnotation lastAnnotation = null; for (PsiElement child : element.getChildren()) { if (child instanceof PsiAnnotation) lastAnnotation = (PsiAnnotation)child; } if (lastAnnotation == null) { return textRange.getStartOffset(); } ASTNode node = lastAnnotation.getNode(); if (node != null) { do { node = TreeUtil.nextLeaf(node); } while (node != null && ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(node.getElementType())); } if (node != null) return node.getTextRange().getStartOffset(); return textRange.getStartOffset(); }
@Override public void advance() { final IElementType tokenType = myDelegate.getTokenType(); if (tokenType == JavaTokenType.IDENTIFIER || tokenType == JavaTokenType.LONG_LITERAL || tokenType == JavaTokenType.INTEGER_LITERAL || tokenType == JavaTokenType.CHARACTER_LITERAL || tokenType == JavaTokenType.ARROW || tokenType == JavaTokenType.DOUBLE_COLON) { addOccurrenceInToken(UsageSearchContext.IN_CODE); } else if (tokenType == JavaTokenType.STRING_LITERAL) { scanWordsInToken(UsageSearchContext.IN_STRINGS | UsageSearchContext.IN_FOREIGN_LANGUAGES, false, true); } else if (ElementType.JAVA_COMMENT_BIT_SET.contains(tokenType)) { scanWordsInToken(UsageSearchContext.IN_COMMENTS, false, false); advanceTodoItemCountsInToken(); } else if (!ourSkipWordsScanSet.contains(tokenType)) { scanWordsInToken(UsageSearchContext.IN_PLAIN_TEXT, false, false); } myDelegate.advance(); }
@Override protected List<Block> buildChildren() { final ArrayList<Block> result = new ArrayList<Block>(); ASTNode child = myNode.getFirstChildNode(); Indent currentIndent = getLabelIndent(); Wrap currentWrap = null; while (child != null) { if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0){ result.add(createJavaBlock(child, mySettings, currentIndent, currentWrap, AlignmentStrategy.getNullStrategy())); if (child.getElementType() == ElementType.COLON) { currentIndent = Indent.getNoneIndent(); currentWrap =Wrap.createWrap(WrapType.ALWAYS, true); } } child = child.getTreeNext(); } return result; }
@Override public void advance() { final IElementType tokenType = myDelegate.getTokenType(); if (tokenType == JavaTokenType.IDENTIFIER || tokenType == JavaTokenType.LONG_LITERAL || tokenType == JavaTokenType.INTEGER_LITERAL || tokenType == JavaTokenType.CHARACTER_LITERAL) { addOccurrenceInToken(UsageSearchContext.IN_CODE); } else if (tokenType == JavaTokenType.STRING_LITERAL) { scanWordsInToken(UsageSearchContext.IN_STRINGS | UsageSearchContext.IN_FOREIGN_LANGUAGES, false, true); } else if (ElementType.JAVA_COMMENT_BIT_SET.contains(tokenType)) { scanWordsInToken(UsageSearchContext.IN_COMMENTS, false, false); advanceTodoItemCountsInToken(); } else if (!ourSkipWordsScanSet.contains(tokenType)) { scanWordsInToken(UsageSearchContext.IN_PLAIN_TEXT, false, false); } myDelegate.advance(); }
private PsiElement getTargetContext(PsiElement context, PsiElement place) { PsiElement targetContext = context; if (place.getParent() instanceof PsiDocMethodOrFieldRef) { PsiDocMethodOrFieldRef methodRef = (PsiDocMethodOrFieldRef) place.getParent(); final IElementType firstChildType = methodRef.getFirstChildNode().getElementType(); if (firstChildType == ElementType.JAVA_CODE_REFERENCE || firstChildType == ElementType.REFERENCE_EXPRESSION) { PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) SourceTreeToPsiMap.treeElementToPsi(methodRef.getFirstChildNode()); final PsiElement element = referenceElement.resolve(); if (element instanceof PsiClass) { targetContext = element.getFirstChild(); } } } return targetContext; }
@Nullable private PsiBuilder.Marker parseClassAccessOrMethodReference(PsiBuilder builder) { PsiBuilder.Marker expr = builder.mark(); boolean primitive = ElementType.PRIMITIVE_TYPE_BIT_SET.contains(builder.getTokenType()); if(myParser.getReferenceParser().parseType(builder, 0) == null) { expr.drop(); return null; } PsiBuilder.Marker result = parseClassAccessOrMethodReference(builder, expr, primitive); if(result == null) { expr.rollbackTo(); } return result; }
@Override public ASTNode findChildByRole(int role) { LOG.assertTrue(ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.MODIFIER_LIST: return findChildByType(MODIFIER_LIST); case ChildRole.TYPE: return findChildByType(TYPE); case ChildRole.NAME: return findChildByType(JavaTokenType.IDENTIFIER); case ChildRole.INITIALIZER_EQ: return findChildByType(JavaTokenType.EQ); case ChildRole.INITIALIZER: return findChildByType(ElementType.EXPRESSION_BIT_SET); case ChildRole.CLOSING_SEMICOLON: return TreeUtil.findChildBackward(this, JavaTokenType.SEMICOLON); } }
@Override public int getChildRole(ASTNode child) { LOG.assertTrue(child.getTreeParent() == this); IElementType i = child.getElementType(); if (i == MODIFIER_LIST) { return ChildRole.MODIFIER_LIST; } else if (i == TYPE) { return getChildRole(child, ChildRole.TYPE); } else if (i == JavaTokenType.IDENTIFIER) { return getChildRole(child, ChildRole.NAME); } else if (i == JavaTokenType.EQ) { return getChildRole(child, ChildRole.INITIALIZER_EQ); } else if (i == JavaTokenType.SEMICOLON) { return getChildRole(child, ChildRole.CLOSING_SEMICOLON); } else { if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) { return ChildRole.INITIALIZER; } return ChildRoleBase.NONE; } }
@Override public int getChildRole(ASTNode child) { LOG.assertTrue(child.getTreeParent() == this); IElementType i = child.getElementType(); if(i == JavaTokenType.DOT) { return ChildRole.DOT; } else if(i == JavaElementType.REFERENCE_PARAMETER_LIST) { return ChildRole.REFERENCE_PARAMETER_LIST; } else if(i == JavaTokenType.IDENTIFIER || i == JavaTokenType.THIS_KEYWORD || i == JavaTokenType.SUPER_KEYWORD) { return ChildRole.REFERENCE_NAME; } else { if(ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) { return ChildRole.QUALIFIER; } return ChildRoleBase.NONE; } }
@Override @NotNull public PsiExpression[] getArrayDimensions() { PsiExpression[] expressions = getChildrenAsPsiElements(ElementType.ARRAY_DIMENSION_BIT_SET, PsiExpression.ARRAY_FACTORY); PsiExpression qualifier = getQualifier(); if(qualifier == null) { return expressions; } else { LOG.assertTrue(expressions[0] == qualifier); PsiExpression[] expressions1 = new PsiExpression[expressions.length - 1]; System.arraycopy(expressions, 1, expressions1, 0, expressions1.length); return expressions1; } }
@Override public int getChildRole(ASTNode child) { IElementType i = child.getElementType(); if(i == JavaTokenType.COMMA) { return ChildRole.COMMA; } else if(i == JavaTokenType.LPARENTH) { return ChildRole.LPARENTH; } else if(i == JavaTokenType.RPARENTH) { return ChildRole.RPARENTH; } else if(ElementType.ANNOTATION_MEMBER_VALUE_BIT_SET.contains(i) || (i == JavaElementType.NAME_VALUE_PAIR && child.getFirstChildNode() != null && child.getFirstChildNode().getElementType() == JavaElementType.ANNOTATION_ARRAY_INITIALIZER)) { return ChildRole.ANNOTATION_VALUE; } else { return ChildRoleBase.NONE; } }
private static boolean checkByTypes(IElementType parentType, IElementType nodeType) { if(ElementType.IMPORT_STATEMENT_BASE_BIT_SET.contains(parentType)) { return true; } if(nodeType == JavaElementType.RECEIVER_PARAMETER) { return true; } if(nodeType == JavaElementType.PARAMETER && parentType != JavaElementType.PARAMETER_LIST) { return true; } if(nodeType == JavaElementType.PARAMETER_LIST && parentType == JavaElementType.LAMBDA_EXPRESSION) { return true; } if(nodeType == JavaDocElementType.DOC_COMMENT) { return true; } return false; }
@NotNull private static Set<LighterASTNode> findCallsWithNulls(LighterAST lighterAst, int[] nullOffsets) { Set<LighterASTNode> calls = new HashSet<>(); for(int offset : nullOffsets) { LighterASTNode leaf = LightTreeUtil.findLeafElementAt(lighterAst, offset); LighterASTNode literal = leaf == null ? null : lighterAst.getParent(leaf); if(isNullLiteral(lighterAst, literal)) { LighterASTNode exprList = lighterAst.getParent(literal); if(exprList != null && exprList.getTokenType() == EXPRESSION_LIST) { ContainerUtil.addIfNotNull(calls, LightTreeUtil.getParentOfType(lighterAst, exprList, CALL_TYPES, ElementType.MEMBER_BIT_SET)); } } } return calls; }
@Override public void advance() { final IElementType tokenType = myDelegate.getTokenType(); if(tokenType == JavaTokenType.IDENTIFIER || tokenType == JavaTokenType.LONG_LITERAL || tokenType == JavaTokenType.INTEGER_LITERAL || tokenType == JavaTokenType.CHARACTER_LITERAL || tokenType == JavaTokenType.ARROW || tokenType == JavaTokenType.DOUBLE_COLON) { addOccurrenceInToken(UsageSearchContext.IN_CODE); } else if(tokenType == JavaTokenType.STRING_LITERAL) { scanWordsInToken(UsageSearchContext.IN_STRINGS | UsageSearchContext.IN_FOREIGN_LANGUAGES, false, true); } else if(ElementType.JAVA_COMMENT_BIT_SET.contains(tokenType)) { scanWordsInToken(UsageSearchContext.IN_COMMENTS, false, false); advanceTodoItemCountsInToken(); } else if(!ourSkipWordsScanSet.contains(tokenType)) { scanWordsInToken(UsageSearchContext.IN_PLAIN_TEXT, false, false); } myDelegate.advance(); }
@Nullable private PsiBuilder.Marker parseClassAccessOrMethodReference(PsiBuilder builder) { PsiBuilder.Marker expr = builder.mark(); boolean primitive = ElementType.PRIMITIVE_TYPE_BIT_SET.contains(builder.getTokenType()); if (myParser.getReferenceParser().parseType(builder, 0) == null) { expr.drop(); return null; } PsiBuilder.Marker result = parseClassAccessOrMethodReference(builder, expr, primitive); if (result == null) expr.rollbackTo(); return result; }
@NotNull public Pair<PsiBuilder.Marker, Boolean> parseModifierList(final PsiBuilder builder, final TokenSet modifiers) { final PsiBuilder.Marker modList = builder.mark(); boolean isEmpty = true; while (true) { final IElementType tokenType = builder.getTokenType(); if (tokenType == null) break; if (modifiers.contains(tokenType)) { builder.advanceLexer(); isEmpty = false; } else if (tokenType == JavaTokenType.AT) { if (ElementType.KEYWORD_BIT_SET.contains(builder.lookAhead(1))) { break; } parseAnnotation(builder); isEmpty = false; } else { break; } } done(modList, JavaElementType.MODIFIER_LIST); return Pair.create(modList, isEmpty); }
@Override public int getEdgePosition(final List<IElementType> tokens, final boolean atStreamEdge, final TokenTextGetter getter) { if (tokens.size() == 0) return 0; // 1. bind doc comment for (int idx = tokens.size() - 1; idx >= 0; idx--) { if (tokens.get(idx) == JavaDocElementType.DOC_COMMENT) return idx; } // 2. bind plain comments int result = tokens.size(); for (int idx = tokens.size() - 1; idx >= 0; idx--) { final IElementType tokenType = tokens.get(idx); if (ElementType.JAVA_WHITESPACE_BIT_SET.contains(tokenType)) { if (StringUtil.getLineBreakCount(getter.get(idx)) > 1) break; } else if (ElementType.JAVA_PLAIN_COMMENT_BIT_SET.contains(tokenType)) { if (atStreamEdge || (idx == 0 && myAfterEmptyImport) || (idx > 0 && ElementType.JAVA_WHITESPACE_BIT_SET.contains(tokens.get(idx - 1)) && StringUtil.containsLineBreak(getter.get(idx - 1)))) { result = idx; } } else break; } return result; }
@Override public PsiFieldStub createStub(final LighterAST tree, final LighterASTNode node, final StubElement parentStub) { final TypeInfo typeInfo = TypeInfo.create(tree, node, parentStub); boolean isDeprecatedByComment = false; boolean hasDeprecatedAnnotation = false; boolean hasDocComment = false; String name = null; String initializer = null; boolean expectingInit = false; for (final LighterASTNode child : tree.getChildren(node)) { final IElementType type = child.getTokenType(); if (type == JavaDocElementType.DOC_COMMENT) { hasDocComment = true; isDeprecatedByComment = RecordUtil.isDeprecatedByDocComment(tree, child); } else if (type == JavaElementType.MODIFIER_LIST) { hasDeprecatedAnnotation = RecordUtil.isDeprecatedByAnnotation(tree, child); } else if (type == JavaTokenType.IDENTIFIER) { name = RecordUtil.intern(tree.getCharTable(), child); } else if (type == JavaTokenType.EQ) { expectingInit = true; } else if (expectingInit && !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(type) && type != JavaTokenType.SEMICOLON) { initializer = encodeInitializer(tree, child); break; } } final boolean isEnumConst = node.getTokenType() == JavaElementType.ENUM_CONSTANT; final byte flags = PsiFieldStubImpl.packFlags(isEnumConst, isDeprecatedByComment, hasDeprecatedAnnotation, hasDocComment); return new PsiFieldStubImpl(parentStub, name, typeInfo, initializer, flags); }
@Override public ASTNode findChildByRole(int role) { LOG.assertTrue(ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.OPERATION_SIGN: return getFirstChildNode(); case ChildRole.OPERAND: return ElementType.EXPRESSION_BIT_SET.contains(getLastChildNode().getElementType()) ? getLastChildNode() : null; } }
@Override public int getChildRole(ASTNode child) { LOG.assertTrue(child.getTreeParent() == this); if (child == getFirstChildNode()) return ChildRole.OPERATION_SIGN; if (child == getLastChildNode() && ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) return ChildRole.OPERAND; return ChildRoleBase.NONE; }
@Override public int getChildRole(ASTNode child) { LOG.assertTrue(child.getTreeParent() == this); if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) { if (child == getFirstChildNode()) return ChildRole.LOPERAND; if (child == getLastChildNode()) return ChildRole.ROPERAND; return ChildRoleBase.NONE; } else if (OUR_OPERATIONS_BIT_SET.contains(child.getElementType())) { return ChildRole.OPERATION_SIGN; } else { return ChildRoleBase.NONE; } }
@Override public int getChildRole(ASTNode child) { LOG.assertTrue(child.getTreeParent() == this); IElementType i = child.getElementType(); if (i == JavaElementType.EXPRESSION_LIST) { return ChildRole.ARGUMENT_LIST; } else { if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) { return ChildRole.METHOD_EXPRESSION; } return ChildRoleBase.NONE; } }
@Override public ASTNode findChildByRole(int role) { LOG.assertTrue(ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.CONDITION: return getFirstChildNode(); case ChildRole.QUEST: return findChildByType(JavaTokenType.QUEST); case ChildRole.THEN_EXPRESSION: ASTNode quest = findChildByRole(ChildRole.QUEST); ASTNode child = quest.getTreeNext(); while(true){ if (child == null) return null; if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) break; child = child.getTreeNext(); } return child; case ChildRole.COLON: return findChildByType(JavaTokenType.COLON); case ChildRole.ELSE_EXPRESSION: ASTNode colon = findChildByRole(ChildRole.COLON); if (colon == null) return null; return ElementType.EXPRESSION_BIT_SET.contains(getLastChildNode().getElementType()) ? getLastChildNode() : null; } }
@NotNull @Override public PsiExpression[] getOperands() { PsiExpression[] operands = cachedOperands; if (operands == null) { cachedOperands = operands = getChildrenAsPsiElements(ElementType.EXPRESSION_BIT_SET, PsiExpression.ARRAY_FACTORY); } return operands; }
@Override public int getChildRole(ASTNode child) { LOG.assertTrue(child.getTreeParent() == this); if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) { if (child == getFirstChildNode()) return ChildRole.LOPERAND; if (child == getLastChildNode()) return ChildRole.ROPERAND; return ChildRoleBase.NONE; } if (OUR_OPERATIONS_BIT_SET.contains(child.getElementType())) { return ChildRole.OPERATION_SIGN; } return ChildRoleBase.NONE; }
@Override public int getChildRole(ASTNode child) { LOG.assertTrue(child.getTreeParent() == this); IElementType i = child.getElementType(); if (i == IF_KEYWORD) { return ChildRole.IF_KEYWORD; } else if (i == ELSE_KEYWORD) { return ChildRole.ELSE_KEYWORD; } else if (i == LPARENTH) { return ChildRole.LPARENTH; } else if (i == RPARENTH) { return ChildRole.RPARENTH; } else { if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) { return ChildRole.CONDITION; } else if (child.getPsi() instanceof PsiStatement) { if (findChildByRoleAsPsiElement(ChildRole.THEN_BRANCH) == child) { return ChildRole.THEN_BRANCH; } else { return ChildRole.ELSE_BRANCH; } } else { return ChildRoleBase.NONE; } } }
private static int getExistingRParenthOffset(final Editor editor, final int tailOffset) { final Document document = editor.getDocument(); if (tailOffset >= document.getTextLength()) return -1; final CharSequence charsSequence = document.getCharsSequence(); EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter(); int existingRParenthOffset = -1; for(HighlighterIterator iterator = highlighter.createIterator(tailOffset); !iterator.atEnd(); iterator.advance()){ final IElementType tokenType = iterator.getTokenType(); if ((!(tokenType instanceof IJavaElementType) || !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(tokenType)) && tokenType != TokenType.WHITE_SPACE) { final int start = iterator.getStart(); if (iterator.getEnd() == start + 1 && ')' == charsSequence.charAt(start)) { existingRParenthOffset = start; } break; } } if (existingRParenthOffset >= 0){ final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(editor.getProject()); psiDocumentManager.commitDocument(document); TextRange range = getRangeToCheckParensBalance(psiDocumentManager.getPsiFile(document), document, editor.getCaretModel().getOffset()); int balance = calcParensBalance(document, highlighter, range.getStartOffset(), range.getEndOffset()); if (balance > 0){ return -1; } } return existingRParenthOffset; }
private static boolean isPairedBracesAllowedBeforeTypeInJava(final IElementType tokenType) { return ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(tokenType) || tokenType == JavaTokenType.SEMICOLON || tokenType == JavaTokenType.COMMA || tokenType == JavaTokenType.RPARENTH || tokenType == JavaTokenType.RBRACKET || tokenType == JavaTokenType.RBRACE || tokenType == JavaTokenType.LBRACE; }