Search in sources :

Example 31 with GroovyFile

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

the class GrCucumberExtension method getGlue.

@Nullable
private String getGlue(PsiElement stepDefinition) {
    if (stepDefinition != null && stepDefinition instanceof GrMethodCall) {
        GroovyFile groovyFile = (GroovyFile) stepDefinition.getContainingFile();
        VirtualFile vfile = groovyFile.getVirtualFile();
        if (vfile != null) {
            VirtualFile parentDir = vfile.getParent();
            return PathUtil.getLocalPath(parentDir);
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project android by JetBrains.

the class GradleFilePsiMerger method mergeGradleFiles.

/**
   * Merges the given source build.gradle content into the given destination build.gradle content,
   * and resolves and dynamic Gradle dependencies into specific versions. If a support library
   * filter is provided, the support libraries will be limited to match that filter. This is
   * typically set to the compileSdkVersion, such that you don't end up mixing and matching
   * compileSdkVersions and support libraries from different versions, which is not supported.
   */
public static String mergeGradleFiles(@NotNull String source, @NotNull String dest, @Nullable Project project, @Nullable final String supportLibVersionFilter) {
    source = source.replace("\r", "");
    dest = dest.replace("\r", "");
    final Project project2;
    boolean projectNeedsCleanup = false;
    if (project != null && !project.isDefault()) {
        project2 = project;
    } else {
        project2 = ((ProjectManagerImpl) ProjectManager.getInstance()).newProject("MergingOnly", "", false, true);
        assert project2 != null;
        ((StartupManagerImpl) StartupManager.getInstance(project2)).runStartupActivities();
        projectNeedsCleanup = true;
    }
    final GroovyFile templateBuildFile = (GroovyFile) PsiFileFactory.getInstance(project2).createFileFromText(SdkConstants.FN_BUILD_GRADLE, GroovyFileType.GROOVY_FILE_TYPE, source);
    final GroovyFile existingBuildFile = (GroovyFile) PsiFileFactory.getInstance(project2).createFileFromText(SdkConstants.FN_BUILD_GRADLE, GroovyFileType.GROOVY_FILE_TYPE, dest);
    String result = (new WriteCommandAction<String>(project2, "Merge Gradle Files", existingBuildFile) {

        @Override
        protected void run(@NotNull Result<String> result) throws Throwable {
            mergePsi(templateBuildFile, existingBuildFile, project2, supportLibVersionFilter);
            PsiElement formatted = CodeStyleManager.getInstance(project2).reformat(existingBuildFile);
            result.setResult(formatted.getText());
        }
    }).execute().getResultObject();
    if (projectNeedsCleanup) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            Disposer.dispose(project2);
        });
    }
    return result;
}
Also used : Project(com.intellij.openapi.project.Project) StartupManagerImpl(com.intellij.ide.startup.impl.StartupManagerImpl) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement)

Example 33 with GroovyFile

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

the class BaseScriptAnnotationChecker method checkApplicability.

@Override
public boolean checkApplicability(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    if (GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT.equals(annotation.getQualifiedName())) {
        PsiFile file = annotation.getContainingFile();
        if (file instanceof GroovyFile && !(((GroovyFile) file).isScript())) {
            holder.createErrorAnnotation(annotation, GroovyBundle.message("base.script.annotation.is.allowed.only.inside.scripts"));
            return true;
        }
        PsiElement pparent = annotation.getParent().getParent();
        if (pparent instanceof GrVariableDeclaration) {
            GrTypeElement typeElement = ((GrVariableDeclaration) pparent).getTypeElementGroovy();
            PsiType type = typeElement != null ? typeElement.getType() : null;
            if (!InheritanceUtil.isInheritor(type, GroovyCommonClassNames.GROOVY_LANG_SCRIPT)) {
                String typeText = type != null ? type.getCanonicalText() : CommonClassNames.JAVA_LANG_OBJECT;
                holder.createErrorAnnotation(annotation, GroovyBundle.message("declared.type.0.have.to.extend.script", typeText));
                return true;
            }
        }
    }
    return false;
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 34 with GroovyFile

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

the class GroovyFileIconProvider method getIcon.

@Nullable
@Override
public Icon getIcon(@NotNull VirtualFile virtualFile, @Iconable.IconFlags int flags, @Nullable Project project) {
    if (project == null || virtualFile.getFileType() != GroovyFileType.GROOVY_FILE_TYPE)
        return null;
    final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
    if (!(psiFile instanceof GroovyFile))
        return null;
    final GroovyFile file = (GroovyFile) psiFile;
    final Icon icon;
    if (file.isScript()) {
        icon = GroovyScriptTypeDetector.getIcon(file);
    } else if (GrFileIndexUtil.isGroovySourceFile(file)) {
        final GrTypeDefinition[] typeDefinitions = file.getTypeDefinitions();
        icon = typeDefinitions.length > 0 ? typeDefinitions[0].getIcon(flags) : JetgroovyIcons.Groovy.GroovyFile;
    } else {
        icon = JetgroovyIcons.Groovy.Groovy_outsideSources;
    }
    return ElementBase.createLayeredIcon(psiFile, icon, ElementBase.transformFlags(psiFile, flags));
}
Also used : PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Nullable(org.jetbrains.annotations.Nullable)

Example 35 with GroovyFile

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

the class GrPackageInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitFile(@NotNull GroovyFileBase file) {
            if (!(file instanceof GroovyFile))
                return;
            if (!myCheckScripts && file.isScript())
                return;
            String expectedPackage = ExpectedPackageNameProviderKt.inferExpectedPackageName((GroovyFile) file);
            String actual = file.getPackageName();
            if (!expectedPackage.equals(actual)) {
                PsiElement toHighlight = getElementToHighlight((GroovyFile) file);
                if (toHighlight == null)
                    return;
                registerError(toHighlight, "Package name mismatch. Actual: '" + actual + "', expected: '" + expectedPackage + "'", new LocalQuickFix[] { new ChangePackageQuickFix(expectedPackage), GroovyQuickFixFactory.getInstance().createGrMoveToDirFix(actual) }, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) NotNull(org.jetbrains.annotations.NotNull) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

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