use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement in project intellij-community by JetBrains.
the class GroovyBlockGenerator method calculateAlignments.
private void calculateAlignments(List<ASTNode> children, boolean classLevel) {
List<GrStatement> currentGroup = null;
boolean spock = true;
for (ASTNode child : children) {
PsiElement psi = child.getPsi();
if (psi instanceof GrLabeledStatement) {
alignGroup(currentGroup, spock, classLevel);
currentGroup = ContainerUtil.newArrayList();
spock = true;
} else if (currentGroup != null && spock && isTablePart(psi)) {
currentGroup.add((GrStatement) psi);
} else if (psi instanceof GrVariableDeclaration) {
GrVariable[] variables = ((GrVariableDeclaration) psi).getVariables();
if (variables.length > 0) {
if (!classLevel || currentGroup == null || fieldGroupEnded(psi) || spock) {
alignGroup(currentGroup, spock, classLevel);
currentGroup = ContainerUtil.newArrayList();
spock = false;
}
currentGroup.add((GrStatement) psi);
}
} else {
if (shouldSkip(classLevel, psi))
continue;
alignGroup(currentGroup, spock, classLevel);
currentGroup = null;
}
}
if (currentGroup != null) {
alignGroup(currentGroup, spock, classLevel);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement in project intellij-community by JetBrains.
the class GroovyRefactoringSupportProvider method isInplaceRenameAvailable.
@Override
public boolean isInplaceRenameAvailable(@NotNull PsiElement elementToRename, PsiElement nameSuggestionContext) {
if (nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile())
return false;
if (!(elementToRename instanceof GrLabeledStatement)) {
return false;
}
SearchScope useScope = PsiSearchHelper.SERVICE.getInstance(elementToRename.getProject()).getUseScope(elementToRename);
if (!(useScope instanceof LocalSearchScope))
return false;
PsiElement[] scopeElements = ((LocalSearchScope) useScope).getScope();
if (scopeElements.length > 1) {
return false;
}
PsiFile containingFile = elementToRename.getContainingFile();
return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement in project intellij-community by JetBrains.
the class SpockUtils method createVariableMap.
// See org.spockframework.compiler.WhereBlockRewriter
public static Map<String, SpockVariableDescriptor> createVariableMap(GrMethod method) {
GrOpenBlock block = method.getBlock();
if (block == null)
return Collections.emptyMap();
PsiElement elementUnderLabel = null;
PsiElement elementAfterLabel = null;
main: for (PsiElement e = block.getFirstChild(); e != null; e = e.getNextSibling()) {
if (e instanceof GrLabeledStatement) {
GrLabeledStatement l = (GrLabeledStatement) e;
elementAfterLabel = l.getNextSibling();
while (true) {
GrStatement statement = l.getStatement();
if ("where".equals(l.getName())) {
elementUnderLabel = statement;
break main;
}
if (statement instanceof GrLabeledStatement) {
l = (GrLabeledStatement) statement;
continue;
}
break;
}
}
}
if (elementUnderLabel == null)
return Collections.emptyMap();
Map<String, SpockVariableDescriptor> res = new HashMap<>();
PsiElement e = elementUnderLabel;
while (e != null) {
if (e instanceof GrBinaryExpression && ((GrBinaryExpression) e).getOperationTokenType() == GroovyElementTypes.COMPOSITE_LSHIFT_SIGN) {
GrBinaryExpression shift = (GrBinaryExpression) e;
GrExpression leftOperand = shift.getLeftOperand();
GrExpression rightOperand = shift.getRightOperand();
if (leftOperand instanceof GrReferenceExpression) {
String name = getNameByReference(leftOperand);
if (name != null) {
SpockVariableDescriptor descriptor = new SpockVariableDescriptor(leftOperand, name);
descriptor.addExpressionOfCollection(rightOperand);
res.put(name, descriptor);
}
} else if (leftOperand instanceof GrListOrMap) {
GrExpression[] variableDefinitions = ((GrListOrMap) leftOperand).getInitializers();
SpockVariableDescriptor[] variables = createVariables(res, Arrays.asList(variableDefinitions));
if (rightOperand instanceof GrListOrMap) {
for (GrExpression expression : ((GrListOrMap) rightOperand).getInitializers()) {
if (expression instanceof GrListOrMap) {
add(variables, Arrays.asList(((GrListOrMap) expression).getInitializers()));
} else {
for (SpockVariableDescriptor variable : variables) {
if (variable != null) {
variable.addExpressionOfCollection(expression);
}
}
}
}
}
}
} else if (e instanceof GrAssignmentExpression) {
GrAssignmentExpression assExpr = (GrAssignmentExpression) e;
GrExpression lValue = assExpr.getLValue();
String name = getNameByReference(lValue);
if (name != null) {
res.put(name, new SpockVariableDescriptor(lValue, name).addExpression(assExpr.getRValue()));
}
} else if (isOrStatement(e)) {
// See org.spockframework.compiler.WhereBlockRewriter#rewriteTableLikeParameterization()
List<GrExpression> variableDefinitions = new ArrayList<>();
splitOr(variableDefinitions, (GrExpression) e);
SpockVariableDescriptor[] variables = createVariables(res, variableDefinitions);
List<GrExpression> row = new ArrayList<>();
PsiElement rowElement = getNext(e, elementUnderLabel, elementAfterLabel);
while (isOrStatement(rowElement)) {
row.clear();
splitOr(row, (GrExpression) rowElement);
add(variables, row);
rowElement = getNext(rowElement, elementUnderLabel, elementAfterLabel);
}
e = rowElement;
continue;
}
e = getNext(e, elementUnderLabel, elementAfterLabel);
}
return res;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement in project intellij-community by JetBrains.
the class GrLabelReference method getVariants.
@Override
@NotNull
public Object[] getVariants() {
final List<PsiElement> result = new ArrayList<>();
PsiElement context = myStatement;
while (context != null) {
if (context instanceof GrLabeledStatement) {
result.add(context);
}
context = context.getContext();
}
return ArrayUtil.toObjectArray(result);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement in project intellij-community by JetBrains.
the class GroovyCompletionUtil method isNewStatement.
/**
* Shows whether keyword may be placed as a new statement beginning
*
* @param element
* @param canBeAfterBrace May be after '{' symbol or not
* @return
*/
public static boolean isNewStatement(PsiElement element, boolean canBeAfterBrace) {
PsiElement previousLeaf = getLeafByOffset(element.getTextRange().getStartOffset() - 1, element);
previousLeaf = PsiImplUtil.realPrevious(previousLeaf);
if (previousLeaf != null) {
if (canBeAfterBrace && GroovyTokenTypes.mLCURLY.equals(previousLeaf.getNode().getElementType())) {
return true;
}
if (GroovyTokenTypes.mCOLON.equals(previousLeaf.getNode().getElementType()) && previousLeaf.getParent() instanceof GrLabeledStatement) {
return true;
}
}
return (previousLeaf == null || SEPARATORS.contains(previousLeaf.getNode().getElementType()));
}
Aggregations