boolean (PsiElement context) { PsiElement nextNonSpace = PsiUtil.getNextNonSpace(context); if (nextNonSpace instanceof PsiErrorElement) nextNonSpace = PsiUtil.getNextNonSpace(nextNonSpace); if (afterAbstractMethod(context, true, false) && nextNonSpace != null && nextNonSpace.getText().startsWith("{") || addExtendsImplements(context).length > 0) { return false; } PsiElement parent = context.getParent(); if (parent instanceof GrTypeDefinitionBody) { return true; } if (parent instanceof GrReferenceExpression) { if (parent.getParent() instanceof GroovyFile) { return true; } if ((parent.getParent() instanceof GrApplicationStatement || parent.getParent() instanceof GrCall) && parent.getParent().getParent() instanceof GroovyFile) { return true; } } /* @Anno cl<caret> */ if (parent instanceof GrVariable && context == ((GrVariable)parent).getNameIdentifierGroovy()) { final PsiElement decl = parent.getParent(); if (decl instanceof GrVariableDeclaration && !((GrVariableDeclaration)decl).isTuple() && ((GrVariableDeclaration)decl).getTypeElementGroovy() == null && (decl.getParent() instanceof GrTypeDefinitionBody || decl.getParent() instanceof GroovyFile)) { return true; } } final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(context.getTextRange().getStartOffset() - 1, context); if (leaf != null) { PsiElement prev = leaf; prev = PsiImplUtil.realPrevious(prev); if (prev instanceof GrModifierList && prev.getParent() != null && prev.getParent().getParent() instanceof GroovyFile) { return true; } if (leaf.getParent() instanceof GroovyFile) { return GroovyCompletionUtil.isNewStatement(context, false); } } return false; } private static boolean afterAtInType(PsiElement context) { PsiElement previous = PsiImplUtil.realPrevious(PsiTreeUtil.prevLeaf(context)); if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()) && (context.getParent() != null && context.getParent().getParent() instanceof GroovyFile || context.getParent() instanceof GrCodeReferenceElement && context.getParent().getParent() instanceof GrAnnotation)) { return true; } return false; } private static boolean isControlStructure(PsiElement context) { final int offset = context.getTextRange().getStartOffset(); PsiElement prevSibling = context.getPrevSibling(); if (context.getParent() instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) { ASTNode node = prevSibling.getNode(); return !TokenSets.DOTS.contains(node.getElementType()); } if (GroovyCompletionUtil.isNewStatement(context, true)) { final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context); if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) { return true; } } if (context.getParent() != null) { PsiElement parent = context.getParent(); if (parent instanceof GrExpression && parent.getParent() instanceof GroovyFile) { return true; } if (parent instanceof GrReferenceExpression) { PsiElement superParent = parent.getParent(); if (superParent instanceof GrStatementOwner || superParent instanceof GrLabeledStatement || superParent instanceof GrControlStatement || superParent instanceof GrMethodCall) { return true; } } return false; } return false; } private static boolean inCaseSection(PsiElement context) { if (context.getParent() instanceof GrReferenceExpression && context.getParent().getParent() instanceof GrCaseSection) { return true; } final GrSwitchElement switchElement = PsiTreeUtil.getParentOfType(context, GrSwitchElement.class, true, GrCodeBlock.class); if (switchElement == null) return false; final GrExpression condition = switchElement.getCondition(); return condition == null || !PsiTreeUtil.isAncestor(condition, context, false); } private static boolean inSwitchExpression(PsiElement context) { return context.getParent() instanceof GrCodeBlock && context.getParent().getParent() instanceof GrSwitchElement; } private static boolean afterTry(PsiElement context) { if (context != null && GroovyCompletionUtil.nearestLeftSibling(context) instanceof GrTryCatchStatement) { GrTryCatchStatement tryStatement = (GrTryCatchStatement) GroovyCompletionUtil.nearestLeftSibling(context); if (tryStatement == null) return false; if (tryStatement.getFinallyClause() == null) { return true; } } if (context != null && GroovyCompletionUtil.nearestLeftSibling(context) instanceof PsiErrorElement && GroovyCompletionUtil.nearestLeftSibling(context).getPrevSibling() instanceof GrTryCatchStatement) { GrTryCatchStatement tryStatement = (GrTryCatchStatement) GroovyCompletionUtil.nearestLeftSibling(context).getPrevSibling(); if (tryStatement == null) return false; if (tryStatement.getFinallyClause() == null) { return true; } } if (context != null && (context.getParent() instanceof GrReferenceExpression || context.getParent() instanceof PsiErrorElement) && GroovyCompletionUtil.nearestLeftSibling(context.getParent()) instanceof GrTryCatchStatement) { GrTryCatchStatement tryStatement = (GrTryCatchStatement) GroovyCompletionUtil.nearestLeftSibling(context.getParent()); if (tryStatement == null) return false; if (tryStatement.getFinallyClause() == null) { return true; } } if (context != null && (context.getParent() instanceof GrReferenceExpression || context.getParent() instanceof PsiErrorElement) && GroovyCompletionUtil.nearestLeftSibling(context.getParent()) instanceof PsiErrorElement && GroovyCompletionUtil.nearestLeftSibling(context.getParent()).getPrevSibling() instanceof GrTryCatchStatement) { GrTryCatchStatement tryStatement = (GrTryCatchStatement) GroovyCompletionUtil.nearestLeftSibling(context.getParent()).getPrevSibling(); if (tryStatement == null) return false; if (tryStatement.getFinallyClause() == null) { return true; } } if (context != null && (context.getParent() instanceof GrReferenceExpression) && (context.getParent().getParent() instanceof GrMethodCall) && GroovyCompletionUtil.nearestLeftSibling(context.getParent().getParent()) instanceof GrTryCatchStatement) { GrTryCatchStatement tryStatement = (GrTryCatchStatement) GroovyCompletionUtil.nearestLeftSibling(context.getParent().getParent()); if (tryStatement == null) return false; if (tryStatement.getFinallyClause() == null) { return true; } } return false; } private static boolean afterIfOrElse(PsiElement context) { if (context.getParent() != null && GroovyCompletionUtil.nearestLeftSibling(context.getParent()) instanceof GrIfStatement) { return true; } if (context.getParent() != null && GroovyCompletionUtil.nearestLeftSibling(context.getParent()) instanceof PsiErrorElement && GroovyCompletionUtil.nearestLeftSibling(GroovyCompletionUtil.nearestLeftSibling(context.getParent())) instanceof GrIfStatement) { return true; } if (context.getParent() != null && GroovyCompletionUtil.nearestLeftSibling(context) != null && GroovyCompletionUtil.nearestLeftSibling(context).getPrevSibling() instanceof GrIfStatement) { GrIfStatement statement = (GrIfStatement) GroovyCompletionUtil.nearestLeftSibling(context).getPrevSibling(); if (statement.getElseBranch() == null) { return true; } } if (context.getParent() != null && context.getParent().getParent() instanceof GrCommandArgumentList && context.getParent().getParent().getParent().getParent() instanceof GrIfStatement) { GrIfStatement statement = (GrIfStatement) context.getParent().getParent().getParent().getParent(); if (statement.getElseBranch() == null) { return true; } } return false; } private static final ElementPattern<PsiElement> SKIP_CONDITION = StandardPatterns.or( psiElement().whitespaceCommentEmptyOrError(), psiElement(GroovyElementTypes.NL) ); private static final ElementPattern<PsiElement> WHILE_KEYWORD_POSITION = StandardPatterns.or( psiElement() .withSuperParent(2, GrDoWhileStatement.class) .afterLeafSkipping(SKIP_CONDITION, psiElement(GroovyElementTypes.T_RBRACE)), psiElement() .withSuperParent(4, GrDoWhileStatement.class) .withSuperParent(3, GrApplicationStatement.class) ); private static boolean afterAbstractMethod(PsiElement context, boolean acceptAnnotationMethods, boolean skipNLs) { PsiElement candidate; if (GroovyCompletionUtil.isInTypeDefinitionBody(context)) { PsiElement run = context; while(!(run.getParent() instanceof GrTypeDefinitionBody)) { run = run.getParent(); assert run != null; } candidate = PsiUtil.skipWhitespacesAndComments(run.getPrevSibling(), false, skipNLs); } else { candidate = PsiUtil.skipWhitespacesAndComments(PsiTreeUtil.prevLeaf(context), false); } if (candidate instanceof PsiErrorElement) candidate = candidate.getPrevSibling(); return candidate instanceof GrMethod && ((GrMethod)candidate).getBlock() == null && (acceptAnnotationMethods || !(candidate instanceof GrAnnotationMethod)); } private static boolean suggestPrimitiveTypes(PsiElement context) { if (isInfixOperatorPosition(context)) return false; if (isAfterForParameter(context)) return false; final PsiElement parent = context.getParent(); if (parent == null) return false; PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling()); if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) { PsiElement prevSibling = context.getPrevSibling(); if (prevSibling != null && prevSibling.getNode() != null) { if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) { return true; } } else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) { return true; } } if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context)) return true; if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) { return false; } if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) { return true; } if ((parent instanceof GrParameter && ((GrParameter)parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) { PsiElement prevSibling = context.getPrevSibling(); if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) { ASTNode node = prevSibling.getNode(); return !TokenSets.DOTS.contains(node.getElementType()); } else { return true; } } if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) { return true; } if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) { return true; } return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false); } private static boolean asVariableAfterModifiers(PsiElement context) { final PsiElement parent = context.getParent(); if (parent instanceof GrVariable && context == ((GrVariable)parent).getNameIdentifierGroovy()) { final PsiElement decl = parent.getParent(); if (decl instanceof GrVariableDeclaration && !((GrVariableDeclaration)decl).isTuple() && ((GrVariableDeclaration)decl).getTypeElementGroovy() == null) { return true; } } return false; } private static boolean isInfixOperatorPosition(PsiElement context) { if (context.getParent() != null && context.getParent() instanceof GrReferenceExpression && context.getParent().getParent() != null && context.getParent().getParent() instanceof GrCommandArgumentList) { return true; } if (GroovyCompletionUtil.nearestLeftSibling(context) instanceof PsiErrorElement && GroovyCompletionUtil.endsWithExpression(GroovyCompletionUtil.nearestLeftSibling(context).getPrevSibling())) { return true; } if (context.getParent() instanceof GrReferenceExpression && GroovyCompletionUtil.nearestLeftLeaf(context) instanceof PsiErrorElement && GroovyCompletionUtil.endsWithExpression(GroovyCompletionUtil.nearestLeftLeaf(context).getPrevSibling())) { return true; } if (context.getParent() instanceof PsiErrorElement && GroovyCompletionUtil.endsWithExpression(GroovyCompletionUtil.nearestLeftSibling(context.getParent()))) { return true; } return false; } private static boolean suggestModifiers(PsiElement context) { if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.isNewStatementInScript(context)) { return true; } if (GroovyCompletionUtil.isFirstElementAfterPossibleModifiersInVariableDeclaration(context, false) && !PsiJavaPatterns.psiElement().afterLeaf("def").accepts(context)) { return true; } if (PsiJavaPatterns.psiElement().afterLeaf(MODIFIERS).accepts(context) || PsiJavaPatterns.psiElement().afterLeaf("synchronized").accepts(context)) { return true; } final PsiElement contextParent = context.getParent(); if (contextParent instanceof GrReferenceElement && contextParent.getParent() instanceof GrTypeElement) { PsiElement parent = contextParent.getParent().getParent(); if (parent instanceof GrVariableDeclaration && (parent.getParent() instanceof GrTypeDefinitionBody || parent.getParent() instanceof GroovyFile) || parent instanceof GrMethod) { return true; } } if (contextParent instanceof GrField) { final GrVariable variable = (GrVariable)contextParent; if (variable.getTypeElementGroovy() == null) { return true; } } if (contextParent instanceof GrExpression && contextParent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false)) { return true; } if (context.getTextRange().getStartOffset() == 0 && !(context instanceof OuterLanguageElement)) { return true; } return contextParent instanceof GrExpression && contextParent.getParent() instanceof GrApplicationStatement && contextParent.getParent().getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false); } public static boolean suggestFinalDef(PsiElement context) { if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || GroovyCompletionUtil.isNewStatementInScript(context) && !GroovyCompletionUtil.isReferenceElementInNewExpr(context) || GroovyCompletionUtil.isTypelessParameter(context) || GroovyCompletionUtil.isCodeReferenceElementApplicableToModifierCompletion(context)) { return true; } if (PsiImplUtil.realPrevious(context.getParent().getPrevSibling()) instanceof GrModifierList) { return true; } if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) { return true; } return context.getParent() instanceof GrExpression && context.getParent().getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false); } }