Search in sources :

Example 16 with GroovyFile

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

the class GrIntroduceFieldProcessor method initializeInMethod.

void initializeInMethod(@NotNull GrVariable field, @NotNull List<PsiElement> replaced) {
    final PsiElement _scope = myContext.getScope();
    final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass) _scope).getContainingFile() : _scope;
    final PsiElement place = replaced.get(0);
    final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
    GrStatementOwner container = member instanceof GrMethod ? ((GrMethod) member).getBlock() : member instanceof GrClassInitializer ? ((GrClassInitializer) member).getBlock() : place.getContainingFile() instanceof GroovyFile ? ((GroovyFile) place.getContainingFile()) : null;
    assert container != null;
    final PsiElement anchor;
    if (mySettings.removeLocalVar()) {
        GrVariable variable = myLocalVariable;
        anchor = PsiTreeUtil.getParentOfType(variable, GrStatement.class);
    } else {
        anchor = GrIntroduceHandlerBase.findAnchor(replaced.toArray(new PsiElement[replaced.size()]), container);
        GrIntroduceHandlerBase.assertStatement(anchor, myContext.getScope());
    }
    initializeInMethodInner(field, container, (GrStatement) anchor, replaced.get(0));
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 17 with GroovyFile

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

the class GrIntroduceFieldProcessor method insertField.

@NotNull
protected GrVariableDeclaration insertField(@NotNull PsiClass targetClass) {
    GrVariableDeclaration declaration = createField(targetClass);
    if (targetClass instanceof GrEnumTypeDefinition) {
        final GrEnumConstantList enumConstants = ((GrEnumTypeDefinition) targetClass).getEnumConstantList();
        return (GrVariableDeclaration) targetClass.addAfter(declaration, enumConstants);
    }
    if (targetClass instanceof GrTypeDefinition) {
        PsiElement anchor = getAnchorForDeclaration((GrTypeDefinition) targetClass);
        return (GrVariableDeclaration) targetClass.addAfter(declaration, anchor);
    } else {
        assert targetClass instanceof GroovyScriptClass;
        final GroovyFile file = ((GroovyScriptClass) targetClass).getContainingFile();
        PsiElement[] elements = file.getMethods();
        if (elements.length == 0)
            elements = file.getStatements();
        final PsiElement anchor = ArrayUtil.getFirstElement(elements);
        return (GrVariableDeclaration) file.addBefore(declaration, anchor);
    }
}
Also used : GrEnumConstantList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstantList) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with GroovyFile

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

the class GrIntroduceConstantDialog method initializeTargetClassEditor.

private void initializeTargetClassEditor() {
    myTargetClassEditor = new ReferenceEditorComboWithBrowseButton(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            TreeClassChooser chooser = TreeClassChooserFactory.getInstance(myContext.getProject()).createWithInnerClassesScopeChooser(RefactoringBundle.message("choose.destination.class"), GlobalSearchScope.projectScope(myContext.getProject()), new ClassFilter() {

                @Override
                public boolean isAccepted(PsiClass aClass) {
                    return aClass.getParent() instanceof GroovyFile || aClass.hasModifierProperty(PsiModifier.STATIC);
                }
            }, null);
            if (myTargetClass != null) {
                chooser.selectDirectory(myTargetClass.getContainingFile().getContainingDirectory());
            }
            chooser.showDialog();
            PsiClass aClass = chooser.getSelected();
            if (aClass != null) {
                myTargetClassEditor.setText(aClass.getQualifiedName());
            }
        }
    }, "", myContext.getProject(), true, RECENTS_KEY);
    myTargetClassPanel.setLayout(new BorderLayout());
    myTargetClassPanel.add(myTargetClassLabel, BorderLayout.NORTH);
    myTargetClassPanel.add(myTargetClassEditor, BorderLayout.CENTER);
    Set<String> possibleClassNames = new LinkedHashSet<>();
    for (final PsiElement occurrence : myContext.getOccurrences()) {
        final PsiClass parentClass = getParentClass(occurrence);
        if (parentClass != null && parentClass.getQualifiedName() != null) {
            possibleClassNames.add(parentClass.getQualifiedName());
        }
    }
    for (String possibleClassName : possibleClassNames) {
        myTargetClassEditor.prependItem(possibleClassName);
    }
    if (myDefaultTargetClass != null) {
        myTargetClassEditor.prependItem(myDefaultTargetClass.getQualifiedName());
    }
    myTargetClassEditor.getChildComponent().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            targetClassChanged();
            updateOkStatus();
        // enableEnumDependant(introduceEnumConstant());
        }
    });
}
Also used : ActionEvent(java.awt.event.ActionEvent) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) ReferenceEditorComboWithBrowseButton(com.intellij.ui.ReferenceEditorComboWithBrowseButton) ActionListener(java.awt.event.ActionListener) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 19 with GroovyFile

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

the class GroovyImportOptimizerRefactoringHelper method performOperation.

@Override
public void performOperation(final Project project, final Set<GroovyFile> files) {
    final ProgressManager progressManager = ProgressManager.getInstance();
    final Map<GroovyFile, Pair<List<GrImportStatement>, Set<GrImportStatement>>> redundants = new HashMap<>();
    final Runnable findUnusedImports = () -> {
        final ProgressIndicator progressIndicator = progressManager.getProgressIndicator();
        final int total = files.size();
        int i = 0;
        for (final GroovyFile file : files) {
            if (!file.isValid())
                continue;
            final VirtualFile virtualFile = file.getVirtualFile();
            if (!ProjectRootManager.getInstance(project).getFileIndex().isInSource(virtualFile)) {
                continue;
            }
            if (progressIndicator != null) {
                progressIndicator.setText2(virtualFile.getPresentableUrl());
                progressIndicator.setFraction((double) i++ / total);
            }
            ApplicationManager.getApplication().runReadAction(() -> {
                final Set<GrImportStatement> usedImports = GroovyImportUtil.findUsedImports(file);
                final List<GrImportStatement> validImports = PsiUtil.getValidImportStatements(file);
                redundants.put(file, Pair.create(validImports, usedImports));
            });
        }
    };
    if (!progressManager.runProcessWithProgressSynchronously(findUnusedImports, "Optimizing imports (Groovy) ... ", false, project)) {
        return;
    }
    WriteAction.run(() -> {
        for (GroovyFile groovyFile : redundants.keySet()) {
            if (!groovyFile.isValid())
                continue;
            final Pair<List<GrImportStatement>, Set<GrImportStatement>> pair = redundants.get(groovyFile);
            final List<GrImportStatement> validImports = pair.getFirst();
            final Set<GrImportStatement> usedImports = pair.getSecond();
            for (GrImportStatement importStatement : validImports) {
                if (!usedImports.contains(importStatement)) {
                    groovyFile.removeImport(importStatement);
                }
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Set(java.util.Set) HashSet(com.intellij.util.containers.hash.HashSet) HashMap(java.util.HashMap) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) ProgressManager(com.intellij.openapi.progress.ProgressManager) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Pair(com.intellij.openapi.util.Pair)

Example 20 with GroovyFile

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

the class GroovyRefactoringSupportProvider method isMemberInplaceRenameAvailable.

@Override
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement element, @Nullable PsiElement context) {
    if (context == null || context.getContainingFile() instanceof GroovyFile)
        return false;
    PsiElement parent = context.getParent();
    //don't try to inplace rename aliased imported references
    if (parent instanceof GrReferenceElement) {
        GroovyResolveResult result = ((GrReferenceElement) parent).advancedResolve();
        PsiElement fileResolveContext = result.getCurrentFileResolveContext();
        if (fileResolveContext instanceof GrImportStatement && ((GrImportStatement) fileResolveContext).isAliasedImport()) {
            return false;
        }
    }
    return element instanceof GrMember;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)91 PsiFile (com.intellij.psi.PsiFile)26 PsiElement (com.intellij.psi.PsiElement)21 NotNull (org.jetbrains.annotations.NotNull)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Project (com.intellij.openapi.project.Project)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)10 PsiClass (com.intellij.psi.PsiClass)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 Module (com.intellij.openapi.module.Module)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5