use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class RemoveUnusedGrParameterFix method isAvailable.
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
PsiElement at = file.findElementAt(editor.getCaretModel().getOffset());
GrParameter parameter = PsiTreeUtil.getParentOfType(at, GrParameter.class);
return parameter != null && myName.equals(parameter.getName());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class ExtractUtil method collectUsedLocalVarsOrParamsDeclaredOutside.
private static Collection<GrVariable> collectUsedLocalVarsOrParamsDeclaredOutside(ExtractInfoHelper helper) {
final Collection<GrVariable> result = new HashSet<>();
final TextRange range = getRangeOfRefactoring(helper);
final int start = range.getStartOffset();
final int end = range.getEndOffset();
final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
final PsiElement resolved = ref.resolve();
if ((resolved instanceof GrParameter || PsiUtil.isLocalVariable(resolved)) && resolved.isPhysical()) {
final int offset = resolved.getTextRange().getStartOffset();
//var is declared outside of selected code
if (offset < start || end <= offset) {
result.add((GrVariable) resolved);
}
}
}
};
final GrStatement[] statements = helper.getStatements();
for (GrStatement statement : statements) {
statement.accept(visitor);
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GrUnresolvableLocalCollisionDetector method visitUpstreamCollisions.
private static void visitUpstreamCollisions(PsiElement element, String newName, GroovyPsiElement place, CollidingVariableVisitor visitor) {
final GrReferenceExpression refExpr = GroovyPsiElementFactory.getInstance(place.getProject()).createReferenceExpressionFromText(newName, place);
final GroovyResolveResult[] results = refExpr.multiResolve(false);
for (GroovyResolveResult result : results) {
final PsiElement resolved = result.getElement();
if (resolved instanceof GrParameter || (resolved instanceof GrVariable && !(resolved instanceof GrField))) {
final PsiElement parent = PsiTreeUtil.findCommonParent(resolved, element);
if (parent != null) {
PsiElement current = element;
while (current != null && current != parent) {
if (current instanceof PsiMethod || current instanceof PsiClass || current instanceof GrClosableBlock) {
return;
}
current = current.getParent();
}
}
if (!place.getManager().areElementsEquivalent(element, resolved)) {
visitor.visitCollidingVariable((PsiVariable) resolved);
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method generateDelegateCall.
private StringBuilder generateDelegateCall(GrReflectedMethod method) {
final GrParameter[] actualParams = method.getParameterList().getParameters();
final GrParameter[] parameters = method.getBaseMethod().getParameters();
Set<String> actual = new HashSet<>(actualParams.length);
for (GrParameter param : actualParams) {
actual.add(param.getName());
}
StringBuilder builder = new StringBuilder();
if (method.isConstructor()) {
builder.append("this");
} else {
if (!PsiType.VOID.equals(context.typeProvider.getReturnType(method))) {
builder.append("return ");
}
builder.append(method.getName());
}
builder.append('(');
for (GrParameter parameter : parameters) {
if (actual.contains(parameter.getName())) {
builder.append(parameter.getName());
} else {
LOG.assertTrue(parameter.isOptional());
final GrExpression initializer = parameter.getInitializerGroovy();
LOG.assertTrue(initializer != null);
builder.append(initializer.getText());
}
builder.append(", ");
}
builder.delete(builder.length() - 2, builder.length());
//builder.removeFromTheEnd(2);
builder.append(')');
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.project);
final GrStatement delegateCall;
if (method.isConstructor()) {
delegateCall = factory.createConstructorInvocation(builder.toString(), method);
} else {
delegateCall = factory.createStatementFromText(builder.toString(), method);
}
final StringBuilder result = new StringBuilder();
delegateCall.accept(new CodeBlockGenerator(result, this.context.extend()));
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class CodeBlockGenerator method generateCodeBlock.
public void generateCodeBlock(@NotNull GrParameter[] parameters, @Nullable GrCodeBlock block, boolean shouldInsertReturnNull) {
builder.append("{");
for (GrParameter parameter : parameters) {
if (context.analyzedVars.toWrap(parameter)) {
StringBuilder typeText = new StringBuilder().append(GroovyCommonClassNames.GROOVY_LANG_REFERENCE);
GenerationUtil.writeTypeParameters(typeText, new PsiType[] { context.typeProvider.getParameterType(parameter) }, parameter, new GeneratorClassNameProvider());
builder.append("final ").append(typeText).append(' ').append(context.analyzedVars.toVarName(parameter)).append(" = new ").append(typeText).append('(').append(parameter.getName()).append(");\n");
}
}
visitStatementOwner(block, shouldInsertReturnNull);
builder.append("}\n");
}
Aggregations