IDEA-143366 Preserve bookmarks when line is moved appcode/146.1696 appcode/146.1698 appcode/146.1705 appcode/162.162
authorVassiliy.Kudryashov <Vassiliy.Kudryashov@jetbrains.com>
Wed, 4 May 2016 13:20:38 +0000 (16:20 +0300)
committerVassiliy.Kudryashov <Vassiliy.Kudryashov@jetbrains.com>
Wed, 4 May 2016 13:23:02 +0000 (16:23 +0300)
platform/lang-impl/src/com/intellij/ide/bookmarks/BookmarkManager.java

index 3af57b7d2b4e2960165f692249e32bdbf42721d5..510b7471a6bcf994ba7b48ecfcfd52ea249472e9 100644 (file)
@@ -569,10 +569,20 @@ public class BookmarkManager extends AbstractProjectComponent implements Persist
         if (document.getLineCount() <= line) {
           continue;
         }
-        int start = document.getLineStartOffset(line);
-        int end = document.getLineEndOffset(line);
-        String lineContent = document.getText(new TextRange(start, end));
-        if (entry.getKey().third.equals(lineContent) && findEditorBookmark(document, line) == null) {
+
+        String lineContent = getLineContent(document, line);
+
+        String bookmarkedText = entry.getKey().third;
+        //'move statement up' action kills line bookmark: fix for single line movement up/down
+        if (!bookmarkedText.equals(lineContent)
+            && line > 1
+            && (bookmarkedText.equals(StringUtil.trimEnd(e.getNewFragment().toString(), "\n"))
+                ||
+                bookmarkedText.equals(StringUtil.trimEnd(e.getOldFragment().toString(), "\n")))) {
+          line -= 2;
+          lineContent = getLineContent(document, line);
+        }
+        if (bookmarkedText.equals(lineContent) && findEditorBookmark(document, line) == null) {
           Bookmark restored = addTextBookmark(bookmark.getFile(), line, bookmark.getDescription());
           if (bookmark.getMnemonic() != 0) {
             setMnemonic(restored, bookmark.getMnemonic());
@@ -581,6 +591,12 @@ public class BookmarkManager extends AbstractProjectComponent implements Persist
         }
       }
     }
+
+    private String getLineContent(Document document, int line) {
+      int start = document.getLineStartOffset(line);
+      int end = document.getLineEndOffset(line);
+      return document.getText(new TextRange(start, end));
+    }
   }
 }