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);
}
}
}
}
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);
}
}
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());
}
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();
}
}
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;
}
Aggregations