Search in sources :

Example 56 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.

the class GroovyBlockGenerator method alignVariableDeclarations.

private void alignVariableDeclarations(List<GrStatement> group, boolean classLevel) {
    AlignmentProvider.Aligner typeElement = myAlignmentProvider.createAligner(true);
    AlignmentProvider.Aligner varName = myAlignmentProvider.createAligner(true);
    AlignmentProvider.Aligner eq = myAlignmentProvider.createAligner(true);
    for (GrStatement statement : group) {
        GrVariableDeclaration varDeclaration = (GrVariableDeclaration) statement;
        GrVariable[] variables = varDeclaration.getVariables();
        for (GrVariable variable : variables) {
            varName.append(variable.getNameIdentifierGroovy());
        }
        if (classLevel && myContext.getSettings().ALIGN_GROUP_FIELD_DECLARATIONS) {
            typeElement.append(varDeclaration.getTypeElementGroovy());
            ASTNode current_eq = variables[variables.length - 1].getNode().findChildByType(GroovyTokenTypes.mASSIGN);
            if (current_eq != null) {
                eq.append(current_eq.getPsi());
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) AlignmentProvider(org.jetbrains.plugins.groovy.formatter.AlignmentProvider) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 57 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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));
        }
    }
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 58 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.

the class UpdateGroovyCopyrightsProvider method createInstance.

@Override
public UpdateCopyright createInstance(Project project, Module module, VirtualFile file, FileType base, CopyrightProfile options) {
    return new UpdateJavaFileCopyright(project, module, file, options) {

        @Override
        protected boolean accept() {
            return getFile() instanceof GroovyFile;
        }

        @Override
        protected PsiElement[] getImportsList() {
            return ((GroovyFile) getFile()).getImportStatements();
        }

        @Override
        protected PsiElement getPackageStatement() {
            return ((GroovyFile) getFile()).getPackageDefinition();
        }

        @Override
        protected void checkCommentsForTopClass(PsiClass topclass, int location, List<PsiComment> comments) {
            if (!(topclass instanceof GroovyScriptClass)) {
                super.checkCommentsForTopClass(topclass, location, comments);
                return;
            }
            final GroovyFile containingFile = (GroovyFile) topclass.getContainingFile();
            PsiElement last = containingFile.getFirstChild();
            while (last != null && !(last instanceof GrStatement)) {
                last = last.getNextSibling();
            }
            checkComments(last, location == JavaOptions.LOCATION_BEFORE_CLASS, comments);
        }
    };
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) UpdateJavaFileCopyright(com.maddyhome.idea.copyright.psi.UpdateJavaFileCopyright) PsiClass(com.intellij.psi.PsiClass) List(java.util.List) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 59 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.

the class GrJoinLinesHandlerBase method skipSemicolonsAndWhitespaces.

@Nullable
private static GrStatement skipSemicolonsAndWhitespaces(PsiElement child, boolean forward) {
    while (child != null && !(child instanceof GrStatement)) {
        final IElementType type = child.getNode().getElementType();
        if (type != GroovyTokenTypes.mSEMI && !(type == TokenType.WHITE_SPACE && !child.getText().contains("\n")))
            return null;
        child = forward ? child.getNextSibling() : child.getPrevSibling();
    }
    return (GrStatement) child;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 60 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.

the class GrJoinLinesHandlerBase method tryJoinRawLines.

@Override
public int tryJoinRawLines(Document document, PsiFile file, int start, int end) {
    if (!(file instanceof GroovyFileBase))
        return CANNOT_JOIN;
    final PsiElement element = file.findElementAt(end);
    final GrStatementOwner statementOwner = PsiTreeUtil.getParentOfType(element, GrStatementOwner.class, true, GroovyFileBase.class);
    if (statementOwner == null)
        return CANNOT_JOIN;
    GrStatement first = null;
    GrStatement last = null;
    for (PsiElement child = statementOwner.getFirstChild(); child != null; child = child.getNextSibling()) {
        final TextRange range = child.getTextRange();
        if (range.getEndOffset() == start) {
            first = skipSemicolonsAndWhitespaces(child, BACK);
        } else if (range.getStartOffset() == end) {
            last = skipSemicolonsAndWhitespaces(child, FORWARD);
        }
    }
    if (last == null || first == null)
        return CANNOT_JOIN;
    return tryJoinStatements(first, last);
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)113 PsiElement (com.intellij.psi.PsiElement)36 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)22 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)21 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)17 TextRange (com.intellij.openapi.util.TextRange)14 Nullable (org.jetbrains.annotations.Nullable)14 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)13 NotNull (org.jetbrains.annotations.NotNull)12 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)12 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 Document (com.intellij.openapi.editor.Document)6