use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GrLightParameterListBuilder method removeParameter.
@NotNull
public GrParameter removeParameter(int index) {
GrParameter removed = myParameters.remove(index);
myCachedParameters = null;
return removed;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class CreateParameterForFieldIntention method addParameter.
private static void addParameter(final GrField selectedValue, final GrMethod constructor, final Project project) {
List<GrParameterInfo> parameters = new ArrayList<>();
GrParameter[] constructorParameters = constructor.getParameters();
for (int i = 0; i < constructorParameters.length; i++) {
parameters.add(new GrParameterInfo(constructorParameters[i], i));
}
final String[] suggestedNames = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.PARAMETER, selectedValue.getName(), null, null).names;
final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(constructor, Collections.<String>emptyList(), false);
String parameterName = ContainerUtil.find(suggestedNames, name -> !nameValidator.validateName(name, false).isEmpty());
if (parameterName == null) {
parameterName = nameValidator.validateName(suggestedNames[0], true);
}
parameters.add(new GrParameterInfo(parameterName, "null", "", selectedValue.getTypeGroovy(), -1, false));
PsiClassType[] exceptionTypes = constructor.getThrowsList().getReferencedTypes();
ThrownExceptionInfo[] thrownExceptionInfos = new ThrownExceptionInfo[exceptionTypes.length];
for (int i = 0; i < exceptionTypes.length; i++) {
new JavaThrownExceptionInfo(i, exceptionTypes[i]);
}
final GrChangeInfoImpl grChangeInfo = new GrChangeInfoImpl(constructor, null, null, constructor.getName(), parameters, thrownExceptionInfos, false);
final String finalParameterName = parameterName;
final GrChangeSignatureProcessor processor = new GrChangeSignatureProcessor(project, grChangeInfo) {
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
super.performRefactoring(usages);
final GrOpenBlock block = constructor.getBlock();
LOG.assertTrue(block != null);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final String text;
if (StringUtil.equals(selectedValue.getName(), finalParameterName)) {
text = "this." + selectedValue.getName() + " = " + finalParameterName;
} else {
text = selectedValue.getName() + " = " + finalParameterName;
}
final GrStatement assignment = factory.createStatementFromText(text);
final GrStatement statement = block.addStatementBefore(assignment, null);
final GrReferenceExpression ref = (GrReferenceExpression) ((GrAssignmentExpression) statement).getLValue();
if (!PsiManager.getInstance(project).areElementsEquivalent(ref.resolve(), selectedValue)) {
PsiUtil.qualifyMemberReference(ref, selectedValue, selectedValue.getName());
}
}
};
processor.run();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class EachToForIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrMethodCallExpression expression = (GrMethodCallExpression) element;
final GrClosableBlock block = expression.getClosureArguments()[0];
final GrParameterList parameterList = block.getParameterList();
final GrParameter[] parameters = parameterList.getParameters();
String var;
if (parameters.length == 1) {
var = parameters[0].getText();
var = StringUtil.replace(var, GrModifier.DEF, "");
} else {
var = "it";
}
final GrExpression invokedExpression = expression.getInvokedExpression();
GrExpression qualifier = ((GrReferenceExpression) invokedExpression).getQualifierExpression();
final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(element.getProject());
if (qualifier == null) {
qualifier = elementFactory.createExpressionFromText("this");
}
StringBuilder builder = new StringBuilder();
builder.append("for (").append(var).append(" in ").append(qualifier.getText()).append(") {\n");
String text = block.getText();
final PsiElement blockArrow = block.getArrow();
int index;
if (blockArrow != null) {
index = blockArrow.getStartOffsetInParent() + blockArrow.getTextLength();
} else {
index = 1;
}
while (index < text.length() && Character.isWhitespace(text.charAt(index))) index++;
text = text.substring(index, text.length() - 1);
builder.append(text);
builder.append("}");
final GrStatement statement = elementFactory.createStatementFromText(builder.toString());
GrForStatement forStatement = (GrForStatement) expression.replaceWithStatement(statement);
final GrForClause clause = forStatement.getClause();
GrVariable variable = clause.getDeclaredVariable();
forStatement = updateReturnStatements(forStatement);
if (variable == null)
return;
if (ApplicationManager.getApplication().isUnitTestMode())
return;
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document doc = documentManager.getDocument(element.getContainingFile());
if (doc == null)
return;
documentManager.doPostponedOperationsAndUnblockDocument(doc);
editor.getCaretModel().moveToOffset(variable.getTextOffset());
new VariableInplaceRenamer(variable, editor).performInplaceRename();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class OldReferencesResolver method inlineParam.
private PsiElement inlineParam(PsiElement newExpr, GrExpression actualArg, PsiParameter parameter) {
if (myParamsToNotInline.contains(parameter))
return newExpr;
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
if (myExpr instanceof GrClosableBlock) {
int count = 0;
for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(myParameterInitializer))) {
count++;
if (count > 1)
break;
}
if (count > 1) {
myParamsToNotInline.add(parameter);
final PsiType type;
if (parameter instanceof GrParameter) {
type = ((GrParameter) parameter).getDeclaredType();
} else {
type = parameter.getType();
}
final GrVariableDeclaration declaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, actualArg, type, parameter.getName());
final GrStatement[] statements = ((GrClosableBlock) myExpr).getStatements();
GrStatement anchor = statements.length > 0 ? statements[0] : null;
return ((GrClosableBlock) myExpr).addStatementBefore(declaration, anchor);
}
}
int copyingSafetyLevel = GroovyRefactoringUtil.verifySafeCopyExpression(actualArg);
if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
actualArg = factory.createExpressionFromText(getTempVar(actualArg));
}
newExpr = newExpr.replace(actualArg);
return newExpr;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyShellLanguageConsoleView method generateClosure.
@NotNull
private GrClosableBlock generateClosure(@NotNull GrMethod method) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
StringBuilder buffer = new StringBuilder();
buffer.append('{');
GrParameter[] parameters = method.getParameters();
for (GrParameter parameter : parameters) {
buffer.append(parameter.getText());
buffer.append(',');
}
if (parameters.length > 0)
buffer.delete(buffer.length() - 1, buffer.length());
buffer.append("->}");
return factory.createClosureFromText(buffer.toString(), getFile());
}
Aggregations