Search in sources :

Example 1 with GrRefactoringError

use of org.jetbrains.plugins.groovy.refactoring.GrRefactoringError in project intellij-community by JetBrains.

the class GroovyExtractMethodHandler method invokeImpl.

private void invokeImpl(Project project, Editor editor, PsiFile file, final int startOffset, final int endOffset) {
    try {
        final InitialInfo initialInfo = GroovyExtractChooser.invoke(project, editor, file, startOffset, endOffset, true);
        if (findConflicts(initialInfo))
            return;
        performRefactoring(initialInfo, editor);
    } catch (GrRefactoringError e) {
        CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), REFACTORING_NAME, HelpID.EXTRACT_METHOD);
    }
}
Also used : GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) InitialInfo(org.jetbrains.plugins.groovy.refactoring.extract.InitialInfo)

Example 2 with GrRefactoringError

use of org.jetbrains.plugins.groovy.refactoring.GrRefactoringError in project intellij-community by JetBrains.

the class GrIntroduceConstantHandler method checkVariable.

@Override
protected void checkVariable(@NotNull GrVariable variable) throws GrRefactoringError {
    final GrExpression initializer = variable.getInitializerGroovy();
    if (initializer == null) {
        throw new GrRefactoringError(RefactoringBundle.message("variable.does.not.have.an.initializer", variable.getName()));
    }
    checkExpression(initializer);
}
Also used : GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 3 with GrRefactoringError

use of org.jetbrains.plugins.groovy.refactoring.GrRefactoringError in project intellij-community by JetBrains.

the class GroovyExtractChooser method invoke.

public static InitialInfo invoke(Project project, Editor editor, PsiFile file, int start, int end, boolean forceStatements) throws GrRefactoringError {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    if (!(file instanceof GroovyFileBase)) {
        throw new GrRefactoringError(GroovyRefactoringBundle.message("only.in.groovy.files"));
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file)) {
        throw new GrRefactoringError(RefactoringBundle.message("readonly.occurences.found"));
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final StringPartInfo stringPart = StringPartInfo.findStringPart(file, start, end);
    if (stringPart != null) {
        return new InitialInfo(new VariableInfo[0], new VariableInfo[0], PsiElement.EMPTY_ARRAY, GrStatement.EMPTY_ARRAY, new ArrayList<>(), stringPart, project, null);
    }
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!forceStatements) {
        GrVariable variable = GrIntroduceHandlerBase.findVariable(file, start, end);
        if (variable != null) {
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                TextRange range = initializer.getTextRange();
                return buildInfo(project, file, range.getStartOffset(), range.getEndOffset(), forceStatements, selectionModel, variable);
            }
        }
    }
    return buildInfo(project, file, start, end, forceStatements, selectionModel, null);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) SelectionModel(com.intellij.openapi.editor.SelectionModel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)

Example 4 with GrRefactoringError

use of org.jetbrains.plugins.groovy.refactoring.GrRefactoringError in project intellij-community by JetBrains.

the class GroovyIntroduceParameterUtil method getOccurrences.

static PsiElement[] getOccurrences(GrIntroduceParameterSettings settings) {
    final GrParametersOwner scope = settings.getToReplaceIn();
    final GrExpression expression = settings.getExpression();
    if (expression != null) {
        final PsiElement expr = PsiUtil.skipParentheses(expression, false);
        if (expr == null)
            return PsiElement.EMPTY_ARRAY;
        final PsiElement[] occurrences = GroovyRefactoringUtil.getExpressionOccurrences(expr, scope);
        if (occurrences == null || occurrences.length == 0) {
            throw new GrRefactoringError(GroovyRefactoringBundle.message("no.occurrences.found"));
        }
        return occurrences;
    } else {
        final GrVariable var = settings.getVar();
        LOG.assertTrue(var != null);
        final List<PsiElement> list = Collections.synchronizedList(new ArrayList<PsiElement>());
        ReferencesSearch.search(var, new LocalSearchScope(scope)).forEach(psiReference -> {
            final PsiElement element = psiReference.getElement();
            if (element != null) {
                list.add(element);
            }
            return true;
        });
        return list.toArray(new PsiElement[list.size()]);
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 5 with GrRefactoringError

use of org.jetbrains.plugins.groovy.refactoring.GrRefactoringError in project intellij-community by JetBrains.

the class GrIntroduceParameterHandler method invoke.

private void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, int startOffset, int endOffset) {
    try {
        final InitialInfo initialInfo = GroovyExtractChooser.invoke(project, editor, file, startOffset, endOffset, false);
        chooseScopeAndRun(initialInfo, editor);
    } catch (GrRefactoringError e) {
        if (ApplicationManager.getApplication().isUnitTestMode())
            throw e;
        CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), RefactoringBundle.message("introduce.parameter.title"), HelpID.GROOVY_INTRODUCE_PARAMETER);
    }
}
Also used : GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) InitialInfo(org.jetbrains.plugins.groovy.refactoring.extract.InitialInfo)

Aggregations

GrRefactoringError (org.jetbrains.plugins.groovy.refactoring.GrRefactoringError)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)4 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 InitialInfo (org.jetbrains.plugins.groovy.refactoring.extract.InitialInfo)2 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 TextRange (com.intellij.openapi.util.TextRange)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 HashSet (com.intellij.util.containers.HashSet)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)1 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrParametersOwner (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)1 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)1 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)1 ControlFlowBuilder (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ControlFlowBuilder)1 FragmentVariableInfos (org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.FragmentVariableInfos)1