Search in sources :

Example 16 with InjectedLanguageManager

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();
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) Project(com.intellij.openapi.project.Project)

Example 17 with InjectedLanguageManager

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();
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager)

Example 18 with InjectedLanguageManager

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;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Example 19 with InjectedLanguageManager

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;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 20 with InjectedLanguageManager

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));
}
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