less daemon restarts on deactivation
authorAlexey Kudravtsev <cdr@intellij.com>
Wed, 8 Sep 2010 08:37:35 +0000 (12:37 +0400)
committerAlexey Kudravtsev <cdr@intellij.com>
Fri, 10 Sep 2010 09:09:14 +0000 (13:09 +0400)
platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java
platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/PsiChangeHandler.java
platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileDocumentManagerImpl.java
platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/FileWatcher.java

index 35d85fbda3241e5ae9fc9492cef5b01be881c76d..a323b4124a8ad37bbcb6ad3f55b43437e5713565 100644 (file)
@@ -109,9 +109,11 @@ class DaemonListeners implements Disposable {
     eventMulticaster.addDocumentListener(new DocumentAdapter() {
       // clearing highlighters before changing document because change can damage editor highlighters drastically, so we'll clear more than necessary
       public void beforeDocumentChange(final DocumentEvent e) {
+        if (isUnderIgnoredAction(null)) return;
         Document document = e.getDocument();
         VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
-        if (!worthBothering(document, virtualFile == null ? null : ProjectUtil.guessProjectForFile(virtualFile))) {
+        Project project = virtualFile == null ? null : ProjectUtil.guessProjectForFile(virtualFile);
+        if (!worthBothering(document, project)) {
           return; //no need to stop daemon if something happened in the console
         }
         stopDaemon(true);
@@ -250,6 +252,12 @@ class DaemonListeners implements Disposable {
     LaterInvocator.addModalityStateListener(myModalityStateListener);
   }
 
+  static boolean isUnderIgnoredAction(Object action) {
+    if (action instanceof DocumentRunnable.IgnoreDocumentRunnable) return true;
+    DocumentRunnable currentWriteAction = ApplicationManager.getApplication().getCurrentWriteAction(DocumentRunnable.IgnoreDocumentRunnable.class);
+    return currentWriteAction instanceof DocumentRunnable.IgnoreDocumentRunnable;
+  }
+
   private boolean worthBothering(final Document document, Project project) {
     if (document == null) return true;
     if (project != null && project != myProject) return false;
@@ -310,33 +318,29 @@ class DaemonListeners implements Disposable {
 
   private class MyApplicationListener extends ApplicationAdapter {
     public void beforeWriteActionStart(Object action) {
-      if (!myDaemonCodeAnalyzer.isRunning()) return;
+      if (!myDaemonCodeAnalyzer.isRunning()) return; // we'll restart in writeActionFinished()
       if (LOG.isDebugEnabled()) {
         LOG.debug("cancelling code highlighting by write action:" + action);
       }
-      if (containsDocumentWorthBothering()) {
-        stopDaemon(false);
-      }
-    }
-
-    private boolean containsDocumentWorthBothering() {
-      DocumentRunnable currentWriteAction = ApplicationManager.getApplication().getCurrentWriteAction(DocumentRunnable.class);
-      if (currentWriteAction != null) {
-        if (currentWriteAction instanceof DocumentRunnable.IgnoreDocumentRunnable) return false;
-        Document document = currentWriteAction.getDocument();
-        if (!worthBothering(document, currentWriteAction.getProject())) {
-          return false;
-        }
+      if (containsDocumentWorthBothering(action)) {
+        stopDaemon(true);
       }
-      return true;
     }
 
     public void writeActionFinished(Object action) {
-      if (myDaemonCodeAnalyzer.isRunning()) return;
-      if (containsDocumentWorthBothering()) {
+      if (containsDocumentWorthBothering(action)) {
         stopDaemon(true);
       }
     }
+
+    private boolean containsDocumentWorthBothering(Object action) {
+      if (isUnderIgnoredAction(action)) return false;
+      DocumentRunnable currentWriteAction = action instanceof DocumentRunnable ? (DocumentRunnable)action
+                                                                               : ApplicationManager.getApplication().getCurrentWriteAction(DocumentRunnable.class);
+      if (currentWriteAction == null) return true;
+      Document document = currentWriteAction.getDocument();
+      return worthBothering(document, currentWriteAction.getProject());
+    }
   }
 
   private class MyCommandListener extends CommandAdapter {
@@ -345,9 +349,8 @@ class DaemonListeners implements Disposable {
 
     public void commandStarted(CommandEvent event) {
       Document affectedDocument = extractDocumentFromCommand(event);
-      if (!worthBothering(affectedDocument, event.getProject())) {
-        return;
-      }
+      if (isUnderIgnoredAction(null)) return;
+      if (!worthBothering(affectedDocument, event.getProject())) return;
 
       cutOperationJustHappened = myCutActionName.equals(event.getCommandName());
       if (!myDaemonCodeAnalyzer.isRunning()) return;
@@ -373,9 +376,8 @@ class DaemonListeners implements Disposable {
 
     public void commandFinished(CommandEvent event) {
       Document affectedDocument = extractDocumentFromCommand(event);
-      if (!worthBothering(affectedDocument, event.getProject())) {
-        return;
-      }
+      if (isUnderIgnoredAction(null)) return;
+      if (!worthBothering(affectedDocument, event.getProject())) return;
 
       if (myEscPressed) {
         myEscPressed = false;
index afc877a3f6ddcdef849aef5adf6212b9ecc66a0f..9e971ea035f9de5e67f7f044d887aa4600c98cd9 100644 (file)
@@ -92,6 +92,7 @@ public class PsiChangeHandler extends PsiTreeChangeAdapter implements Disposable
   }
 
   private void updateChangesForDocument(@NotNull Document document) {
+    if (DaemonListeners.isUnderIgnoredAction(null)) return;
     List<Pair<PsiElement, Boolean>> toUpdate = changedElements.get(document);
     if (toUpdate != null) {
       for (Pair<PsiElement, Boolean> changedElement : toUpdate) {
index 898eef9e0bdd54ab15bb80d950ad94a44aa3ee7f..9e48bb3e6db7813d8f1ba5dc8b23feca368707c2 100644 (file)
@@ -28,6 +28,7 @@ import com.intellij.openapi.diff.SimpleDiffRequest;
 import com.intellij.openapi.diff.ex.DiffPanelOptions;
 import com.intellij.openapi.diff.impl.DiffPanelImpl;
 import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.editor.DocumentRunnable;
 import com.intellij.openapi.editor.EditorFactory;
 import com.intellij.openapi.editor.event.DocumentAdapter;
 import com.intellij.openapi.editor.event.DocumentEvent;
@@ -65,7 +66,10 @@ import java.io.Writer;
 import java.lang.ref.Reference;
 import java.lang.ref.SoftReference;
 import java.lang.ref.WeakReference;
-import java.util.*;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public class FileDocumentManagerImpl extends FileDocumentManager implements ApplicationComponent, VirtualFileListener, SafeWriteRequestor {
   private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl");
@@ -217,13 +221,11 @@ public class FileDocumentManagerImpl extends FileDocumentManager implements Appl
   public void saveDocument(@NotNull final Document document) {
     if (!myUnsavedDocuments.contains(document)) return;
 
-    ApplicationManager.getApplication().runWriteAction(
-      new Runnable() {
-        public void run() {
-          _saveDocument(document);
-        }
+    ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) {
+      public void run() {
+        _saveDocument(document);
       }
-    );
+    });
   }
 
   private void _saveDocument(@NotNull final Document document) {
index d5bb3b448436b8e25c06e38116b3163aeba892c2..ee9e790bb32dc8f81a8dfea3ec54a5ec4e6e152e 100644 (file)
@@ -372,13 +372,19 @@ public class FileWatcher {
 
     synchronized (LOCK) {
       for (String root : myRecursiveWatchRoots) {
-        if (FileUtil.startsWith(path, root)) return true;
+        if (FileUtil.startsWith(path, root)) {
+          return true;
+        }
       }
 
       for (String root : myFlatWatchRoots) {
-        if (FileUtil.pathsEqual(path, root)) return true;
+        if (FileUtil.pathsEqual(path, root)) {
+          return true;
+        }
         final File parentFile = new File(path).getParentFile();
-        if (parentFile != null && FileUtil.pathsEqual(parentFile.getPath(), root)) return true;
+        if (parentFile != null && FileUtil.pathsEqual(parentFile.getPath(), root)) {
+          return true;
+        }
       }
     }