Recognizing html tag inside of php string

I'm developing plugin for PhpStorm.
When I'm trying to find html tag in php file I get html tag inside of StringLiteralExpression as a result because of Language Injection.

How to recognize that the html tag not in real HTML context, but inside php string?

 

0
正式评论

I don't know how do you find HTML tags but I try to guess and would suggest you to work with XmlFile instead of PhpFile.

final FileViewProvider viewProvider = phpFile.getViewProvider();
final PsiFile psi = viewProvider.getPsi(HTMLLanguage.INSTANCE);
if (psi instanceof XmlFile) {
psi.accept(new XmlRecursiveElementWalkingVisitor() {
@Override
public void visitXmlTag(final XmlTag tag) {
if ("div".equals(tag.getName())) {
System.out.println(tag);
}
else {
super.visitXmlTag(tag);
}
}
});
}

Working with XmlFile you'll never get any PHP content and v.v.

Thank you for the reply.

But the task is not to find html tag in php file. I have annotator which finds cyrillic text inside html tags in php file and adds warning annotation. 

public class HTMLLangPhraseAnnotator implements Annotator
{
@Override
public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder annotationHolder)
{
if (!(psiElement instanceof XmlTextImpl) || Utils.isLangFile(psiElement.getContainingFile()))
return;

String t = ((XmlTextImpl) psiElement).getValue();

if(!Utils.containsCyrillic(t))
return;


TextRange range = new TextRange(psiElement.getTextRange().getStartOffset(), psiElement.getTextRange().getEndOffset());
annotationHolder.createWarningAnnotation(range, "Возможно, стоит вынести в языковой файл").registerFix(
new CreateLangPhraseIntentionAction(psiElement),
range
);
}
}

For example 

 

<?php
$text = "<div>I don't need this text because it is php string</div>";
?>

<div>I need this one</div>
0

The next code snippet should help you.

InjectedLanguageManager.getInstance(psiElement.getProject()).isInjectedFragment(psiElement.getContainingFile())

P.S. Also, as we both now know that I'm bad in guessing it'd be better if you provide as much information as possible when you ask a question the next time ;) It'll save our time, thanks.

0

Thanks!

I'll give you more details next time :)

0

请先登录再写评论。