Search in sources :

Example 6 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager 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());
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 7 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class LowLevelSearchUtil method processTreeUp.

/**
   * @return null to stop or last found TreeElement
   * to be reused via <code>lastElement<code/> param in subsequent calls to avoid full tree rescan (n^2->n).
   */
private static TreeElement processTreeUp(@NotNull Project project, @NotNull TextOccurenceProcessor processor, @NotNull PsiElement scope, @NotNull StringSearcher searcher, final int offset, final boolean processInjectedPsi, @NotNull ProgressIndicator progress, TreeElement lastElement) {
    if (scope instanceof PsiCompiledElement) {
        throw new IllegalArgumentException("Scope is compiled, can't scan: " + scope);
    }
    final int scopeStartOffset = scope.getTextRange().getStartOffset();
    final int patternLength = searcher.getPatternLength();
    ASTNode scopeNode = scope.getNode();
    boolean useTree = scopeNode != null;
    assert scope.isValid();
    int start;
    TreeElement leafNode = null;
    PsiElement leafElement = null;
    if (useTree) {
        leafNode = findNextLeafElementAt(scopeNode, lastElement, offset);
        if (leafNode == null)
            return lastElement;
        start = offset - leafNode.getStartOffset() + scopeStartOffset;
    } else {
        if (scope instanceof PsiFile) {
            leafElement = ((PsiFile) scope).getViewProvider().findElementAt(offset, scope.getLanguage());
        } else {
            leafElement = scope.findElementAt(offset);
        }
        if (leafElement == null)
            return lastElement;
        assert leafElement.isValid();
        start = offset - leafElement.getTextRange().getStartOffset() + scopeStartOffset;
    }
    if (start < 0) {
        throw new AssertionError("offset=" + offset + "; scopeStartOffset=" + scopeStartOffset + "; leafElement=" + leafElement + ";  scope=" + scope + "; leafElement.isValid(): " + (leafElement == null ? null : leafElement.isValid()));
    }
    InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(project);
    lastElement = leafNode;
    boolean contains = false;
    PsiElement prev = null;
    TreeElement prevNode = null;
    PsiElement run = null;
    while (run != scope) {
        progress.checkCanceled();
        if (useTree) {
            start += prevNode == null ? 0 : prevNode.getStartOffsetInParent();
            prevNode = leafNode;
            run = leafNode.getPsi();
        } else {
            start += prev == null ? 0 : prev.getStartOffsetInParent();
            prev = run;
            run = leafElement;
        }
        //do not compute if already contains
        if (!contains)
            contains = run.getTextLength() - start >= patternLength;
        if (contains) {
            if (processInjectedPsi) {
                Boolean result = processInjectedFile(run, processor, searcher, progress, injectedLanguageManager);
                if (result != null) {
                    return result.booleanValue() ? lastElement : null;
                }
            }
            if (!processor.execute(run, start)) {
                return null;
            }
        }
        if (useTree) {
            leafNode = leafNode.getTreeParent();
            if (leafNode == null)
                break;
        } else {
            leafElement = leafElement.getParent();
            if (leafElement == null)
                break;
        }
    }
    assert run == scope : "Malbuilt PSI; scopeNode: " + scope + "; containingFile:" + PsiTreeUtil.getParentOfType(scope, PsiFile.class, false) + "; leafNode: " + run + "; isAncestor=" + PsiTreeUtil.isAncestor(scope, run, false) + "; in same file: " + (PsiTreeUtil.getParentOfType(scope, PsiFile.class, false) == PsiTreeUtil.getParentOfType(run, PsiFile.class, false));
    return lastElement;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) ASTNode(com.intellij.lang.ASTNode) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 8 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class InjectedGeneralHighlightingPass method addInjectedPsiHighlights.

// returns false if canceled
private boolean addInjectedPsiHighlights(@NotNull final Set<PsiFile> injectedFiles, @NotNull final ProgressIndicator progress, @NotNull final Collection<HighlightInfo> outInfos) {
    if (injectedFiles.isEmpty())
        return true;
    final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(myProject);
    final TextAttributes injectedAttributes = myGlobalScheme.getAttributes(EditorColors.INJECTED_LANGUAGE_FRAGMENT);
    return JobLauncher.getInstance().invokeConcurrentlyUnderProgress(new ArrayList<>(injectedFiles), progress, isFailFastOnAcquireReadAction(), injectedPsi -> addInjectedPsiHighlights(injectedPsi, injectedAttributes, outInfos, progress, injectedLanguageManager));
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 9 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class HighlightUsagesHandler method getNameIdentifierRange.

@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;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PomTarget(com.intellij.pom.PomTarget) TextRange(com.intellij.openapi.util.TextRange) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) PsiDeclaredTarget(com.intellij.pom.PsiDeclaredTarget) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class TemplateLanguageErrorFilter method shouldHighlightErrorElement.

@Override
public boolean shouldHighlightErrorElement(@NotNull 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;
        }
        //
        if (shouldIgnoreErrorAt(viewProvider, offset) || shouldIgnoreErrorAt(viewProvider, offset + 1))
            return false;
    }
    return true;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager)

Aggregations

InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)41 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)12 PsiFile (com.intellij.psi.PsiFile)11 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)10 Pair (com.intellij.openapi.util.Pair)7 Nullable (org.jetbrains.annotations.Nullable)7 Document (com.intellij.openapi.editor.Document)5 Project (com.intellij.openapi.project.Project)5 DocumentWindow (com.intellij.injected.editor.DocumentWindow)4 NotNull (org.jetbrains.annotations.NotNull)4 ASTNode (com.intellij.lang.ASTNode)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)2 THashMap (gnu.trove.THashMap)2 THashSet (gnu.trove.THashSet)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 HighlightInfoHolder (com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder)1 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1