use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method processPrimaryMethodInner.
private static boolean processPrimaryMethodInner(JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
if (changeInfo.isNameChanged()) {
String newName = baseMethod == null ? changeInfo.getNewName() : RefactoringUtil.suggestNewOverriderName(method.getName(), baseMethod.getName(), changeInfo.getNewName());
if (newName != null && !newName.equals(method.getName())) {
method.setName(changeInfo.getNewName());
}
}
final GrModifierList modifierList = method.getModifierList();
if (changeInfo.isVisibilityChanged()) {
modifierList.setModifierProperty(changeInfo.getNewVisibility(), true);
}
PsiSubstitutor substitutor = baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY;
final PsiMethod context = changeInfo.getMethod();
GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy();
if (changeInfo.isReturnTypeChanged()) {
CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType();
if (newReturnType == null) {
if (oldReturnTypeElement != null) {
oldReturnTypeElement.delete();
if (modifierList.getModifiers().length == 0) {
modifierList.setModifierProperty(GrModifier.DEF, true);
}
}
} else {
PsiType type = newReturnType.getType(context, method.getManager());
GrReferenceAdjuster.shortenAllReferencesIn(method.setReturnType(substitutor.substitute(type)));
if (oldReturnTypeElement == null) {
modifierList.setModifierProperty(GrModifier.DEF, false);
}
}
}
JavaParameterInfo[] newParameters = changeInfo.getNewParameters();
final GrParameterList parameterList = method.getParameterList();
GrParameter[] oldParameters = parameterList.getParameters();
final PsiParameter[] oldBaseParams = baseMethod != null ? baseMethod.getParameterList().getParameters() : null;
Set<GrParameter> toRemove = new HashSet<>(oldParameters.length);
ContainerUtil.addAll(toRemove, oldParameters);
GrParameter anchor = null;
final GrDocComment docComment = method.getDocComment();
final GrDocTag[] tags = docComment == null ? null : docComment.getTags();
int newParamIndex = 0;
for (JavaParameterInfo newParameter : newParameters) {
//if old parameter name differs from base method parameter name we don't change it
final String newName;
final int oldIndex = newParameter.getOldIndex();
if (oldIndex >= 0 && oldBaseParams != null) {
final String oldName = oldParameters[oldIndex].getName();
if (oldName.equals(oldBaseParams[oldIndex].getName())) {
newName = newParameter.getName();
} else {
newName = oldName;
}
} else {
newName = newParameter.getName();
}
final GrParameter oldParameter = oldIndex >= 0 ? oldParameters[oldIndex] : null;
if (docComment != null && oldParameter != null) {
final String oldName = oldParameter.getName();
for (GrDocTag tag : tags) {
if ("@param".equals(tag.getName())) {
final GrDocParameterReference parameterReference = tag.getDocParameterReference();
if (parameterReference != null && oldName.equals(parameterReference.getText())) {
parameterReference.handleElementRename(newName);
}
}
}
}
GrParameter grParameter = createNewParameter(substitutor, context, parameterList, newParameter, newName);
if (oldParameter != null) {
grParameter.getModifierList().replace(oldParameter.getModifierList());
}
if ("def".equals(newParameter.getTypeText())) {
grParameter.getModifierList().setModifierProperty(GrModifier.DEF, true);
} else if (StringUtil.isEmpty(newParameter.getTypeText())) {
grParameter.getModifierList().setModifierProperty(GrModifier.DEF, false);
}
anchor = (GrParameter) parameterList.addAfter(grParameter, anchor);
if (newParamIndex < oldParameters.length) {
GrParameter oldParam = oldParameters[newParamIndex];
PsiElement prev = oldParam.getPrevSibling();
if (prev instanceof PsiWhiteSpace) {
parameterList.addBefore(prev, anchor);
}
}
newParamIndex++;
}
for (GrParameter oldParameter : toRemove) {
oldParameter.delete();
}
JavaCodeStyleManager.getInstance(parameterList.getProject()).shortenClassReferences(parameterList);
CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList);
if (changeInfo.isExceptionSetOrOrderChanged()) {
final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions();
PsiClassType[] exceptionTypes = new PsiClassType[infos.length];
for (int i = 0; i < infos.length; i++) {
ThrownExceptionInfo info = infos[i];
exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager());
}
PsiReferenceList thrownList = GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes);
thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList);
JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList);
CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList());
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class GrChangeSignatureHandler method findTargetMember.
@Override
@Nullable
public PsiElement findTargetMember(PsiElement element) {
final GrParameterList parameterList = PsiTreeUtil.getParentOfType(element, GrParameterList.class);
if (parameterList != null) {
final PsiElement parent = parameterList.getParent();
if (parent instanceof PsiMethod)
return parent;
}
if (element.getParent() instanceof GrMethod && ((GrMethod) element.getParent()).getNameIdentifierGroovy() == element) {
return element.getParent();
}
final GrCall expression = PsiTreeUtil.getParentOfType(element, GrCall.class);
if (expression != null) {
return expression.resolveMethod();
}
final PsiReference ref = element.getReference();
if (ref == null)
return null;
return ref.resolve();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method performRefactoring.
private static void performRefactoring(final PsiElement element, final GrParametersOwner owner, final Collection<PsiElement> occurrences, final boolean createNewFirstParam, @Nullable final String mapParamName, final boolean specifyMapType) {
final GrParameter param = getAppropriateParameter(element);
assert param != null;
final String paramName = param.getName();
final String mapName = createNewFirstParam ? mapParamName : getFirstParameter(owner).getName();
final Project project = element.getProject();
final Runnable runnable = () -> {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final GrParameterList list = owner.getParameterList();
assert list != null;
final int index = list.getParameterNumber(param);
if (!createNewFirstParam && index <= 0) {
// bad undo
return;
}
//final List<GrCall> calls = getCallOccurrences(occurrences);
try {
for (PsiElement occurrence : occurrences) {
GrReferenceExpression refExpr = null;
GroovyResolveResult resolveResult = null;
boolean isExplicitGetterCall = false;
if (occurrence instanceof GrReferenceExpression) {
final PsiElement parent = occurrence.getParent();
if (parent instanceof GrCall) {
refExpr = (GrReferenceExpression) occurrence;
resolveResult = refExpr.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyGetter(((PsiMethod) resolved)) && //check for explicit getter call
((PsiMethod) resolved).getName().equals(refExpr.getReferenceName())) {
isExplicitGetterCall = true;
}
} else if (parent instanceof GrReferenceExpression) {
resolveResult = ((GrReferenceExpression) parent).advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
refExpr = (GrReferenceExpression) parent;
}
}
}
if (refExpr == null)
continue;
final GrClosureSignature signature = generateSignature(owner, refExpr);
if (signature == null)
continue;
GrCall call;
if (isExplicitGetterCall) {
PsiElement parent = refExpr.getParent();
LOG.assertTrue(parent instanceof GrCall);
parent = parent.getParent();
if (parent instanceof GrReferenceExpression && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
parent = parent.getParent();
}
if (parent instanceof GrCall) {
call = (GrCall) parent;
} else {
continue;
}
} else {
call = (GrCall) refExpr.getParent();
}
if (resolveResult.isInvokedOnProperty()) {
final PsiElement parent = call.getParent();
if (parent instanceof GrCall) {
call = (GrCall) parent;
} else if (parent instanceof GrReferenceExpression && parent.getParent() instanceof GrCall) {
final PsiElement resolved = ((GrReferenceExpression) parent).resolve();
if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
call = (GrCall) parent.getParent();
} else {
continue;
}
}
}
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
if (argInfos == null)
continue;
final GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = argInfos[index];
final GrNamedArgument namedArg;
if (argInfo.isMultiArg) {
if (argInfo.args.isEmpty())
continue;
String arg = "[" + StringUtil.join(ContainerUtil.map(argInfo.args, element1 -> element1.getText()), ", ") + "]";
for (PsiElement psiElement : argInfo.args) {
psiElement.delete();
}
namedArg = factory.createNamedArgument(paramName, factory.createExpressionFromText(arg));
} else {
if (argInfo.args.isEmpty())
continue;
final PsiElement argument = argInfo.args.iterator().next();
assert argument instanceof GrExpression;
namedArg = factory.createNamedArgument(paramName, (GrExpression) argument);
argument.delete();
}
call.addNamedArgument(namedArg);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
//Replace of occurrences of old parameter in closure/method
final Collection<PsiReference> references = ReferencesSearch.search(param).findAll();
for (PsiReference ref : references) {
final PsiElement elt = ref.getElement();
if (elt instanceof GrReferenceExpression) {
GrReferenceExpression expr = (GrReferenceExpression) elt;
final GrExpression newExpr = factory.createExpressionFromText(mapName + "." + paramName);
expr.replaceWithExpression(newExpr, true);
}
}
//Add new map parameter to closure/method if it's necessary
if (createNewFirstParam) {
try {
final GrParameter newParam = factory.createParameter(mapName, specifyMapType ? MAP_TYPE_TEXT : "", null);
list.addAfter(newParam, null);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
//Eliminate obsolete parameter from parameter list
param.delete();
};
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(runnable), REFACTORING_NAME, null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class GroovyBlock method getChildAttributes.
@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
ASTNode astNode = getNode();
final PsiElement psiParent = astNode.getPsi();
if (psiParent instanceof GroovyFileBase) {
return new ChildAttributes(Indent.getNoneIndent(), null);
}
if (psiParent instanceof GrSwitchStatement) {
List<Block> subBlocks = getSubBlocks();
if (newChildIndex > 0) {
Block block = subBlocks.get(newChildIndex - 1);
if (block instanceof GroovyBlock) {
PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
if (anchorPsi instanceof GrCaseSection) {
for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
return new ChildAttributes(indent, null);
}
}
int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
}
}
}
}
if (psiParent instanceof GrCaseLabel) {
return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
}
if (psiParent instanceof GrCaseSection) {
return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
}
if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
}
if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
}
if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
return new ChildAttributes(indent, null);
}
return new ChildAttributes(Indent.getNoneIndent(), null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class GrParameterImpl method isMainMethodFirstUntypedParameter.
private boolean isMainMethodFirstUntypedParameter() {
if (getTypeElementGroovy() != null)
return false;
if (!(getParent() instanceof GrParameterList))
return false;
if (isOptional())
return false;
GrParameterList parameterList = (GrParameterList) getParent();
if (!(parameterList.getParent() instanceof GrMethod))
return false;
GrMethod method = (GrMethod) parameterList.getParent();
return PsiImplUtil.isMainMethod(method);
}
Aggregations