Search in sources :

Example 1 with StringPartInfo

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

the class GrFieldNameSuggester method suggestNames.

@NotNull
public LinkedHashSet<String> suggestNames() {
    final GrExpression expression = myContext.getExpression();
    final GrVariable var = myContext.getVar();
    final StringPartInfo stringPart = myContext.getStringPart();
    if (expression != null) {
        return new LinkedHashSet<>(Arrays.asList(GroovyNameSuggestionUtil.suggestVariableNames(expression, myValidator, myForStatic)));
    } else if (stringPart != null) {
        return new LinkedHashSet<>(Arrays.asList(GroovyNameSuggestionUtil.suggestVariableNames(stringPart.getLiteral(), myValidator, myForStatic)));
    } else {
        assert var != null;
        return new LinkedHashSet<>(Arrays.asList(GroovyNameSuggestionUtil.suggestVariableNameByType(var.getType(), myValidator)));
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) LinkedHashSet(java.util.LinkedHashSet) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with StringPartInfo

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

the class GrIntroduceFieldDialog method canBeInitializedOutsideBlock.

private static boolean canBeInitializedOutsideBlock(@NotNull GrIntroduceContext context, @NotNull PsiClass clazz) {
    final StringPartInfo part = context.getStringPart();
    GrExpression expression = context.getExpression();
    if (expression != null) {
        expression = (GrExpression) PsiUtil.skipParentheses(expression, false);
        if (expression == null)
            return false;
        if (expression instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) expression).resolve();
            if (PsiUtil.isLocalVariable(resolved)) {
                expression = ((GrVariable) resolved).getInitializerGroovy();
                if (expression == null)
                    return false;
            }
        }
        ExpressionChecker visitor = new ExpressionChecker(clazz, expression);
        expression.accept(visitor);
        return visitor.isResult();
    }
    if (part != null) {
        for (GrStringInjection injection : part.getInjections()) {
            GroovyPsiElement scope = injection.getExpression() != null ? injection.getExpression() : injection.getClosableBlock();
            assert scope != null;
            ExpressionChecker visitor = new ExpressionChecker(clazz, scope);
            scope.accept(visitor);
            if (!visitor.isResult()) {
                return visitor.isResult();
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 3 with StringPartInfo

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

the class ExtractUtil method getRangeOfRefactoring.

public static TextRange getRangeOfRefactoring(ExtractInfoHelper helper) {
    final StringPartInfo stringPartInfo = helper.getStringPartInfo();
    if (stringPartInfo != null) {
        return stringPartInfo.getRange();
    } else {
        final GrStatement[] statements = helper.getStatements();
        int start = statements[0].getTextRange().getStartOffset();
        int end = statements[statements.length - 1].getTextRange().getEndOffset();
        return new TextRange(start, end);
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 4 with StringPartInfo

use of org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo 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 5 with StringPartInfo

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

the class GrIntroduceParameterDialog method doOKAction.

@Override
public void doOKAction() {
    saveSettings();
    super.doOKAction();
    final GrParametersOwner toReplaceIn = myInfo.getToReplaceIn();
    final GrExpression expr = GroovyIntroduceParameterUtil.findExpr(myInfo);
    final GrVariable var = GroovyIntroduceParameterUtil.findVar(myInfo);
    final StringPartInfo stringPart = findStringPart();
    if (myTypeComboBox.isClosureSelected() || expr == null && var == null && stringPart == null) {
        GrIntroduceParameterSettings settings = new ExtractClosureHelperImpl(myInfo, getEnteredName(), myDeclareFinalCheckBox.isSelected(), getParametersToRemove(), myDelegateViaOverloadingMethodCheckBox.isSelected(), getReplaceFieldsWithGetter(), myForceReturnCheckBox.isSelected(), false, myTypeComboBox.getSelectedType() == null);
        if (toReplaceIn instanceof GrMethod) {
            invokeRefactoring(new ExtractClosureFromMethodProcessor(settings));
        } else {
            invokeRefactoring(new ExtractClosureFromClosureProcessor(settings));
        }
    } else {
        GrIntroduceParameterSettings settings = new GrIntroduceExpressionSettingsImpl(myInfo, getEnteredName(), myDeclareFinalCheckBox.isSelected(), getParametersToRemove(), myDelegateViaOverloadingMethodCheckBox.isSelected(), getReplaceFieldsWithGetter(), expr, var, myTypeComboBox.getSelectedType(), var != null, true, myForceReturnCheckBox.isSelected());
        if (toReplaceIn instanceof GrMethod) {
            invokeRefactoring(new GrIntroduceParameterProcessor(settings));
        } else {
            invokeRefactoring(new GrIntroduceClosureParameterProcessor(settings));
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ExtractClosureFromClosureProcessor(org.jetbrains.plugins.groovy.refactoring.extract.closure.ExtractClosureFromClosureProcessor) ExtractClosureFromMethodProcessor(org.jetbrains.plugins.groovy.refactoring.extract.closure.ExtractClosureFromMethodProcessor) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) ExtractClosureHelperImpl(org.jetbrains.plugins.groovy.refactoring.extract.closure.ExtractClosureHelperImpl) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)

Aggregations

StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)16 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)11 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 Editor (com.intellij.openapi.editor.Editor)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiType (com.intellij.psi.PsiType)2 NameSuggestionsField (com.intellij.refactoring.ui.NameSuggestionsField)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 PsiElement (com.intellij.psi.PsiElement)1 ChangedMethodCallInfo (com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo)1 ExternalUsageInfo (com.intellij.refactoring.introduceParameter.ExternalUsageInfo)1 InternalUsageInfo (com.intellij.refactoring.introduceParameter.InternalUsageInfo)1 JavaVisibilityPanel (com.intellij.refactoring.ui.JavaVisibilityPanel)1 UsageInfo (com.intellij.usageView.UsageInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1