use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method getOrCreateTypeElement.
@NotNull
private static TypeInfo getOrCreateTypeElement(@NotNull PsiElement parent, @NotNull PsiElement elementToBuildTemplateOn) {
GrModifierList modifierList = getModifierList(parent);
if (modifierList != null && modifierList.hasModifierProperty(GrModifier.DEF) && modifierList.getModifiers().length == 1) {
PsiElement modifier = modifierList.getModifier(GrModifier.DEF);
LOG.assertTrue(modifier != null);
int modifierOffset = modifier.getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
return new TypeInfo(modifier, modifierOffset);
} else {
int nameElementOffset;
final PsiClassType typeToUse = TypesUtil.createType("Abc", parent);
if (elementToBuildTemplateOn instanceof GrVariableDeclaration) {
GrVariableDeclaration decl = (GrVariableDeclaration) elementToBuildTemplateOn;
decl.setType(typeToUse);
nameElementOffset = decl.getModifierList().getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
} else {
GrVariable var = (GrVariable) parent;
var.setType(typeToUse);
nameElementOffset = var.getNameIdentifierGroovy().getTextRange().getStartOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
}
return new TypeInfo(getTypeElement(parent), nameElementOffset);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
PsiElement parent = element.getParent();
PsiElement pparent;
if (isNameIdentifierOfVariable(element, parent) || isModifierListOfVar(element, parent)) {
pparent = parent.getParent();
} else if (isModifierListOfVarDecl(element, parent)) {
pparent = parent;
} else {
return false;
}
if (pparent instanceof GrVariableDeclaration) {
if (((GrVariableDeclaration) pparent).getTypeElementGroovy() != null)
return false;
GrVariable[] variables = ((GrVariableDeclaration) pparent).getVariables();
for (GrVariable variable : variables) {
if (isVarDeclaredWithInitializer(variable))
return true;
}
} else if (pparent instanceof GrForInClause) {
final GrVariable variable = ((GrForInClause) pparent).getDeclaredVariable();
return variable != null && variable.getTypeElementGroovy() == null && PsiUtil.extractIteratedType((GrForInClause) pparent) != null;
} else if (parent instanceof GrParameter && pparent instanceof GrParameterList) {
return ((GrParameter) parent).getTypeElementGroovy() == null && getClosureParameterType((PsiParameter) parent) != null;
} else {
final GrVariable variable = (GrVariable) parent;
return variable.getTypeElementGroovy() == null && isVarDeclaredWithInitializer(variable);
}
return false;
}
private boolean isModifierListOfVarDecl(PsiElement element, PsiElement parent) {
return parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getModifierList() == element;
}
private boolean isModifierListOfVar(PsiElement element, PsiElement parent) {
return parent instanceof GrVariable && ((GrVariable) parent).getModifierList() == element;
}
private boolean isNameIdentifierOfVariable(PsiElement element, PsiElement parent) {
return parent instanceof GrVariable && ((GrVariable) parent).getTypeElementGroovy() == null && element == ((GrVariable) parent).getNameIdentifierGroovy();
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrIntroduceParameterDialog method doOKAction.
@Override
public void doOKAction() {
saveSettings();
super.doOKAction();
final GrParametersOwner toReplaceIn = myInfo.getToReplaceIn();
final GrExpression expr = GroovyIntroduceParameterUtil.findExpr(myInfo);
final GrVariable var = GroovyIntroduceParameterUtil.findVar(myInfo);
final StringPartInfo stringPart = findStringPart();
if (myTypeComboBox.isClosureSelected() || expr == null && var == null && stringPart == null) {
GrIntroduceParameterSettings settings = new ExtractClosureHelperImpl(myInfo, getEnteredName(), myDeclareFinalCheckBox.isSelected(), getParametersToRemove(), myDelegateViaOverloadingMethodCheckBox.isSelected(), getReplaceFieldsWithGetter(), myForceReturnCheckBox.isSelected(), false, myTypeComboBox.getSelectedType() == null);
if (toReplaceIn instanceof GrMethod) {
invokeRefactoring(new ExtractClosureFromMethodProcessor(settings));
} else {
invokeRefactoring(new ExtractClosureFromClosureProcessor(settings));
}
} else {
GrIntroduceParameterSettings settings = new GrIntroduceExpressionSettingsImpl(myInfo, getEnteredName(), myDeclareFinalCheckBox.isSelected(), getParametersToRemove(), myDelegateViaOverloadingMethodCheckBox.isSelected(), getReplaceFieldsWithGetter(), expr, var, myTypeComboBox.getSelectedType(), var != null, true, myForceReturnCheckBox.isSelected());
if (toReplaceIn instanceof GrMethod) {
invokeRefactoring(new GrIntroduceParameterProcessor(settings));
} else {
invokeRefactoring(new GrIntroduceClosureParameterProcessor(settings));
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable 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.GrVariable 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);
}
Aggregations