use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class PsiImplUtil method isMainMethod.
public static boolean isMainMethod(GrMethod method) {
if (!method.getName().equals(MAIN_METHOD))
return false;
else if (!method.hasModifierProperty(PsiModifier.STATIC))
return false;
final GrParameter[] parameters = method.getParameters();
if (parameters.length == 0)
return false;
if (parameters.length == 1 && parameters[0].getTypeElementGroovy() == null)
return true;
int args_count = 0;
int optional_count = 0;
for (GrParameter p : parameters) {
final GrTypeElement declaredType = p.getTypeElementGroovy();
if ((declaredType == null || declaredType.getType().equalsToText(CommonClassNames.JAVA_LANG_STRING + "[]")) && p.getInitializerGroovy() == null) {
args_count++;
}
if (p.getInitializerGroovy() != null)
optional_count++;
}
return optional_count == parameters.length - 1 && args_count == 1;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method checkForMapParameters.
private static boolean checkForMapParameters(GrParametersOwner owner) {
final GrParameter[] parameters = owner.getParameters();
if (parameters.length != 1)
return true;
final GrParameter parameter = parameters[0];
final PsiType type = parameter.getTypeGroovy();
if (!(type instanceof PsiClassType))
return true;
final PsiClass psiClass = ((PsiClassType) type).resolve();
return psiClass == null || !CommonClassNames.JAVA_UTIL_MAP.equals(psiClass.getQualifiedName());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method processIntention.
@Override
protected void processIntention(@NotNull final PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
// Method or closure to be refactored
final GrParametersOwner owner = PsiTreeUtil.getParentOfType(element, GrParametersOwner.class);
final Collection<PsiElement> occurrences = new ArrayList<>();
// Find all referenced expressions
final boolean success = collectOwnerOccurrences(project, owner, occurrences);
if (!success)
return;
// Checking for Groovy files only
final boolean isClosure = owner instanceof GrClosableBlock;
if (!checkOwnerOccurrences(project, occurrences, isClosure))
return;
// To add or not to add new parameter for map entries
final GrParameter firstParam = getFirstParameter(owner);
switch(analyzeForNamedArguments(owner, occurrences)) {
case ERROR:
{
final GrNamedElement namedElement = getReferencedElement(owner);
LOG.assertTrue(namedElement != null);
final String msg = GroovyIntentionsBundle.message("wrong.first.parameter.type", isClosure ? CLOSURE_CAPTION_CAP : METHOD_CAPTION_CAP, namedElement.getName(), firstParam.getName());
showErrorMessage(msg, project);
return;
}
case MUST_BE_MAP:
{
if (firstParam == getAppropriateParameter(element)) {
final String msg = GroovyIntentionsBundle.message("convert.cannot.itself");
showErrorMessage(msg, project);
return;
}
performRefactoring(element, owner, occurrences, false, null, false);
break;
}
case IS_NOT_MAP:
{
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final String[] possibleNames = generateValidNames(MY_POSSIBLE_NAMES, firstParam);
final GroovyMapParameterDialog dialog = new GroovyMapParameterDialog(project, possibleNames, true) {
@Override
protected void doOKAction() {
String name = getEnteredName();
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
assert name != null;
GroovyValidationUtil.validateNewParameterName(firstParam, conflicts, name);
if (isClosure) {
findClosureConflictUsages(conflicts, occurrences);
}
if (reportConflicts(conflicts, project)) {
performRefactoring(element, owner, occurrences, createNewFirst(), name, specifyTypeExplicitly());
}
super.doOKAction();
}
};
dialog.show();
} else {
//todo add statictics manager
performRefactoring(element, owner, occurrences, true, (new GroovyValidationUtil.ParameterNameSuggester("attrs", firstParam)).generateName(), true);
}
break;
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, final Editor editor) throws IncorrectOperationException {
PsiElement parent = element.getParent();
PsiElement elementToBuildTemplate;
GrVariable[] variables;
if (parent instanceof GrVariable && parent.getParent() instanceof GrVariableDeclaration) {
variables = ((GrVariableDeclaration) parent.getParent()).getVariables();
elementToBuildTemplate = parent.getParent();
} else if (parent instanceof GrVariable && parent.getParent() instanceof GrForInClause) {
variables = new GrVariable[] { (GrVariable) parent };
elementToBuildTemplate = parent.getParent().getParent();
} else if (parent instanceof GrVariableDeclaration) {
variables = ((GrVariableDeclaration) parent).getVariables();
elementToBuildTemplate = parent;
} else if (parent instanceof GrParameter && parent.getParent() instanceof GrParameterList) {
variables = new GrVariable[] { (GrVariable) parent };
elementToBuildTemplate = parent.getParent().getParent();
} else if (parent instanceof GrVariable) {
variables = new GrVariable[] { ((GrVariable) parent) };
elementToBuildTemplate = parent;
} else {
return;
}
ArrayList<TypeConstraint> types = new ArrayList<>();
if (parent.getParent() instanceof GrForInClause) {
types.add(SupertypeConstraint.create(PsiUtil.extractIteratedType((GrForInClause) parent.getParent())));
} else {
for (GrVariable variable : variables) {
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
PsiType type = initializer.getType();
if (type != null) {
types.add(SupertypeConstraint.create(type));
}
}
if (variable instanceof GrParameter) {
final PsiParameter parameter = (PsiParameter) variable;
final PsiType type = getClosureParameterType(parameter);
if (type != null) {
types.add(SupertypeConstraint.create(type));
}
}
}
}
final String originalText = elementToBuildTemplate.getText();
final TypeInfo typeInfo = getOrCreateTypeElement(parent, elementToBuildTemplate);
final PsiElement replaceElement = typeInfo.elementToReplace;
TypeConstraint[] constraints = types.toArray(new TypeConstraint[types.size()]);
ChooseTypeExpression chooseTypeExpression = new ChooseTypeExpression(constraints, element.getManager(), replaceElement.getResolveScope());
TemplateBuilderImpl builder = new TemplateBuilderImpl(elementToBuildTemplate);
builder.replaceElement(replaceElement, chooseTypeExpression);
final Document document = editor.getDocument();
final RangeMarker rangeMarker = document.createRangeMarker(elementToBuildTemplate.getTextRange());
rangeMarker.setGreedyToRight(true);
rangeMarker.setGreedyToLeft(true);
final PsiElement afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(elementToBuildTemplate);
final Template template = builder.buildTemplate();
TextRange range = afterPostprocess.getTextRange();
document.deleteString(range.getStartOffset(), range.getEndOffset());
TemplateManager templateManager = TemplateManager.getInstance(project);
templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
if (rangeMarker.isValid()) {
document.replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), originalText);
editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset() + typeInfo.originalOffset);
}
});
}
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class MyPredicate method checkForMethodParameter.
@Nullable
public static GrParameter checkForMethodParameter(GrExpression map) {
final GrParameter parameter = getParameterByArgument(map);
if (parameter == null)
return null;
final PsiElement parent = parameter.getParent().getParent();
if (!(parent instanceof PsiMethod))
return null;
final PsiMethod method = (PsiMethod) parent;
if (ApplicationManager.getApplication().isUnitTestMode() || Messages.showYesNoDialog(map.getProject(), GroovyIntentionsBundle.message("do.you.want.to.change.type.of.parameter.in.method", parameter.getName(), method.getName()), GroovyIntentionsBundle.message("convert.map.to.class.intention.name"), Messages.getQuestionIcon()) == Messages.YES) {
return parameter;
}
return null;
}
Aggregations