use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration 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.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrVariableDeclarationElementType method shouldCreateStub.
@Override
public boolean shouldCreateStub(ASTNode node) {
PsiElement parent = SharedImplUtil.getParent(node);
if (parent instanceof GrTypeDefinitionBody) {
// store fields
return true;
}
if (PsiTreeUtil.getParentOfType(parent, GrTypeDefinition.class) != null) {
// do not store variable declarations within classes, as they are not scripts
return false;
}
PsiElement psi = node.getPsi();
if (!(psi instanceof GrVariableDeclaration) || ((GrVariableDeclaration) psi).getModifierList().getRawAnnotations().length == 0) {
// store only annotated declarations
return false;
}
PsiFile file = psi.getContainingFile();
return file instanceof GroovyFile && ((GroovyFile) file).isScript();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GroovyDocumentationProvider method getQuickNavigateInfo.
@Override
@Nullable
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
if (element instanceof GrVariable || element instanceof GrImplicitVariable) {
StringBuilder buffer = new StringBuilder();
PsiVariable variable = (PsiVariable) element;
if (originalElement instanceof GrVariableDeclaration && ((GrVariableDeclaration) originalElement).getVariables().length > 1) {
for (GrVariable var : ((GrVariableDeclaration) originalElement).getVariables()) {
generateVariableInfo(originalElement, buffer, var);
buffer.append("\n\n");
}
} else {
generateVariableInfo(originalElement, buffer, variable);
}
return buffer.toString();
} else if (element instanceof PsiMethod) {
StringBuilder buffer = new StringBuilder();
PsiMethod method = (PsiMethod) element;
if (method instanceof GrGdkMethod) {
buffer.append("[GDK] ");
} else {
PsiClass hisClass = method.getContainingClass();
if (hisClass != null) {
String qName = hisClass.getQualifiedName();
if (qName != null) {
buffer.append(qName).append("\n");
}
}
}
PsiSubstitutor substitutor = calcSubstitutor(originalElement);
if (!method.isConstructor()) {
final PsiType substituted = substitutor.substitute(PsiUtil.getSmartReturnType(method));
PsiImplUtil.appendTypeString(buffer, substituted, originalElement);
buffer.append(" ");
}
buffer.append(method.getName()).append(" ");
buffer.append("(");
PsiParameter[] parameters = method.getParameterList().getParameters();
for (int i = 0; i < parameters.length; i++) {
PsiParameter parameter = parameters[i];
if (i > 0)
buffer.append(", ");
if (parameter instanceof GrParameter) {
GroovyPresentationUtil.appendParameterPresentation((GrParameter) parameter, substitutor, TypePresentation.LINK, buffer);
} else {
PsiType type = parameter.getType();
PsiImplUtil.appendTypeString(buffer, substitutor.substitute(type), originalElement);
buffer.append(" ");
buffer.append(parameter.getName());
}
}
buffer.append(")");
final PsiClassType[] referencedTypes = method.getThrowsList().getReferencedTypes();
if (referencedTypes.length > 0) {
buffer.append("\nthrows ");
for (PsiClassType referencedType : referencedTypes) {
PsiImplUtil.appendTypeString(buffer, referencedType, originalElement);
buffer.append(", ");
}
buffer.delete(buffer.length() - 2, buffer.length());
}
return buffer.toString();
} else if (element instanceof GrTypeDefinition) {
return generateClassInfo((GrTypeDefinition) element);
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GroovyCompletionUtil method isInPossibleClosureParameter.
public static boolean isInPossibleClosureParameter(PsiElement position) {
//Closure cl={String x, <caret>...
if (position == null)
return false;
if (position instanceof PsiWhiteSpace || position.getNode().getElementType() == GroovyTokenTypes.mNLS) {
position = FilterPositionUtil.searchNonSpaceNonCommentBack(position);
}
boolean hasCommas = false;
while (position != null) {
PsiElement parent = position.getParent();
if (parent instanceof GrVariable) {
PsiElement prev = FilterPositionUtil.searchNonSpaceNonCommentBack(parent);
hasCommas = prev != null && prev.getNode().getElementType() == GroovyTokenTypes.mCOMMA;
}
if (parent instanceof GrClosableBlock) {
PsiElement sibling = position.getPrevSibling();
while (sibling != null) {
if (sibling instanceof GrParameterList) {
return hasCommas;
}
boolean isComma = sibling instanceof LeafPsiElement && GroovyTokenTypes.mCOMMA == ((LeafPsiElement) sibling).getElementType();
hasCommas |= isComma;
if (isComma || sibling instanceof PsiWhiteSpace || sibling instanceof PsiErrorElement || sibling instanceof GrVariableDeclaration || sibling instanceof GrReferenceExpression && !((GrReferenceExpression) sibling).isQualified()) {
sibling = sibling.getPrevSibling();
} else {
return false;
}
}
return false;
}
position = parent;
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GroovyCompletionUtil method isFirstElementAfterPossibleModifiersInVariableDeclaration.
/**
* return true, if the element is first element after modifiers and there is no type element
*/
public static boolean isFirstElementAfterPossibleModifiersInVariableDeclaration(PsiElement element, boolean acceptParameter) {
if (element.getParent() instanceof GrTypeDefinitionBody && !(element instanceof PsiComment)) {
//is first on the line?
String text = element.getContainingFile().getText();
int i = CharArrayUtil.shiftBackward(text, element.getTextRange().getStartOffset() - 1, " \t");
return i >= 0 && (text.charAt(i) == '\n' || text.charAt(i) == '{');
}
final PsiElement parent = element.getParent();
if (!(parent instanceof GrVariable))
return false;
if (acceptParameter && parent instanceof GrParameter) {
return ((GrParameter) parent).getTypeElementGroovy() == null;
}
final PsiElement pparent = parent.getParent();
if (!(pparent instanceof GrVariableDeclaration))
return false;
if (((GrVariableDeclaration) pparent).isTuple())
return false;
final GrVariableDeclaration variableDeclaration = (GrVariableDeclaration) pparent;
if (variableDeclaration.getTypeElementGroovy() != null)
return false;
return variableDeclaration.getVariables()[0] == parent;
}
Aggregations