Search in sources :

Example 1 with PerlPackageProcessor

use of com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor in project Perl5-IDEA by Camelcade.

the class PerlStringCompletionUtil method fillWithUseParameters.

public static void fillWithUseParameters(@NotNull final PsiElement stringContentElement, @NotNull final CompletionResultSet resultSet) {
    @SuppressWarnings("unchecked") PsiPerlUseStatement useStatement = PsiTreeUtil.getParentOfType(stringContentElement, PsiPerlUseStatement.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()) {
            if (!typedStringsSet.contains(option.getKey())) {
                resultSet.addElement(LookupElementBuilder.create(option.getKey()).withTypeText(option.getValue(), true).withIcon(PerlIcons.PERL_OPTION));
            }
        }
        options = ((PerlPackageOptionsProvider) packageProcessor).getOptionsBundles();
        for (Map.Entry<String, String> option : options.entrySet()) {
            if (!typedStringsSet.contains(option.getKey())) {
                resultSet.addElement(LookupElementBuilder.create(option.getKey()).withTypeText(option.getValue(), true).withIcon(PerlIcons.PERL_OPTIONS));
            }
        }
    }
    if (packageProcessor instanceof PerlPackageParentsProvider && ((PerlPackageParentsProvider) packageProcessor).hasPackageFilesOptions()) {
        PerlPackageUtil.processPackageFilesForPsiElement(stringContentElement, s -> {
            if (!typedStringsSet.contains(s)) {
                resultSet.addElement(PerlPackageCompletionUtil.getPackageLookupElement(s, null));
            }
            return true;
        });
    }
    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)) {
            resultSet.addElement(LookupElementBuilder.create(subName).withIcon(PerlIcons.SUB_GUTTER_ICON).withTypeText("default", true));
        }
    }
    for (String subName : exportOk) {
        if (!typedStringsSet.contains(subName)) {
            resultSet.addElement(LookupElementBuilder.create(subName).withIcon(PerlIcons.SUB_GUTTER_ICON).withTypeText("optional", true));
        }
    }
}
Also used : PerlPackageParentsProvider(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageParentsProvider) PerlPackageOptionsProvider(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageOptionsProvider) PerlPackageProcessor(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor) THashSet(gnu.trove.THashSet)

Example 2 with PerlPackageProcessor

use of com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor in project Perl5-IDEA by Camelcade.

the class PerlUseStatementMixin method getPackageProcessor.

@NotNull
@Override
public PerlPackageProcessor getPackageProcessor() {
    PerlPackageProcessor packageProcessor = null;
    // package name processor
    String packageName = getPackageName();
    if (packageName != null) {
        packageProcessor = PerlPackageProcessorEP.EP.findSingle(packageName);
    } else if (getVersionElement() != null) {
        packageProcessor = PerlVersionProcessor.getProcessor(PerlUseStatementMixin.this);
    }
    return packageProcessor == null ? PerlPackageProcessorDefault.INSTANCE : packageProcessor;
}
Also used : PerlPackageProcessor(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PerlPackageProcessor

use of com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor in project Perl5-IDEA by Camelcade.

the class PerlParserUtil method parseUseParameters.

/**
 * Parses use parameters with package processor if it's possible. If not, uses default parsing logic.
 */
public static boolean parseUseParameters(@NotNull PsiBuilder b, int l, @NotNull Parser defaultParser) {
    if (b.getTokenType() == PACKAGE) {
        String packageName = b.getTokenText();
        if (StringUtil.isEmpty(packageName)) {
            return false;
        }
        PerlPackageProcessor packageProcessor = PerlPackageProcessorEP.EP.findSingle(packageName);
        if (packageProcessor != null) {
            assert b instanceof PerlBuilder;
            PsiBuilder.Marker m = b.mark();
            if (packageProcessor.parseUseParameters((PerlBuilder) b, l)) {
                m.drop();
                return true;
            }
            m.rollbackTo();
        }
    }
    return defaultParser.parse(b, l);
}
Also used : PerlPackageProcessor(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor) PerlBuilder(com.perl5.lang.perl.parser.builder.PerlBuilder) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 4 with PerlPackageProcessor

use of com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor in project Perl5-IDEA by Camelcade.

the class packageProcessorsTest method testModernPerl.

public void testModernPerl() {
    initWithFileSmart("ModernPerl");
    PerlNamespaceDefinitionElement namespaceDefinition = getElementAtCaret(PerlNamespaceDefinitionElement.class);
    assertNotNull(namespaceDefinition);
    assertEquals(PerlMroType.C3, namespaceDefinition.getMroType());
    PerlUseStatement useStatement = getElementAtCaret(PerlUseStatement.class);
    assertNotNull(useStatement);
    PerlPackageProcessor packageProcessor = useStatement.getPackageProcessor();
    assertNotNull(packageProcessor);
    assertInstanceOf(packageProcessor, PerlStrictProvider.class);
    assertInstanceOf(packageProcessor, PerlMroProvider.class);
    assertInstanceOf(packageProcessor, PerlWarningsProvider.class);
    assertInstanceOf(packageProcessor, PerlPackageLoader.class);
}
Also used : PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PerlUseStatement(com.perl5.lang.perl.psi.PerlUseStatement)

Example 5 with PerlPackageProcessor

use of com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor in project Perl5-IDEA by Camelcade.

the class PerlPackageUtil method getIncDirsForPsiElement.

/**
 * Returns List of lib directories including class roots, current directory and use lib ones
 *
 * @param psiElement to resolve for
 * @return list of lib dirs
 */
@NotNull
public static List<VirtualFile> getIncDirsForPsiElement(@NotNull PsiElement psiElement) {
    PsiFile psiFile = psiElement.getContainingFile().getOriginalFile();
    List<VirtualFile> result = new ArrayList<>();
    // libdirs providers fixme we need to use stubs or psi here
    for (PerlUseStatement useStatement : PsiTreeUtil.findChildrenOfType(psiFile, PerlUseStatement.class)) {
        PerlPackageProcessor packageProcessor = useStatement.getPackageProcessor();
        if (packageProcessor instanceof PerlLibProvider) {
            ((PerlLibProvider) packageProcessor).addLibDirs(useStatement, result);
        }
    }
    // classpath
    result.addAll(PerlProjectManager.getInstance(psiElement.getProject()).getAllLibraryRoots());
    // current dir
    if (PerlSharedSettings.getInstance(psiFile.getProject()).getTargetPerlVersion().lesserThan(PerlVersion.V5_26)) {
        VirtualFile virtualFile = psiFile.getVirtualFile();
        if (virtualFile != null && virtualFile.getParent() != null) {
            result.add(virtualFile.getParent());
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PerlPackageProcessor(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor) PerlLibProvider(com.perl5.lang.perl.extensions.packageprocessor.PerlLibProvider) PsiFile(com.intellij.psi.PsiFile) PerlUseStatement(com.perl5.lang.perl.psi.PerlUseStatement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PerlPackageProcessor (com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor)4 PerlUseStatement (com.perl5.lang.perl.psi.PerlUseStatement)2 NotNull (org.jetbrains.annotations.NotNull)2 PsiBuilder (com.intellij.lang.PsiBuilder)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 PerlLibProvider (com.perl5.lang.perl.extensions.packageprocessor.PerlLibProvider)1 PerlPackageOptionsProvider (com.perl5.lang.perl.extensions.packageprocessor.PerlPackageOptionsProvider)1 PerlPackageParentsProvider (com.perl5.lang.perl.extensions.packageprocessor.PerlPackageParentsProvider)1 PerlBuilder (com.perl5.lang.perl.parser.builder.PerlBuilder)1 PerlNamespaceDefinitionElement (com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)1 THashSet (gnu.trove.THashSet)1