Search in sources :

Example 61 with GroovyFile

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

the class GradlePositionManager method getRuntimeScriptName.

@NotNull
public String getRuntimeScriptName(@NotNull GroovyFile groovyFile) {
    VirtualFile virtualFile = groovyFile.getVirtualFile();
    if (virtualFile == null)
        return "";
    final Module module = ModuleUtilCore.findModuleForPsiElement(groovyFile);
    if (module == null) {
        return "";
    }
    final File scriptFile = VfsUtilCore.virtualToIoFile(virtualFile);
    final String className = CachedValuesManager.getManager(module.getProject()).getCachedValue(module, GRADLE_CLASS_NAME, new ScriptSourceMapCalculator(module), false).get(scriptFile);
    return className == null ? "" : className;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with GroovyFile

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

the class CreateClassFix method createClassFixAction.

public static IntentionAction createClassFixAction(final GrReferenceElement refElement, GrCreateClassKind type) {
    return new CreateClassActionBase(type, refElement) {

        @Override
        protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
            final PsiFile file = element.getContainingFile();
            if (!(file instanceof GroovyFileBase))
                return;
            GroovyFileBase groovyFile = (GroovyFileBase) file;
            PsiElement qualifier = myRefElement.getQualifier();
            if (qualifier == null || qualifier instanceof GrReferenceElement && ((GrReferenceElement) qualifier).resolve() instanceof PsiPackage) {
                createTopLevelClass(project, groovyFile);
            } else {
                createInnerClass(project, editor, qualifier);
            }
        }

        private void createInnerClass(Project project, final Editor editor, PsiElement qualifier) {
            PsiElement resolved = resolveQualifier(qualifier);
            if (!(resolved instanceof PsiClass))
                return;
            JVMElementFactory factory = JVMElementFactories.getFactory(resolved.getLanguage(), project);
            if (factory == null)
                return;
            String name = myRefElement.getReferenceName();
            PsiClass template = createTemplate(factory, name);
            if (template == null) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    if (editor != null && editor.getComponent().isDisplayable()) {
                        HintManager.getInstance().showErrorHint(editor, GroovyIntentionsBundle.message("cannot.create.class"));
                    }
                });
                return;
            }
            if (!FileModificationService.getInstance().preparePsiElementForWrite(resolved))
                return;
            WriteAction.run(() -> {
                PsiClass added = (PsiClass) resolved.add(template);
                PsiModifierList modifierList = added.getModifierList();
                if (modifierList != null) {
                    modifierList.setModifierProperty(PsiModifier.STATIC, true);
                }
                IntentionUtils.positionCursor(project, added.getContainingFile(), added);
            });
        }

        @Nullable
        private PsiElement resolveQualifier(@NotNull PsiElement qualifier) {
            if (qualifier instanceof GrCodeReferenceElement) {
                return ((GrCodeReferenceElement) qualifier).resolve();
            } else if (qualifier instanceof GrExpression) {
                PsiType type = ((GrExpression) qualifier).getType();
                if (type instanceof PsiClassType) {
                    return ((PsiClassType) type).resolve();
                } else if (qualifier instanceof GrReferenceExpression) {
                    final PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
                    if (resolved instanceof PsiClass || resolved instanceof PsiPackage) {
                        return resolved;
                    }
                }
            }
            return null;
        }

        @Nullable
        private PsiClass createTemplate(JVMElementFactory factory, String name) {
            switch(getType()) {
                case ENUM:
                    return factory.createEnum(name);
                case TRAIT:
                    if (factory instanceof GroovyPsiElementFactory) {
                        return ((GroovyPsiElementFactory) factory).createTrait(name);
                    } else {
                        return null;
                    }
                case CLASS:
                    return factory.createClass(name);
                case INTERFACE:
                    return factory.createInterface(name);
                case ANNOTATION:
                    return factory.createAnnotationType(name);
                default:
                    return null;
            }
        }

        private void createTopLevelClass(@NotNull Project project, @NotNull GroovyFileBase file) {
            final String pack = getPackage(file);
            final PsiManager manager = PsiManager.getInstance(project);
            final String name = myRefElement.getReferenceName();
            assert name != null;
            final Module module = ModuleUtilCore.findModuleForPsiElement(file);
            PsiDirectory targetDirectory = getTargetDirectory(project, pack, name, module, getText());
            if (targetDirectory == null)
                return;
            String templateName = getTemplateName(getType());
            final PsiClass targetClass = createClassByType(targetDirectory, name, manager, myRefElement, templateName, true);
            if (targetClass == null)
                return;
            bindRef(targetClass, myRefElement);
            IntentionUtils.positionCursor(project, targetClass.getContainingFile(), targetClass);
        }

        @NotNull
        private String getPackage(@NotNull PsiClassOwner file) {
            final PsiElement qualifier = myRefElement.getQualifier();
            if (qualifier instanceof GrReferenceElement) {
                final PsiElement resolved = ((GrReferenceElement) qualifier).resolve();
                if (resolved instanceof PsiPackage) {
                    return ((PsiPackage) resolved).getQualifiedName();
                }
            }
            return file instanceof GroovyFile ? file.getPackageName() : "";
        }

        @Override
        public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
            if (!super.isAvailable(project, editor, file))
                return false;
            final PsiElement qualifier = myRefElement.getQualifier();
            if (qualifier != null && resolveQualifier(qualifier) == null) {
                return false;
            }
            return true;
        }
    };
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) Editor(com.intellij.openapi.editor.Editor) Module(com.intellij.openapi.module.Module) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 63 with GroovyFile

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

the class CreateClassFix method createClassFromNewAction.

public static IntentionAction createClassFromNewAction(final GrNewExpression expression) {
    return new CreateClassActionBase(GrCreateClassKind.CLASS, expression.getReferenceElement()) {

        @Override
        protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
            final PsiFile file = element.getContainingFile();
            if (!(file instanceof GroovyFileBase))
                return;
            GroovyFileBase groovyFile = (GroovyFileBase) file;
            final PsiManager manager = myRefElement.getManager();
            final String qualifier;
            final String name;
            final Module module;
            final AccessToken accessToken = ReadAction.start();
            try {
                qualifier = groovyFile instanceof GroovyFile ? groovyFile.getPackageName() : "";
                name = myRefElement.getReferenceName();
                assert name != null;
                module = ModuleUtilCore.findModuleForPsiElement(file);
            } finally {
                accessToken.finish();
            }
            PsiDirectory targetDirectory = getTargetDirectory(project, qualifier, name, module, getText());
            if (targetDirectory == null)
                return;
            final GrTypeDefinition targetClass = createClassByType(targetDirectory, name, manager, myRefElement, GroovyTemplates.GROOVY_CLASS, true);
            if (targetClass == null)
                return;
            PsiType[] argTypes = getArgTypes(myRefElement);
            if (argTypes != null && argTypes.length > 0) {
                generateConstructor(myRefElement, name, argTypes, targetClass, project);
                bindRef(targetClass, myRefElement);
            } else {
                bindRef(targetClass, myRefElement);
                IntentionUtils.positionCursor(project, targetClass.getContainingFile(), targetClass);
            }
        }
    };
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) AccessToken(com.intellij.openapi.application.AccessToken) Editor(com.intellij.openapi.editor.Editor) Module(com.intellij.openapi.module.Module) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 64 with GroovyFile

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

the class GroovyOptimizeImportsFix method containsErrorsPreventingOptimize.

private static boolean containsErrorsPreventingOptimize(GroovyFile myFile, Document myDocument) {
    // ignore unresolved imports errors
    final TextRange ignoreRange;
    final GrImportStatement[] imports = myFile.getImportStatements();
    if (imports.length != 0) {
        final int start = imports[0].getTextRange().getStartOffset();
        final int end = imports[imports.length - 1].getTextRange().getEndOffset();
        ignoreRange = new TextRange(start, end);
    } else {
        ignoreRange = TextRange.EMPTY_RANGE;
    }
    return !DaemonCodeAnalyzerEx.processHighlights(myDocument, myFile.getProject(), HighlightSeverity.ERROR, 0, myDocument.getTextLength(), error -> {
        int infoStart = error.getActualStartOffset();
        int infoEnd = error.getActualEndOffset();
        return ignoreRange.containsRange(infoStart, infoEnd) && error.type.equals(HighlightInfoType.WRONG_REF);
    });
}
Also used : CodeInsightWorkspaceSettings(com.intellij.codeInsight.CodeInsightWorkspaceSettings) DaemonListeners(com.intellij.codeInsight.daemon.impl.DaemonListeners) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) DaemonCodeAnalyzer(com.intellij.codeInsight.daemon.DaemonCodeAnalyzer) DaemonCodeAnalyzerEx(com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx) DocumentUtil(com.intellij.util.DocumentUtil) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) GroovyImportOptimizer(org.jetbrains.plugins.groovy.editor.GroovyImportOptimizer) UndoManager(com.intellij.openapi.command.undo.UndoManager) Attachment(com.intellij.openapi.diagnostic.Attachment) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) DaemonCodeAnalyzerImpl(com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl) NotNull(org.jetbrains.annotations.NotNull) TransactionGuard(com.intellij.openapi.application.TransactionGuard) TextRange(com.intellij.openapi.util.TextRange) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)

Example 65 with GroovyFile

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

the class GrMoveToDirFix method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    PsiFile file = descriptor.getPsiElement().getContainingFile();
    if (!(file instanceof GroovyFile))
        return;
    VirtualFile vfile = file.getVirtualFile();
    if (vfile == null)
        return;
    final Module module = ModuleUtilCore.findModuleForFile(vfile, project);
    if (module == null)
        return;
    final String packageName = ((GroovyFile) file).getPackageName();
    PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(module, packageName, null, true);
    if (directory == null)
        return;
    String error = RefactoringMessageUtil.checkCanCreateFile(directory, file.getName());
    if (error != null) {
        Messages.showMessageDialog(project, error, CommonBundle.getErrorTitle(), Messages.getErrorIcon());
        return;
    }
    new MoveFilesOrDirectoriesProcessor(project, new PsiElement[] { file }, directory, false, false, false, null, null).run();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) MoveFilesOrDirectoriesProcessor(com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesProcessor) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement)

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