Search in sources :

Example 6 with PerlString

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

the class PerlElementFactory method createString.

public static PerlString createString(Project project, String code) {
    PerlFileImpl file = createFile(project, code + ";");
    PerlString string = PsiTreeUtil.findChildOfType(file, PerlString.class);
    assert string != null : "While creating bare string from: " + code;
    return string;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl)

Example 7 with PerlString

use of com.perl5.lang.perl.psi.PerlString 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 8 with PerlString

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

the class HTMLMasonComponentReferencesProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    if (element.getChildren().length == 0) {
        assert element instanceof PerlString;
        String content = ElementManipulators.getValueText(element);
        if (StringUtil.isNotEmpty(content)) {
            Matcher m;
            TextRange range = ElementManipulators.getValueTextRange(element);
            List<PsiReference> result = new ArrayList<>();
            if (// method call
            (m = METHOD_CALL_PATTERN.matcher(content)).matches()) {
                String fileOrSlug = m.group(1);
                String methodName = m.group(2);
                if (methodName == null) {
                    methodName = "";
                }
                TextRange componentRange = new TextRange(range.getStartOffset(), range.getStartOffset() + fileOrSlug.length());
                TextRange methodRange = new TextRange(range.getEndOffset() - methodName.length(), range.getEndOffset());
                if (StringUtil.equals(fileOrSlug, COMPONENT_SLUG_SELF)) {
                    result.add(new HTMLMasonComponentSimpleReference((PerlString) element, componentRange));
                } else if (StringUtil.equals(fileOrSlug, COMPONENT_SLUG_PARENT)) {
                    result.add(new HTMLMasonComponentParentReference((PerlString) element, componentRange));
                } else if (StringUtil.equals(fileOrSlug, COMPONENT_SLUG_REQUEST)) {
                    result.add(new HTMLMasonComponentSimpleReference((PerlString) element, componentRange));
                } else // component or subcomponent
                {
                    result.add(new HTMLMasonComponentReference((PerlString) element, componentRange));
                }
                if (methodRange.getLength() > 0) {
                    result.add(new HTMLMasonMethodReference((PerlString) element, methodRange));
                }
            } else // it's subcomponent or other component
            {
                result.add(new HTMLMasonComponentReference((PerlString) element, range));
            }
            return result.toArray(new PsiReference[result.size()]);
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : PerlString(com.perl5.lang.perl.psi.PerlString) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) PerlString(com.perl5.lang.perl.psi.PerlString) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PerlString

use of com.perl5.lang.perl.psi.PerlString 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)

Example 10 with PerlString

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

the class MasonComponentsCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement position = parameters.getPosition();
    PsiElement parent = position.getParent();
    if (parent instanceof PerlString) {
        Project project = position.getProject();
        MasonSettings masonSettings = MasonSettings.getInstance(project);
        String fullPrefix = ElementManipulators.getValueText(parent).replace(CompletionInitializationContext.DUMMY_IDENTIFIER, "").replace(CompletionInitializationContext.DUMMY_IDENTIFIER_TRIMMED, "");
        result = result.withPrefixMatcher(new PlainPrefixMatcher(fullPrefix));
        final CompletionResultSet finalResultSet = result;
        PsiFile psiFile = position.getContainingFile();
        if (psiFile instanceof MasonFileImpl) {
            final VirtualFile containingFile = MasonCoreUtil.getContainingVirtualFile(psiFile);
            VirtualFile containingDir;
            if (containingFile != null && (containingDir = containingFile.getParent()) != null) {
                VfsUtil.processFilesRecursively(containingDir, new MasonRootsProcessor(containingDir) {

                    @Override
                    public boolean process(VirtualFile virtualFile) {
                        FileType fileType = virtualFile.getFileType();
                        if (fileType instanceof MasonPurePerlComponentFileType && !containingFile.equals(virtualFile)) {
                            String relativePath = VfsUtil.getRelativePath(virtualFile, getRoot());
                            if (StringUtil.isNotEmpty(relativePath)) {
                                finalResultSet.addElement(LookupElementBuilder.create(relativePath).withIcon(fileType.getIcon()));
                            }
                        }
                        return true;
                    }
                });
            }
        }
        for (VirtualFile componentRoot : masonSettings.getComponentsRoots()) {
            VfsUtil.processFilesRecursively(componentRoot, new MasonRootsProcessor(componentRoot) {

                @Override
                public boolean process(VirtualFile virtualFile) {
                    FileType fileType = virtualFile.getFileType();
                    if (fileType instanceof MasonPurePerlComponentFileType) {
                        String relativePath = VfsUtil.getRelativePath(virtualFile, getRoot());
                        if (StringUtil.isNotEmpty(relativePath)) {
                            finalResultSet.addElement(LookupElementBuilder.create("/" + relativePath).withIcon(fileType.getIcon()));
                        }
                    }
                    return true;
                }
            });
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MasonFileImpl(com.perl5.lang.mason2.psi.impl.MasonFileImpl) PerlString(com.perl5.lang.perl.psi.PerlString) MasonSettings(com.perl5.lang.mason2.idea.configuration.MasonSettings) Project(com.intellij.openapi.project.Project) PerlString(com.perl5.lang.perl.psi.PerlString) FileType(com.intellij.openapi.fileTypes.FileType) MasonPurePerlComponentFileType(com.perl5.lang.mason2.filetypes.MasonPurePerlComponentFileType) MasonPurePerlComponentFileType(com.perl5.lang.mason2.filetypes.MasonPurePerlComponentFileType) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PerlString (com.perl5.lang.perl.psi.PerlString)7 PsiElement (com.intellij.psi.PsiElement)5 PsiFile (com.intellij.psi.PsiFile)3 HTMLMasonFileImpl (com.perl5.lang.htmlmason.parser.psi.impl.HTMLMasonFileImpl)3 NotNull (org.jetbrains.annotations.NotNull)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiReference (com.intellij.psi.PsiReference)2 PsiPerlCommaSequenceExpr (com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr)2 PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 FileType (com.intellij.openapi.fileTypes.FileType)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 IElementType (com.intellij.psi.tree.IElementType)1 HTMLMasonMethodDefinition (com.perl5.lang.htmlmason.parser.psi.HTMLMasonMethodDefinition)1 HTMLMasonMethodReference (com.perl5.lang.htmlmason.parser.psi.references.HTMLMasonMethodReference)1 HTMLMasonFlagsStatementStub (com.perl5.lang.htmlmason.parser.stubs.HTMLMasonFlagsStatementStub)1 MasonPurePerlComponentFileType (com.perl5.lang.mason2.filetypes.MasonPurePerlComponentFileType)1