Search in sources :

Example 1 with PerlFileImpl

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;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PerlFileImpl

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;
    }
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Contract(org.jetbrains.annotations.Contract)

Example 3 with PerlFileImpl

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();
}
Also used : PerlNamespaceElementImpl(com.perl5.lang.perl.psi.impl.PerlNamespaceElementImpl) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlNamespaceDefinitionMixin(com.perl5.lang.perl.psi.mixins.PerlNamespaceDefinitionMixin)

Example 4 with PerlFileImpl

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;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlHeredocElementImpl(com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PerlFileImpl

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;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl)

Aggregations

PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)21 PsiElement (com.intellij.psi.PsiElement)8 PsiFile (com.intellij.psi.PsiFile)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ArrayList (java.util.ArrayList)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PerlNamespaceDefinitionElement (com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)2 PerlVariableDeclarationElement (com.perl5.lang.perl.psi.PerlVariableDeclarationElement)2 PsiPerlStatement (com.perl5.lang.perl.psi.PsiPerlStatement)2 PsiPerlStatementImpl (com.perl5.lang.perl.psi.impl.PsiPerlStatementImpl)2 LocalQuickFixOnPsiElement (com.intellij.codeInspection.LocalQuickFixOnPsiElement)1 Document (com.intellij.openapi.editor.Document)1 PsiReference (com.intellij.psi.PsiReference)1 ResolveResult (com.intellij.psi.ResolveResult)1 ResolveState (com.intellij.psi.ResolveState)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 XSourcePosition (com.intellij.xdebugger.XSourcePosition)1 PerlCastExpression (com.perl5.lang.perl.psi.PerlCastExpression)1