use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class GroovyBlockGenerator method alignSpockTable.
private void alignSpockTable(List<GrStatement> group) {
if (group.size() < 2) {
return;
}
GrStatement inner = group.get(0);
boolean embedded = inner != null && isTablePart(inner);
GrStatement first = embedded ? inner : group.get(1);
List<AlignmentProvider.Aligner> alignments = ContainerUtil.map2List(getSpockTable(first), leaf -> myAlignmentProvider.createAligner(leaf, true, Alignment.Anchor.RIGHT));
int second = embedded ? 1 : 2;
for (int i = second; i < group.size(); i++) {
List<LeafPsiElement> table = getSpockTable(group.get(i));
for (int j = 0; j < Math.min(table.size(), alignments.size()); j++) {
alignments.get(j).append(table.get(j));
}
}
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method findStatementsInRange.
@NotNull
public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) {
if (!(file instanceof GroovyFileBase))
return PsiElement.EMPTY_ARRAY;
Language language = GroovyLanguage.INSTANCE;
PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language);
PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
if (element1 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element1)) {
startOffset = element1.getTextRange().getEndOffset();
element1 = file.findElementAt(startOffset);
}
if (element2 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element2)) {
endOffset = element2.getTextRange().getStartOffset();
element2 = file.findElementAt(endOffset - 1);
}
if (element1 == null || element2 == null)
return PsiElement.EMPTY_ARRAY;
PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
if (parent == null)
return PsiElement.EMPTY_ARRAY;
while (true) {
if (parent instanceof GrCodeBlock)
break;
if (parent instanceof GroovyFileBase)
break;
if (parent instanceof GrCaseSection)
break;
if (parent instanceof GrStatement) {
parent = parent.getParent();
break;
}
if (parent == null)
return PsiElement.EMPTY_ARRAY;
final PsiElement prev = parent;
parent = parent.getParent();
if (parent instanceof GrCodeBlock && prev instanceof LeafPsiElement) {
//braces
parent = parent.getParent();
}
}
if (!parent.equals(element1)) {
while (!parent.equals(element1.getParent())) {
element1 = element1.getParent();
}
}
if (startOffset != element1.getTextRange().getStartOffset() && strict)
return PsiElement.EMPTY_ARRAY;
if (!parent.equals(element2)) {
while (!parent.equals(element2.getParent())) {
element2 = element2.getParent();
}
}
if (endOffset != element2.getTextRange().getEndOffset() && strict)
return PsiElement.EMPTY_ARRAY;
if (parent instanceof GrCodeBlock && parent.getParent() instanceof GrBlockStatement && element1 == ((GrCodeBlock) parent).getLBrace() && element2 == ((GrCodeBlock) parent).getRBrace()) {
return new PsiElement[] { parent.getParent() };
}
// calculate children
PsiElement[] children = PsiElement.EMPTY_ARRAY;
PsiElement psiChild = parent.getFirstChild();
if (psiChild != null) {
List<PsiElement> result = new ArrayList<>();
while (psiChild != null) {
result.add(psiChild);
psiChild = psiChild.getNextSibling();
}
children = PsiUtilCore.toPsiElementArray(result);
}
ArrayList<PsiElement> possibleStatements = new ArrayList<>();
boolean flag = false;
for (PsiElement child : children) {
if (child == element1) {
flag = true;
}
if (flag) {
possibleStatements.add(child);
}
if (child == element2) {
break;
}
}
for (PsiElement element : possibleStatements) {
if (!(element instanceof GrStatement || element instanceof PsiWhiteSpace || element instanceof PsiComment || TokenSets.SEPARATORS.contains(element.getNode().getElementType()))) {
return PsiElement.EMPTY_ARRAY;
}
}
return PsiUtilCore.toPsiElementArray(possibleStatements);
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class GrTypeDefinitionImpl method getDefaultAnchor.
@Nullable
private PsiElement getDefaultAnchor(GrTypeDefinitionBody body, PsiMember member) {
GroovyCodeStyleSettingsFacade settings = GroovyCodeStyleSettingsFacade.getInstance(getProject());
int order = getMemberOrderWeight(member, settings);
if (order < 0)
return null;
PsiElement lastMember = null;
for (PsiElement child = body.getFirstChild(); child != null; child = child.getNextSibling()) {
int order1 = getMemberOrderWeight(getAnyMember(child), settings);
if (order1 < 0)
continue;
if (order1 > order) {
final PsiElement lBrace = body.getLBrace();
if (lastMember != null) {
PsiElement nextSibling = lastMember.getNextSibling();
while (nextSibling instanceof LeafPsiElement && (nextSibling.getText().equals(",") || nextSibling.getText().equals(";"))) {
nextSibling = nextSibling.getNextSibling();
}
return nextSibling == null && lBrace != null ? PsiUtil.skipWhitespacesAndComments(lBrace.getNextSibling(), true) : nextSibling;
} else if (lBrace != null) {
return PsiUtil.skipWhitespacesAndComments(lBrace.getNextSibling(), true);
}
}
lastMember = child;
}
return body.getRBrace();
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class GrListOrMapImpl method deleteChildInternal.
@Override
public void deleteChildInternal(@NotNull ASTNode child) {
final PsiElement psi = child.getPsi();
if (psi instanceof GrExpression || psi instanceof GrNamedArgument) {
PsiElement prev = PsiUtil.getPrevNonSpace(psi);
PsiElement next = PsiUtil.getNextNonSpace(psi);
if (prev != null && prev.getNode() != null && prev.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
super.deleteChildInternal(prev.getNode());
} else if (next instanceof LeafPsiElement && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
super.deleteChildInternal(next.getNode());
}
}
super.deleteChildInternal(child);
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement 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;
}
Aggregations