use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrIntroduceParameterHandler method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable final DataContext dataContext) {
if (editor == null || file == null)
return;
final SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
final int offset = editor.getCaretModel().getOffset();
final List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(file, editor, offset, false);
if (expressions.isEmpty()) {
GrIntroduceHandlerBase.updateSelectionForVariable(editor, file, selectionModel, offset);
} else if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
final TextRange textRange = expressions.get(0).getTextRange();
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
} else {
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {
@Override
public void pass(final GrExpression selectedValue) {
invoke(project, editor, file, selectedValue.getTextRange().getStartOffset(), selectedValue.getTextRange().getEndOffset());
}
}, grExpression -> grExpression.getText());
return;
}
}
invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrIntroduceParameterHandler method createContext.
private static GrIntroduceContext createContext(@NotNull IntroduceParameterInfo info, @NotNull Editor editor) {
GrExpression expr = GroovyIntroduceParameterUtil.findExpr(info);
GrVariable var = GroovyIntroduceParameterUtil.findVar(info);
StringPartInfo stringPart = info.getStringPartInfo();
return new GrIntroduceVariableHandler().getContext(info.getProject(), editor, expr, var, stringPart, info.getToReplaceIn());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrIntroduceParameterProcessor method createExpressionWrapper.
private static GrExpressionWrapper createExpressionWrapper(GrIntroduceParameterSettings settings) {
LOG.assertTrue(settings.getToReplaceIn() instanceof GrMethod);
LOG.assertTrue(settings.getToSearchFor() instanceof PsiMethod);
final StringPartInfo stringPartInfo = settings.getStringPartInfo();
GrVariable var = settings.getVar();
final GrExpression expression = stringPartInfo != null ? stringPartInfo.createLiteralFromSelected() : var != null ? var.getInitializerGroovy() : settings.getExpression();
return new GrExpressionWrapper(expression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class RenameAliasImportedMethodProcessor method renameElement.
@Override
public void renameElement(PsiElement psiElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
boolean isGetter = GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) psiElement);
boolean isSetter = GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) psiElement);
List<UsageInfo> methodAccess = new ArrayList<>(usages.length);
List<UsageInfo> propertyAccess = new ArrayList<>(usages.length);
for (UsageInfo usage : usages) {
final PsiElement element = usage.getElement();
if (element instanceof GrReferenceExpression && ((GrReferenceExpression) element).advancedResolve().isInvokedOnProperty()) {
propertyAccess.add(usage);
} else {
methodAccess.add(usage);
}
}
super.renameElement(psiElement, newName, methodAccess.toArray(new UsageInfo[methodAccess.size()]), listener);
final String propertyName;
if (isGetter) {
propertyName = GroovyPropertyUtils.getPropertyNameByGetterName(newName, true);
} else if (isSetter) {
propertyName = GroovyPropertyUtils.getPropertyNameBySetterName(newName);
} else {
propertyName = null;
}
if (propertyName == null) {
//it means accessor is renamed to not-accessor and we should replace all property-access-refs with method-access-refs
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
for (UsageInfo info : propertyAccess) {
final PsiElement element = info.getElement();
if (element instanceof GrReferenceExpression) {
final PsiElement qualifier = ((GrReferenceExpression) element).getQualifier();
String qualifierPrefix = qualifier == null ? "" : qualifier.getText() + ".";
if (isGetter) {
final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "()");
((GrReferenceExpression) element).replaceWithExpression(call, true);
} else {
final PsiElement parent = element.getParent();
assert parent instanceof GrAssignmentExpression;
final GrExpression rValue = ((GrAssignmentExpression) parent).getRValue();
final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "(" + (rValue == null ? "" : rValue.getText()) + ")");
((GrAssignmentExpression) parent).replaceWithExpression(call, true);
}
}
}
} else {
for (UsageInfo usage : propertyAccess) {
final PsiReference ref = usage.getReference();
if (ref != null) {
ref.handleElementRename(propertyName);
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method getOccurrences.
static PsiElement[] getOccurrences(GrIntroduceParameterSettings settings) {
final GrParametersOwner scope = settings.getToReplaceIn();
final GrExpression expression = settings.getExpression();
if (expression != null) {
final PsiElement expr = PsiUtil.skipParentheses(expression, false);
if (expr == null)
return PsiElement.EMPTY_ARRAY;
final PsiElement[] occurrences = GroovyRefactoringUtil.getExpressionOccurrences(expr, scope);
if (occurrences == null || occurrences.length == 0) {
throw new GrRefactoringError(GroovyRefactoringBundle.message("no.occurrences.found"));
}
return occurrences;
} else {
final GrVariable var = settings.getVar();
LOG.assertTrue(var != null);
final List<PsiElement> list = Collections.synchronizedList(new ArrayList<PsiElement>());
ReferencesSearch.search(var, new LocalSearchScope(scope)).forEach(psiReference -> {
final PsiElement element = psiReference.getElement();
if (element != null) {
list.add(element);
}
return true;
});
return list.toArray(new PsiElement[list.size()]);
}
}
Aggregations