Search in sources :

Example 1 with GrIntroduceContextImpl

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

the class IntroduceVariableValidatorTest method processFile.

private String processFile(String fileText) throws IncorrectOperationException, InvalidDataException, IOException {
    String result = "";
    int startOffset = fileText.indexOf(TestUtils.BEGIN_MARKER);
    if (startOffset < 0) {
        startOffset = fileText.indexOf(ALL_MARKER);
        replaceAllOccurences = true;
        fileText = IntroduceVariableTest.removeAllMarker(fileText);
    } else {
        replaceAllOccurences = false;
        fileText = TestUtils.removeBeginMarker(fileText);
    }
    int endOffset = fileText.indexOf(TestUtils.END_MARKER);
    fileText = TestUtils.removeEndMarker(fileText);
    myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, fileText);
    Editor myEditor = myFixture.getEditor();
    myEditor.getSelectionModel().setSelection(startOffset, endOffset);
    GrExpression selectedExpr = PsiImplUtil.findElementInRange(myFixture.getFile(), startOffset, endOffset, GrExpression.class);
    Assert.assertNotNull("Selected expression reference points to null", selectedExpr);
    final PsiElement tempContainer = GrIntroduceHandlerBase.getEnclosingContainer(selectedExpr);
    Assert.assertTrue(tempContainer instanceof GroovyPsiElement);
    PsiElement[] occurences = GroovyRefactoringUtil.getExpressionOccurrences(PsiUtil.skipParentheses(selectedExpr, false), tempContainer);
    String varName = "preved";
    GroovyVariableValidator validator = new GroovyVariableValidator(new GrIntroduceContextImpl(getProject(), myEditor, selectedExpr, null, null, occurences, tempContainer));
    result = validator.isOKTest(varName, replaceAllOccurences);
    return result;
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Editor(com.intellij.openapi.editor.Editor) GroovyVariableValidator(org.jetbrains.plugins.groovy.refactoring.introduce.variable.GroovyVariableValidator) GrIntroduceContextImpl(org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceContextImpl) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 2 with GrIntroduceContextImpl

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

the class GroovyIntroduceParameterUtil method suggestNames.

static LinkedHashSet<String> suggestNames(GrVariable var, GrExpression expr, StringPartInfo stringPart, GrParametersOwner scope, Project project) {
    if (expr != null) {
        final GrIntroduceContext introduceContext = new GrIntroduceContextImpl(project, null, expr, var, stringPart, PsiElement.EMPTY_ARRAY, scope);
        final GroovyFieldValidator validator = new GroovyFieldValidator(introduceContext);
        return new LinkedHashSet<>(Arrays.asList(GroovyNameSuggestionUtil.suggestVariableNames(expr, validator, true)));
    } else if (var != null) {
        final GrIntroduceContext introduceContext = new GrIntroduceContextImpl(project, null, expr, var, stringPart, PsiElement.EMPTY_ARRAY, scope);
        final GroovyFieldValidator validator = new GroovyFieldValidator(introduceContext);
        LinkedHashSet<String> names = new LinkedHashSet<>();
        names.add(var.getName());
        ContainerUtil.addAll(names, GroovyNameSuggestionUtil.suggestVariableNameByType(var.getType(), validator));
        return names;
    } else {
        LinkedHashSet<String> names = new LinkedHashSet<>();
        names.add("closure");
        return names;
    }
}
Also used : GrIntroduceContext(org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceContext) GroovyFieldValidator(org.jetbrains.plugins.groovy.refactoring.introduce.field.GroovyFieldValidator) GrIntroduceContextImpl(org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceContextImpl)

Aggregations

GrIntroduceContextImpl (org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceContextImpl)2 Editor (com.intellij.openapi.editor.Editor)1 PsiElement (com.intellij.psi.PsiElement)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrIntroduceContext (org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceContext)1 GroovyFieldValidator (org.jetbrains.plugins.groovy.refactoring.introduce.field.GroovyFieldValidator)1 GroovyVariableValidator (org.jetbrains.plugins.groovy.refactoring.introduce.variable.GroovyVariableValidator)1