use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class ReplaceConstructorWithBuilderProcessor method createMethodSignature.
private PsiMethod createMethodSignature(String createMethodName) {
JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(myProject);
final StringBuffer buf = new StringBuffer();
final PsiMethod constructor = getWorkingConstructor();
for (PsiParameter parameter : constructor.getParameterList().getParameters()) {
final String pureParamName = styleManager.variableNameToPropertyName(parameter.getName(), VariableKind.PARAMETER);
if (buf.length() > 0)
buf.append(", ");
buf.append(myParametersMap.get(pureParamName).getFieldName());
}
return myElementFactory.createMethodFromText("public " + constructor.getName() + " " + createMethodName + "(){\n return new " + constructor.getName() + "(" + buf.toString() + ")" + ";\n}", constructor);
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class ReplaceConstructorWithSettersChainInfo method fixUsage.
public void fixUsage() throws IncorrectOperationException {
final PsiNewExpression expr = (PsiNewExpression) getElement();
if (expr != null) {
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory();
final PsiMethod constructor = expr.resolveConstructor();
if (constructor != null) {
StringBuffer buf = new StringBuffer();
final PsiExpressionList argumentList = expr.getArgumentList();
if (argumentList != null) {
final PsiExpression[] args = argumentList.getExpressions();
final PsiParameter[] parameters = constructor.getParameterList().getParameters();
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(constructor.getProject());
for (int i = 0; i < Math.min(constructor.getParameterList().getParametersCount(), args.length); i++) {
String arg = args[i].getText();
if (parameters[i].isVarArgs()) {
for (int ia = i + 1; ia < args.length; ia++) {
arg += ", " + args[ia].getText();
}
}
final String pureParamName = styleManager.variableNameToPropertyName(parameters[i].getName(), VariableKind.PARAMETER);
final ParameterData data = myParametersMap.get(pureParamName);
if (!Comparing.strEqual(arg, data.getDefaultValue()) || data.isInsertSetter()) {
buf.append(data.getSetterName()).append("(").append(arg).append(").");
}
}
final PsiExpression settersChain = elementFactory.createExpressionFromText("new " + myBuilderClass + "()." + buf.toString() + "create" + StringUtil.capitalize(constructor.getName()) + "()", null);
styleManager.shortenClassReferences(expr.replace(settersChain));
}
}
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class PsiJavaCodeReferenceElementImpl method bindToClass.
private PsiElement bindToClass(@NotNull PsiClass aClass, @NotNull PsiFile containingFile) throws IncorrectOperationException {
String qName = aClass.getQualifiedName();
Project project = containingFile.getProject();
boolean preserveQualification = JavaCodeStyleSettingsFacade.getInstance(project).useFQClassNames() && isFullyQualified(containingFile);
JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
if (qName == null) {
qName = aClass.getName();
assert qName != null : aClass;
PsiClass psiClass = facade.getResolveHelper().resolveReferencedClass(qName, this);
if (!getManager().areElementsEquivalent(psiClass, aClass)) {
throw cannotBindError(aClass);
}
} else if (facade.findClass(qName, getResolveScope()) == null && !preserveQualification) {
return this;
}
String text = qName;
PsiReferenceParameterList parameterList = getParameterList();
if (parameterList != null) {
text += parameterList.getText();
}
PsiJavaCodeReferenceElement ref;
try {
ref = facade.getParserFacade().createReferenceFromText(text, getParent());
} catch (IncorrectOperationException e) {
throw new IncorrectOperationException(e.getMessage() + " [qname=" + qName + " class=" + aClass + ";" + aClass.getClass().getName() + "]");
}
getTreeParent().replaceChildInternal(this, (TreeElement) ref.getNode());
if (!preserveQualification) {
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
ref = (PsiJavaCodeReferenceElement) codeStyleManager.shortenClassReferences(ref, JavaCodeStyleManager.INCOMPLETE_CODE);
}
return ref;
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class JavaMemberNameCompletionContributor method completeFieldName.
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);
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class JavaMemberNameCompletionContributor method completeVariableNameForRefactoring.
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);
}
Aggregations