use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class InjectedLanguageUtil method isInInjectedLanguagePrefixSuffix.
public static boolean isInInjectedLanguagePrefixSuffix(@NotNull final PsiElement element) {
PsiFile injectedFile = element.getContainingFile();
if (injectedFile == null)
return false;
Project project = injectedFile.getProject();
InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(project);
if (!languageManager.isInjectedFragment(injectedFile))
return false;
TextRange elementRange = element.getTextRange();
List<TextRange> editables = languageManager.intersectWithAllEditableFragments(injectedFile, elementRange);
int combinedEditablesLength = editables.stream().mapToInt(TextRange::getLength).sum();
return combinedEditablesLength != elementRange.getLength();
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class InjectedLanguageUtil method getUnescapedText.
public static String getUnescapedText(@NotNull PsiFile file, @Nullable final PsiElement startElement, @Nullable final PsiElement endElement) {
final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(file.getProject());
if (manager.getInjectionHost(file) == null) {
return file.getText().substring(startElement == null ? 0 : startElement.getTextRange().getStartOffset(), endElement == null ? file.getTextLength() : endElement.getTextRange().getStartOffset());
}
final StringBuilder sb = new StringBuilder();
file.accept(new PsiRecursiveElementWalkingVisitor() {
Boolean myState = startElement == null ? Boolean.TRUE : null;
@Override
public void visitElement(PsiElement element) {
if (element == startElement)
myState = Boolean.TRUE;
if (element == endElement)
myState = Boolean.FALSE;
if (Boolean.FALSE == myState)
return;
if (Boolean.TRUE == myState && element.getFirstChild() == null) {
sb.append(getUnescapedLeafText(element, false));
} else {
super.visitElement(element);
}
}
});
return sb.toString();
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class MethodGroupingRule method groupUsage.
@Override
public UsageGroup groupUsage(@NotNull Usage usage) {
if (!(usage instanceof PsiElementUsage))
return null;
PsiElement psiElement = ((PsiElementUsage) usage).getElement();
PsiFile containingFile = psiElement.getContainingFile();
if (containingFile == null)
return null;
InjectedLanguageManager manager = InjectedLanguageManager.getInstance(containingFile.getProject());
PsiFile topLevelFile = manager.getTopLevelFile(containingFile);
if (topLevelFile instanceof PsiJavaFile) {
PsiElement containingMethod = topLevelFile == containingFile ? psiElement : manager.getInjectionHost(containingFile);
if (usage instanceof UsageInfo2UsageAdapter && topLevelFile == containingFile) {
int offset = ((UsageInfo2UsageAdapter) usage).getUsageInfo().getNavigationOffset();
containingMethod = containingFile.findElementAt(offset);
}
do {
containingMethod = PsiTreeUtil.getParentOfType(containingMethod, PsiMethod.class, true);
if (containingMethod == null)
break;
final PsiClass containingClass = ((PsiMethod) containingMethod).getContainingClass();
if (containingClass == null || containingClass.getQualifiedName() != null)
break;
} while (true);
if (containingMethod != null) {
return new MethodUsageGroup((PsiMethod) containingMethod);
}
}
return null;
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class ProblemDescriptorBase method getLineNumber.
@Override
public int getLineNumber() {
if (myLineNumber == -1) {
PsiElement psiElement = getPsiElement();
if (psiElement == null)
return -1;
if (!psiElement.isValid())
return -1;
LOG.assertTrue(psiElement.isPhysical());
InjectedLanguageManager manager = InjectedLanguageManager.getInstance(psiElement.getProject());
PsiFile containingFile = manager.getTopLevelFile(psiElement);
Document document = PsiDocumentManager.getInstance(psiElement.getProject()).getDocument(containingFile);
if (document == null)
return -1;
TextRange textRange = getTextRange();
if (textRange == null)
return -1;
textRange = manager.injectedToHost(psiElement, textRange);
final int startOffset = textRange.getStartOffset();
final int textLength = document.getTextLength();
LOG.assertTrue(startOffset <= textLength, getDescriptionTemplate() + " at " + startOffset + ", " + textLength);
myLineNumber = document.getLineNumber(startOffset);
}
return myLineNumber;
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class HighlightVisitorImpl method registerReferencesFromInjectedFragments.
private void registerReferencesFromInjectedFragments(@NotNull PsiElement element) {
InjectedLanguageManager manager = InjectedLanguageManager.getInstance(myFile.getProject());
manager.enumerateEx(element, myFile, false, (injectedPsi, places) -> injectedPsi.accept(REGISTER_REFERENCES_VISITOR));
}
Aggregations