use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class DefaultQuickFixProvider method registerPriorityActions.
private static void registerPriorityActions(@NotNull QuickFixActionRegistrar registrar, @NotNull TextRange fixRange, @NotNull PsiReferenceExpression refExpr) {
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(refExpr.getProject());
final Map<VariableKind, IntentionAction> map = new EnumMap<>(VariableKind.class);
map.put(VariableKind.FIELD, new CreateFieldFromUsageFix(refExpr));
map.put(VariableKind.STATIC_FINAL_FIELD, new CreateConstantFieldFromUsageFix(refExpr));
if (!refExpr.isQualified()) {
map.put(VariableKind.LOCAL_VARIABLE, new CreateLocalFromUsageFix(refExpr));
map.put(VariableKind.PARAMETER, new CreateParameterFromUsageFix(refExpr));
}
final VariableKind kind = getKind(styleManager, refExpr);
if (map.containsKey(kind)) {
map.put(kind, PriorityIntentionActionWrapper.highPriority(map.get(kind)));
}
for (IntentionAction action : map.values()) {
registrar.register(fixRange, action, null);
}
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class DefaultQuickFixProvider method getKind.
@Nullable
private static VariableKind getKind(@NotNull JavaCodeStyleManager styleManager, @NotNull PsiReferenceExpression refExpr) {
final String reference = refExpr.getText();
boolean upperCase = true;
for (int i = 0; i < reference.length(); i++) {
if (!Character.isUpperCase(reference.charAt(i))) {
upperCase = false;
break;
}
}
if (upperCase) {
return VariableKind.STATIC_FINAL_FIELD;
}
for (VariableKind kind : VariableKind.values()) {
final String prefix = styleManager.getPrefixByVariableKind(kind);
final String suffix = styleManager.getSuffixByVariableKind(kind);
if (prefix.isEmpty() && suffix.isEmpty()) {
continue;
}
if (reference.startsWith(prefix) && reference.endsWith(suffix)) {
return kind;
}
}
if (StringUtil.isCapitalized(reference)) {
return null;
}
return VariableKind.LOCAL_VARIABLE;
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class GenerationHelper method getPropertyName.
public static String getPropertyName(FieldElement fieldElement, Project project) {
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
final VariableKind variableKind = fieldElement.isModifierStatic() ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
final String propertyName = codeStyleManager.variableNameToPropertyName(fieldElement.getName(), variableKind);
if (!fieldElement.isModifierStatic() && fieldElement.isBoolean()) {
if (propertyName.startsWith("is") && propertyName.length() > "is".length() && Character.isUpperCase(propertyName.charAt("is".length()))) {
return StringUtil.decapitalize(propertyName.substring("is".length()));
}
}
return propertyName;
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class GroovyPropertyUtils method generateSetterPrototype.
public static GrMethod generateSetterPrototype(PsiField field) {
Project project = field.getProject();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
String name = field.getName();
boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
VariableKind kind = codeStyleManager.getVariableKind(field);
String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
String setName = getSetterName(field.getName());
final PsiClass containingClass = field.getContainingClass();
try {
GrMethod setMethod = factory.createMethod(setName, PsiType.VOID);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
final PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
GrParameter param = factory.createParameter(parameterName, type);
annotateWithNullableStuff(field, param);
setMethod.getParameterList().add(param);
PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
@NonNls StringBuilder builder = new StringBuilder();
if (name.equals(parameterName)) {
if (!isStatic) {
builder.append("this.");
} else {
String className = containingClass.getName();
if (className != null) {
builder.append(className);
builder.append(".");
}
}
}
builder.append(name);
builder.append("=");
builder.append(parameterName);
builder.append("\n");
GrCodeBlock body = factory.createMethodBodyFromText(builder.toString());
setMethod.getBlock().replace(body);
return setMethod;
} catch (IncorrectOperationException e) {
LOG.error(e);
return null;
}
}
use of com.intellij.psi.codeStyle.VariableKind 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;
}
Aggregations