Search in sources :

Example 1 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.

the class PyTemporaryInjector method getInjectedLanguage.

@Nullable
@Override
public Language getInjectedLanguage(@NotNull PsiElement context) {
    final TemporaryPlacesRegistry registry = TemporaryPlacesRegistry.getInstance(context.getProject());
    if (context instanceof PsiLanguageInjectionHost) {
        final PsiFile file = context.getContainingFile();
        final InjectedLanguage injectedLanguage = registry.getLanguageFor((PsiLanguageInjectionHost) context, file);
        if (injectedLanguage != null) {
            return injectedLanguage.getLanguage();
        }
    }
    return null;
}
Also used : TemporaryPlacesRegistry(org.intellij.plugins.intelliLang.inject.TemporaryPlacesRegistry) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.

the class XsltCodeInsightUtil method getRangeInsideHost.

@NotNull
public static TextRange getRangeInsideHost(XPathElement expr) {
    final PsiLanguageInjectionHost host = PsiTreeUtil.getContextOfType(expr, PsiLanguageInjectionHost.class, true);
    assert host != null;
    final List<Pair<PsiElement, TextRange>> psi = InjectedLanguageManager.getInstance(host.getProject()).getInjectedPsiFiles(host);
    assert psi != null;
    for (Pair<PsiElement, TextRange> pair : psi) {
        if (PsiTreeUtil.isAncestor(pair.first, expr, false)) {
            return pair.second;
        }
    }
    assert false;
    return null;
}
Also used : PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.

the class ImportToggleAliasIntention method doInvoke.

@Override
public void doInvoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    // sanity check: isAvailable must have set it.
    final IntentionState state = IntentionState.fromContext(editor, file);
    //
    // we set in in the source
    final String target_name;
    // we replace it in the source
    final String remove_name;
    PyReferenceExpression reference = sure(state.myImportElement.getImportReferenceExpression());
    // search for references to us with the right name
    try {
        String imported_name = PyPsiUtils.toPath(reference);
        if (state.myAlias != null) {
            // have to remove alias, rename everything to original
            target_name = imported_name;
            remove_name = state.myAlias;
        } else {
            // ask for and add alias
            Application application = ApplicationManager.getApplication();
            if (application != null && !application.isUnitTestMode()) {
                String alias = Messages.showInputDialog(project, PyBundle.message("INTN.alias.for.$0.dialog.title", imported_name), "Add Alias", Messages.getQuestionIcon(), "", new InputValidator() {

                    @Override
                    public boolean checkInput(String inputString) {
                        return PyNames.isIdentifier(inputString);
                    }

                    @Override
                    public boolean canClose(String inputString) {
                        return PyNames.isIdentifier(inputString);
                    }
                });
                if (alias == null) {
                    return;
                }
                target_name = alias;
            } else {
                // test mode
                target_name = "alias";
            }
            remove_name = imported_name;
        }
        final PsiElement referee = reference.getReference().resolve();
        if (referee != null && imported_name != null) {
            final Collection<PsiReference> references = new ArrayList<>();
            final ScopeOwner scope = PsiTreeUtil.getParentOfType(state.myImportElement, ScopeOwner.class);
            PsiTreeUtil.processElements(scope, new PsiElementProcessor() {

                public boolean execute(@NotNull PsiElement element) {
                    getReferences(element);
                    if (element instanceof PyStringLiteralExpression) {
                        final PsiLanguageInjectionHost host = (PsiLanguageInjectionHost) element;
                        final List<Pair<PsiElement, TextRange>> files = InjectedLanguageManager.getInstance(project).getInjectedPsiFiles(host);
                        if (files != null) {
                            for (Pair<PsiElement, TextRange> pair : files) {
                                final PsiElement first = pair.getFirst();
                                if (first instanceof ScopeOwner) {
                                    final ScopeOwner scopeOwner = (ScopeOwner) first;
                                    PsiTreeUtil.processElements(scopeOwner, new PsiElementProcessor() {

                                        public boolean execute(@NotNull PsiElement element) {
                                            getReferences(element);
                                            return true;
                                        }
                                    });
                                }
                            }
                        }
                    }
                    return true;
                }

                private void getReferences(PsiElement element) {
                    if (element instanceof PyReferenceExpression && PsiTreeUtil.getParentOfType(element, PyImportElement.class) == null) {
                        PyReferenceExpression ref = (PyReferenceExpression) element;
                        if (remove_name.equals(PyPsiUtils.toPath(ref))) {
                            // filter out other names that might resolve to our target
                            PsiElement resolved = ref.getReference().resolve();
                            if (resolved == referee)
                                references.add(ref.getReference());
                        }
                    }
                }
            });
            // no references here is OK by us.
            if (showConflicts(project, findDefinitions(target_name, references, Collections.<PsiElement>emptySet()), target_name, null)) {
                // got conflicts
                return;
            }
            // alter the import element
            PyElementGenerator generator = PyElementGenerator.getInstance(project);
            final LanguageLevel languageLevel = LanguageLevel.forElement(state.myImportElement);
            if (state.myAlias != null) {
                // remove alias
                ASTNode node = sure(state.myImportElement.getNode());
                ASTNode parent = sure(node.getTreeParent());
                // this is the reference
                node = sure(node.getFirstChildNode());
                // things past the reference: space, 'as', and alias
                node = sure(node.getTreeNext());
                parent.removeRange(node, null);
            } else {
                // add alias
                ASTNode my_ielt_node = sure(state.myImportElement.getNode());
                PyImportElement fountain = generator.createFromText(languageLevel, PyImportElement.class, "import foo as " + target_name, new int[] { 0, 2 });
                // at import elt
                ASTNode graft_node = sure(fountain.getNode());
                // at ref
                graft_node = sure(graft_node.getFirstChildNode());
                // space
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // 'as'
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // space
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // alias
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
            }
            // alter references
            for (PsiReference ref : references) {
                ASTNode ref_name_node = sure(sure(ref.getElement()).getNode());
                ASTNode parent = sure(ref_name_node.getTreeParent());
                ASTNode new_name_node = generator.createExpressionFromText(languageLevel, target_name).getNode();
                assert new_name_node != null;
                parent.replaceChild(ref_name_node, new_name_node);
            }
        }
    } catch (IncorrectOperationException ignored) {
        PyUtil.showBalloon(project, PyBundle.message("QFIX.action.failed"), MessageType.WARNING);
    }
}
Also used : ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) InputValidator(com.intellij.openapi.ui.InputValidator) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Application(com.intellij.openapi.application.Application) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 4 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost 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 5 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost 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)

Aggregations

PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)41 PsiElement (com.intellij.psi.PsiElement)23 TextRange (com.intellij.openapi.util.TextRange)19 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)10 NotNull (org.jetbrains.annotations.NotNull)10 PsiFile (com.intellij.psi.PsiFile)9 Pair (com.intellij.openapi.util.Pair)8 Language (com.intellij.lang.Language)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)5 List (java.util.List)4 Nullable (org.jetbrains.annotations.Nullable)4 ASTNode (com.intellij.lang.ASTNode)3 XmlAttributeValueImpl (com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)3 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)3 InjectedLanguage (org.intellij.plugins.intelliLang.inject.InjectedLanguage)3 NonNls (org.jetbrains.annotations.NonNls)3 Project (com.intellij.openapi.project.Project)2 HtmlTag (com.intellij.psi.html.HtmlTag)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)1