Search in sources :

Example 61 with GrStatement

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

the class GroovyElseSelectioner method select.

@Nullable
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    if (!(e instanceof GrIfStatement))
        return null;
    GrIfStatement ifSt = (GrIfStatement) e;
    GrStatement branch = ifSt.getElseBranch();
    PsiElement elseKeyword = ifSt.getElseKeyword();
    if (branch == null || elseKeyword == null)
        return null;
    return expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), branch.getTextRange().getEndOffset()));
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 62 with GrStatement

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

the class GroovyStatementSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    TextRange originalRange;
    PsiElement first;
    PsiElement last;
    if (e instanceof GrStatement) {
        first = e;
        PsiElement next = e.getNextSibling();
        next = skipWhitespacesForward(next);
        if (next != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI) {
            originalRange = new TextRange(e.getTextRange().getStartOffset(), next.getTextRange().getEndOffset());
            last = next;
        } else {
            originalRange = e.getTextRange();
            last = e;
        }
    } else {
        last = e;
        PsiElement prev = e.getPrevSibling();
        prev = skipWhitespaceBack(prev);
        if (prev instanceof GrStatement) {
            originalRange = new TextRange(prev.getTextRange().getStartOffset(), e.getTextRange().getEndOffset());
            first = prev;
        } else {
            originalRange = e.getTextRange();
            first = e;
        }
    }
    final List<TextRange> ranges = ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, originalRange);
    final TextRange blockRange = inferBlockRange(first, last);
    if (!blockRange.equals(originalRange)) {
        ranges.addAll(ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, blockRange, true));
    }
    return ranges;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 63 with GrStatement

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

the class GroovyExtractMethodHandler method findConflicts.

private static boolean findConflicts(InitialInfo info) {
    //new ConflictsDialog()
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final PsiElement declarationOwner = info.getContext().getParent();
    GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
            PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
            if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
                conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
            }
        }

        //skip 'print' and 'println'
        private boolean skipResult(GroovyResolveResult result) {
            PsiElement element = result.getElement();
            if (element instanceof PsiMethod) {
                String name = ((PsiMethod) element).getName();
                if (!name.startsWith("print"))
                    return false;
                if (element instanceof GrGdkMethod)
                    element = ((GrGdkMethod) element).getStaticMethod();
                PsiClass aClass = ((PsiMethod) element).getContainingClass();
                if (aClass != null) {
                    String qname = aClass.getQualifiedName();
                    return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
                }
            }
            return false;
        }
    };
    GrStatement[] statements = info.getStatements();
    for (GrStatement statement : statements) {
        statement.accept(visitor);
    }
    if (conflicts.isEmpty())
        return false;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
    }
    ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
    dialog.show();
    return !dialog.isOK();
}
Also used : GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) MultiMap(com.intellij.util.containers.MultiMap) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog)

Example 64 with GrStatement

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

the class GroovyExtractMethodHandler method performRefactoring.

private void performRefactoring(@NotNull final InitialInfo initialInfo, @Nullable final Editor editor) {
    final PsiClass owner = PsiUtil.getContextClass(initialInfo.getContext());
    LOG.assertTrue(owner != null);
    final ExtractMethodInfoHelper helper = getSettings(initialInfo, owner);
    if (helper == null)
        return;
    CommandProcessor.getInstance().executeCommand(helper.getProject(), () -> {
        WriteAction.run(() -> {
            createMethod(helper, owner);
            GrStatementOwner declarationOwner = helper.getStringPartInfo() == null ? GroovyRefactoringUtil.getDeclarationOwner(helper.getStatements()[0]) : null;
            GrStatement realStatement = ExtractUtil.replaceStatement(declarationOwner, helper);
            // move to offset
            if (editor != null) {
                PsiDocumentManager.getInstance(helper.getProject()).commitDocument(editor.getDocument());
                editor.getSelectionModel().removeSelection();
                editor.getCaretModel().moveToOffset(ExtractUtil.getCaretOffset(realStatement));
            }
        });
    }, REFACTORING_NAME, null);
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 65 with GrStatement

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

the class GroovyIntroduceParameterMethodUsagesProcessor method processAddSuperCall.

@Override
public boolean processAddSuperCall(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    if (!(usage.getElement() instanceof GrMethod) || !isGroovyUsage(usage))
        return true;
    GrMethod constructor = (GrMethod) usage.getElement();
    if (!constructor.isConstructor())
        return true;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
    GrConstructorInvocation superCall = factory.createConstructorInvocation("super();");
    GrOpenBlock body = constructor.getBlock();
    final GrStatement[] statements = body.getStatements();
    if (statements.length > 0) {
        superCall = (GrConstructorInvocation) body.addStatementBefore(superCall, statements[0]);
    } else {
        superCall = (GrConstructorInvocation) body.addStatementBefore(superCall, null);
    }
    processChangeMethodUsage(data, new UsageInfo(superCall), usages);
    return false;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) UsageInfo(com.intellij.usageView.UsageInfo) 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