use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlElementFactory method createHereDocElements.
public static List<PsiElement> createHereDocElements(Project project, char quoteSymbol, String markerText, String contentText) {
PerlFileImpl file = createFile(project, String.format("<<%c%s%c\n%s\n%s\n;", quoteSymbol, markerText, quoteSymbol, contentText, markerText));
PsiElement heredocOpener = PsiTreeUtil.findChildOfType(file, PsiPerlHeredocOpener.class);
@SuppressWarnings("ConstantConditions") PsiElement headingNewLine = heredocOpener.getNextSibling();
PsiElement tailingNewLine = headingNewLine.getNextSibling().getNextSibling().getNextSibling();
return new ArrayList<>(Arrays.asList(heredocOpener, headingNewLine, tailingNewLine));
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class CompoundToStatementIntention method replaceWithStatement.
/**
* Generating new code, extracting declarations from control expression and replacing compound statement if possible.
*
* @param compound compound statement
* @param statementText statement text
* @param controlExpr control expression (condition or iterable)
*/
private static void replaceWithStatement(@NotNull PerlConvertableCompound compound, @NotNull String statementText, @NotNull PsiPerlExpr controlExpr) {
List<PsiElement> declarations = new ArrayList<>();
controlExpr.accept(new PerlRecursiveVisitor() {
@Override
public void visitPerlVariableDeclarationExpr(@NotNull PerlVariableDeclarationExpr o) {
declarations.add(o);
super.visitPerlVariableDeclarationExpr(o);
}
});
String controlExprText = controlExpr.getText();
StringBuilder sb = new StringBuilder();
if (!declarations.isEmpty()) {
// extracting declarations from control statement
TextRange controlExprTextRange = controlExpr.getTextRange();
for (int i = declarations.size() - 1; i >= 0; i--) {
PsiElement declaration = declarations.get(i);
sb.append(declaration.getText()).append(";\n");
PsiElement keywordElement = declaration.getFirstChild();
if (keywordElement instanceof LeafPsiElement) {
// removing keyword from control expression my $var => $var, my ($var1, $var2) => ($var1, $var2)
controlExprText = keywordElement.getTextRange().shiftRight(-controlExprTextRange.getStartOffset()).replace(controlExprText, "");
}
}
}
sb.append(statementText).append(" ").append(compound.getFirstChild().getText()).append(" ").append(controlExprText).append(";");
PerlFileImpl perlFile = PerlElementFactory.createFile(compound.getProject(), sb.toString());
PsiPerlStatementImpl[] statements = PsiTreeUtil.getChildrenOfType(perlFile, PsiPerlStatementImpl.class);
if (statements == null || statements.length == 0) {
return;
}
PsiElement container = compound.getParent();
if (container == null) {
return;
}
container.addRangeBefore(statements[0], statements[statements.length - 1], compound);
compound.delete();
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlDebuggerEditorsProvider method createExpressionCodeFragment.
@Override
protected PsiFile createExpressionCodeFragment(@NotNull Project project, @NotNull String text, @Nullable PsiElement context, boolean isPhysical) {
PsiFile fileFromText = PsiFileFactory.getInstance(project).createFileFromText("file.dummy", getFileType(), text, 0, isPhysical);
((PerlFileImpl) fileFromText).setFileContext(context);
return fileFromText;
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlXNamedValue method computeMySourcePosition.
protected boolean computeMySourcePosition(@Nullable XNavigatable navigatable, @Nullable final XInlineDebuggerDataCallback callback) {
String name = myPerlValueDescriptor.getName();
if (StringUtil.isEmpty(name) || name.length() < 2) {
return false;
}
final PerlVariableType variableType = PerlVariableType.bySigil(name.charAt(0));
if (variableType == null || variableType == PerlVariableType.CODE) {
return false;
}
final String variableName = name.substring(1);
final XSourcePosition sourcePosition = myStackFrame.getSourcePosition();
if (sourcePosition == null) {
return false;
}
final Project project = myStackFrame.getPerlExecutionStack().getSuspendContext().getDebugSession().getProject();
final VirtualFile virtualFile = sourcePosition.getFile();
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (!(psiFile instanceof PerlFileImpl)) {
return false;
}
PsiElement element = psiFile.findElementAt(sourcePosition.getOffset());
if (element == null) {
return false;
}
if (navigatable != null) {
PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variableName, variableType, element);
PerlResolveUtil.treeWalkUp(element, variableProcessor);
PerlVariableDeclarationElement result = variableProcessor.getResult();
if (result == null) {
return false;
}
navigatable.setSourcePosition(XSourcePositionImpl.createByElement(result));
} else if (callback != null) {
final Document document = psiFile.getViewProvider().getDocument();
if (document == null) {
return false;
}
final boolean[] found = new boolean[] { false };
PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variableName, variableType, element) {
@Override
public boolean execute(@NotNull PsiElement possibleElement, @NotNull ResolveState state) {
boolean result = super.execute(possibleElement, state);
if (!result) {
registerElement(getResult());
} else if (possibleElement instanceof PerlVariable && ((PerlVariable) possibleElement).getActualType() == variableType && StringUtil.equals(variableName, ((PerlVariable) possibleElement).getName())) {
registerElement(possibleElement);
}
return result;
}
private void registerElement(@Nullable PsiElement targetElement) {
if (targetElement == null) {
return;
}
found[0] = true;
try {
if (mySourcePositionMethod != null) {
mySourcePositionMethod.invoke(callback, XSourcePositionImpl.createByElement(targetElement));
} else if (myLegacyMethod != null) {
myLegacyMethod.invoke(callback, virtualFile, document, document.getLineNumber(targetElement.getTextOffset()));
} else {
found[0] = false;
}
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
};
PerlResolveUtil.treeWalkUp(element, variableProcessor);
return found[0];
}
return true;
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl 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;
}
Aggregations