protected boolean shouldParenthesizeQualifier( final PsiFile file, final int startOffset, final int endOffset) { PsiElement element = file.findElementAt(startOffset); if (element == null) { return false; } PsiElement last = element; while (element != null && element.getTextRange().getStartOffset() >= startOffset && element.getTextRange().getEndOffset() <= endOffset && !(element instanceof PsiExpressionStatement)) { last = element; element = element.getParent(); } PsiExpression expr = PsiTreeUtil.getParentOfType(last, PsiExpression.class, false, PsiClass.class); if (expr == null) { return false; } if (expr.getTextRange().getEndOffset() > endOffset) { return true; } if (expr instanceof PsiJavaCodeReferenceElement || expr instanceof PsiMethodCallExpression || expr instanceof PsiArrayAccessExpression) { return false; } return true; }
@Override public String handleEmptyLookup( @NotNull final CompletionParameters parameters, final Editor editor) { if (!(parameters.getOriginalFile() instanceof PsiJavaFile)) return null; final String ad = advertise(parameters); final String suffix = ad == null ? "" : "; " + StringUtil.decapitalize(ad); if (parameters.getCompletionType() == CompletionType.SMART) { if (!ApplicationManager.getApplication().isUnitTestMode()) { final Project project = parameters.getPosition().getProject(); final PsiFile file = parameters.getOriginalFile(); PsiExpression expression = PsiTreeUtil.getContextOfType(parameters.getPosition(), PsiExpression.class, true); if (expression != null && expression.getParent() instanceof PsiExpressionList) { int lbraceOffset = expression.getParent().getTextRange().getStartOffset(); ShowParameterInfoHandler.invoke(project, editor, file, lbraceOffset, null); } if (expression instanceof PsiLiteralExpression) { return LangBundle.message("completion.no.suggestions") + suffix; } if (expression instanceof PsiInstanceOfExpression) { final PsiInstanceOfExpression instanceOfExpression = (PsiInstanceOfExpression) expression; if (PsiTreeUtil.isAncestor( instanceOfExpression.getCheckType(), parameters.getPosition(), false)) { return LangBundle.message("completion.no.suggestions") + suffix; } } } final Set<PsiType> expectedTypes = JavaCompletionUtil.getExpectedTypes(parameters); if (expectedTypes != null) { PsiType type = expectedTypes.size() == 1 ? expectedTypes.iterator().next() : null; if (type != null) { final PsiType deepComponentType = type.getDeepComponentType(); if (deepComponentType instanceof PsiClassType) { if (((PsiClassType) deepComponentType).resolve() != null) { return CompletionBundle.message( "completion.no.suggestions.of.type", type.getPresentableText()) + suffix; } return CompletionBundle.message("completion.unknown.type", type.getPresentableText()) + suffix; } if (!PsiType.NULL.equals(type)) { return CompletionBundle.message( "completion.no.suggestions.of.type", type.getPresentableText()) + suffix; } } } } return LangBundle.message("completion.no.suggestions") + suffix; }
@Override public boolean accepts( @NotNull PsiSwitchStatement psiSwitchStatement, ProcessingContext context) { final PsiExpression expression = psiSwitchStatement.getExpression(); if (expression == null) return false; PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(expression.getType()); return aClass != null && aClass.isEnum(); }
@Nullable private static PsiType getQualifierCastType( PsiJavaReference javaReference, CompletionParameters parameters) { if (javaReference instanceof PsiReferenceExpression) { final PsiReferenceExpression refExpr = (PsiReferenceExpression) javaReference; final PsiExpression qualifier = refExpr.getQualifierExpression(); if (qualifier != null) { final Project project = qualifier.getProject(); PsiType type = null; final PairFunction<PsiExpression, CompletionParameters, PsiType> evaluator = refExpr.getContainingFile().getCopyableUserData(DYNAMIC_TYPE_EVALUATOR); if (evaluator != null) { type = evaluator.fun(qualifier, parameters); } if (type == null) { type = GuessManager.getInstance(project).getControlFlowExpressionType(qualifier); } return type; } } return null; }