Search in sources :

Example 11 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class ParametersFolder method isParameterFoldable.

boolean isParameterFoldable(@NotNull VariableData data, @NotNull LocalSearchScope scope, @NotNull final List<? extends PsiVariable> inputVariables, UniqueNameGenerator nameGenerator, String defaultName) {
    final List<PsiExpression> mentionedInExpressions = getMentionedExpressions(data.variable, scope, inputVariables);
    if (mentionedInExpressions == null)
        return false;
    int currentRank = 0;
    PsiExpression mostRanked = null;
    for (int i = mentionedInExpressions.size() - 1; i >= 0; i--) {
        PsiExpression expression = mentionedInExpressions.get(i);
        boolean arrayAccess = expression instanceof PsiArrayAccessExpression && !isConditional(expression, scope);
        if (arrayAccess) {
            myFoldingSelectedByDefault = true;
        }
        final int r = findUsedVariables(data, inputVariables, expression).size();
        if (currentRank < r || arrayAccess && currentRank == r) {
            currentRank = r;
            mostRanked = expression;
        }
    }
    if (mostRanked != null) {
        myExpressions.put(data.variable, mostRanked);
        myArgs.put(data.variable, mostRanked.getText());
        data.type = RefactoringChangeUtil.getTypeByExpression(mostRanked);
        final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(mostRanked.getProject());
        final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, mostRanked, data.type);
        if (nameInfo.names.length > 0 && !Comparing.equal(nameInfo.names[0], data.name) && !Comparing.equal(nameInfo.names[0], defaultName)) {
            data.name = nameInfo.names[0];
            setUniqueName(data, nameGenerator, scope, mostRanked);
        }
    }
    return mostRanked != null;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 12 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class ExpressionUtil method getNamesForIdentifier.

@Nullable
private static String[] getNamesForIdentifier(Project project, PsiIdentifier identifier) {
    if (identifier.getParent() instanceof PsiVariable) {
        PsiVariable var = (PsiVariable) identifier.getParent();
        if (identifier.equals(var.getNameIdentifier())) {
            JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
            VariableKind variableKind = codeStyleManager.getVariableKind(var);
            PsiExpression initializer = var.getInitializer();
            if (var instanceof PsiParameter && ((PsiParameter) var).getDeclarationScope() instanceof PsiForeachStatement) {
                //synthesize initializer
                PsiForeachStatement foreachStatement = (PsiForeachStatement) ((PsiParameter) var).getDeclarationScope();
                final PsiExpression iteratedValue = foreachStatement.getIteratedValue();
                if (iteratedValue != null) {
                    try {
                        final PsiArrayAccessExpression expr = (PsiArrayAccessExpression) JavaPsiFacade.getInstance(iteratedValue.getProject()).getElementFactory().createExpressionFromText("a[0]", var);
                        expr.getArrayExpression().replace(iteratedValue);
                        //note: non physical with no parent
                        initializer = expr;
                    } catch (IncorrectOperationException e) {
                    //do nothing
                    }
                }
            }
            SuggestedNameInfo suggestedInfo = codeStyleManager.suggestVariableName(variableKind, null, initializer, var.getType());
            return suggestedInfo.names;
        }
    }
    return null;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class NameSuggestionsManager method updateSuggestions.

private void updateSuggestions(PsiType selectedType) {
    SuggestedNameInfo nameInfo = myTypesToSuggestions.get(selectedType);
    if (nameInfo == null) {
        nameInfo = myGenerator.getSuggestedNameInfo(selectedType);
        myTypesToSuggestions.put(selectedType, nameInfo);
    }
    myNameField.setSuggestions(nameInfo.names);
}
Also used : SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 14 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class MoveInstanceMethodHandler method suggestParameterNameForThisClass.

public static String suggestParameterNameForThisClass(final PsiClass thisClass) {
    PsiManager manager = thisClass.getManager();
    PsiType type = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createType(thisClass);
    final SuggestedNameInfo suggestedNameInfo = JavaCodeStyleManager.getInstance(manager.getProject()).suggestVariableName(VariableKind.PARAMETER, null, null, type);
    return suggestedNameInfo.names.length > 0 ? suggestedNameInfo.names[0] : "";
}
Also used : SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 15 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class JavaIntroduceVariableTest method doTest.

protected static void doTest(String expression, VariableKind kind, PsiType type, String... results) throws Exception {
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(getProject());
    final PsiExpression expr = factory.createExpressionFromText(expression, null);
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(getProject());
    final SuggestedNameInfo info = codeStyleManager.suggestVariableName(kind, null, expr, type);
    assert info.names.length >= results.length : msg("Can't find some variants", info.names, results);
    for (int i = 0; i < results.length; i++) {
        if (!results[i].equals(info.names[i])) {
            throw new Exception(msg("", info.names, results));
        }
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) PsiElementFactory(com.intellij.psi.PsiElementFactory) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Aggregations

SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)40 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)22 VariableKind (com.intellij.psi.codeStyle.VariableKind)11 Nullable (org.jetbrains.annotations.Nullable)7 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)6 ArrayList (java.util.ArrayList)4 LinkedHashSet (java.util.LinkedHashSet)4 NonNls (org.jetbrains.annotations.NonNls)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Editor (com.intellij.openapi.editor.Editor)3 PsiType (com.intellij.psi.PsiType)3 PreferrableNameSuggestionProvider (com.intellij.refactoring.rename.PreferrableNameSuggestionProvider)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 List (java.util.List)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 com.intellij.psi (com.intellij.psi)2 THashSet (gnu.trove.THashSet)2 JavaPsiEquivalenceUtil (com.intellij.codeInsight.JavaPsiEquivalenceUtil)1