Search in sources :

Example 1 with PerlUseStatementElement

use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.

the class PerlUseWarningsInspection method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PerlVisitor() {

        @Override
        public void visitFile(@NotNull PsiFile file) {
            if (!file.isWritable() || !file.isPhysical() || file.getVirtualFile() instanceof LightVirtualFile || InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) {
                return;
            }
            FileType fileType = file.getFileType();
            if (!(fileType instanceof PerlFileType) || !((PerlFileType) fileType).checkWarningsPragma()) {
                return;
            }
            for (PerlUseStatementElement useStatement : PsiTreeUtil.findChildrenOfType(file, PerlUseStatementElement.class)) {
                if (useStatement.getPackageProcessor() instanceof PerlWarningsProvider) {
                    return;
                }
            }
            holder.registerProblem(file, PerlBundle.message("perl.inspection.use.warnings"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new PerlUsePackageQuickFix("warnings FATAL => 'all'"), new PerlUsePackageQuickFix("warnings"));
        }
    };
}
Also used : PerlFileType(com.perl5.lang.perl.fileTypes.PerlFileType) PerlUsePackageQuickFix(com.perl5.lang.perl.idea.quickfixes.PerlUsePackageQuickFix) PerlUseStatementElement(com.perl5.lang.perl.psi.impl.PerlUseStatementElement) FileType(com.intellij.openapi.fileTypes.FileType) PerlFileType(com.perl5.lang.perl.fileTypes.PerlFileType) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiFile(com.intellij.psi.PsiFile) PerlVisitor(com.perl5.lang.perl.psi.PerlVisitor) PerlWarningsProvider(com.perl5.lang.perl.extensions.packageprocessor.PerlWarningsProvider) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PerlUseStatementElement

use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.

the class PerlUnresolvedPackageFileInspection method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PerlVisitor() {

        @Override
        public void visitUseStatement(@NotNull PerlUseStatementElement o) {
            if (o.getNamespaceElement() != null) {
                checkPackageFile(o.getNamespaceElement());
            }
        }

        @Override
        public void visitNoStatement(@NotNull PerlNoStatementElement o) {
            if (o.getNamespaceElement() != null) {
                checkPackageFile(o.getNamespaceElement());
            }
        }

        @Override
        public void visitRequireExpr(@NotNull PsiPerlRequireExpr o) {
            if (o.getNamespaceElement() != null) {
                checkPackageFile(o.getNamespaceElement());
            }
        }

        public void checkPackageFile(PerlNamespaceElement o) {
            List<PerlFileImpl> namespaceFiles = o.getNamespaceFiles();
            String packageName = o.getCanonicalName();
            if (namespaceFiles.isEmpty() && StringUtil.isNotEmpty(packageName)) {
                List<LocalQuickFix> fixes = new ArrayList<>();
                Project project = o.getProject();
                Sdk perlSdk = PerlProjectManager.getSdk(project);
                if (perlSdk != null) {
                    if (CpanminusAdapter.isAvailable(perlSdk)) {
                        fixes.add(new InstallPackageQuickfix(new CpanminusAdapter(perlSdk, project), packageName));
                    }
                    fixes.add(new InstallPackageQuickfix(new CpanAdapter(perlSdk, project), packageName));
                }
                registerProblem(holder, o, PerlBundle.message("perl.inspection.missing.package.file", packageName), fixes.toArray(LocalQuickFix.EMPTY_ARRAY));
            }
        }
    };
}
Also used : PerlUseStatementElement(com.perl5.lang.perl.psi.impl.PerlUseStatementElement) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) CpanAdapter(com.perl5.lang.perl.adapters.CpanAdapter) ArrayList(java.util.ArrayList) PerlNamespaceElement(com.perl5.lang.perl.psi.PerlNamespaceElement) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PerlVisitor(com.perl5.lang.perl.psi.PerlVisitor) NotNull(org.jetbrains.annotations.NotNull) CpanminusAdapter(com.perl5.lang.perl.adapters.CpanminusAdapter) Project(com.intellij.openapi.project.Project) PsiPerlRequireExpr(com.perl5.lang.perl.psi.PsiPerlRequireExpr) PerlNoStatementElement(com.perl5.lang.perl.psi.impl.PerlNoStatementElement) Sdk(com.intellij.openapi.projectRoots.Sdk) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PerlUseStatementElement

use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.

the class PerlStringCompletionUtil method fillWithUseParameters.

public static void fillWithUseParameters(@NotNull PerlCompletionProcessor completionProcessor) {
    PsiElement baseElement = completionProcessor.getLeafElement();
    PerlUseStatementElement useStatement = PsiTreeUtil.getParentOfType(baseElement, PerlUseStatementElement.class, true, PsiPerlStatement.class);
    if (useStatement == null) {
        return;
    }
    List<String> typedParameters = useStatement.getImportParameters();
    Set<String> typedStringsSet = typedParameters == null ? Collections.emptySet() : new THashSet<>(typedParameters);
    PerlPackageProcessor packageProcessor = useStatement.getPackageProcessor();
    // fixme we should allow lookup elements customization by package processor
    if (packageProcessor instanceof PerlPackageOptionsProvider) {
        Map<String, String> options = ((PerlPackageOptionsProvider) packageProcessor).getOptions();
        for (Map.Entry<String, String> option : options.entrySet()) {
            String lookupString = option.getKey();
            if (!typedStringsSet.contains(lookupString) && completionProcessor.matches(lookupString) && !completionProcessor.process(LookupElementBuilder.create(lookupString).withTypeText(option.getValue(), true).withIcon(PerlIcons.PERL_OPTION))) {
                return;
            }
        }
        options = ((PerlPackageOptionsProvider) packageProcessor).getOptionsBundles();
        for (Map.Entry<String, String> option : options.entrySet()) {
            String lookupString = option.getKey();
            if (!typedStringsSet.contains(lookupString) && completionProcessor.matches(lookupString) && !completionProcessor.process(LookupElementBuilder.create(lookupString).withTypeText(option.getValue(), true).withIcon(PerlIcons.PERL_OPTIONS))) {
                return;
            }
        }
    }
    if (packageProcessor instanceof PerlPackageParentsProvider && ((PerlPackageParentsProvider) packageProcessor).hasPackageFilesOptions() && !PerlPackageUtil.processPackageFilesForPsiElement(baseElement, (packageName, file) -> typedStringsSet.contains(packageName) || PerlPackageCompletionUtil.processPackageLookupElement(file, packageName, null, completionProcessor, false))) {
        return;
    }
    Set<String> export = new HashSet<>();
    Set<String> exportOk = new HashSet<>();
    packageProcessor.addExports(useStatement, export, exportOk);
    exportOk.removeAll(export);
    for (String subName : export) {
        if (!typedStringsSet.contains(subName) && completionProcessor.matches(subName) && !completionProcessor.process(LookupElementBuilder.create(subName).withIcon(PerlIcons.SUB_GUTTER_ICON).withTypeText("default", true))) {
            return;
        }
    }
    for (String subName : exportOk) {
        if (!typedStringsSet.contains(subName) && completionProcessor.matches(subName) && !completionProcessor.process(LookupElementBuilder.create(subName).withIcon(PerlIcons.SUB_GUTTER_ICON).withTypeText("optional", true))) {
            return;
        }
    }
}
Also used : PerlPackageParentsProvider(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageParentsProvider) PerlUseStatementElement(com.perl5.lang.perl.psi.impl.PerlUseStatementElement) PerlPackageOptionsProvider(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageOptionsProvider) PerlPackageProcessor(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor) PsiElement(com.intellij.psi.PsiElement) THashSet(gnu.trove.THashSet)

Example 4 with PerlUseStatementElement

use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.

the class ExceptionClassProcessor method processExceptionElement.

private void processExceptionElement(@NotNull List<PsiElement> listElements, int currentIndex, @NotNull List<PerlDelegatingLightNamedElement<?>> result, @NotNull PerlUseStatementElement useStatementElement) {
    PsiElement listElement = listElements.get(currentIndex);
    if (!useStatementElement.isAcceptableIdentifierElement(listElement)) {
        return;
    }
    String namespaceName = ElementManipulators.getValueText(listElement);
    if (StringUtil.isEmpty(namespaceName)) {
        return;
    }
    Map<String, PerlHashEntry> exceptionSettings = listElements.size() > currentIndex + 1 ? PerlHashUtil.collectHashMap(listElements.get(currentIndex + 1)) : Collections.emptyMap();
    // Building fields
    Set<PerlSubArgument> throwArguments = Collections.emptySet();
    PerlHashEntry fieldsEntry = exceptionSettings.get("fields");
    if (fieldsEntry != null && fieldsEntry.isComplete()) {
        PsiElement fieldsContainer = fieldsEntry.getNonNullValueElement();
        if (fieldsContainer instanceof PsiPerlAnonArray) {
            fieldsContainer = ((PsiPerlAnonArray) fieldsContainer).getExpr();
        }
        List<PsiElement> elements = PerlArrayUtil.collectListElements(fieldsContainer);
        if (!elements.isEmpty()) {
            // Fields method
            result.add(new PerlLightMethodDefinitionElement<>(useStatementElement, FIELDS_METHOD_NAME, LIGHT_METHOD_DEFINITION, fieldsEntry.keyElement, namespaceName, Collections.emptyList(), null));
            // fields themselves
            throwArguments = new LinkedHashSet<>();
            for (PsiElement fieldElement : elements) {
                if (useStatementElement.isAcceptableIdentifierElement(fieldElement)) {
                    String fieldName = PerlScalarUtil.getStringContent(fieldElement);
                    if (StringUtil.isNotEmpty(fieldName)) {
                        throwArguments.add(PerlSubArgument.mandatoryScalar(fieldName));
                        result.add(new PerlLightMethodDefinitionElement<>(useStatementElement, fieldName, LIGHT_METHOD_DEFINITION, fieldElement, namespaceName, Collections.emptyList(), null));
                    }
                }
            }
        }
    }
    // making exception class
    PerlHashEntry isaEntry = exceptionSettings.get("isa");
    String parentClass = "Exception::Class::Base";
    if (isaEntry != null && isaEntry.isComplete()) {
        String manualIsa = isaEntry.getValueString();
        if (manualIsa != null) {
            parentClass = manualIsa;
        }
    }
    result.add(new PerlLightExceptionClassDefinition(useStatementElement, namespaceName, LIGHT_NAMESPACE_DEFINITION, listElement, PerlMroType.DFS, Collections.singletonList(parentClass), PerlNamespaceAnnotations.tryToFindAnnotations(listElement, useStatementElement), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap()));
    // making alias
    PerlHashEntry aliasEntry = exceptionSettings.get("alias");
    if (aliasEntry != null && aliasEntry.isComplete()) {
        if (useStatementElement.isAcceptableIdentifierElement(aliasEntry.valueElement)) {
            String aliasName = aliasEntry.getValueString();
            if (StringUtil.isNotEmpty(aliasName)) {
                result.add(new PerlLightSubDefinitionElement<>(useStatementElement, aliasName, LIGHT_SUB_DEFINITION, aliasEntry.getNonNullValueElement(), PerlPackageUtil.getContextNamespaceName(useStatementElement), new ArrayList<>(throwArguments), PerlSubAnnotations.tryToFindAnnotations(aliasEntry.keyElement, aliasEntry.valueElement), UNKNOWN_VALUE_PROVIDER, null));
            }
        }
    }
}
Also used : PerlLightExceptionClassDefinition(com.perl5.lang.perl.parser.Exception.Class.psi.light.PerlLightExceptionClassDefinition) PerlSubArgument(com.perl5.lang.perl.psi.utils.PerlSubArgument) PsiPerlAnonArray(com.perl5.lang.perl.psi.PsiPerlAnonArray) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PerlUseStatementElement

use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.

the class PerlPackageProcessorBase method getImports.

@Override
@NotNull
public List<PerlExportDescriptor> getImports(@NotNull PerlUseStatementElement useStatement) {
    String packageName = useStatement.getPackageName();
    if (packageName == null) {
        return Collections.emptyList();
    }
    PerlTimeLogger logger = PerlTimeLogger.create(LOG);
    List<PerlExportDescriptor> result = new ArrayList<>();
    List<String> importParameters = getImportParameters(useStatement);
    Set<String> exportNames = new HashSet<>();
    Set<String> exportOkNames = new HashSet<>();
    addExports(useStatement, exportNames, exportOkNames);
    if (importParameters == null) {
        exportNames.forEach(name -> result.add(createDescriptor(packageName, name)));
    } else {
        importParameters.stream().filter(exportOkNames::contains).forEach(name -> result.add(createDescriptor(packageName, name)));
    }
    logger.debug("Collected imports for ", packageName);
    return result;
}
Also used : PerlTimeLogger(com.perl5.lang.perl.util.PerlTimeLogger) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PerlUseStatementElement (com.perl5.lang.perl.psi.impl.PerlUseStatementElement)9 NotNull (org.jetbrains.annotations.NotNull)8 PsiElement (com.intellij.psi.PsiElement)5 PerlVisitor (com.perl5.lang.perl.psi.PerlVisitor)4 PsiFile (com.intellij.psi.PsiFile)3 PerlPackageProcessor (com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor)3 PerlDelegatingLightNamedElement (com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement)3 FileType (com.intellij.openapi.fileTypes.FileType)2 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)2 PerlFileType (com.perl5.lang.perl.fileTypes.PerlFileType)2 PerlUsePackageQuickFix (com.perl5.lang.perl.idea.quickfixes.PerlUsePackageQuickFix)2 PerlNamespaceDefinitionElement (com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)2 PsiPerlExpr (com.perl5.lang.perl.psi.PsiPerlExpr)2 ArrayList (java.util.ArrayList)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 AnnotationHolder (com.intellij.lang.annotation.AnnotationHolder)1 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)1 CodeInsightColors (com.intellij.openapi.editor.colors.CodeInsightColors)1 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 Project (com.intellij.openapi.project.Project)1