Search in sources :

Example 36 with SuggestedNameInfo

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

the class ForEachMigration method migrate.

@Override
PsiElement migrate(@NotNull Project project, @NotNull PsiStatement body, @NotNull TerminalBlock tb) {
    PsiLoopStatement loopStatement = tb.getMainLoop();
    restoreComments(loopStatement, body);
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
    PsiExpression mapExpression = tryExtractMapExpression(tb);
    if (mapExpression != null) {
        PsiMethodCallExpression call = tb.getSingleMethodCall();
        LOG.assertTrue(call != null);
        PsiType addedType = getAddedElementType(call);
        if (addedType == null)
            addedType = call.getType();
        JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
        SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, addedType, false);
        if (suggestedNameInfo.names.length == 0) {
            suggestedNameInfo = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, "item", null, null, false);
        }
        String varName = codeStyleManager.suggestUniqueVariableName(suggestedNameInfo, call, false).names[0];
        String streamText = tb.add(new StreamApiMigrationInspection.MapOp(mapExpression, tb.getVariable(), addedType)).generate();
        String forEachBody = varName + "->" + call.getMethodExpression().getText() + "(" + varName + ")";
        String callText = streamText + "." + getReplacement() + "(" + forEachBody + ");";
        return loopStatement.replace(factory.createStatementFromText(callText, loopStatement));
    }
    String stream = tb.generate(true) + "." + getReplacement() + "(";
    PsiElement block = tb.convertToElement(factory);
    final String functionalExpressionText = tb.getVariable().getName() + " -> " + wrapInBlock(block);
    PsiExpressionStatement callStatement = (PsiExpressionStatement) factory.createStatementFromText(stream + functionalExpressionText + ");", loopStatement);
    callStatement = (PsiExpressionStatement) loopStatement.replace(callStatement);
    final PsiExpressionList argumentList = ((PsiCallExpression) callStatement.getExpression()).getArgumentList();
    LOG.assertTrue(argumentList != null, callStatement.getText());
    final PsiExpression[] expressions = argumentList.getExpressions();
    LOG.assertTrue(expressions.length == 1);
    if (expressions[0] instanceof PsiFunctionalExpression && ((PsiFunctionalExpression) expressions[0]).getFunctionalInterfaceType() == null) {
        callStatement = (PsiExpressionStatement) callStatement.replace(factory.createStatementFromText(stream + "(" + tb.getVariable().getText() + ") -> " + wrapInBlock(block) + ");", callStatement));
    }
    return callStatement;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 37 with SuggestedNameInfo

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

the class ParameterNameExpression method calculateLookupItems.

@Override
public LookupElement[] calculateLookupItems(ExpressionContext context) {
    SuggestedNameInfo info = getNameInfo(context);
    if (info == null)
        return null;
    LookupElement[] result = new LookupElement[info.names.length];
    int i = 0;
    for (String name : info.names) {
        result[i++] = LookupElementBuilder.create(name);
    }
    return result;
}
Also used : SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 38 with SuggestedNameInfo

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

the class GrMainCompletionProvider method suggestVariableNames.

private static void suggestVariableNames(PsiElement context, CompletionResultSet result) {
    final PsiElement parent = context.getParent();
    if (GroovyCompletionUtil.isWildcardCompletion(context))
        return;
    if (parent instanceof GrVariable) {
        final GrVariable variable = (GrVariable) parent;
        if (context.equals(variable.getNameIdentifierGroovy())) {
            final PsiType type = variable.getTypeGroovy();
            if (type != null) {
                final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
                VariableKind kind = variable instanceof GrParameter ? VariableKind.PARAMETER : variable instanceof GrField ? VariableKind.FIELD : VariableKind.LOCAL_VARIABLE;
                SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(kind, null, null, type);
                String[] names = suggestedNameInfo.names;
                if (names.length > 0) {
                    String name = names[0];
                    String newName = InlineMethodConflictSolver.suggestNewName(name, null, parent);
                    if (!name.equals(newName)) {
                        result.addElement(LookupElementBuilder.create(newName));
                        return;
                    }
                }
                for (String name : names) {
                    result.addElement(LookupElementBuilder.create(name));
                }
            }
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                for (String name : GroovyNameSuggestionUtil.suggestVariableNames(initializer, new DefaultGroovyVariableNameValidator(variable), variable.hasModifierProperty(PsiModifier.STATIC))) {
                    result.addElement(LookupElementBuilder.create(name));
                }
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 39 with SuggestedNameInfo

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

the class GrAliasImportIntention method getSuggestedNames.

public static LinkedHashSet<String> getSuggestedNames(PsiElement psiElement, final PsiElement nameSuggestionContext) {
    final LinkedHashSet<String> result = new LinkedHashSet<>();
    result.add(UsageViewUtil.getShortName(psiElement));
    final NameSuggestionProvider[] providers = Extensions.getExtensions(NameSuggestionProvider.EP_NAME);
    for (NameSuggestionProvider provider : providers) {
        SuggestedNameInfo info = provider.getSuggestedNames(psiElement, nameSuggestionContext, result);
        if (info != null) {
            if (provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider) provider).shouldCheckOthers()) {
                break;
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) NameSuggestionProvider(com.intellij.refactoring.rename.NameSuggestionProvider) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider)

Example 40 with SuggestedNameInfo

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

the class ActionNameCustomReferenceConverter method createReferences.

@NotNull
@Override
public PsiReference[] createReferences(final GenericDomValue<String> genericDomValue, final PsiElement psiElement, final ConvertContext convertContext) {
    final PsiReferenceBase<PsiElement> ref = new PsiReferenceBase<PsiElement>(psiElement) {

        @SuppressWarnings({ "ConstantConditions" })
        public PsiElement resolve() {
            return genericDomValue.getParent().getXmlTag();
        }

        public boolean isSoft() {
            return true;
        }

        // do nothing. the element will be renamed via PsiMetaData
        public PsiElement handleElementRename(final String newElementName) throws IncorrectOperationException {
            return getElement();
        }

        @NotNull
        public Object[] getVariants() {
            final DomElement invocationElement = convertContext.getInvocationElement();
            final Action action = invocationElement.getParentOfType(Action.class, true);
            assert action != null;
            final PsiClass psiClass = action.searchActionClass();
            if (psiClass == null) {
                return EMPTY_ARRAY;
            }
            final Project project = psiClass.getProject();
            final PsiClassType classType = PsiTypesUtil.getClassType(psiClass);
            final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
            final SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, classType);
            final Set<String> variants = new HashSet<>(Arrays.asList(info.names));
            variants.remove(ACTION_SUFFIX);
            // remove existing action-names
            final List<Action> actions = action.getStrutsPackage().getActions();
            ContainerUtil.process(actions, action1 -> {
                variants.remove(action1.getName().getStringValue());
                return true;
            });
            return ContainerUtil.map2Array(variants, ACTION_NAME_FUNCTION);
        }
    };
    return new PsiReference[] { ref };
}
Also used : Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

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