use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method getClosureParameterType.
@Nullable
private static PsiType getClosureParameterType(@NotNull PsiParameter parameter) {
final PsiElement scope = parameter.getDeclarationScope();
final PsiType type;
if (scope instanceof GrClosableBlock) {
type = ClosureParameterEnhancer.inferType((GrClosableBlock) scope, ((GrParameterList) parameter.getParent()).getParameterIndex(parameter));
} else {
type = null;
}
return type;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
PsiElement parent = element.getParent();
PsiElement pparent;
if (isNameIdentifierOfVariable(element, parent) || isModifierListOfVar(element, parent)) {
pparent = parent.getParent();
} else if (isModifierListOfVarDecl(element, parent)) {
pparent = parent;
} else {
return false;
}
if (pparent instanceof GrVariableDeclaration) {
if (((GrVariableDeclaration) pparent).getTypeElementGroovy() != null)
return false;
GrVariable[] variables = ((GrVariableDeclaration) pparent).getVariables();
for (GrVariable variable : variables) {
if (isVarDeclaredWithInitializer(variable))
return true;
}
} else if (pparent instanceof GrForInClause) {
final GrVariable variable = ((GrForInClause) pparent).getDeclaredVariable();
return variable != null && variable.getTypeElementGroovy() == null && PsiUtil.extractIteratedType((GrForInClause) pparent) != null;
} else if (parent instanceof GrParameter && pparent instanceof GrParameterList) {
return ((GrParameter) parent).getTypeElementGroovy() == null && getClosureParameterType((PsiParameter) parent) != null;
} else {
final GrVariable variable = (GrVariable) parent;
return variable.getTypeElementGroovy() == null && isVarDeclaredWithInitializer(variable);
}
return false;
}
private boolean isModifierListOfVarDecl(PsiElement element, PsiElement parent) {
return parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getModifierList() == element;
}
private boolean isModifierListOfVar(PsiElement element, PsiElement parent) {
return parent instanceof GrVariable && ((GrVariable) parent).getModifierList() == element;
}
private boolean isNameIdentifierOfVariable(PsiElement element, PsiElement parent) {
return parent instanceof GrVariable && ((GrVariable) parent).getTypeElementGroovy() == null && element == ((GrVariable) parent).getNameIdentifierGroovy();
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method generateDelegateClosure.
private GrClosableBlock generateDelegateClosure(GrClosableBlock originalClosure, GrVariable anchor, String newName) {
GrClosableBlock result;
if (originalClosure.hasParametersSection()) {
result = myFactory.createClosureFromText("{->}", anchor);
final GrParameterList parameterList = (GrParameterList) originalClosure.getParameterList().copy();
result.getParameterList().replace(parameterList);
} else {
result = myFactory.createClosureFromText("{}", anchor);
}
StringBuilder call = new StringBuilder();
call.append(newName).append('(');
final GrParameter[] parameters = result.getParameters();
for (int i = 0; i < parameters.length; i++) {
if (!mySettings.parametersToRemove().contains(i)) {
call.append(parameters[i].getName()).append(", ");
}
}
call.append(myParameterInitializer.getText());
call.append(")");
final GrStatement statement = myFactory.createStatementFromText(call.toString());
result.addStatementBefore(statement, null);
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class GroovyIntroduceParameterMethodUsagesProcessor method addParameter.
@NotNull
public static GrParameter addParameter(@NotNull GrParametersOwner parametersOwner, @Nullable MethodJavaDocHelper javaDocHelper, @NotNull PsiType forcedType, @NotNull String parameterName, boolean isFinal, @NotNull Project project) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final String typeText = forcedType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) || forcedType == PsiType.NULL || PsiType.VOID.equals(forcedType) ? null : forcedType.getCanonicalText();
GrParameter parameter = factory.createParameter(parameterName, typeText, parametersOwner);
parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, isFinal);
final PsiParameter anchorParameter = getAnchorParameter(parametersOwner);
final GrParameterList parameterList = parametersOwner.getParameterList();
parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(parameter);
if (javaDocHelper != null) {
final PsiDocTag tagForAnchorParameter = javaDocHelper.getTagForParameter(anchorParameter);
javaDocHelper.addParameterAfter(parameterName, tagForAnchorParameter);
}
return parameter;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.
the class ConvertClosureArgToItIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrClosableBlock closure = (GrClosableBlock) element;
final GrParameterList parameterList = closure.getParameterList();
final GrParameter parameter = parameterList.getParameters()[0];
final Set<GrReferenceExpression> referencesToChange = new HashSet<>();
final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
if (!referenceExpression.getText().equals(parameter.getName())) {
return;
}
final PsiElement referent = referenceExpression.resolve();
if (parameter.equals(referent)) {
referencesToChange.add(referenceExpression);
}
}
};
closure.accept(visitor);
parameter.delete();
for (GrReferenceExpression referenceExpression : referencesToChange) {
PsiImplUtil.replaceExpression("it", referenceExpression);
}
}
Aggregations