Search in sources :

Example 1 with PerlUseStatement

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

the class PerlUseWarningsInspection method buildVisitor.

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

        @Override
        public void visitFile(PsiFile file) {
            if (file.getViewProvider() instanceof InjectedFileViewProvider || !file.isWritable() || !file.isPhysical() || file.getVirtualFile() instanceof LightVirtualFile) {
                return;
            }
            FileType fileType = file.getFileType();
            if (!(fileType instanceof PerlFileType) || !((PerlFileType) fileType).checkWarningsPragma()) {
                return;
            }
            for (PerlUseStatement useStatement : PsiTreeUtil.findChildrenOfType(file, PerlUseStatement.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 : InjectedFileViewProvider(com.intellij.psi.impl.source.tree.injected.InjectedFileViewProvider) PerlFileType(com.perl5.lang.perl.fileTypes.PerlFileType) PerlUsePackageQuickFix(com.perl5.lang.perl.idea.quickfixes.PerlUsePackageQuickFix) 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) PerlUseStatement(com.perl5.lang.perl.psi.PerlUseStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PerlUseStatement

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

the class PerlStringCollectionTest method doTest.

protected void doTest(String fileName, String... result) {
    initWithFileSmart(fileName);
    PerlUseStatement useStatement = getElementAtCaret(PerlUseStatement.class);
    assertNotNull(useStatement);
    assertEquals(Arrays.asList(result), useStatement.getImportParameters());
}
Also used : PerlUseStatement(com.perl5.lang.perl.psi.PerlUseStatement)

Example 3 with PerlUseStatement

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

the class PerlPackageProcessorBase method addExports.

public void addExports(@NotNull PerlUseStatement useStatement, @NotNull Set<String> export, @NotNull Set<String> exportOk) {
    String packageName = useStatement.getPackageName();
    if (StringUtil.isEmpty(packageName)) {
        return;
    }
    // fixme handle tags
    for (PerlNamespaceDefinitionElement namespaceDefinition : PerlPackageUtil.getNamespaceDefinitions(useStatement.getProject(), packageName)) {
        export.addAll(namespaceDefinition.getEXPORT());
        exportOk.addAll(namespaceDefinition.getEXPORT_OK());
    }
    exportOk.addAll(export);
}
Also used : PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)

Example 4 with PerlUseStatement

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

the class PerlElementFactory method createUseStatement.

public static PerlUseStatement createUseStatement(Project project, String packageName) {
    assert packageName != null;
    PerlFileImpl file = createFile(project, String.format("use %s;", packageName));
    PerlUseStatement def = PsiTreeUtil.findChildOfType(file, PerlUseStatement.class);
    assert def != null;
    return def;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl)

Example 5 with PerlUseStatement

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

the class PerlUsePackageQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement newStatementContainer = descriptor.getPsiElement();
    if (!FileModificationService.getInstance().prepareFileForWrite(newStatementContainer.getContainingFile())) {
        return;
    }
    PsiElement newStatement = PerlElementFactory.createUseStatement(project, myPackageName);
    PsiElement afterAnchor = null;
    PsiElement beforeAnchor = null;
    PsiElement baseUseStatement = PsiTreeUtil.findChildOfType(newStatementContainer, PerlUseStatement.class);
    if (baseUseStatement != null) {
        if (// pragma or version
        ((PerlUseStatement) baseUseStatement).isPragmaOrVersion()) {
            while (true) {
                // trying to find next use statement
                PsiElement nextStatement = baseUseStatement;
                while ((nextStatement = nextStatement.getNextSibling()) != null && PerlParserDefinition.WHITE_SPACE_AND_COMMENTS.contains(nextStatement.getNode().getElementType())) {
                }
                if (nextStatement instanceof PerlUseStatement && // found more use pragma/version
                ((PerlUseStatement) nextStatement).isPragmaOrVersion()) {
                    baseUseStatement = nextStatement;
                } else {
                    afterAnchor = baseUseStatement;
                    // we've got last pragma statement
                    break;
                }
            }
        } else // not a pragma
        {
            beforeAnchor = baseUseStatement;
        }
    } else // no uses found
    {
        PsiPerlNamespaceDefinition baseNamespace = PsiTreeUtil.findChildOfType(newStatementContainer, PsiPerlNamespaceDefinition.class);
        if (// got a namespace definition
        baseNamespace != null && baseNamespace.getBlock() != null) {
            newStatementContainer = baseNamespace.getBlock();
            if (newStatementContainer != null && !(newStatementContainer instanceof PsiPerlNamespaceContent)) {
                afterAnchor = newStatementContainer.getFirstChild();
            } else if (newStatementContainer != null && newStatementContainer.getFirstChild() != null) {
                beforeAnchor = newStatementContainer.getFirstChild();
            }
        } else {
            PsiElement anchor = newStatementContainer.getFirstChild();
            if (anchor instanceof PsiComment) {
                while (anchor.getNextSibling() != null && PerlPsiUtil.isCommentOrSpace(anchor.getNextSibling())) {
                    anchor = anchor.getNextSibling();
                }
                afterAnchor = anchor;
            } else if (anchor != null) {
                beforeAnchor = anchor;
            }
        }
    }
    if (afterAnchor != null) {
        newStatementContainer = afterAnchor.getParent();
        newStatement = newStatementContainer.addAfter(newStatement, afterAnchor);
    } else if (beforeAnchor != null) {
        newStatementContainer = beforeAnchor.getParent();
        newStatement = newStatementContainer.addBefore(newStatement, beforeAnchor);
    } else if (newStatementContainer != null) {
        newStatement = newStatementContainer.add(newStatement);
    }
    if (newStatement != null) {
        PsiElement newLineElement = PerlElementFactory.createNewLine(project);
        PsiElement nextSibling = newStatement.getNextSibling();
        PsiElement preveSibling = newStatement.getPrevSibling();
        newStatementContainer = newStatement.getParent();
        if (nextSibling == null || !(nextSibling instanceof PsiWhiteSpace) || !StringUtil.equals(nextSibling.getText(), "\n")) {
            newStatementContainer.addAfter(newLineElement, newStatement);
        }
        if ((preveSibling == null && !(newStatementContainer instanceof PsiFile)) || !(preveSibling instanceof PsiWhiteSpace) || !StringUtil.equals(preveSibling.getText(), "\n")) {
            newStatementContainer.addBefore(newLineElement, newStatement);
        }
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiPerlNamespaceDefinition(com.perl5.lang.perl.psi.PsiPerlNamespaceDefinition) PsiFile(com.intellij.psi.PsiFile) PerlUseStatement(com.perl5.lang.perl.psi.PerlUseStatement) PsiElement(com.intellij.psi.PsiElement) PsiPerlNamespaceContent(com.perl5.lang.perl.psi.PsiPerlNamespaceContent) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PerlUseStatement (com.perl5.lang.perl.psi.PerlUseStatement)7 PsiFile (com.intellij.psi.PsiFile)4 NotNull (org.jetbrains.annotations.NotNull)3 FileType (com.intellij.openapi.fileTypes.FileType)2 PsiElement (com.intellij.psi.PsiElement)2 InjectedFileViewProvider (com.intellij.psi.impl.source.tree.injected.InjectedFileViewProvider)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 PerlVisitor (com.perl5.lang.perl.psi.PerlVisitor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiComment (com.intellij.psi.PsiComment)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 PerlExportDescriptor (com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor)1 PerlLibProvider (com.perl5.lang.perl.extensions.packageprocessor.PerlLibProvider)1 PerlPackageProcessor (com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor)1 PerlStrictProvider (com.perl5.lang.perl.extensions.packageprocessor.PerlStrictProvider)1 PerlWarningsProvider (com.perl5.lang.perl.extensions.packageprocessor.PerlWarningsProvider)1 PsiPerlNamespaceContent (com.perl5.lang.perl.psi.PsiPerlNamespaceContent)1