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

The following examples show how to use com.intellij.lang.injection.InjectedLanguageManager#getInjectionHost() . 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: BladeInjectTypeProvider.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
/**
 *  Resolve PHP language injection in Blade
 *
 *  @see BladeVariableTypeProvider#getHostPhpFileForInjectedIfExists
 */
@Nullable
private static BladeFileImpl getHostBladeFileForInjectionIfExists(PsiElement element) {
    PsiFile file = element.getContainingFile();
    InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(element.getProject());
    if (injectedLanguageManager.isInjectedFragment(file)) {
        PsiLanguageInjectionHost host = injectedLanguageManager.getInjectionHost(element);
        if (host instanceof BladePsiLanguageInjectionHost && host.isValidHost()) {
            PsiFile bladeFile = host.getContainingFile();
            if (bladeFile instanceof BladeFileImpl) {
                return (BladeFileImpl) bladeFile;
            }
        }
    }

    return null;
}
 
Example 2
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static String getUnescapedText(@Nonnull 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();
}
 
Example 3
Source File: BashPsiUtils.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the start text offset of the element in the toplevel file, i.e the PsiFile which containing the real document. If an element
 * is injected then the outer file is returned.
 *
 * @param element The element to work on
 * @return The start text offset in the physical PsiFile, injected virtual PsiFiles are not used for text offset calculation
 */
public static int getFileTextOffset(@NotNull PsiElement element) {
    int offset = element.getTextOffset();
    if (isInjectedElement(element)) {
        //fixme languageManager is probably expensive
        InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(element.getProject());

        PsiLanguageInjectionHost injectionHost = languageManager.getInjectionHost(element);
        if (injectionHost != null) {
            offset += injectionHost.getTextOffset();
        }
    }

    return offset;
}
 
Example 4
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 5
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean checkRangeContainsOffset(int offset, final TextRange textRange, PsiElement element) {
  int startOffset = element.getTextRange().getStartOffset();
  final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(myProject);
  final PsiLanguageInjectionHost injectionHost = injectedLanguageManager.getInjectionHost(element);
  if (injectionHost != null) {
    final PsiElement nameIdentifier = getNameIdentifier();
    final PsiLanguageInjectionHost initialInjectedHost = nameIdentifier != null ? injectedLanguageManager.getInjectionHost(nameIdentifier) : null;
    if (initialInjectedHost != null && initialInjectedHost != injectionHost) {
      return false;
    }
  }
  return textRange.shiftRight(startOffset).containsOffset(offset);
}