use of com.perl5.lang.perl.psi.PsiPerlNamespaceContent in project Perl5-IDEA by Camelcade.
the class PerlClassAccessorWrapper method isFollowBestPractice.
public boolean isFollowBestPractice() {
PerlPolyNamedElementStub stub = getStub();
if (stub != null) {
return ((PerlClassAccessorWrapperStub) stub).isFollowBestPractice();
}
PerlClassAccessorWrapperStub greenStub = getGreenStub();
if (greenStub != null) {
return greenStub.isFollowBestPractice();
}
boolean[] result = new boolean[] { false };
// fixme we need a smarter treewalkup here, scopes are not necessary here
PerlResolveUtil.treeWalkUp(this, (element, state) -> {
if (element instanceof PsiPerlNamespaceContent) {
return false;
}
if (PsiUtilCore.getElementType(element) == CLASS_ACCESSOR_FBP) {
result[0] = true;
return false;
}
return true;
});
return result[0];
}
use of com.perl5.lang.perl.psi.PsiPerlNamespaceContent 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