Search in sources :

Example 6 with GroovyFileBase

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

the class GrIntroduceFieldProcessor method run.

@Nullable
public GrVariable run() {
    PsiElement scope = myContext.getScope();
    final PsiClass targetClass = scope instanceof GroovyFileBase ? ((GroovyFileBase) scope).getScriptClass() : (PsiClass) scope;
    if (targetClass == null)
        return null;
    final GrVariableDeclaration declaration = insertField(targetClass);
    final GrVariable field = declaration.getVariables()[0];
    if (mySettings.removeLocalVar()) {
        myLocalVariable = GrIntroduceHandlerBase.resolveLocalVar(myContext);
        assert myLocalVariable != null : myContext.getExpression() + ", " + myContext.getVar() + ", " + myContext.getStringPart();
    }
    GrExpression originalInitializer = getInitializer();
    myInitializer = originalInitializer == null ? null : (GrExpression) originalInitializer.copy();
    List<PsiElement> replaced = processOccurrences(targetClass, field);
    switch(mySettings.initializeIn()) {
        case CUR_METHOD:
            initializeInMethod(field, replaced);
            break;
        case FIELD_DECLARATION:
            field.setInitializerGroovy(myInitializer);
            break;
        case CONSTRUCTOR:
            initializeInConstructor(field, replaced);
            break;
        case SETUP_METHOD:
            initializeInSetup(field, replaced);
            break;
    }
    JavaCodeStyleManager.getInstance(declaration.getProject()).shortenClassReferences(declaration);
    if (mySettings.removeLocalVar()) {
        GrIntroduceHandlerBase.deleteLocalVar(myLocalVariable);
    }
    return field;
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GroovyFileBase

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

the class GrIntroduceValidatorEngine method validateVariableOccurrencesUp.

private void validateVariableOccurrencesUp(PsiElement startElement, MultiMap<PsiElement, String> conflicts, @NotNull String varName, double startOffset) {
    PsiElement prevSibling = startElement.getPrevSibling();
    while (prevSibling != null) {
        if (!(GroovyRefactoringUtil.isAppropriateContainerForIntroduceVariable(prevSibling) && prevSibling.getTextRange().getEndOffset() < startOffset)) {
            validateOccurrencesDown(prevSibling, conflicts, varName, startOffset);
        }
        prevSibling = prevSibling.getPrevSibling();
    }
    PsiElement parent = startElement.getParent();
    // Do not check context out of method, type definition and directories
    if (parent == null || parent instanceof GrMethod || parent instanceof GrTypeDefinition || parent instanceof GroovyFileBase || parent instanceof PsiDirectory) {
        return;
    }
    validateVariableOccurrencesUp(parent, conflicts, varName, startOffset);
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiDirectory(com.intellij.psi.PsiDirectory) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiElement(com.intellij.psi.PsiElement)

Example 8 with GroovyFileBase

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

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

the class OverridingTester method doTest.

public void doTest() throws Throwable {
    final String testFile = getTestName(true) + ".test";
    final List<String> strings = TestUtils.readInput(getTestDataPath() + "/" + testFile);
    GroovyFileBase psiFile = (GroovyFileBase) myFixture.addFileToProject("foo.groovy", strings.get(0));
    StringBuilder buffer = new StringBuilder();
    GrTypeDefinition[] grTypeDefinitions = psiFile.getTypeDefinitions();
    GrTypeDefinition lastTypeDefinition = psiFile.getTypeDefinitions()[grTypeDefinitions.length - 1];
    PsiMethod[] psiMethods = lastTypeDefinition.getMethods();
    for (PsiMethod method : psiMethods) {
        PsiMethod[] superMethods = findMethod(method);
        String[] classes = sortUseContainingClass(superMethods);
        for (String classAsString : classes) {
            buffer.append(classAsString);
            //between different super methods
            buffer.append("\n");
        }
        //between different methods
        buffer.append("\n");
    }
    //between class definitions
    buffer.append("\n");
    assertEquals(strings.get(1), buffer.toString().trim());
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiMethod(com.intellij.psi.PsiMethod)

Example 10 with GroovyFileBase

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

the class GrAliasImportIntention method findUsages.

private static List<UsageInfo> findUsages(PsiMember member, GroovyFileBase file) {
    LocalSearchScope scope = new LocalSearchScope(file);
    final ArrayList<UsageInfo> infos = new ArrayList<>();
    final HashSet<Object> usedRefs = ContainerUtil.newHashSet();
    final Processor<PsiReference> consumer = reference -> {
        if (usedRefs.add(reference)) {
            infos.add(new UsageInfo(reference));
        }
        return true;
    };
    if (member instanceof PsiMethod) {
        MethodReferencesSearch.search((PsiMethod) member, scope, false).forEach(consumer);
    } else {
        ReferencesSearch.search(member, scope).forEach(consumer);
        if (member instanceof PsiField) {
            final PsiMethod getter = GroovyPropertyUtils.findGetterForField((PsiField) member);
            if (getter != null) {
                MethodReferencesSearch.search(getter, scope, false).forEach(consumer);
            }
            final PsiMethod setter = GroovyPropertyUtils.findSetterForField((PsiField) member);
            if (setter != null) {
                MethodReferencesSearch.search(setter, scope, false).forEach(consumer);
            }
        }
    }
    return infos;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) IntentionUtils(org.jetbrains.plugins.groovy.intentions.base.IntentionUtils) Document(com.intellij.openapi.editor.Document) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) Computable(com.intellij.openapi.util.Computable) UsageInfo(com.intellij.usageView.UsageInfo) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) ContainerUtil(com.intellij.util.containers.ContainerUtil) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Project(com.intellij.openapi.project.Project) MyLookupExpression(com.intellij.refactoring.rename.inplace.MyLookupExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) LinkedHashSet(java.util.LinkedHashSet) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) RangeMarker(com.intellij.openapi.editor.RangeMarker) Extensions(com.intellij.openapi.extensions.Extensions) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Template(com.intellij.codeInsight.template.Template) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPropertyUtils(org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils) UsageViewUtil(com.intellij.usageView.UsageViewUtil) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Processor(com.intellij.util.Processor) NameSuggestionProvider(com.intellij.refactoring.rename.NameSuggestionProvider) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) PostprocessReformattingAspect(com.intellij.psi.impl.source.PostprocessReformattingAspect) ArrayList(java.util.ArrayList) UsageInfo(com.intellij.usageView.UsageInfo)

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