Search in sources :

Example 6 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.

the class HtmlLinkUtil method processInjectedContent.

public static void processInjectedContent(final XmlTag element, @NotNull final Processor<XmlTag> tagProcessor) {
    final PsiLanguageInjectionHost.InjectedPsiVisitor injectedPsiVisitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() {

        @Override
        public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
            if (injectedPsi instanceof XmlFile) {
                final XmlDocument injectedDocument = ((XmlFile) injectedPsi).getDocument();
                if (injectedDocument != null) {
                    final XmlTag rootTag = injectedDocument.getRootTag();
                    if (rootTag != null) {
                        for (PsiElement element = rootTag; element != null; element = element.getNextSibling()) {
                            if (element instanceof XmlTag) {
                                final XmlTag tag = (XmlTag) element;
                                String tagName = tag.getLocalName();
                                if (element instanceof HtmlTag || tag.getNamespacePrefix().length() > 0)
                                    tagName = tagName.toLowerCase();
                                if (LINK.equalsIgnoreCase(tagName)) {
                                    tagProcessor.process((XmlTag) element);
                                }
                            }
                        }
                    }
                }
            }
        }
    };
    final XmlText[] texts = PsiTreeUtil.getChildrenOfType(element, XmlText.class);
    if (texts != null && texts.length > 0) {
        for (final XmlText text : texts) {
            for (PsiElement _element : text.getChildren()) {
                if (_element instanceof PsiLanguageInjectionHost) {
                    InjectedLanguageUtil.enumerate(_element, injectedPsiVisitor);
                }
            }
        }
    }
    final XmlComment[] comments = PsiTreeUtil.getChildrenOfType(element, XmlComment.class);
    if (comments != null && comments.length > 0) {
        for (final XmlComment comment : comments) {
            if (comment instanceof PsiLanguageInjectionHost) {
                InjectedLanguageUtil.enumerate(comment, injectedPsiVisitor);
            }
        }
    }
}
Also used : HtmlTag(com.intellij.psi.html.HtmlTag) NotNull(org.jetbrains.annotations.NotNull) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.

the class AbstractDomDeclarationSearcher method findDeclarationsAt.

@Override
public void findDeclarationsAt(@NotNull PsiElement psiElement, int offsetInElement, Consumer<PomTarget> consumer) {
    if (!(psiElement instanceof XmlToken))
        return;
    final IElementType tokenType = ((XmlToken) psiElement).getTokenType();
    final DomManager domManager = DomManager.getDomManager(psiElement.getProject());
    final DomElement nameElement;
    if (tokenType == XmlTokenType.XML_DATA_CHARACTERS && psiElement.getParent() instanceof XmlText && psiElement.getParent().getParent() instanceof XmlTag) {
        final XmlTag tag = (XmlTag) psiElement.getParent().getParent();
        for (XmlText text : tag.getValue().getTextElements()) {
            if (InjectedLanguageUtil.hasInjections((PsiLanguageInjectionHost) text)) {
                return;
            }
        }
        nameElement = domManager.getDomElement(tag);
    } else if (tokenType == XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN && psiElement.getParent() instanceof XmlAttributeValue && psiElement.getParent().getParent() instanceof XmlAttribute) {
        final PsiElement attributeValue = psiElement.getParent();
        if (InjectedLanguageUtil.hasInjections((PsiLanguageInjectionHost) attributeValue)) {
            return;
        }
        nameElement = domManager.getDomElement((XmlAttribute) attributeValue.getParent());
    } else {
        return;
    }
    if (!(nameElement instanceof GenericDomValue)) {
        return;
    }
    DomElement parent = nameElement.getParent();
    if (parent == null) {
        return;
    }
    final DomTarget target = createDomTarget(parent, nameElement);
    if (target != null) {
        consumer.consume(target);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiElement(com.intellij.psi.PsiElement)

Example 8 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.

the class PyTypingTest method doTestInjectedText.

private void doTestInjectedText(@NotNull String text, @NotNull String expected) {
    myFixture.configureByText(PythonFileType.INSTANCE, text);
    final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());
    final PsiLanguageInjectionHost host = languageManager.getInjectionHost(getElementAtCaret());
    assertNotNull(host);
    final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host);
    assertNotNull(files);
    assertFalse(files.isEmpty());
    final PsiElement injected = files.get(0).getFirst();
    assertEquals(expected, injected.getText());
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 9 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.

the class HtmlConditionalCommentInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull final PsiElement host) {
    Pair<ASTNode, ASTNode> pair = parseConditionalCommentBoundaries(host);
    if (pair == null) {
        return;
    }
    final TextRange textRange = host.getTextRange();
    final int startOffset = textRange.getStartOffset();
    Language language = host.getParent().getLanguage();
    ASTNode conditionalStart = pair.first;
    ASTNode conditionalEnd = pair.second;
    TextRange range = new UnfairTextRange(conditionalStart.getTextRange().getEndOffset() - startOffset, conditionalEnd.getStartOffset() - startOffset);
    if (range.getStartOffset() < range.getEndOffset()) {
        registrar.startInjecting(language).addPlace(null, null, (PsiLanguageInjectionHost) host, range).doneInjecting();
    }
}
Also used : Language(com.intellij.lang.Language) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Example 10 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.

the class CheckRegExpForm method isMatchingText.

static RegExpMatchResult isMatchingText(@NotNull final PsiFile regexpFile, @NotNull String sampleText) {
    final String regExp = regexpFile.getText();
    final Language regexpFileLanguage = regexpFile.getLanguage();
    final RegExpMatcherProvider matcherProvider = RegExpMatcherProvider.EP.forLanguage(regexpFileLanguage);
    if (matcherProvider != null) {
        final RegExpMatchResult result = ReadAction.compute(() -> {
            final PsiLanguageInjectionHost host = InjectedLanguageUtil.findInjectionHost(regexpFile);
            if (host != null) {
                return matcherProvider.matches(regExp, regexpFile, host, sampleText, 1000L);
            }
            return null;
        });
        if (result != null) {
            return result;
        }
    }
    final Integer patternFlags = ReadAction.compute(() -> {
        final PsiLanguageInjectionHost host = InjectedLanguageUtil.findInjectionHost(regexpFile);
        int flags = 0;
        if (host != null) {
            for (RegExpModifierProvider provider : RegExpModifierProvider.EP.allForLanguage(host.getLanguage())) {
                flags = provider.getFlags(host, regexpFile);
                if (flags > 0)
                    break;
            }
        }
        return flags;
    });
    try {
        //noinspection MagicConstant
        return Pattern.compile(regExp, patternFlags).matcher(StringUtil.newBombedCharSequence(sampleText, 1000)).matches() ? RegExpMatchResult.MATCHES : RegExpMatchResult.NO_MATCH;
    } catch (ProcessCanceledException pc) {
        return RegExpMatchResult.TIMEOUT;
    } catch (Exception ignore) {
    }
    return RegExpMatchResult.BAD_REGEXP;
}
Also used : Language(com.intellij.lang.Language) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Aggregations

PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)41 PsiElement (com.intellij.psi.PsiElement)23 TextRange (com.intellij.openapi.util.TextRange)19 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)10 NotNull (org.jetbrains.annotations.NotNull)10 PsiFile (com.intellij.psi.PsiFile)9 Pair (com.intellij.openapi.util.Pair)8 Language (com.intellij.lang.Language)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)5 List (java.util.List)4 Nullable (org.jetbrains.annotations.Nullable)4 ASTNode (com.intellij.lang.ASTNode)3 XmlAttributeValueImpl (com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)3 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)3 InjectedLanguage (org.intellij.plugins.intelliLang.inject.InjectedLanguage)3 NonNls (org.jetbrains.annotations.NonNls)3 Project (com.intellij.openapi.project.Project)2 HtmlTag (com.intellij.psi.html.HtmlTag)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)1