Search in sources :

Example 16 with GroovyFileBase

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

the class GrJoinBlockStatementHandler method tryJoinLines.

@Override
public int tryJoinLines(Document document, PsiFile file, int start, int end) {
    if (!(file instanceof GroovyFileBase))
        return CANNOT_JOIN;
    final PsiElement startElement = file.findElementAt(start);
    if (startElement == null || startElement.getNode().getElementType() != GroovyTokenTypes.mLCURLY)
        return CANNOT_JOIN;
    final PsiElement parent = startElement.getParent();
    if (!(parent instanceof GrOpenBlock))
        return CANNOT_JOIN;
    final GrStatement[] statements = ((GrOpenBlock) parent).getStatements();
    if (statements.length != 1)
        return CANNOT_JOIN;
    final PsiElement parent1 = parent.getParent();
    if (!(parent1 instanceof GrBlockStatement))
        return CANNOT_JOIN;
    final PsiElement parent2 = parent1.getParent();
    if (!(parent2 instanceof GrIfStatement) && !(parent2 instanceof GrWhileStatement) && !(parent2 instanceof GrForStatement)) {
        return CANNOT_JOIN;
    }
    final GrStatement statement = ((GrBlockStatement) parent1).replaceWithStatement(statements[0]);
    return statement.getTextRange().getStartOffset();
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement)

Example 17 with GroovyFileBase

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

the class GrJoinControlStatementHandler method tryJoinLines.

@Override
public int tryJoinLines(Document document, PsiFile file, int start, int end) {
    if (!(file instanceof GroovyFileBase))
        return CANNOT_JOIN;
    final PsiElement startElement = file.findElementAt(start);
    if (startElement == null || !startElement.getNode().getElementType().equals(GroovyTokenTypes.mRPAREN))
        return CANNOT_JOIN;
    final PsiElement parent = startElement.getParent();
    if (!(parent instanceof GrIfStatement || parent instanceof GrWhileStatement || parent instanceof GrForStatement))
        return CANNOT_JOIN;
    GrStatement inner;
    if (parent instanceof GrIfStatement) {
        inner = ((GrIfStatement) parent).getThenBranch();
    } else if (parent instanceof GrWhileStatement) {
        inner = ((GrWhileStatement) parent).getBody();
    } else /*if (parent instanceof GrForStatement)*/
    {
        inner = ((GrForStatement) parent).getBody();
    }
    if (inner instanceof GrBlockStatement)
        return CANNOT_JOIN;
    document.replaceString(start + 1, end, " ");
    return start + 2;
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) PsiElement(com.intellij.psi.PsiElement)

Example 18 with GroovyFileBase

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase 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)

Example 19 with GroovyFileBase

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

the class GrIntroduceValidatorEngine method isOKImpl.

private MultiMap<PsiElement, String> isOKImpl(String varName, boolean replaceAllOccurrences) {
    PsiElement firstOccurrence = getFirstOccurrence(replaceAllOccurrences);
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    assert varName != null;
    final int offset = firstOccurrence.getTextRange().getStartOffset();
    validateOccurrencesDown(myContext.getScope(), conflicts, varName, offset);
    if (!(myContext.getScope() instanceof GroovyFileBase)) {
        validateVariableOccurrencesUp(myContext.getScope(), conflicts, varName, offset);
    }
    if (replaceAllOccurrences) {
        for (PsiElement element : myContext.getOccurrences()) {
            if (element == firstOccurrence)
                continue;
            validateVariableOccurrencesUp(element, conflicts, varName, element.getTextRange().getStartOffset());
        }
    }
    return conflicts;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) PsiElement(com.intellij.psi.PsiElement)

Example 20 with GroovyFileBase

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

the class MavenGroovyPomCompletionWeigher method weigh.

@Override
public Comparable weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) {
    PsiFile containingFile = location.getCompletionParameters().getPosition().getContainingFile();
    if (!(containingFile instanceof GroovyFileBase)) {
        return null;
    }
    if (!"pom.groovy".equals(containingFile.getName()))
        return null;
    Object o = element.getObject();
    if (o instanceof ResolveResult) {
        o = ((ResolveResult) o).getElement();
    }
    if (o instanceof DynamicMemberUtils.DynamicElement) {
        return 1;
    } else {
        return -1;
    }
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) PsiFile(com.intellij.psi.PsiFile) ResolveResult(com.intellij.psi.ResolveResult)

Aggregations

GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)30 PsiElement (com.intellij.psi.PsiElement)12 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)9 Project (com.intellij.openapi.project.Project)7 PsiFile (com.intellij.psi.PsiFile)7 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 Editor (com.intellij.openapi.editor.Editor)5 Document (com.intellij.openapi.editor.Document)4 TextRange (com.intellij.openapi.util.TextRange)4 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)3 ArrayList (java.util.ArrayList)3 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)3 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 ASTNode (com.intellij.lang.ASTNode)2 Module (com.intellij.openapi.module.Module)2 PsiRecursiveElementVisitor (com.intellij.psi.PsiRecursiveElementVisitor)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2