private void updateLater() { myUpdateAlarm.cancelAllRequests(); myUpdateAlarm.addRequest(new Runnable() { @Override public void run() { if (myProject.isDisposed()) return; PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject); documentManager.cancelAndRunWhenAllCommitted("UpdateUsageView", new Runnable() { @Override public void run() { updateImmediately(); } }); } }, 300); }
private static boolean cacheEverything(@NotNull Place place, @NotNull DocumentWindowImpl documentWindow, @NotNull InjectedFileViewProvider viewProvider, @NotNull PsiFile psiFile, @NotNull SmartPsiElementPointer<PsiLanguageInjectionHost> pointer) { FileDocumentManagerImpl.registerDocument(documentWindow, viewProvider.getVirtualFile()); DebugUtil.startPsiModification("MultiHostRegistrar cacheEverything"); try { viewProvider.forceCachedPsi(psiFile); } finally { DebugUtil.finishPsiModification(); } psiFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT, pointer); PsiDocumentManagerBase.cachePsi(documentWindow, psiFile); keepTreeFromChameleoningBack(psiFile); return viewProvider.setShreds(place, psiFile.getProject()); }
@Override @Nullable Segment getRange() { if (hasRange()) { Document document = getDocumentToSynchronize(); if (document != null) { PsiDocumentManagerBase documentManager = myManager.getPsiDocumentManager(); List<DocumentEvent> events = documentManager.getEventsSinceCommit(document); if (!events.isEmpty()) { SmartPointerTracker tracker = myManager.getTracker(getVirtualFile()); if (tracker != null) { return tracker.getUpdatedRange(this, (FrozenDocument)documentManager.getLastCommittedDocument(document), events); } } } } return calcPsiRange(); }
@Override public void doneInjecting() { checkThreading(); try { if (myLanguageVersion == null) { throw new IllegalStateException("Seems you haven't called startInjecting()"); } if (placeInfos.isEmpty()) { throw new IllegalStateException("Seems you haven't called addPlace()"); } Language forcedLanguage = myContextElement.getUserData(InjectedFileViewProvider.LANGUAGE_FOR_INJECTED_COPY_KEY); checkForCorrectContextElement(placeInfos, myContextElement, myLanguageVersion, myHostPsiFile, myHostVirtualFile, myHostDocument); synchronized (InjectedLanguageManagerImpl.ourInjectionPsiLock) { PsiFile psiFile = createInjectedFile(myLanguageVersion, forcedLanguage, myHostDocument, myHostVirtualFile, myHostPsiFile, fileExtension, placeInfos); addFileToResults(psiFile); PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject); DocumentWindowImpl documentWindow = (DocumentWindowImpl)documentManager.getDocument(psiFile); assertEverythingIsAllright(documentManager, documentWindow, psiFile); } } finally { clear(); } }
@Override @Nullable public Segment getRange() { if (myHasRange) { Document document = getDocumentToSynchronize(); if (document != null) { PsiDocumentManagerBase documentManager = myManager.getPsiDocumentManager(); List<DocumentEvent> events = documentManager.getEventsSinceCommit(document); if (!events.isEmpty()) { return myMarkerCache.getUpdatedRange(markerCacheKey(), (FrozenDocument)documentManager.getLastCommittedDocument(document), events); } } } return calcPsiRange(); }
@Nullable @Override public Segment getPsiRange() { Document currentDoc = FileDocumentManager.getInstance().getCachedDocument(myVirtualFile); Document committedDoc = currentDoc == null ? null : ((PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject)).getLastCommittedDocument(currentDoc); return committedDoc == null ? getRange() : new TextRange(0, committedDoc.getTextLength()); }
@NotNull private Runnable createUpdateRunnable() { return new Runnable() { @Override public void run() { ApplicationManager.getApplication().assertIsDispatchThread(); if (myDisposed || !myProject.isInitialized() || PowerSaveMode.isEnabled()) { return; } if (HeavyProcessLatch.INSTANCE.isRunning()) { if (myAlarm.isEmpty()) { // wait for heavy processing to stop, re-schedule daemon but not too soon myAlarm.addRequest(myUpdateRunnable, Math.max(mySettings.AUTOREPARSE_DELAY,100)); } return; } Editor activeEditor = FileEditorManager.getInstance(myProject).getSelectedTextEditor(); if (activeEditor == null) { submitPassesRunnable.run(); } else { ((PsiDocumentManagerBase)myPsiDocumentManager).cancelAndRunWhenAllCommitted("start daemon when all committed", submitPassesRunnable); } } }; }
@NotNull public static DocumentWindow freezeWindow(@NotNull DocumentWindowImpl window) { Place shreds = window.getShreds(); Project project = shreds.getHostPointer().getProject(); DocumentEx delegate = ((PsiDocumentManagerBase)PsiDocumentManager.getInstance(project)).getLastCommittedDocument(window.getDelegate()); Place place = new Place(ContainerUtil.map(shreds, new Function<PsiLanguageInjectionHost.Shred, PsiLanguageInjectionHost.Shred>() { @Override public PsiLanguageInjectionHost.Shred fun(final PsiLanguageInjectionHost.Shred shred) { return ((ShredImpl) shred).withPsiRange(); } })); return new DocumentWindowImpl(delegate, window.isOneLine(), place); }
public TagNameSynchronizer(Editor editor, Project project, Language language) { myEditor = editor; myLanguage = language; final Disposable disposable = ((EditorImpl)editor).getDisposable(); final Document document = editor.getDocument(); document.addDocumentListener(this, disposable); editor.putUserData(SYNCHRONIZER_KEY, this); myDocumentManager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(project); }
private void updateLater() { myUpdateAlarm.cancelAllRequests(); myUpdateAlarm.addRequest(() -> { if (myProject.isDisposed()) return; PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject); documentManager.cancelAndRunWhenAllCommitted("UpdateUsageView", this::updateImmediately); }, 300); }
public static Segment calcActualRangeAfterDocumentEvents(@Nonnull PsiFile containingFile, @Nonnull Document document, @Nonnull Segment segment, boolean isSegmentGreedy) { Project project = containingFile.getProject(); PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(project); List<DocumentEvent> events = documentManager.getEventsSinceCommit(document); if (!events.isEmpty()) { SmartPointerManagerImpl pointerManager = (SmartPointerManagerImpl)SmartPointerManager.getInstance(project); SmartPointerTracker tracker = pointerManager.getTracker(containingFile.getViewProvider().getVirtualFile()); if (tracker != null) { return tracker.getUpdatedRange(containingFile, segment, isSegmentGreedy, (FrozenDocument)documentManager.getLastCommittedDocument(document), events); } } return null; }
@Override public void run() { ApplicationManager.getApplication().assertIsDispatchThread(); Project project = myProject; DaemonCodeAnalyzerImpl daemonCodeAnalyzer; if (project == null || !project.isInitialized() || project.isDisposed() || PowerSaveMode.isEnabled() || (daemonCodeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(project)).myDisposed) { return; } // wait for heavy processing to stop, re-schedule daemon but not too soon if (HeavyProcessLatch.INSTANCE.isRunning()) { HeavyProcessLatch.INSTANCE.executeOutOfHeavyProcess(() -> daemonCodeAnalyzer.stopProcess(true, "re-scheduled to execute after heavy processing finished")); return; } Editor activeEditor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (activeEditor == null) { AutoPopupController.runTransactionWithEverythingCommitted(project, daemonCodeAnalyzer.submitPassesRunnable); } else { ((PsiDocumentManagerBase)PsiDocumentManager.getInstance(project)).cancelAndRunWhenAllCommitted("start daemon when all committed", daemonCodeAnalyzer.submitPassesRunnable); } }
private static void assertEverythingIsAllright(@Nonnull PsiDocumentManagerBase documentManager, @Nonnull DocumentWindowImpl documentWindow, @Nonnull PsiFile psiFile) { InjectedFileViewProvider injectedFileViewProvider = (InjectedFileViewProvider)psiFile.getViewProvider(); assert injectedFileViewProvider.isValid() : "Invalid view provider: " + injectedFileViewProvider; DocumentEx frozenWindow = documentManager.getLastCommittedDocument(documentWindow); assert psiFile.textMatches(frozenWindow.getText()) : "Document window text mismatch"; assert injectedFileViewProvider.getDocument() == documentWindow : "Provider document mismatch"; assert documentManager.getCachedDocument(psiFile) == documentWindow : "Cached document mismatch"; assert Comparing.equal(psiFile.getVirtualFile(), injectedFileViewProvider.getVirtualFile()) : "Virtual file mismatch: " + psiFile.getVirtualFile() + "; " + injectedFileViewProvider.getVirtualFile(); PsiDocumentManagerBase.checkConsistency(psiFile, frozenWindow); }
@Nonnull @Override public DocumentWindow freezeWindow(@Nonnull DocumentWindow document) { Place shreds = ((DocumentWindowImpl)document).getShreds(); Project project = shreds.getHostPointer().getProject(); DocumentEx delegate = ((PsiDocumentManagerBase)PsiDocumentManager.getInstance(project)).getLastCommittedDocument(document.getDelegate()); Place place = new Place(); place.addAll(ContainerUtil.map(shreds, shred -> ((ShredImpl)shred).withPsiRange())); return new DocumentWindowImpl(delegate, place); }
private void reparseByTextIfNeeded(@Nonnull FileViewProvider viewProvider, @Nonnull Document document) { if (PsiDocumentManager.getInstance(myProject).isCommitted(document)) { for (PsiFile file : viewProvider.getAllFiles()) { if (file.getUserData(REPARSE_PENDING) != null) { ((PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject)).reparseFileFromText((PsiFileImpl)file); file.putUserData(REPARSE_PENDING, null); } } } }
public TagNameSynchronizer(Editor editor, Project project, Language language) { myEditor = editor; myLanguage = language; final Disposable disposable = ((EditorImpl) editor).getDisposable(); final Document document = editor.getDocument(); document.addDocumentListener(this, disposable); editor.putUserData(SYNCHRONIZER_KEY, this); myDocumentManager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(project); }
public SmartPointerManagerImpl(Project project) { myProject = project; myPsiDocManager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject); POINTERS_KEY = Key.create("SMART_POINTERS for "+project); }
PsiDocumentManagerBase getPsiDocumentManager() { return myPsiDocManager; }
@Override public void run() { PassExecutorService.log(getUpdateProgress(), null, "Update Runnable. myUpdateByTimerEnabled:", myUpdateByTimerEnabled, " something disposed:", PowerSaveMode.isEnabled() || myDisposed || !myProject.isInitialized(), " activeEditors:", myProject.isDisposed() ? null : getSelectedEditors()); if (!myUpdateByTimerEnabled) return; if (myDisposed) return; ApplicationManager.getApplication().assertIsDispatchThread(); final Collection<FileEditor> activeEditors = getSelectedEditors(); if (activeEditors.isEmpty()) return; if (ApplicationManager.getApplication().isWriteAccessAllowed()) { // makes no sense to start from within write action, will cancel anyway // we'll restart when the write action finish return; } final PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase)myPsiDocumentManager; if (documentManager.hasUncommitedDocuments()) { documentManager.cancelAndRunWhenAllCommitted("restart daemon when all committed", this); return; } if (RefResolveService.ENABLED && !RefResolveService.getInstance(myProject).isUpToDate() && RefResolveService.getInstance(myProject).getQueueSize() == 1) { return; // if the user have just typed in something, wait until the file is re-resolved // (or else it will blink like crazy since unused symbols calculation depends on resolve service) } Map<FileEditor, HighlightingPass[]> passes = new THashMap<FileEditor, HighlightingPass[]>(activeEditors.size()); for (FileEditor fileEditor : activeEditors) { BackgroundEditorHighlighter highlighter = fileEditor.getBackgroundHighlighter(); if (highlighter != null) { HighlightingPass[] highlightingPasses = highlighter.createPassesForEditor(); passes.put(fileEditor, highlightingPasses); } } // cancel all after calling createPasses() since there are perverts {@link com.intellij.util.xml.ui.DomUIFactoryImpl} who are changing PSI there cancelUpdateProgress(true, "Cancel by alarm"); myAlarm.cancelAllRequests(); DaemonProgressIndicator progress = createUpdateProgress(); myPassExecutorService.submitPasses(passes, progress); }
public SmartPointerManagerImpl(Project project) { myProject = project; myPsiDocManager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject); LowMemoryWatcher.register(() -> processQueue(), project); }
@Override public void run() { boolean updateByTimerEnabled = isUpdateByTimerEnabled(); PassExecutorService.log(getUpdateProgress(), null, "Update Runnable. myUpdateByTimerEnabled:", updateByTimerEnabled, " something disposed:", PowerSaveMode.isEnabled() || myDisposed || !myProject.isInitialized(), " activeEditors:", myProject.isDisposed() ? null : getSelectedEditors()); if (!updateByTimerEnabled) return; if (myDisposed) return; ApplicationManager.getApplication().assertIsDispatchThread(); final Collection<FileEditor> activeEditors = getSelectedEditors(); if (activeEditors.isEmpty()) return; if (ApplicationManager.getApplication().isWriteAccessAllowed()) { // makes no sense to start from within write action, will cancel anyway // we'll restart when the write action finish return; } final PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase)myPsiDocumentManager; if (documentManager.hasUncommitedDocuments()) { documentManager.cancelAndRunWhenAllCommitted("restart daemon when all committed", this); return; } if (RefResolveService.ENABLED && !RefResolveService.getInstance(myProject).isUpToDate() && RefResolveService.getInstance(myProject).getQueueSize() == 1) { return; // if the user have just typed in something, wait until the file is re-resolved // (or else it will blink like crazy since unused symbols calculation depends on resolve service) } Map<FileEditor, HighlightingPass[]> passes = new THashMap<>(activeEditors.size()); for (FileEditor fileEditor : activeEditors) { BackgroundEditorHighlighter highlighter = fileEditor.getBackgroundHighlighter(); if (highlighter != null) { HighlightingPass[] highlightingPasses = highlighter.createPassesForEditor(); passes.put(fileEditor, highlightingPasses); } } // cancel all after calling createPasses() since there are perverts {@link com.intellij.util.xml.ui.DomUIFactoryImpl} who are changing PSI there cancelUpdateProgress(true, "Cancel by alarm"); myUpdateRunnableFuture.cancel(false); DaemonProgressIndicator progress = createUpdateProgress(); myPassExecutorService.submitPasses(passes, progress); }