IDEA-130532 Bookmarks: merging lines with Delete or Backspace keys joins several... appcode/138.2661
authorVassiliy.Kudryashov <Vassiliy.Kudryashov@jetbrains.com>
Sat, 4 Oct 2014 19:34:18 +0000 (23:34 +0400)
committerVassiliy.Kudryashov <Vassiliy.Kudryashov@jetbrains.com>
Sat, 4 Oct 2014 19:34:47 +0000 (23:34 +0400)
platform/core-impl/src/com/intellij/openapi/editor/impl/PersistentRangeMarkerUtil.java
platform/lang-impl/src/com/intellij/ide/bookmarks/Bookmark.java
platform/lang-impl/src/com/intellij/ide/bookmarks/BookmarkManager.java

index 2e78c2cc067b475f79b41ea6a61f3f1bfb126861..5a8b633ff95bcc534cc6cbb98e1efd3ffa274e30 100644 (file)
@@ -48,6 +48,11 @@ public class PersistentRangeMarkerUtil {
       return false;
     }
 
+    if (rangeMarker.getStartOffset() == rangeMarker.getEndOffset() && e.getOffset() == rangeMarker.getStartOffset() && e.getOldLength() == 0) {
+      //One-point range marker
+      return true;
+    }
+
     if (e.getOffset() >= rangeMarker.getEndOffset() || e.getOffset() + e.getOldLength() <= rangeMarker.getStartOffset()) {
       // Don't perform complex processing if the change doesn't affect target range.
       return false;
index d22e251ed4ca64262dee1c13ae73988ac8c4a426..818acef1bcc7426078bb0c9a0cd360127ebb2df1 100644 (file)
@@ -120,8 +120,9 @@ public class Bookmark implements Navigatable {
     return myHighlighter;
   }
 
+  @Nullable
   public Document getDocument() {
-    return FileDocumentManager.getInstance().getDocument(getFile());
+    return FileDocumentManager.getInstance().getCachedDocument(getFile());
   }
 
   public void release() {
index e97e800f7115999c3946e12e721dad97610948ec..1d61ab4d986d4a25ab84bedcc148c78497660cc2 100644 (file)
@@ -18,7 +18,6 @@ package com.intellij.ide.bookmarks;
 
 import com.intellij.ide.IdeBundle;
 import com.intellij.openapi.components.*;
-import com.intellij.openapi.components.StoragePathMacros;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.EditorFactory;
@@ -214,9 +213,10 @@ public class BookmarkManager extends AbstractProjectComponent implements Persist
   }
 
   public void removeBookmark(@NotNull Bookmark bookmark) {
-    myBookmarks.remove(bookmark);
-    bookmark.release();
-    myBus.syncPublisher(BookmarksListener.TOPIC).bookmarkRemoved(bookmark);
+    if (myBookmarks.remove(bookmark)) {
+      bookmark.release();
+      myBus.syncPublisher(BookmarksListener.TOPIC).bookmarkRemoved(bookmark);
+    }
   }
 
   @Override
@@ -443,14 +443,15 @@ public class BookmarkManager extends AbstractProjectComponent implements Persist
   private class MyDocumentListener extends DocumentAdapter {
     @Override
     public void beforeDocumentChange(DocumentEvent e) {
+      if (e.isWholeTextReplaced()) return;
       List<Bookmark> bookmarksToRemove = null;
       for (Bookmark bookmark : myBookmarks) {
-        Document document = FileDocumentManager.getInstance().getCachedDocument(bookmark.getFile());
+        Document document = bookmark.getDocument();
         if (document == null || document != e.getDocument()) continue;
         if (bookmark.getLine() ==-1) continue;
 
-        int start = bookmark.getDocument().getLineStartOffset(bookmark.getLine());
-        int end = bookmark.getDocument().getLineEndOffset(bookmark.getLine());
+        int start = document.getLineStartOffset(bookmark.getLine());
+        int end = document.getLineEndOffset(bookmark.getLine());
         if (start >= e.getOffset() && end <= e.getOffset() + e.getOldLength() ) {
           if (bookmarksToRemove == null) {
             bookmarksToRemove = new ArrayList<Bookmark>();
@@ -465,11 +466,24 @@ public class BookmarkManager extends AbstractProjectComponent implements Persist
       }
     }
 
+    private boolean isDuplicate(Bookmark bookmark, @Nullable List<Bookmark> toRemove) {
+      for (Bookmark b : myBookmarks) {
+        if (b == bookmark) continue;
+        if (!b.isValid()) continue;
+        if (Comparing.equal(b.getFile(), bookmark.getFile()) && b.getLine() == bookmark.getLine()) {
+          if (toRemove == null || !toRemove.contains(b)) {
+            return true;
+          }
+        }
+      }
+      return false;
+    }
+
     @Override
     public void documentChanged(DocumentEvent e) {
       List<Bookmark> bookmarksToRemove = null;
       for (Bookmark bookmark : myBookmarks) {
-        if (!bookmark.isValid()) {
+        if (!bookmark.isValid() || isDuplicate(bookmark, bookmarksToRemove)) {
           if (bookmarksToRemove == null) {
             bookmarksToRemove = new ArrayList<Bookmark>();
           }