Search in sources :

Example 1 with HTMLMasonMethodReference

use of com.perl5.lang.htmlmason.parser.psi.references.HTMLMasonMethodReference in project Perl5-IDEA by Camelcade.

the class HTMLMasonComponentCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement position = parameters.getPosition();
    PsiElement parent = position.getParent();
    PsiFile file = position.getContainingFile();
    if (parent instanceof PerlString && file instanceof HTMLMasonFileImpl) {
        PsiReference[] references = parent.getReferences();
        for (PsiReference reference : references) {
            TextRange refRange = reference.getRangeInElement();
            if (refRange.contains(position.getStartOffsetInParent())) {
                Project project = position.getProject();
                String fullPrefix = refRange.substring(parent.getText()).replace(CompletionInitializationContext.DUMMY_IDENTIFIER, "").replace(CompletionInitializationContext.DUMMY_IDENTIFIER_TRIMMED, "");
                final CompletionResultSet finalResultSet = result.withPrefixMatcher(fullPrefix);
                ;
                if (reference instanceof HTMLMasonMethodReference) {
                    PsiElement firstReferenceTarget = references[0].resolve();
                    if (firstReferenceTarget instanceof HTMLMasonFileImpl) {
                        HTMLMasonCompletionUtil.fillWithMethods(finalResultSet, (HTMLMasonFileImpl) firstReferenceTarget);
                    }
                } else {
                    HTMLMasonCompletionUtil.fillWithComponentSlugs(finalResultSet);
                    HTMLMasonCompletionUtil.fillWithSubcomponents(finalResultSet, (HTMLMasonFileImpl) file);
                    if (!StringUtil.startsWith(fullPrefix, "/")) {
                        HTMLMasonCompletionUtil.fillWithRelativeSubcomponents(finalResultSet, project, (HTMLMasonFileImpl) file);
                    } else {
                        HTMLMasonCompletionUtil.fillWithAbsoluteSubcomponents(finalResultSet, project);
                    }
                }
                result.stopHere();
                break;
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) HTMLMasonFileImpl(com.perl5.lang.htmlmason.parser.psi.impl.HTMLMasonFileImpl) PerlString(com.perl5.lang.perl.psi.PerlString) HTMLMasonMethodReference(com.perl5.lang.htmlmason.parser.psi.references.HTMLMasonMethodReference) PsiReference(com.intellij.psi.PsiReference) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PerlString(com.perl5.lang.perl.psi.PerlString) PsiElement(com.intellij.psi.PsiElement)

Example 2 with HTMLMasonMethodReference

use of com.perl5.lang.htmlmason.parser.psi.references.HTMLMasonMethodReference 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)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)2 PsiReference (com.intellij.psi.PsiReference)2 PerlString (com.perl5.lang.perl.psi.PerlString)2 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 HTMLMasonFileImpl (com.perl5.lang.htmlmason.parser.psi.impl.HTMLMasonFileImpl)1 HTMLMasonMethodReference (com.perl5.lang.htmlmason.parser.psi.references.HTMLMasonMethodReference)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 NotNull (org.jetbrains.annotations.NotNull)1