use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlFormattingScalarDerefExpand method apply.
@Override
public int apply() {
int delta = 0;
if (myScalarDereference.isValid()) {
PsiElement parent = myScalarDereference.getParent();
PsiPerlScalarVariable scalarVariable = PsiTreeUtil.findChildOfType(myScalarDereference, PsiPerlScalarVariable.class);
if (parent.isValid() && (parent instanceof PsiPerlHashElement || parent instanceof PsiPerlArrayElement) && scalarVariable != null && scalarVariable.isValid()) {
PsiElement indexElement = parent.getLastChild();
if (indexElement.isValid() && (indexElement instanceof PsiPerlHashIndex || indexElement instanceof PsiPerlArrayIndex)) {
String newCode = scalarVariable.getNode().getText() + "->" + indexElement.getText();
PerlFileImpl newFile = PerlElementFactory.createFile(myScalarDereference.getProject(), newCode);
PsiPerlDerefExpr derefExpr = PsiTreeUtil.findChildOfType(newFile, PsiPerlDerefExpr.class);
if (derefExpr != null) {
if (parent.getParent() instanceof PsiPerlDerefExpr) {
delta = new PerlFormattingReplace(parent, parent, derefExpr.getFirstChild(), derefExpr.getLastChild()).apply();
} else {
delta = new PerlFormattingReplace(parent, derefExpr).apply();
}
}
}
}
}
return delta;
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlPackageUtil method getContextPackageName.
/**
* Searching of namespace element is in. If no explicit namespaces defined, main is returned
*
* @param element psi element to find definition for
* @return canonical package name
*/
@Contract("null -> null")
public static String getContextPackageName(@Nullable PsiElement element) {
if (element == null) {
return null;
}
PerlNamespaceDefinitionElement namespaceDefinition = getContainingNamespace(element);
if (// checking that definition is valid and got namespace
namespaceDefinition != null && namespaceDefinition.getPackageName() != null) {
String name = namespaceDefinition.getPackageName();
assert name != null;
return name;
}
// default value
PsiFile file = element.getContainingFile();
if (file instanceof PerlFileImpl) {
PsiElement contextParent = file.getContext();
PsiElement realParent = file.getParent();
if (contextParent != null && !contextParent.equals(realParent)) {
return getContextPackageName(contextParent);
}
return ((PerlFileImpl) file).getPackageName();
} else {
return MAIN_PACKAGE;
}
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlElementFactory method createPackageName.
// fixme probably we don't need package name and sub. just identifier
public static PerlNamespaceElementImpl createPackageName(Project project, String name) {
PerlFileImpl file = createFile(project, "package " + name + ";");
PerlNamespaceDefinitionMixin def = PsiTreeUtil.findChildOfType(file, PerlNamespaceDefinitionMixin.class);
assert def != null;
return (PerlNamespaceElementImpl) def.getNamespaceElement();
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlElementFactory method createHeredocBodyReplacement.
/**
* Create a replacement heredoc element with new text
*
* @param originalElement element to replace
* @param newText new element text
* @return new generated element
*/
@NotNull
public static PerlHeredocElementImpl createHeredocBodyReplacement(@NotNull PerlHeredocElementImpl originalElement, @NotNull String newText) {
StringBuilder sb = new StringBuilder("<<");
String marker = getSafeHeredocMarker(newText);
IElementType originalElementType = PsiUtilCore.getElementType(originalElement);
char quote;
if (originalElementType == HEREDOC) {
quote = '\'';
} else if (originalElementType == HEREDOC_QQ) {
quote = '"';
} else {
quote = '`';
}
sb.append(quote).append(marker).append(quote).append(";\n").append(newText);
if (!newText.endsWith("\n")) {
sb.append("\n");
}
PerlFileImpl newFile = createFile(originalElement.getProject(), sb.append(marker).toString());
PerlHeredocElementImpl newElement = newFile.findChildByClass(originalElement.getClass());
assert newElement != null : "Can't find element " + originalElement.getClass() + " generated with: \n" + sb.toString();
return newElement;
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlElementFactory method createBareString.
public static PerlString createBareString(Project project, String content) {
PerlFileImpl file = createFile(project, content + " => 42;");
PerlString string = PsiTreeUtil.findChildOfType(file, PerlString.class);
assert string != null : "While creating bare string from: " + content;
return string;
}
Aggregations