Java Code Examples for com.intellij.lang.injection.InjectedLanguageManager#injectedToHost()

The following examples show how to use com.intellij.lang.injection.InjectedLanguageManager#injectedToHost() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProblemDescriptorBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@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) + 1;
  }
  return myLineNumber;
}
 
Example 2
Source File: TemplateLanguageErrorFilter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean shouldHighlightErrorElement(@Nonnull PsiErrorElement element) {
  if (isKnownSubLanguage(element.getParent().getLanguage())) {
    //
    // Immediately discard filters with non-matching template class if already known
    //
    Class templateClass = element.getUserData(TEMPLATE_VIEW_PROVIDER_CLASS_KEY);
    if (templateClass != null && (templateClass != myTemplateFileViewProviderClass)) return true;
    
    PsiFile psiFile = element.getContainingFile();
    int offset = element.getTextOffset();
    InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(element.getProject());
    if (injectedLanguageManager.isInjectedFragment(psiFile)) {
      PsiElement host = injectedLanguageManager.getInjectionHost(element);
      if (host != null) {
        psiFile = host.getContainingFile();
        offset = injectedLanguageManager.injectedToHost(element, offset);
      }
    }
    final FileViewProvider viewProvider = psiFile.getViewProvider();
    element.putUserData(TEMPLATE_VIEW_PROVIDER_CLASS_KEY, viewProvider.getClass());
    if (!(viewProvider.getClass() == myTemplateFileViewProviderClass)) {
      return true;
    }
    //
    // An error can occur at template element or before it. Check both.
    //
    if (shouldIgnoreErrorAt(viewProvider, offset) || shouldIgnoreErrorAt(viewProvider, offset + 1)) return false;
  }
  return true;
}
 
Example 3
Source File: HighlightUsagesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static TextRange getNameIdentifierRange(PsiFile file, PsiElement element) {
  final InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(element.getProject());
  if (element instanceof PomTargetPsiElement) {
    final PomTarget target = ((PomTargetPsiElement)element).getTarget();
    if (target instanceof PsiDeclaredTarget) {
      final PsiDeclaredTarget declaredTarget = (PsiDeclaredTarget)target;
      final TextRange range = declaredTarget.getNameIdentifierRange();
      if (range != null) {
        if (range.getStartOffset() < 0 || range.getLength() <= 0) {
          return null;
        }
        final PsiElement navElement = declaredTarget.getNavigationElement();
        if (PsiUtilBase.isUnderPsiRoot(file, navElement)) {
          return injectedManager.injectedToHost(navElement, range.shiftRight(navElement.getTextRange().getStartOffset()));
        }
      }
    }
  }

  if (!PsiUtilBase.isUnderPsiRoot(file, element)) {
    return null;
  }

  PsiElement identifier = IdentifierUtil.getNameIdentifier(element);
  if (identifier != null && PsiUtilBase.isUnderPsiRoot(file, identifier)) {
    return injectedManager.injectedToHost(identifier, identifier.getTextRange());
  }
  return null;
}
 
Example 4
Source File: EditorFoldingInfo.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
TextRange getPsiElementRange(@Nonnull FoldRegion region) {
  PsiElement element = getPsiElement(region);
  if (element == null) return null;
  PsiFile containingFile = element.getContainingFile();
  InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(containingFile.getProject());
  boolean isInjected = injectedManager.isInjectedFragment(containingFile);
  TextRange range = element.getTextRange();
  if (isInjected) {
    range = injectedManager.injectedToHost(element, range);
  }
  return range;
}