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"));
}
};
}
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());
}
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);
}
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;
}
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);
}
}
}
Aggregations