Search in sources :

Example 1 with InjectedLanguageManager

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

the class AddImportHelper method addFromImportStatement.

/**
   * Adds a new {@link PyFromImportStatement} statement within other top-level imports or as specified by anchor.
   *
   * @param file      where to operate
   * @param newImport new "from import" statement to insert. It may be generated, because it won't be used for resolving anyway. 
   *                  You might want to use overloaded version of this method to generate such statement automatically.
   * @param anchor    place where the imported name was used. It will be used to determine proper block where new import should be inserted,
   *                  e.g. inside conditional block or try/except statement. Also if anchor is another import statement, new import statement
   *                  will be inserted right after it.
   * @see #addFromImportStatement(PsiFile, String, String, String, ImportPriority, PsiElement)
   * @see #addFromImportStatement
   */
public static void addFromImportStatement(@NotNull PsiFile file, @NotNull PyFromImportStatement newImport, @Nullable ImportPriority priority, @Nullable PsiElement anchor) {
    try {
        final PyImportStatementBase parentImport = PsiTreeUtil.getParentOfType(anchor, PyImportStatementBase.class, false);
        final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(file.getProject());
        final PsiLanguageInjectionHost injectionHost = manager.getInjectionHost(file);
        final boolean insideDoctest = file instanceof PyDocstringFile && injectionHost != null && DocStringUtil.getParentDefinitionDocString(injectionHost) == injectionHost;
        final PsiElement insertParent;
        if (parentImport != null && parentImport.getContainingFile() == file) {
            insertParent = parentImport.getParent();
        } else if (injectionHost != null && !insideDoctest) {
            insertParent = manager.getTopLevelFile(file);
        } else {
            insertParent = file;
        }
        if (insideDoctest) {
            final PsiElement element = insertParent.addBefore(newImport, getInsertPosition(insertParent, newImport, priority));
            PsiElement whitespace = element.getNextSibling();
            if (!(whitespace instanceof PsiWhiteSpace)) {
                whitespace = PsiParserFacade.SERVICE.getInstance(file.getProject()).createWhiteSpaceFromText("  >>> ");
            }
            insertParent.addBefore(whitespace, element);
        } else if (anchor instanceof PyImportStatementBase) {
            insertParent.addAfter(newImport, anchor);
        } else {
            insertParent.addBefore(newImport, getInsertPosition(insertParent, newImport, priority));
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PyDocstringFile(com.jetbrains.python.documentation.doctest.PyDocstringFile)

Example 2 with InjectedLanguageManager

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

the class PyDocReference method getHostScopeOwner.

@Nullable
private ScopeOwner getHostScopeOwner() {
    final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myElement.getProject());
    final PsiLanguageInjectionHost host = languageManager.getInjectionHost(myElement);
    if (host != null) {
        final PsiFile file = host.getContainingFile();
        ScopeOwner result = ScopeUtil.getScopeOwner(host);
        if (result == null && file instanceof ScopeOwner) {
            result = (ScopeOwner) file;
        }
        return result;
    }
    return null;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with InjectedLanguageManager

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

the class PyDocReference method findContainingStringNode.

@Nullable
private static ASTNode findContainingStringNode(@NotNull PsiElement injectedElement, @NotNull PyStringLiteralExpression host) {
    final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(host.getProject());
    final List<Pair<PsiElement, TextRange>> files = manager.getInjectedPsiFiles(host);
    if (files != null) {
        final PsiFile injectedFile = injectedElement.getContainingFile();
        final Pair<PsiElement, TextRange> first = ContainerUtil.find(files, pair -> pair.getFirst() == injectedFile);
        if (first != null) {
            final int hostOffset = -host.getTextRange().getStartOffset();
            for (ASTNode node : host.getStringNodes()) {
                final TextRange relativeNodeRange = node.getTextRange().shiftRight(hostOffset);
                if (relativeNodeRange.contains(first.getSecond())) {
                    return node;
                }
            }
        }
    }
    return null;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with InjectedLanguageManager

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

the class PyDocReference method multiResolve.

@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
    ResolveResult[] results = super.multiResolve(incompleteCode);
    if (results.length == 0) {
        final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myElement.getProject());
        final PsiLanguageInjectionHost host = languageManager.getInjectionHost(myElement);
        final String referencedName = myElement.getReferencedName();
        if (referencedName == null)
            return ResolveResult.EMPTY_ARRAY;
        if (host != null) {
            final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host);
            if (files != null) {
                for (Pair<PsiElement, TextRange> pair : files) {
                    final PyResolveProcessor processor = new PyResolveProcessor(referencedName);
                    PyResolveUtil.scopeCrawlUp(processor, (ScopeOwner) pair.getFirst(), referencedName, pair.getFirst());
                    final List<RatedResolveResult> resultList = getResultsFromProcessor(referencedName, processor, pair.getFirst(), pair.getFirst());
                    if (resultList.size() > 0) {
                        List<RatedResolveResult> ret = RatedResolveResult.sorted(resultList);
                        return ret.toArray(new RatedResolveResult[ret.size()]);
                    }
                }
            }
            final PyResolveProcessor processor = new PyResolveProcessor(referencedName);
            final ScopeOwner scopeOwner = getHostScopeOwner();
            if (scopeOwner != null) {
                final PsiFile topLevel = scopeOwner.getContainingFile();
                PyResolveUtil.scopeCrawlUp(processor, scopeOwner, referencedName, topLevel);
                final PsiElement referenceAnchor = getScopeControlFlowAnchor(host);
                final List<RatedResolveResult> resultList = getResultsFromProcessor(referencedName, processor, referenceAnchor, topLevel);
                if (resultList.size() > 0) {
                    final List<RatedResolveResult> ret = RatedResolveResult.sorted(resultList);
                    return ret.toArray(new RatedResolveResult[ret.size()]);
                }
            }
        }
    }
    return results;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with InjectedLanguageManager

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

the class PyInjectionResolveTest method doResolve.

@Override
protected PsiElement doResolve() {
    myFixture.configureByFile("resolve/" + getTestName(false) + ".py");
    final PsiFile psiFile = myFixture.getFile();
    final int markerOffset = findMarkerOffset(psiFile);
    final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(myFixture.getProject());
    final PsiElement injectedElement = manager.findInjectedElementAt(psiFile, markerOffset);
    assertNotNull("no injected element found at <ref> position", injectedElement);
    PsiReference reference = null;
    final PyReferenceOwner referenceOwner = PsiTreeUtil.getParentOfType(injectedElement, PyReferenceOwner.class);
    if (referenceOwner != null) {
        reference = referenceOwner.getReference();
    }
    assertNotNull("no reference found at <ref> position", reference);
    return reference.resolve();
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

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