Java 类com.intellij.psi.codeStyle.SuggestedNameInfo 实例源码

项目:intellij-ce-playground    文件:JavaMemberNameCompletionContributor.java   
private static void addLookupItems(Set<LookupElement> lookupElements, @Nullable final SuggestedNameInfo callback, PrefixMatcher matcher, Project project, String... strings) {
  outer:
  for (int i = 0; i < strings.length; i++) {
    String name = strings[i];
    if (!matcher.prefixMatches(name) || !PsiNameHelper.getInstance(project).isIdentifier(name, LanguageLevel.HIGHEST)) {
      continue;
    }

    for (LookupElement lookupElement : lookupElements) {
      if (lookupElement.getAllLookupStrings().contains(name)) {
        continue outer;
      }
    }

    LookupElement element = PrioritizedLookupElement.withPriority(LookupElementBuilder.create(name).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE), -i);
    if (callback != null) {
      element = withInsertHandler(callback, element);
    }
    lookupElements.add(element);
  }
}
项目:intellij-ce-playground    文件:MethodParameterFix.java   
private ParameterInfoImpl[] getNewParametersInfo(PsiMethod method) throws IncorrectOperationException {
  List<ParameterInfoImpl> result = new ArrayList<ParameterInfoImpl>();
  PsiParameter[] parameters = method.getParameterList().getParameters();
  PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();
  JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
  SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, myParameterType);
  PsiParameter newParameter = factory.createParameter(nameInfo.names[0], myParameterType);
  if (method.getContainingClass().isInterface()) {
    PsiUtil.setModifierProperty(newParameter, PsiModifier.FINAL, false);
    }

  for (int i = 0; i < parameters.length; i++) {
    PsiParameter parameter = parameters[i];
    if (i == myIndex) {
      newParameter.setName(parameter.getName());
      parameter = newParameter;
    }
    result.add(new ParameterInfoImpl(i, parameter.getName(), parameter.getType()));
  }
  if (parameters.length == myIndex) {
    result.add(new ParameterInfoImpl(-1, newParameter.getName(), newParameter.getType()));
  }
  return result.toArray(new ParameterInfoImpl[result.size()]);
}
项目:intellij-ce-playground    文件:AssignFieldFromParameterAction.java   
@Nullable
private static PsiField findFieldToAssign(@NotNull Project project,
                                          @NotNull PsiParameter myParameter) {
  final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
  final String parameterName = myParameter.getName();
  final String propertyName = styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER);

  final PsiMethod method = (PsiMethod)myParameter.getDeclarationScope();

  final boolean isMethodStatic = method.hasModifierProperty(PsiModifier.STATIC);
  final VariableKind kind = isMethodStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
  final SuggestedNameInfo suggestedNameInfo = styleManager.suggestVariableName(kind, propertyName, null, FieldFromParameterUtils.getSubstitutedType(myParameter));

  final String fieldName = suggestedNameInfo.names[0];

  PsiClass aClass = method.getContainingClass();
  if (aClass == null) return null;
  PsiField field = aClass.findFieldByName(fieldName, false);
  if (field == null) return null;
  if (!field.hasModifierProperty(PsiModifier.STATIC) && isMethodStatic) return null;

  return field;
}
项目:intellij-ce-playground    文件:JavaNameSuggestionProvider.java   
@Nullable
private static SuggestedNameInfo suggestNamesForElement(final PsiElement element, PsiElement nameSuggestionContext) {
  PsiVariable var = null;
  if (element instanceof PsiVariable) {
    var = (PsiVariable)element;
  }
  else if (element instanceof PsiIdentifier) {
    PsiIdentifier identifier = (PsiIdentifier)element;
    if (identifier.getParent() instanceof PsiVariable) {
      var = (PsiVariable)identifier.getParent();
    }
  }

  if (var == null) return null;

  JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(element.getProject());
  VariableKind variableKind = codeStyleManager.getVariableKind(var);
  final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(variableKind, null, var.getInitializer(), var.getType());
  final PsiExpression expression = PsiTreeUtil.getParentOfType(nameSuggestionContext, PsiExpression.class, false);
  if (expression != null) {
    return new SuggestedNameInfo.Delegate(codeStyleManager.suggestVariableName(variableKind, null, expression, var.getType()).names, nameInfo);

  }
  return nameInfo;
}
项目:intellij-ce-playground    文件:IntroduceParameterHandler.java   
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final PsiExpression expr,
                                                                        final String propName,
                                                                        final Project project,
                                                                        final String enteredName) {
  return new NameSuggestionsGenerator() {
    public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
      final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
      SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, propName, expr != null && expr.isValid() ? expr : null, type);
      if (expr != null && expr.isValid()) {
        info = codeStyleManager.suggestUniqueVariableName(info, expr, true);
      }
      final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil
        .completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, info), expr);
      return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, info);
    }

  };
}
项目:intellij-ce-playground    文件:IntroduceConstantDialog.java   
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final String propertyName,
                                                                        final PsiExpression psiExpression,
                                                                        final JavaCodeStyleManager codeStyleManager,
                                                                        final String enteredName, final PsiClass parentClass) {
  return new NameSuggestionsGenerator() {
    public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
      SuggestedNameInfo nameInfo =
          codeStyleManager.suggestVariableName(VariableKind.STATIC_FINAL_FIELD, propertyName, psiExpression, type);
      if (psiExpression != null) {
        String[] names = nameInfo.names;
        for (int i = 0, namesLength = names.length; i < namesLength; i++) {
          String name = names[i];
          if (parentClass.findFieldByName(name, false) != null) {
            names[i] = codeStyleManager.suggestUniqueVariableName(name, psiExpression, true);
          }
        }
      }
      final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil
        .completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo), psiExpression);
      return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, nameInfo);
    }

  };
}
项目:intellij-ce-playground    文件:InheritanceToDelegationDialog.java   
private void updateTargetClass() {
  final PsiClass targetClass = getSelectedTargetClass();
  PsiManager psiManager = myClass.getManager();
  PsiType superType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(targetClass);
  SuggestedNameInfo suggestedNameInfo =
    JavaCodeStyleManager.getInstance(psiManager.getProject()).suggestVariableName(VariableKind.FIELD, null, null, superType);
  myFieldNameField.setSuggestions(suggestedNameInfo.names);
  myInnerClassNameField.getComponent().setEnabled(InheritanceToDelegationUtil.isInnerClassNeeded(myClass, targetClass));
  @NonNls final String suggestion = "My" + targetClass.getName();
  myInnerClassNameField.setSuggestions(new String[]{suggestion});

  myDataChangedListener = new NameSuggestionsField.DataChanged() {
    public void dataChanged() {
      validateButtons();
    }
  };
  myInnerClassNameField.addDataChangedListener(myDataChangedListener);
  myFieldNameField.addDataChangedListener(myDataChangedListener);

  myMemberSelectionPanel.getTable().setMemberInfos(myBasesToMemberInfos.get(targetClass));
  myMemberSelectionPanel.getTable().fireExternalDataChange();
}
项目:intellij-ce-playground    文件:DictionarySuggestionProvider.java   
@Override
public SuggestedNameInfo getSuggestedNames(PsiElement element, PsiElement nameSuggestionContext, Set<String> result) {
  assert result != null;
  if (!active || nameSuggestionContext==null) {
    return null;
  }
  String text = nameSuggestionContext.getText();
  if (nameSuggestionContext instanceof PsiNamedElement) {
    //noinspection ConstantConditions
    text = ((PsiNamedElement)element).getName();
  }
  if (text == null) {
    return null;
  }

  SpellCheckerManager manager = SpellCheckerManager.getInstance(element.getProject());

  ContainerUtil.addAllNotNull(result, manager.getSuggestions(text));
  return SuggestedNameInfo.NULL_INFO;
}
项目:intellij-ce-playground    文件:RenameDialog.java   
public String[] getSuggestedNames() {
  final LinkedHashSet<String> result = new LinkedHashSet<String>();
  final String initialName = VariableInplaceRenameHandler.getInitialName();
  if (initialName != null) {
    result.add(initialName);
  }
  result.add(UsageViewUtil.getShortName(myPsiElement));
  final NameSuggestionProvider[] providers = Extensions.getExtensions(NameSuggestionProvider.EP_NAME);
  for(NameSuggestionProvider provider: providers) {
    SuggestedNameInfo info = provider.getSuggestedNames(myPsiElement, myNameSuggestionContext, result);
    if (info != null) {
      mySuggestedNameInfo = info;
      if (provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider)provider).shouldCheckOthers()) break;
    }
  }
  return ArrayUtil.toStringArray(result);
}
项目:intellij-ce-playground    文件:PyNameSuggestionProvider.java   
public SuggestedNameInfo getSuggestedNames(PsiElement element, PsiElement nameSuggestionContext, Set<String> result) {
  if (!(element instanceof PyElement)) return null;
  final String name = ((PyElement)element).getName();
  if (name == null) return null;

  if (element instanceof PyClass) {
    result.add(toCamelCase(name, true));
  }
  else if (element instanceof PyFunction || element instanceof PyParameter) {
    result.add(name.toLowerCase());
  }
  else {
    result.add(name.toLowerCase());
    final PyAssignmentStatement assignmentStatement = PsiTreeUtil.getParentOfType(element, PyAssignmentStatement.class);
    if (assignmentStatement != null) return null;
    result.add(name.toUpperCase());
    result.add(toCamelCase(name, false));
  }
  return SuggestedNameInfo.NULL_INFO;
}
项目:intellij-ce-playground    文件:WhileCanBeForeachInspection.java   
private static String createNewVariableName(@NotNull PsiWhileStatement scope, PsiType type, String containerName) {
  final Project project = scope.getProject();
  final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
  @NonNls String baseName;
  if (containerName != null) {
    baseName = StringUtils.createSingularFromName(containerName);
  }
  else {
    final SuggestedNameInfo suggestions = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type);
    final String[] names = suggestions.names;
    if (names != null && names.length > 0) {
      baseName = names[0];
    }
    else {
      baseName = "value";
    }
  }
  if (baseName == null || baseName.isEmpty()) {
    baseName = "value";
  }
  return codeStyleManager.suggestUniqueVariableName(baseName, scope, true);
}
项目:intellij-ce-playground    文件:KeySetIterationMayUseEntrySetInspection.java   
private static String createNewVariableName(@NotNull PsiElement scope, @NotNull PsiType type) {
  final Project project = scope.getProject();
  final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
  @NonNls String baseName;
  final SuggestedNameInfo suggestions = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type);
  final String[] names = suggestions.names;
  if (names != null && names.length > 0) {
    baseName = names[0];
  }
  else {
    baseName = "entry";
  }
  if (baseName == null || baseName.isEmpty()) {
    baseName = "entry";
  }
  return codeStyleManager.suggestUniqueVariableName(baseName, scope, true);
}
项目:intellij-ce-playground    文件:GroovyNameSuggestionProvider.java   
@Override
public SuggestedNameInfo getSuggestedNames(final PsiElement element, @Nullable PsiElement nameSuggestionContext, Set<String> result) {
  if (nameSuggestionContext == null) nameSuggestionContext = element;
  if (element instanceof GrVariable && nameSuggestionContext instanceof GroovyPsiElement) {
    final PsiType type = ((GrVariable)element).getTypeGroovy();
    if (type != null) {
      final String[] names = GroovyNameSuggestionUtil
        .suggestVariableNameByType(type, new DefaultGroovyVariableNameValidator((GroovyPsiElement)nameSuggestionContext));
      result.addAll(Arrays.asList(names));
      return new SuggestedNameInfo(names) {
        @Override
        public void nameChosen(String name) {
          JavaStatisticsManager
            .incVariableNameUseCount(name, JavaCodeStyleManager.getInstance(element.getProject()).getVariableKind((GrVariable)element),
                                     ((GrVariable)element).getName(), type);
        }
      };
    }
  }
  return null;
}
项目:tools-idea    文件:JavaMemberNameCompletionContributor.java   
private static void addLookupItems(Set<LookupElement> lookupElements, @Nullable final SuggestedNameInfo callback, PrefixMatcher matcher, Project project, String... strings) {
  outer:
  for (int i = 0; i < strings.length; i++) {
    String name = strings[i];
    if (!matcher.prefixMatches(name) || !JavaPsiFacade.getInstance(project).getNameHelper().isIdentifier(name, LanguageLevel.HIGHEST)) {
      continue;
    }

    for (LookupElement lookupElement : lookupElements) {
      if (lookupElement.getAllLookupStrings().contains(name)) {
        continue outer;
      }
    }

    LookupElement element = PrioritizedLookupElement.withPriority(LookupElementBuilder.create(name).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE), -i);
    if (callback != null) {
      element = LookupElementDecorator.withInsertHandler(element, new InsertHandler<LookupElementDecorator<LookupElement>>() {
        @Override
        public void handleInsert(InsertionContext context, LookupElementDecorator<LookupElement> item) {
          callback.nameChosen(item.getLookupString());
        }
      });
    }
    lookupElements.add(element);
  }
}
项目:tools-idea    文件:MethodParameterFix.java   
private ParameterInfoImpl[] getNewParametersInfo(PsiMethod method) throws IncorrectOperationException {
  List<ParameterInfoImpl> result = new ArrayList<ParameterInfoImpl>();
  PsiParameter[] parameters = method.getParameterList().getParameters();
  PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();
  JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
  SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, myParameterType);
  PsiParameter newParameter = factory.createParameter(nameInfo.names[0], myParameterType);
  if (method.getContainingClass().isInterface()) {
    PsiUtil.setModifierProperty(newParameter, PsiModifier.FINAL, false);
    }

  for (int i = 0; i < parameters.length; i++) {
    PsiParameter parameter = parameters[i];
    if (i == myIndex) {
      newParameter.setName(parameter.getName());
      parameter = newParameter;
    }
    result.add(new ParameterInfoImpl(i, parameter.getName(), parameter.getType()));
  }
  if (parameters.length == myIndex) {
    result.add(new ParameterInfoImpl(-1, newParameter.getName(), newParameter.getType()));
  }
  return result.toArray(new ParameterInfoImpl[result.size()]);
}
项目:tools-idea    文件:CreateConstructorParameterFromFieldFix.java   
private static String getUniqueParameterName(PsiParameter[] parameters, PsiVariable variable, HashMap<PsiField, String> usedNames) {
  final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(variable.getProject());
  final SuggestedNameInfo nameInfo = styleManager
    .suggestVariableName(VariableKind.PARAMETER,
                         styleManager.variableNameToPropertyName(variable.getName(), VariableKind.FIELD),
                         null, variable.getType());
  String newName = nameInfo.names[0];
  int n = 1;
  while (true) {
    if (isUnique(parameters, newName, usedNames)) {
      break;
    }
    newName = nameInfo.names[0] + n++;
  }
  return newName;
}
项目:tools-idea    文件:AssignFieldFromParameterAction.java   
@Nullable
private static PsiField findFieldToAssign(@NotNull Project project,
                                          @NotNull PsiParameter myParameter) {
  final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
  final String parameterName = myParameter.getName();
  final String propertyName = styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER);

  final PsiMethod method = (PsiMethod)myParameter.getDeclarationScope();

  final boolean isMethodStatic = method.hasModifierProperty(PsiModifier.STATIC);
  final VariableKind kind = isMethodStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
  final SuggestedNameInfo suggestedNameInfo = styleManager.suggestVariableName(kind, propertyName, null, FieldFromParameterUtils.getSubstitutedType(myParameter));

  final String fieldName = suggestedNameInfo.names[0];

  PsiClass aClass = method.getContainingClass();
  if (aClass == null) return null;
  PsiField field = aClass.findFieldByName(fieldName, false);
  if (field == null) return null;
  if (!field.hasModifierProperty(PsiModifier.STATIC) && isMethodStatic) return null;

  return field;
}
项目:tools-idea    文件:JavaNameSuggestionProvider.java   
@Nullable
private static SuggestedNameInfo suggestNamesForElement(final PsiElement element, PsiElement nameSuggestionContext) {
  PsiVariable var = null;
  if (element instanceof PsiVariable) {
    var = (PsiVariable)element;
  }
  else if (element instanceof PsiIdentifier) {
    PsiIdentifier identifier = (PsiIdentifier)element;
    if (identifier.getParent() instanceof PsiVariable) {
      var = (PsiVariable)identifier.getParent();
    }
  }

  if (var == null) return null;

  JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(element.getProject());
  VariableKind variableKind = codeStyleManager.getVariableKind(var);
  final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(variableKind, null, var.getInitializer(), var.getType());
  final PsiExpression expression = PsiTreeUtil.getParentOfType(nameSuggestionContext, PsiExpression.class, false);
  if (expression != null) {
    return new SuggestedNameInfo.Delegate(codeStyleManager.suggestVariableName(variableKind, null, expression, var.getType()).names, nameInfo);

  }
  return nameInfo;
}
项目:tools-idea    文件:IntroduceParameterHandler.java   
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final PsiExpression expr,
                                                                        final String propName,
                                                                        final Project project,
                                                                        final String enteredName) {
  return new NameSuggestionsGenerator() {
    public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
      final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
      SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, propName, expr != null && expr.isValid() ? expr : null, type);
      if (expr != null && expr.isValid()) {
        info = codeStyleManager.suggestUniqueVariableName(info, expr, true);
      }
      final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil
        .completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, info), expr);
      return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, info);
    }

  };
}
项目:tools-idea    文件:IntroduceConstantDialog.java   
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final String propertyName,
                                                                        final PsiExpression psiExpression,
                                                                        final JavaCodeStyleManager codeStyleManager,
                                                                        final String enteredName, final PsiClass parentClass) {
  return new NameSuggestionsGenerator() {
    public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
      SuggestedNameInfo nameInfo =
          codeStyleManager.suggestVariableName(VariableKind.STATIC_FINAL_FIELD, propertyName, psiExpression, type);
      if (psiExpression != null) {
        String[] names = nameInfo.names;
        for (int i = 0, namesLength = names.length; i < namesLength; i++) {
          String name = names[i];
          if (parentClass.findFieldByName(name, false) != null) {
            names[i] = codeStyleManager.suggestUniqueVariableName(name, psiExpression, true);
          }
        }
      }
      final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil
        .completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo), psiExpression);
      return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, nameInfo);
    }

  };
}
项目:tools-idea    文件:InheritanceToDelegationDialog.java   
private void updateTargetClass() {
  final PsiClass targetClass = getSelectedTargetClass();
  PsiManager psiManager = myClass.getManager();
  PsiType superType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(targetClass);
  SuggestedNameInfo suggestedNameInfo =
    JavaCodeStyleManager.getInstance(psiManager.getProject()).suggestVariableName(VariableKind.FIELD, null, null, superType);
  myFieldNameField.setSuggestions(suggestedNameInfo.names);
  myInnerClassNameField.getComponent().setEnabled(InheritanceToDelegationUtil.isInnerClassNeeded(myClass, targetClass));
  @NonNls final String suggestion = "My" + targetClass.getName();
  myInnerClassNameField.setSuggestions(new String[]{suggestion});

  myDataChangedListener = new NameSuggestionsField.DataChanged() {
    public void dataChanged() {
      validateButtons();
    }
  };
  myInnerClassNameField.addDataChangedListener(myDataChangedListener);
  myFieldNameField.addDataChangedListener(myDataChangedListener);

  myMemberSelectionPanel.getTable().setMemberInfos(myBasesToMemberInfos.get(targetClass));
  myMemberSelectionPanel.getTable().fireExternalDataChange();
}
项目:tools-idea    文件:DictionarySuggestionProvider.java   
@Override
public SuggestedNameInfo getSuggestedNames(PsiElement element, PsiElement nameSuggestionContext, Set<String> result) {
  assert result != null;
  if (!active || nameSuggestionContext==null) {
    return null;
  }
  String text = nameSuggestionContext.getText();
  if (nameSuggestionContext instanceof PsiNamedElement) {
    //noinspection ConstantConditions
    text = ((PsiNamedElement)element).getName();
  }
  if (text == null) {
    return null;
  }

  SpellCheckerManager manager = SpellCheckerManager.getInstance(element.getProject());

  result.addAll(manager.getSuggestions(text));
  return SuggestedNameInfo.NULL_INFO;
}
项目:tools-idea    文件:KeySetIterationMayUseEntrySetInspection.java   
private static String createNewVariableName(
  @NotNull PsiElement scope, @NotNull PsiType type) {
  final Project project = scope.getProject();
  final JavaCodeStyleManager codeStyleManager =
    JavaCodeStyleManager.getInstance(project);
  @NonNls String baseName;
  final SuggestedNameInfo suggestions =
    codeStyleManager.suggestVariableName(
      VariableKind.LOCAL_VARIABLE, null, null, type);
  final String[] names = suggestions.names;
  if (names != null && names.length > 0) {
    baseName = names[0];
  }
  else {
    baseName = "entry";
  }
  if (baseName == null || baseName.length() == 0) {
    baseName = "entry";
  }
  return codeStyleManager.suggestUniqueVariableName(baseName, scope,
                                                    true);
}
项目:tools-idea    文件:GroovyNameSuggestionProvider.java   
@Override
public SuggestedNameInfo getSuggestedNames(final PsiElement element, @Nullable PsiElement nameSuggestionContext, Set<String> result) {
  if (nameSuggestionContext == null) nameSuggestionContext = element;
  if (element instanceof GrVariable && nameSuggestionContext instanceof GroovyPsiElement) {
    final PsiType type = ((GrVariable)element).getTypeGroovy();
    if (type != null) {
      final String[] names = GroovyNameSuggestionUtil
        .suggestVariableNameByType(type, new DefaultGroovyVariableNameValidator((GroovyPsiElement)nameSuggestionContext));
      result.addAll(Arrays.asList(names));
      return new SuggestedNameInfo(names) {
        @Override
        public void nameChosen(String name) {
          JavaStatisticsManager
            .incVariableNameUseCount(name, JavaCodeStyleManager.getInstance(element.getProject()).getVariableKind((GrVariable)element),
                                     ((GrVariable)element).getName(), type);
        }
      };
    }
  }
  return null;
}
项目:consulo    文件:RenameDialog.java   
public String[] getSuggestedNames() {
  final LinkedHashSet<String> result = new LinkedHashSet<String>();
  final String initialName = VariableInplaceRenameHandler.getInitialName();
  if (initialName != null) {
    result.add(initialName);
  }
  result.add(UsageViewUtil.getShortName(myPsiElement));
  final NameSuggestionProvider[] providers = Extensions.getExtensions(NameSuggestionProvider.EP_NAME);
  for(NameSuggestionProvider provider: providers) {
    SuggestedNameInfo info = provider.getSuggestedNames(myPsiElement, myNameSuggestionContext, result);
    if (info != null) {
      mySuggestedNameInfo = info;
      if (provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider)provider).shouldCheckOthers()) break;
    }
  }
  return ArrayUtil.toStringArray(result);
}
项目:consulo-java    文件:StreamFilterNotNullFix.java   
@NotNull
private static String suggestVariableName(@NotNull PsiFunctionalExpression function, @NotNull PsiExpression qualifier)
{
    String name = null;
    if(function instanceof PsiLambdaExpression)
    {
        PsiParameter parameter = ArrayUtil.getFirstElement(((PsiLambdaExpression) function).getParameterList().getParameters());
        if(parameter != null)
        {
            name = parameter.getName();
        }
    }
    PsiType type = StreamApiUtil.getStreamElementType(qualifier.getType());
    JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(function.getProject());
    SuggestedNameInfo info = javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, name, null, type, true);
    name = ArrayUtil.getFirstElement(info.names);
    return javaCodeStyleManager.suggestUniqueVariableName(name == null ? "obj" : name, qualifier, false);
}
项目:consulo-java    文件:JavaMemberNameCompletionContributor.java   
private static LookupElementDecorator<LookupElement> withInsertHandler(final SuggestedNameInfo callback, LookupElement element)
{
    return LookupElementDecorator.withInsertHandler(element, new InsertHandler<LookupElementDecorator<LookupElement>>()
    {
        @Override
        public void handleInsert(InsertionContext context, LookupElementDecorator<LookupElement> item)
        {
            TailType tailType = LookupItem.getDefaultTailType(context.getCompletionChar());
            if(tailType != null)
            {
                context.setAddCompletionChar(false);
                tailType.processTail(context.getEditor(), context.getTailOffset());
            }
            callback.nameChosen(item.getLookupString());
        }
    });
}
项目:consulo-java    文件:MethodParameterFix.java   
private ParameterInfoImpl[] getNewParametersInfo(PsiMethod method) throws IncorrectOperationException {
  List<ParameterInfoImpl> result = new ArrayList<ParameterInfoImpl>();
  PsiParameter[] parameters = method.getParameterList().getParameters();
  PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();
  JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
  SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, myParameterType);
  PsiParameter newParameter = factory.createParameter(nameInfo.names[0], myParameterType);
  if (method.getContainingClass().isInterface()) {
    PsiUtil.setModifierProperty(newParameter, PsiModifier.FINAL, false);
    }

  for (int i = 0; i < parameters.length; i++) {
    PsiParameter parameter = parameters[i];
    if (i == myIndex) {
      newParameter.setName(parameter.getName());
      parameter = newParameter;
    }
    result.add(new ParameterInfoImpl(i, parameter.getName(), parameter.getType()));
  }
  if (parameters.length == myIndex) {
    result.add(new ParameterInfoImpl(-1, newParameter.getName(), newParameter.getType()));
  }
  return result.toArray(new ParameterInfoImpl[result.size()]);
}
项目:consulo-java    文件:CreateConstructorParameterFromFieldFix.java   
private static String getUniqueParameterName(PsiParameter[] parameters, PsiVariable variable, HashMap<PsiField, String> usedNames) {
  final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(variable.getProject());
  final SuggestedNameInfo nameInfo = styleManager
    .suggestVariableName(VariableKind.PARAMETER,
                         styleManager.variableNameToPropertyName(variable.getName(), VariableKind.FIELD),
                         null, variable.getType());
  String newName = nameInfo.names[0];
  int n = 1;
  while (true) {
    if (isUnique(parameters, newName, usedNames)) {
      break;
    }
    newName = nameInfo.names[0] + n++;
  }
  return newName;
}
项目:consulo-java    文件:AssignFieldFromParameterAction.java   
@Nullable
private static PsiField findFieldToAssign(@NotNull Project project,
                                          @NotNull PsiParameter myParameter) {
  final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
  final String parameterName = myParameter.getName();
  final String propertyName = styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER);

  final PsiMethod method = (PsiMethod)myParameter.getDeclarationScope();

  final boolean isMethodStatic = method.hasModifierProperty(PsiModifier.STATIC);
  final VariableKind kind = isMethodStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
  final SuggestedNameInfo suggestedNameInfo = styleManager.suggestVariableName(kind, propertyName, null, FieldFromParameterUtils.getSubstitutedType(myParameter));

  final String fieldName = suggestedNameInfo.names[0];

  PsiClass aClass = method.getContainingClass();
  if (aClass == null) return null;
  PsiField field = aClass.findFieldByName(fieldName, false);
  if (field == null) return null;
  if (!field.hasModifierProperty(PsiModifier.STATIC) && isMethodStatic) return null;

  return field;
}
项目:consulo-java    文件:JavaNameSuggestionProvider.java   
@Nullable
private static SuggestedNameInfo suggestNamesForElement(final PsiElement element, PsiElement nameSuggestionContext) {
  PsiVariable var = null;
  if (element instanceof PsiVariable) {
    var = (PsiVariable)element;
  }
  else if (element instanceof PsiIdentifier) {
    PsiIdentifier identifier = (PsiIdentifier)element;
    if (identifier.getParent() instanceof PsiVariable) {
      var = (PsiVariable)identifier.getParent();
    }
  }

  if (var == null) return null;

  JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(element.getProject());
  VariableKind variableKind = codeStyleManager.getVariableKind(var);
  final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(variableKind, null, var.getInitializer(), var.getType());
  final PsiExpression expression = PsiTreeUtil.getParentOfType(nameSuggestionContext, PsiExpression.class, false);
  if (expression != null) {
    return new SuggestedNameInfo.Delegate(codeStyleManager.suggestVariableName(variableKind, null, expression, var.getType()).names, nameInfo);

  }
  return nameInfo;
}
项目:consulo-java    文件:IntroduceParameterHandler.java   
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final PsiExpression expr,
                                                                        final String propName,
                                                                        final Project project,
                                                                        final String enteredName) {
  return new NameSuggestionsGenerator() {
    public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
      final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
      SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, propName, expr != null && expr.isValid() ? expr : null, type);
      if (expr != null && expr.isValid()) {
        info = codeStyleManager.suggestUniqueVariableName(info, expr, true);
      }
      final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil
        .completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, info), expr);
      return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, info);
    }

  };
}
项目:consulo-java    文件:IntroduceConstantDialog.java   
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final String propertyName,
                                                                        final PsiExpression psiExpression,
                                                                        final JavaCodeStyleManager codeStyleManager,
                                                                        final String enteredName, final PsiClass parentClass) {
  return new NameSuggestionsGenerator() {
    public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
      SuggestedNameInfo nameInfo =
          codeStyleManager.suggestVariableName(VariableKind.STATIC_FINAL_FIELD, propertyName, psiExpression, type);
      if (psiExpression != null) {
        String[] names = nameInfo.names;
        for (int i = 0, namesLength = names.length; i < namesLength; i++) {
          String name = names[i];
          if (parentClass.findFieldByName(name, false) != null) {
            names[i] = codeStyleManager.suggestUniqueVariableName(name, psiExpression, true);
          }
        }
      }
      final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil
        .completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo), psiExpression);
      return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, nameInfo);
    }

  };
}
项目:consulo-java    文件:InheritanceToDelegationDialog.java   
private void updateTargetClass() {
  final PsiClass targetClass = getSelectedTargetClass();
  PsiManager psiManager = myClass.getManager();
  PsiType superType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(targetClass);
  SuggestedNameInfo suggestedNameInfo =
    JavaCodeStyleManager.getInstance(psiManager.getProject()).suggestVariableName(VariableKind.FIELD, null, null, superType);
  myFieldNameField.setSuggestions(suggestedNameInfo.names);
  myInnerClassNameField.getComponent().setEnabled(InheritanceToDelegationUtil.isInnerClassNeeded(myClass, targetClass));
  @NonNls final String suggestion = "My" + targetClass.getName();
  myInnerClassNameField.setSuggestions(new String[]{suggestion});

  myDataChangedListener = new NameSuggestionsField.DataChanged() {
    public void dataChanged() {
      validateButtons();
    }
  };
  myInnerClassNameField.addDataChangedListener(myDataChangedListener);
  myFieldNameField.addDataChangedListener(myDataChangedListener);

  myMemberSelectionPanel.getTable().setMemberInfos(myBasesToMemberInfos.get(targetClass));
  myMemberSelectionPanel.getTable().fireExternalDataChange();
}
项目:consulo-java    文件:KeySetIterationMayUseEntrySetInspection.java   
private static String createNewVariableName(
  @NotNull PsiElement scope, @NotNull PsiType type) {
  final Project project = scope.getProject();
  final JavaCodeStyleManager codeStyleManager =
    JavaCodeStyleManager.getInstance(project);
  @NonNls String baseName;
  final SuggestedNameInfo suggestions =
    codeStyleManager.suggestVariableName(
      VariableKind.LOCAL_VARIABLE, null, null, type);
  final String[] names = suggestions.names;
  if (names != null && names.length > 0) {
    baseName = names[0];
  }
  else {
    baseName = "entry";
  }
  if (baseName == null || baseName.length() == 0) {
    baseName = "entry";
  }
  return codeStyleManager.suggestUniqueVariableName(baseName, scope,
                                                    true);
}
项目:intellij-ce-playground    文件:CoreJavaCodeStyleManager.java   
@NotNull
@Override
public SuggestedNameInfo suggestVariableName(@NotNull VariableKind kind,
                                             @Nullable String propertyName,
                                             @Nullable PsiExpression expr,
                                             @Nullable PsiType type,
                                             boolean correctKeywords) {
  return SuggestedNameInfo.NULL_INFO;
}
项目:intellij-ce-playground    文件:ParametersFolder.java   
public boolean isParameterFoldable(@NotNull VariableData data,
                                   @NotNull LocalSearchScope scope,
                                   @NotNull final List<? extends PsiVariable> inputVariables) {
  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);
    if (expression instanceof PsiArrayAccessExpression) {
      mostRanked = expression;
      if (!isConditional(expression, scope)) {
        myFoldingSelectedByDefault = true;
        break;
      }
    }
    final int r = findUsedVariables(data, inputVariables, expression).size();
    if (currentRank < r) {
      currentRank = r;
      mostRanked = expression;
    }
  }

  if (mostRanked != null) {
    myExpressions.put(data.variable, mostRanked);
    data.type = mostRanked.getType();
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(mostRanked.getProject());
    final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, mostRanked, data.type);
    if (nameInfo.names.length > 0) {
      data.name = nameInfo.names[0];
    }
    setUniqueName(data, scope, mostRanked);
  }

  return mostRanked != null;
}
项目:intellij-ce-playground    文件:JavaMemberNameCompletionContributor.java   
private static void completeFieldName(Set<LookupElement> set, PsiField var, final PrefixMatcher matcher, boolean includeOverlapped) {
  FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.variable.name");

  Project project = var.getProject();
  JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
  final VariableKind variableKind = JavaCodeStyleManager.getInstance(project).getVariableKind(var);

  final String prefix = matcher.getPrefix();
  if (PsiType.VOID.equals(var.getType()) || psiField().inClass(psiClass().isInterface().andNot(psiClass().isAnnotationType())).accepts(var)) {
    completeVariableNameForRefactoring(project, set, matcher, var.getType(), variableKind, includeOverlapped, true);
    return;
  }

  SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(variableKind, null, null, var.getType());
  final String[] suggestedNames = suggestedNameInfo.names;
  addLookupItems(set, suggestedNameInfo, matcher, project, suggestedNames);

  if (!hasStartMatches(set, matcher) && includeOverlapped) {
    // use suggested names as suffixes
    final String requiredSuffix = codeStyleManager.getSuffixByVariableKind(variableKind);
    if(variableKind != VariableKind.STATIC_FINAL_FIELD){
      for (int i = 0; i < suggestedNames.length; i++)
        suggestedNames[i] = codeStyleManager.variableNameToPropertyName(suggestedNames[i], variableKind);
    }


    addLookupItems(set, null, matcher, project, getOverlappedNameVersions(prefix, suggestedNames, requiredSuffix));
  }

  addLookupItems(set, suggestedNameInfo, matcher, project, getUnresolvedReferences(var.getParent(), false));

  PsiExpression initializer = var.getInitializer();
  PsiClass containingClass = var.getContainingClass();
  if (initializer != null && containingClass != null) {
    SuggestedNameInfo initializerSuggestions = InplaceIntroduceFieldPopup.
      suggestFieldName(var.getType(), null, initializer, var.hasModifierProperty(PsiModifier.STATIC), containingClass);
    addLookupItems(set, initializerSuggestions, matcher, project, initializerSuggestions.names);
  }
}
项目:intellij-ce-playground    文件:JavaMemberNameCompletionContributor.java   
public static void completeVariableNameForRefactoring(Project project,
                                                      Set<LookupElement> set,
                                                      PrefixMatcher matcher,
                                                      PsiType varType,
                                                      VariableKind varKind, final boolean includeOverlapped, final boolean methodPrefix) {
  JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
  SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(varKind, null, null, varType);
  final String[] strings = completeVariableNameForRefactoring(codeStyleManager, matcher, varType, varKind, suggestedNameInfo,
                                                              includeOverlapped, methodPrefix);
  addLookupItems(set, suggestedNameInfo, matcher, project, strings);
}
项目:intellij-ce-playground    文件:JavaMemberNameCompletionContributor.java   
public static String[] completeVariableNameForRefactoring(JavaCodeStyleManager codeStyleManager,
                                                          final PrefixMatcher matcher,
                                                          @Nullable final PsiType varType,
                                                          final VariableKind varKind,
                                                          SuggestedNameInfo suggestedNameInfo,
                                                          final boolean includeOverlapped, final boolean methodPrefix) {
  Set<String> result = new LinkedHashSet<String>();
  final String[] suggestedNames = suggestedNameInfo.names;
  for (final String suggestedName : suggestedNames) {
    if (matcher.prefixMatches(suggestedName)) {
      result.add(suggestedName);
    }
  }

  if (!hasStartMatches(matcher, result) && PsiType.VOID != varType && includeOverlapped) {
    // use suggested names as suffixes
    final String requiredSuffix = codeStyleManager.getSuffixByVariableKind(varKind);
    final String prefix = matcher.getPrefix();
    if (varKind != VariableKind.STATIC_FINAL_FIELD || methodPrefix) {
      for (int i = 0; i < suggestedNames.length; i++) {
        suggestedNames[i] = codeStyleManager.variableNameToPropertyName(suggestedNames[i], varKind);
      }
    }

    ContainerUtil.addAll(result, getOverlappedNameVersions(prefix, suggestedNames, requiredSuffix));


  }
  return ArrayUtil.toStringArray(result);
}