Search in sources :

Example 31 with InjectedLanguageManager

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

the class MemberInplaceRenamer method notSameFile.

@Override
protected boolean notSameFile(@Nullable VirtualFile file, @NotNull PsiFile containingFile) {
    final PsiFile currentFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
    if (currentFile == null)
        return true;
    InjectedLanguageManager manager = InjectedLanguageManager.getInstance(containingFile.getProject());
    return manager.getTopLevelFile(containingFile) != manager.getTopLevelFile(currentFile);
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager)

Example 32 with InjectedLanguageManager

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

the class InjectedGeneralHighlightingPass method getInjectedPsiFiles.

@NotNull
private Set<PsiFile> getInjectedPsiFiles(@NotNull final List<PsiElement> elements1, @NotNull final List<PsiElement> elements2, @NotNull final ProgressIndicator progress) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    List<DocumentWindow> injected = InjectedLanguageUtil.getCachedInjectedDocuments(myFile);
    final Collection<PsiElement> hosts = new THashSet<>(elements1.size() + elements2.size() + injected.size());
    //since change in one place can lead to invalidation of injected PSI in (completely) other place.
    for (DocumentWindow documentRange : injected) {
        progress.checkCanceled();
        if (!documentRange.isValid())
            continue;
        PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(documentRange);
        if (file == null)
            continue;
        PsiElement context = InjectedLanguageManager.getInstance(file.getProject()).getInjectionHost(file);
        if (context != null && context.isValid() && !file.getProject().isDisposed() && (myUpdateAll || myRestrictRange.intersects(context.getTextRange()))) {
            hosts.add(context);
        }
    }
    InjectedLanguageManagerImpl injectedLanguageManager = InjectedLanguageManagerImpl.getInstanceImpl(myProject);
    Processor<PsiElement> collectInjectableProcessor = Processors.cancelableCollectProcessor(hosts);
    injectedLanguageManager.processInjectableElements(elements1, collectInjectableProcessor);
    injectedLanguageManager.processInjectableElements(elements2, collectInjectableProcessor);
    final Set<PsiFile> outInjected = new THashSet<>();
    final PsiLanguageInjectionHost.InjectedPsiVisitor visitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() {

        @Override
        public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
            synchronized (outInjected) {
                outInjected.add(injectedPsi);
            }
        }
    };
    if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(new ArrayList<>(hosts), progress, true, element -> {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        progress.checkCanceled();
        InjectedLanguageUtil.enumerate(element, myFile, false, visitor);
        return true;
    })) {
        throw new ProcessCanceledException();
    }
    synchronized (outInjected) {
        return outInjected;
    }
}
Also used : Language(com.intellij.lang.Language) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) IElementType(com.intellij.psi.tree.IElementType) JobLauncher(com.intellij.concurrency.JobLauncher) InjectedLanguageManagerImpl(com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) Place(com.intellij.psi.impl.source.tree.injected.Place) Project(com.intellij.openapi.project.Project) HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) DumbAware(com.intellij.openapi.project.DumbAware) SyntaxHighlighterFactory(com.intellij.openapi.fileTypes.SyntaxHighlighterFactory) InjectedLanguageUtil(com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil) CommonProcessors(com.intellij.util.CommonProcessors) ProgressManager(com.intellij.openapi.progress.ProgressManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) Processors(com.intellij.util.Processors) HighlighterColors(com.intellij.openapi.editor.HighlighterColors) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) EditorColors(com.intellij.openapi.editor.colors.EditorColors) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull) THashSet(gnu.trove.THashSet) DocumentWindow(com.intellij.injected.editor.DocumentWindow) InjectedLanguageManagerImpl(com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with InjectedLanguageManager

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

the class LocalInspectionsPass method addHighlightsFromResults.

private void addHighlightsFromResults(@NotNull List<HighlightInfo> outInfos, @NotNull ProgressIndicator indicator) {
    InspectionProfile inspectionProfile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
    InjectedLanguageManager ilManager = InjectedLanguageManager.getInstance(myProject);
    Set<Pair<TextRange, String>> emptyActionRegistered = new THashSet<>();
    for (Map.Entry<PsiFile, List<InspectionResult>> entry : result.entrySet()) {
        indicator.checkCanceled();
        PsiFile file = entry.getKey();
        Document documentRange = documentManager.getDocument(file);
        if (documentRange == null)
            continue;
        List<InspectionResult> resultList = entry.getValue();
        synchronized (resultList) {
            for (InspectionResult inspectionResult : resultList) {
                indicator.checkCanceled();
                LocalInspectionToolWrapper tool = inspectionResult.tool;
                HighlightSeverity severity = inspectionProfile.getErrorLevel(HighlightDisplayKey.find(tool.getShortName()), file).getSeverity();
                for (ProblemDescriptor descriptor : inspectionResult.foundProblems) {
                    indicator.checkCanceled();
                    PsiElement element = descriptor.getPsiElement();
                    if (element != null) {
                        createHighlightsForDescriptor(outInfos, emptyActionRegistered, ilManager, file, documentRange, tool, severity, descriptor, element);
                    }
                }
            }
        }
    }
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) THashMap(gnu.trove.THashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Pair(com.intellij.openapi.util.Pair)

Example 34 with InjectedLanguageManager

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

the class LocalInspectionsPass method addDescriptorsFromInjectedResults.

private void addDescriptorsFromInjectedResults(@NotNull InspectionManager iManager, @NotNull GlobalInspectionContextImpl context) {
    InjectedLanguageManager ilManager = InjectedLanguageManager.getInstance(myProject);
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
    for (Map.Entry<PsiFile, List<InspectionResult>> entry : result.entrySet()) {
        PsiFile file = entry.getKey();
        // not injected
        if (file == getFile())
            continue;
        DocumentWindow documentRange = (DocumentWindow) documentManager.getDocument(file);
        List<InspectionResult> resultList = entry.getValue();
        for (InspectionResult inspectionResult : resultList) {
            LocalInspectionToolWrapper toolWrapper = inspectionResult.tool;
            for (ProblemDescriptor descriptor : inspectionResult.foundProblems) {
                PsiElement psiElement = descriptor.getPsiElement();
                if (psiElement == null)
                    continue;
                if (SuppressionUtil.inspectionResultSuppressed(psiElement, toolWrapper.getTool()))
                    continue;
                List<TextRange> editables = ilManager.intersectWithAllEditableFragments(file, ((ProblemDescriptorBase) descriptor).getTextRange());
                for (TextRange editable : editables) {
                    TextRange hostRange = documentRange.injectedToHost(editable);
                    QuickFix[] fixes = descriptor.getFixes();
                    LocalQuickFix[] localFixes = null;
                    if (fixes != null) {
                        localFixes = new LocalQuickFix[fixes.length];
                        for (int k = 0; k < fixes.length; k++) {
                            QuickFix fix = fixes[k];
                            localFixes[k] = (LocalQuickFix) fix;
                        }
                    }
                    ProblemDescriptor patchedDescriptor = iManager.createProblemDescriptor(getFile(), hostRange, descriptor.getDescriptionTemplate(), descriptor.getHighlightType(), true, localFixes);
                    addDescriptors(toolWrapper, patchedDescriptor, context);
                }
            }
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) THashMap(gnu.trove.THashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 35 with InjectedLanguageManager

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

the class LineMarkersPass method queryLineMarkersForInjected.

private static void queryLineMarkersForInjected(@NotNull PsiElement element, @NotNull final PsiFile containingFile, @NotNull Set<PsiFile> visitedInjectedFiles, @NotNull final PairConsumer<PsiElement, LineMarkerInfo> consumer) {
    if (containingFile.getViewProvider() instanceof InjectedFileViewProvider)
        return;
    final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(containingFile.getProject());
    InjectedLanguageUtil.enumerate(element, containingFile, false, (injectedPsi, places) -> {
        // there may be several concatenated literals making the one injected file
        if (!visitedInjectedFiles.add(injectedPsi))
            return;
        final Project project = injectedPsi.getProject();
        Document document = PsiDocumentManager.getInstance(project).getCachedDocument(injectedPsi);
        if (!(document instanceof DocumentWindow))
            return;
        List<PsiElement> injElements = CollectHighlightsUtil.getElementsInRange(injectedPsi, 0, injectedPsi.getTextLength());
        final List<LineMarkerProvider> providers = getMarkerProviders(injectedPsi.getLanguage(), project);
        queryProviders(injElements, injectedPsi, providers, (injectedElement, injectedMarker) -> {
            GutterIconRenderer gutterRenderer = injectedMarker.createGutterRenderer();
            TextRange injectedRange = new TextRange(injectedMarker.startOffset, injectedMarker.endOffset);
            List<TextRange> editables = manager.intersectWithAllEditableFragments(injectedPsi, injectedRange);
            for (TextRange editable : editables) {
                TextRange hostRange = manager.injectedToHost(injectedPsi, editable);
                Icon icon = gutterRenderer == null ? null : gutterRenderer.getIcon();
                GutterIconNavigationHandler<PsiElement> navigationHandler = injectedMarker.getNavigationHandler();
                LineMarkerInfo<PsiElement> converted = new LineMarkerInfo<>(injectedElement, hostRange, icon, injectedMarker.updatePass, e -> injectedMarker.getLineMarkerTooltip(), navigationHandler, GutterIconRenderer.Alignment.RIGHT);
                consumer.consume(injectedElement, converted);
            }
        });
    });
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) InjectedFileViewProvider(com.intellij.psi.impl.source.tree.injected.InjectedFileViewProvider) Project(com.intellij.openapi.project.Project) GutterIconRenderer(com.intellij.openapi.editor.markup.GutterIconRenderer) 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