use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable 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()]);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrInplaceVariableIntroducer method addAdditionalVariables.
@Override
protected void addAdditionalVariables(TemplateBuilderImpl builder) {
GrVariable variable = getVariable();
assert variable != null && variable.getInitializerGroovy() != null;
final PsiType initializerType = variable.getInitializerGroovy().getType();
TypeConstraint[] constraints = initializerType != null && !initializerType.equals(PsiType.NULL) ? new SupertypeConstraint[] { SupertypeConstraint.create(initializerType) } : TypeConstraint.EMPTY_ARRAY;
ChooseTypeExpression typeExpression = new ChooseTypeExpression(constraints, variable.getManager(), variable.getResolveScope(), true, GroovyApplicationSettings.getInstance().INTRODUCE_LOCAL_SELECT_DEF);
PsiElement element = getTypeELementOrDef(variable);
if (element == null)
return;
builder.replaceElement(element, "Variable_type", typeExpression, true, true);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrChangeVariableType method doFix.
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
final PsiElement parent = element.getParent();
try {
final PsiType type = JavaPsiFacade.getElementFactory(project).createTypeFromText(myType, element);
if (parent instanceof GrVariable) {
((GrVariable) parent).setType(type);
} else if (element instanceof GrReferenceExpression && parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getLValue() == element) {
final PsiElement resolved = ((GrReferenceExpression) element).resolve();
if (resolved instanceof GrVariable && !(resolved instanceof GrParameter)) {
((GrVariable) resolved).setType(type);
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class WritesCounterDFAInstance method getVariable.
@Contract("null -> null")
@Nullable
private static GrVariable getVariable(@Nullable PsiElement instructionElement) {
final GrVariable variable;
if (instructionElement instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) instructionElement).resolve();
variable = resolved instanceof GrVariable ? (GrVariable) resolved : null;
} else if (instructionElement instanceof GrVariable) {
variable = (GrVariable) instructionElement;
} else {
variable = null;
}
return variable != null && PsiUtil.isLocalOrParameter(variable) ? variable : null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GroovyNamedArgumentProvider method getNamedArgumentsFromAllProviders.
@Nullable
public static Map<String, NamedArgumentDescriptor> getNamedArgumentsFromAllProviders(@NotNull GrCall call, @Nullable String argumentName, boolean forCompletion) {
Map<String, NamedArgumentDescriptor> namedArguments = new HashMap<String, NamedArgumentDescriptor>() {
@Override
public NamedArgumentDescriptor put(String key, NamedArgumentDescriptor value) {
NamedArgumentDescriptor oldValue = super.put(key, value);
if (oldValue != null) {
super.put(key, oldValue);
}
//noinspection ConstantConditions
return oldValue;
}
};
GroovyResolveResult[] callVariants = call.getCallVariants(null);
if (callVariants.length == 0 || PsiUtil.isSingleBindingVariant(callVariants)) {
for (GroovyNamedArgumentProvider namedArgumentProvider : EP_NAME.getExtensions()) {
namedArgumentProvider.getNamedArguments(call, GroovyResolveResult.EMPTY_RESULT, argumentName, forCompletion, namedArguments);
}
} else {
boolean mapExpected = false;
for (GroovyResolveResult result : callVariants) {
PsiElement element = result.getElement();
if (element instanceof GrAccessorMethod)
continue;
if (element instanceof PsiMethod) {
PsiMethod method = (PsiMethod) element;
PsiParameter[] parameters = method.getParameterList().getParameters();
if (!method.isConstructor() && !(parameters.length > 0 && canBeMap(parameters[0])))
continue;
mapExpected = true;
for (GroovyMethodInfo methodInfo : GroovyMethodInfo.getInfos(method)) {
if (methodInfo.getNamedArguments() != null || methodInfo.isNamedArgumentProviderDefined()) {
if (methodInfo.isApplicable(method)) {
if (methodInfo.isNamedArgumentProviderDefined()) {
methodInfo.getNamedArgProvider().getNamedArguments(call, result, argumentName, forCompletion, namedArguments);
}
if (methodInfo.getNamedArguments() != null) {
namedArguments.putAll(methodInfo.getNamedArguments());
}
}
}
}
}
for (GroovyNamedArgumentProvider namedArgumentProvider : EP_NAME.getExtensions()) {
namedArgumentProvider.getNamedArguments(call, result, argumentName, forCompletion, namedArguments);
}
if (element instanceof GrVariable && InheritanceUtil.isInheritor(((GrVariable) element).getTypeGroovy(), GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
mapExpected = true;
}
}
if (!mapExpected && namedArguments.isEmpty()) {
return null;
}
}
return namedArguments;
}
Aggregations