Search in sources :

Example 1 with PsiPerlCommaSequenceExpr

use of com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr in project Perl5-IDEA by Camelcade.

the class PerlMooseExtendsStatementImpl method getParentsList.

@NotNull
protected List<String> getParentsList() {
    List<String> result = new ArrayList<>();
    PsiElement expr = getExpr();
    if (expr != null) {
        if (expr instanceof PerlString) {
            String content = ElementManipulators.getValueText(expr);
            if (!content.isEmpty()) {
                result.add(content);
            }
        } else if (expr instanceof PsiPerlCommaSequenceExpr) {
            PsiElement element = expr.getFirstChild();
            while (element != null) {
                if (element instanceof PerlString) {
                    String content = ElementManipulators.getValueText(element);
                    if (!content.isEmpty()) {
                        result.add(content);
                    }
                }
                element = element.getNextSibling();
            }
        } else if (expr instanceof PsiPerlStringList) {
            for (PsiElement element : PerlPsiUtil.collectStringElements(expr.getFirstChild())) {
                String content = element.getText();
                if (!content.isEmpty()) {
                    result.add(content);
                }
            }
        } else {
        // todo we need to somehow mark statement as bad
        }
    }
    return result;
}
Also used : PerlString(com.perl5.lang.perl.psi.PerlString) ArrayList(java.util.ArrayList) PsiPerlStringList(com.perl5.lang.perl.psi.PsiPerlStringList) PerlString(com.perl5.lang.perl.psi.PerlString) PsiPerlCommaSequenceExpr(com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiPerlCommaSequenceExpr

use of com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr in project Perl5-IDEA by Camelcade.

the class MasonFlagsStatementImpl method getExtendsFlagValue.

@Nullable
private String getExtendsFlagValue() {
    PsiElement expr = getExpr();
    // fixme this can be a prototype for scanning a hash to map
    if (expr instanceof PsiPerlCommaSequenceExpr) {
        PsiElement currentElement = expr.getFirstChild();
        while (currentElement != null) {
            PsiElement keyElement = currentElement;
            // comma between key and val
            if (isNullOrNotComma(currentElement = PerlPsiUtil.getNextSignificantSibling(currentElement))) {
                break;
            }
            PsiElement valElement = PerlPsiUtil.getNextSignificantSibling(currentElement);
            if (valElement != null && StringUtil.equals("extends", ElementManipulators.getValueText(keyElement))) {
                return ElementManipulators.getValueText(valElement);
            }
            // comma between pairs
            if (isNullOrNotComma(currentElement = PerlPsiUtil.getNextSignificantSibling(currentElement))) {
                break;
            }
            currentElement = PerlPsiUtil.getNextSignificantSibling(currentElement);
        }
    }
    return null;
}
Also used : PsiPerlCommaSequenceExpr(com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PsiPerlCommaSequenceExpr

use of com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr in project Perl5-IDEA by Camelcade.

the class PerlStringCompletionUtil method fillWithHashIndexes.

public static void fillWithHashIndexes(@NotNull final PsiElement element, @NotNull final CompletionResultSet result) {
    for (String text : HASH_INDEXES_CACHE) {
        addElement(text, result);
    }
    PsiFile file = element.getContainingFile();
    file.accept(new PerlRecursiveVisitor() {

        @Override
        public void visitStringContentElement(@NotNull PerlStringContentElementImpl o) {
            if (o != element && SIMPLE_HASH_INDEX.accepts(o)) {
                processStringElement(o);
            }
            super.visitStringContentElement(o);
        }

        @Override
        public void visitCommaSequenceExpr(@NotNull PsiPerlCommaSequenceExpr o) {
            if (o.getParent() instanceof PsiPerlAnonHash) {
                PsiElement sequenceElement = o.getFirstChild();
                boolean isKey = true;
                while (sequenceElement != null) {
                    ProgressManager.checkCanceled();
                    IElementType elementType = sequenceElement.getNode().getElementType();
                    if (isKey && sequenceElement instanceof PerlString) {
                        for (PerlStringContentElement stringElement : PerlPsiUtil.collectStringElements(sequenceElement)) {
                            processStringElement(stringElement);
                        }
                    } else if (elementType == COMMA || elementType == FAT_COMMA) {
                        isKey = !isKey;
                    }
                    sequenceElement = PerlPsiUtil.getNextSignificantSibling(sequenceElement);
                }
            }
            super.visitCommaSequenceExpr(o);
        }

        protected void processStringElement(PerlStringContentElement stringContentElement) {
            String text = stringContentElement.getText();
            if (StringUtil.isNotEmpty(text) && !HASH_INDEXES_CACHE.contains(text) && PerlLexer.IDENTIFIER_PATTERN.matcher(text).matches()) {
                HASH_INDEXES_CACHE.add(text);
                addElement(text, result);
            }
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) PerlStringContentElementImpl(com.perl5.lang.perl.psi.impl.PerlStringContentElementImpl)

Example 4 with PsiPerlCommaSequenceExpr

use of com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr in project Perl5-IDEA by Camelcade.

the class PerlTypedHandler method tryToAddFatComma.

@Nullable
private Result tryToAddFatComma(@NotNull Editor editor, @NotNull PsiFile file, int offset) {
    if (!Perl5CodeInsightSettings.getInstance().SMART_COMMA_SEQUENCE_TYPING) {
        return null;
    }
    PsiElement elementAtCaret = file.findElementAt(offset);
    if (!(elementAtCaret instanceof PsiWhiteSpace)) {
        return null;
    }
    PsiElement commaSequence = elementAtCaret.getPrevSibling();
    if (!(commaSequence instanceof PsiPerlCommaSequenceExpr)) {
        return null;
    }
    PsiElement lastChild = commaSequence.getLastChild();
    IElementType lastChildElementType = PsiUtilCore.getElementType(lastChild);
    if (lastChildElementType == COMMA || lastChildElementType == FAT_COMMA) {
        return null;
    }
    PsiElement commaElement = PerlPsiUtil.getPrevSignificantSibling(lastChild);
    if (PsiUtilCore.getElementType(commaElement) != COMMA) {
        return null;
    }
    PsiElement fatCommaElement = PerlPsiUtil.getPrevSignificantSibling(PerlPsiUtil.getPrevSignificantSibling(commaElement));
    if (PsiUtilCore.getElementType(fatCommaElement) != FAT_COMMA) {
        return null;
    }
    int reformatFrom = commaSequence.getNode().getStartOffset();
    Document document = editor.getDocument();
    document.insertString(offset, "=>");
    editor.getCaretModel().moveToOffset(offset + 2);
    Project project = file.getProject();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    CodeStyleManager.getInstance(project).reformatText(file, reformatFrom, offset + 2);
    AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
    return Result.CONTINUE;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) PsiPerlCommaSequenceExpr(com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PsiPerlCommaSequenceExpr

use of com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr in project Perl5-IDEA by Camelcade.

the class HTMLMasonFlagsStatementImpl method getParentComponentPath.

@Nullable
@Override
public String getParentComponentPath() {
    HTMLMasonFlagsStatementStub stub = getStub();
    if (stub != null) {
        return stub.getParentComponentPath();
    }
    PsiPerlCommaSequenceExpr expr = PsiTreeUtil.findChildOfType(this, PsiPerlCommaSequenceExpr.class);
    if (expr != null) {
        PsiElement firstChild = expr.getFirstChild();
        PsiElement lastChild = expr.getLastChild();
        if (firstChild instanceof PerlString && StringUtil.equals("inherit", ElementManipulators.getValueText(firstChild)) && lastChild instanceof PerlString) {
            return ElementManipulators.getValueText(lastChild);
        }
    }
    return UNDEF_RESULT;
}
Also used : PerlString(com.perl5.lang.perl.psi.PerlString) HTMLMasonFlagsStatementStub(com.perl5.lang.htmlmason.parser.stubs.HTMLMasonFlagsStatementStub) PsiPerlCommaSequenceExpr(com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)5 PsiPerlCommaSequenceExpr (com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr)4 Nullable (org.jetbrains.annotations.Nullable)3 IElementType (com.intellij.psi.tree.IElementType)2 PerlString (com.perl5.lang.perl.psi.PerlString)2 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 PsiFile (com.intellij.psi.PsiFile)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 HTMLMasonFlagsStatementStub (com.perl5.lang.htmlmason.parser.stubs.HTMLMasonFlagsStatementStub)1 PsiPerlStringList (com.perl5.lang.perl.psi.PsiPerlStringList)1 PerlStringContentElementImpl (com.perl5.lang.perl.psi.impl.PerlStringContentElementImpl)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1