Search in sources :

Example 1 with ConcurrentList

use of com.intellij.util.containers.ConcurrentList in project intellij-community by JetBrains.

the class InjectedLanguageManagerImpl method startRunInjectors.

@Override
public void startRunInjectors(@NotNull final Document hostDocument, final boolean synchronously) {
    if (myProject.isDisposed())
        return;
    if (!synchronously && ApplicationManager.getApplication().isWriteAccessAllowed())
        return;
    // use cached to avoid recreate PSI in alien project
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
    final PsiFile hostPsiFile = documentManager.getCachedPsiFile(hostDocument);
    if (hostPsiFile == null)
        return;
    final ConcurrentList<DocumentWindow> injected = InjectedLanguageUtil.getCachedInjectedDocuments(hostPsiFile);
    if (injected.isEmpty())
        return;
    if (myProgress.isCanceled()) {
        myProgress = new DaemonProgressIndicator();
    }
    final Set<DocumentWindow> newDocuments = Collections.synchronizedSet(new THashSet<>());
    final Processor<DocumentWindow> commitProcessor = documentWindow -> {
        if (myProject.isDisposed())
            return false;
        ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
        if (indicator != null && indicator.isCanceled())
            return false;
        if (documentManager.isUncommited(hostDocument) || !hostPsiFile.isValid())
            return false;
        InjectedLanguageUtil.enumerate(documentWindow, hostPsiFile, (injectedPsi, places) -> {
            DocumentWindow newDocument = (DocumentWindow) injectedPsi.getViewProvider().getDocument();
            if (newDocument != null) {
                PsiDocumentManagerBase.checkConsistency(injectedPsi, newDocument);
                newDocuments.add(newDocument);
            }
        });
        return true;
    };
    final Runnable commitInjectionsRunnable = () -> {
        if (myProgress.isCanceled())
            return;
        JobLauncher.getInstance().invokeConcurrentlyUnderProgress(new ArrayList<>(injected), myProgress, true, commitProcessor);
        synchronized (ourInjectionPsiLock) {
            injected.clear();
            injected.addAll(newDocuments);
        }
    };
    if (synchronously) {
        if (Thread.holdsLock(PsiLock.LOCK)) {
            // hack for the case when docCommit was called from within PSI modification, e.g. in formatter.
            // we can't spawn threads to do injections there, otherwise a deadlock is imminent
            ContainerUtil.process(new ArrayList<>(injected), commitProcessor);
        } else {
            commitInjectionsRunnable.run();
        }
    } else {
        JobLauncher.getInstance().submitToJobThread(() -> ApplicationManagerEx.getApplicationEx().tryRunReadAction(commitInjectionsRunnable), null);
    }
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) JobLauncher(com.intellij.concurrency.JobLauncher) VirtualFile(com.intellij.openapi.vfs.VirtualFile) EditorWindowImpl(com.intellij.injected.editor.EditorWindowImpl) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) DaemonCodeAnalyzer(com.intellij.codeInsight.daemon.DaemonCodeAnalyzer) ContainerUtil(com.intellij.util.containers.ContainerUtil) FileContextUtil(com.intellij.psi.impl.source.resolve.FileContextUtil) SmartList(com.intellij.util.SmartList) MultiHostInjector(com.intellij.lang.injection.MultiHostInjector) Project(com.intellij.openapi.project.Project) Segment(com.intellij.openapi.util.Segment) Logger(com.intellij.openapi.diagnostic.Logger) MultiHostRegistrar(com.intellij.lang.injection.MultiHostRegistrar) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) DumbService(com.intellij.openapi.project.DumbService) DocumentWindow(com.intellij.injected.editor.DocumentWindow) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) ExtensionPointListener(com.intellij.openapi.extensions.ExtensionPointListener) DocumentWindowImpl(com.intellij.injected.editor.DocumentWindowImpl) TextRange(com.intellij.openapi.util.TextRange) Disposable(com.intellij.openapi.Disposable) TestOnly(org.jetbrains.annotations.TestOnly) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ExtensionPointImpl(com.intellij.openapi.extensions.impl.ExtensionPointImpl) ConcurrentList(com.intellij.util.containers.ConcurrentList) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) Processor(com.intellij.util.Processor) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) PluginDescriptor(com.intellij.openapi.extensions.PluginDescriptor) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator) DocumentWindow(com.intellij.injected.editor.DocumentWindow) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)

Aggregations

DaemonCodeAnalyzer (com.intellij.codeInsight.daemon.DaemonCodeAnalyzer)1 DaemonProgressIndicator (com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)1 JobLauncher (com.intellij.concurrency.JobLauncher)1 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 DocumentWindowImpl (com.intellij.injected.editor.DocumentWindowImpl)1 EditorWindowImpl (com.intellij.injected.editor.EditorWindowImpl)1 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 MultiHostInjector (com.intellij.lang.injection.MultiHostInjector)1 MultiHostRegistrar (com.intellij.lang.injection.MultiHostRegistrar)1 Disposable (com.intellij.openapi.Disposable)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ApplicationManagerEx (com.intellij.openapi.application.ex.ApplicationManagerEx)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Document (com.intellij.openapi.editor.Document)1 ExtensionPoint (com.intellij.openapi.extensions.ExtensionPoint)1 ExtensionPointListener (com.intellij.openapi.extensions.ExtensionPointListener)1 Extensions (com.intellij.openapi.extensions.Extensions)1 PluginDescriptor (com.intellij.openapi.extensions.PluginDescriptor)1 ExtensionPointImpl (com.intellij.openapi.extensions.impl.ExtensionPointImpl)1