use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class JavaReplaceHandler method postProcess.
@Override
public void postProcess(PsiElement affectedElement, ReplaceOptions options) {
if (!affectedElement.isValid()) {
return;
}
if (options.isToUseStaticImport()) {
shortenWithStaticImports(affectedElement, 0, affectedElement.getTextLength());
}
if (options.isToShortenFQN()) {
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(affectedElement.getProject());
codeStyleManager.shortenClassReferences(affectedElement, 0, affectedElement.getTextLength());
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class ClassMappingNameConverter method getVariants.
@NotNull
@Override
public Collection<String> getVariants(ConvertContext context) {
DomElement parent = context.getInvocationElement().getParent();
assert parent != null;
List<DomElement> children = DomUtil.getDefinedChildren(parent, true, true);
DomElement classElement = ContainerUtil.find(children, domElement -> domElement.getAnnotation(MappingClass.class) != null);
if (classElement == null)
return Collections.emptyList();
Object value = ((GenericDomValue) classElement).getValue();
if (value == null)
return Collections.emptyList();
PsiType type;
if (value instanceof PsiType) {
type = (PsiType) value;
} else if (value instanceof PsiClass) {
type = PsiTypesUtil.getClassType((PsiClass) value);
} else {
LOG.error("wrong type: " + value.getClass());
return Collections.emptyList();
}
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type);
return Arrays.asList(info.names);
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager 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;
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager 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;
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class JavaTreeCopyHandler method decodeInformation.
@Override
public TreeElement decodeInformation(TreeElement element, final Map<Object, Object> decodingState) {
boolean shallDecodeEscapedTexts = shallEncodeEscapedTexts(element, decodingState);
if (element instanceof CompositeElement) {
final IElementType elementType = element.getElementType();
if (elementType == JavaElementType.JAVA_CODE_REFERENCE || elementType == JavaElementType.REFERENCE_EXPRESSION || elementType == JavaElementType.METHOD_REF_EXPRESSION) {
PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement) SourceTreeToPsiMap.treeElementToPsi(element);
final PsiClass refClass = element.getCopyableUserData(JavaTreeGenerator.REFERENCED_CLASS_KEY);
if (refClass != null) {
element.putCopyableUserData(JavaTreeGenerator.REFERENCED_CLASS_KEY, null);
PsiManager manager = refClass.getManager();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(refClass.getProject());
PsiElement refElement1 = ref.resolve();
try {
if (refClass != refElement1 && !manager.areElementsEquivalent(refClass, refElement1)) {
if (((CompositeElement) element).findChildByRole(ChildRole.QUALIFIER) == null) {
// can restore only if short (otherwise qualifier should be already restored)
ref = (PsiJavaCodeReferenceElement) ref.bindToElement(refClass);
}
} else {
// shorten references to the same package and to inner classes that can be accessed by short name
ref = (PsiJavaCodeReferenceElement) codeStyleManager.shortenClassReferences(ref, JavaCodeStyleManager.DO_NOT_ADD_IMPORTS);
}
return (TreeElement) SourceTreeToPsiMap.psiElementToTree(ref);
} catch (IncorrectOperationException e) {
((PsiImportHolder) ref.getContainingFile()).importClass(refClass);
}
} else {
final PsiMember refMember = element.getCopyableUserData(JavaTreeGenerator.REFERENCED_MEMBER_KEY);
if (refMember != null) {
LOG.assertTrue(ref instanceof PsiReferenceExpression);
element.putCopyableUserData(JavaTreeGenerator.REFERENCED_MEMBER_KEY, null);
PsiElement refElement1 = ref.resolve();
if (refMember != refElement1 && !refMember.getManager().areElementsEquivalent(refMember, refElement1)) {
try {
ref = (PsiJavaCodeReferenceElement) ((PsiReferenceExpression) ref).bindToElementViaStaticImport(refMember.getContainingClass());
} catch (IncorrectOperationException e) {
// TODO[yole] ignore?
}
return (TreeElement) SourceTreeToPsiMap.psiElementToTree(ref);
}
}
}
} else if (element.getElementType() == JavaElementType.MODIFIER_LIST) {
if (element.getUserData(INTERFACE_MODIFIERS_FLAG_KEY) != null) {
element.putUserData(INTERFACE_MODIFIERS_FLAG_KEY, null);
try {
PsiModifierList modifierList = (PsiModifierList) SourceTreeToPsiMap.treeElementToPsi(element);
if (element.getTreeParent().getElementType() == JavaElementType.FIELD) {
modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
modifierList.setModifierProperty(PsiModifier.STATIC, true);
modifierList.setModifierProperty(PsiModifier.FINAL, true);
} else if (element.getTreeParent().getElementType() == JavaElementType.METHOD || element.getTreeParent().getElementType() == JavaElementType.ANNOTATION_METHOD) {
modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
modifierList.setModifierProperty(PsiModifier.ABSTRACT, true);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
} else if (shallDecodeEscapedTexts && element instanceof LeafElement && !(element instanceof OuterLanguageElement)) {
if (!isInCData(element)) {
final String original = element.getText();
final String escaped = StringUtil.escapeXml(original);
if (!Comparing.equal(original, escaped) && element.getCopyableUserData(ALREADY_ESCAPED) == null) {
LeafElement copy = ((LeafElement) element).replaceWithText(escaped);
copy.putCopyableUserData(ALREADY_ESCAPED, Boolean.TRUE);
return copy;
}
}
}
return null;
}
Aggregations