vcs: LST - scroll to change, not to line after it
authorAleksey Pivovarov <AMPivovarov@gmail.com>
Fri, 6 Feb 2015 09:28:34 +0000 (12:28 +0300)
committerAleksey Pivovarov <AMPivovarov@gmail.com>
Fri, 6 Feb 2015 10:29:04 +0000 (13:29 +0300)
* fix horizontal offset: show changed text line-under-line
* use same editor settings for popup (ex: line height)
* do not hide current line under popup for mouse click

platform/vcs-impl/src/com/intellij/openapi/vcs/ex/LineStatusTrackerDrawing.java

index 4e8962494f08d5bbbb85c5c47fe74910c9dd50ba..e21caefa1ddcc561a854b402048784f170f29574 100644 (file)
 package com.intellij.openapi.vcs.ex;
 
 import com.intellij.codeInsight.hint.EditorFragmentComponent;
 package com.intellij.openapi.vcs.ex;
 
 import com.intellij.codeInsight.hint.EditorFragmentComponent;
+import com.intellij.codeInsight.hint.HintManager;
 import com.intellij.codeInsight.hint.HintManagerImpl;
 import com.intellij.openapi.actionSystem.*;
 import com.intellij.openapi.actionSystem.ex.ActionUtil;
 import com.intellij.openapi.diff.DiffColors;
 import com.intellij.codeInsight.hint.HintManagerImpl;
 import com.intellij.openapi.actionSystem.*;
 import com.intellij.openapi.actionSystem.ex.ActionUtil;
 import com.intellij.openapi.diff.DiffColors;
-import com.intellij.openapi.editor.Document;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.editor.EditorFactory;
-import com.intellij.openapi.editor.ScrollType;
+import com.intellij.openapi.editor.*;
 import com.intellij.openapi.editor.colors.EditorColors;
 import com.intellij.openapi.editor.colors.EditorColorsManager;
 import com.intellij.openapi.editor.colors.EditorColorsScheme;
 import com.intellij.openapi.editor.colors.EditorColors;
 import com.intellij.openapi.editor.colors.EditorColorsManager;
 import com.intellij.openapi.editor.colors.EditorColorsScheme;
@@ -192,7 +190,10 @@ public class LineStatusTrackerDrawing {
     };
   }
 
     };
   }
 
-  public static void showActiveHint(final Range range, final Editor editor, final Point point, final LineStatusTracker tracker) {
+  public static void showActiveHint(@NotNull Range range,
+                                    @NotNull final Editor editor,
+                                    @Nullable Point mousePosition,
+                                    @NotNull LineStatusTracker tracker) {
     final DefaultActionGroup group = new DefaultActionGroup();
 
     final ShowPrevChangeMarkerAction localShowPrevAction = new ShowPrevChangeMarkerAction(tracker.getPrevRange(range), tracker, editor);
     final DefaultActionGroup group = new DefaultActionGroup();
 
     final ShowPrevChangeMarkerAction localShowPrevAction = new ShowPrevChangeMarkerAction(tracker.getPrevRange(range), tracker, editor);
@@ -216,6 +217,7 @@ public class LineStatusTrackerDrawing {
     EmptyAction.setupAction(copyRange, IdeActions.ACTION_COPY, editorComponent);
 
 
     EmptyAction.setupAction(copyRange, IdeActions.ACTION_COPY, editorComponent);
 
 
+    int offsetX = 0;
     final JComponent toolbar =
       ActionManager.getInstance().createActionToolbar(ActionPlaces.FILEHISTORY_VIEW_TOOLBAR, group, true).getComponent();
 
     final JComponent toolbar =
       ActionManager.getInstance().createActionToolbar(ActionPlaces.FILEHISTORY_VIEW_TOOLBAR, group, true).getComponent();
 
@@ -258,6 +260,7 @@ public class LineStatusTrackerDrawing {
     if (range.getType() != Range.INSERTED) {
       final DocumentEx doc = (DocumentEx)tracker.getVcsDocument();
       final EditorEx uEditor = (EditorEx)EditorFactory.getInstance().createViewer(doc, tracker.getProject());
     if (range.getType() != Range.INSERTED) {
       final DocumentEx doc = (DocumentEx)tracker.getVcsDocument();
       final EditorEx uEditor = (EditorEx)EditorFactory.getInstance().createViewer(doc, tracker.getProject());
+      uEditor.setColorsScheme(editor.getColorsScheme());
       final EditorHighlighter highlighter =
         EditorHighlighterFactory.getInstance().createEditorHighlighter(tracker.getProject(), getFileName(tracker.getDocument()));
       uEditor.setHighlighter(highlighter);
       final EditorHighlighter highlighter =
         EditorHighlighterFactory.getInstance().createEditorHighlighter(tracker.getProject(), getFileName(tracker.getDocument()));
       uEditor.setHighlighter(highlighter);
@@ -266,13 +269,14 @@ public class LineStatusTrackerDrawing {
         EditorFragmentComponent.createEditorFragmentComponent(uEditor, range.getVcsLine1(), range.getVcsLine2(), false, false);
 
       component.add(editorFragmentComponent, BorderLayout.CENTER);
         EditorFragmentComponent.createEditorFragmentComponent(uEditor, range.getVcsLine1(), range.getVcsLine2(), false, false);
 
       component.add(editorFragmentComponent, BorderLayout.CENTER);
+      offsetX -= editorFragmentComponent.getBorder().getBorderInsets(editorFragmentComponent).left;
 
       EditorFactory.getInstance().releaseEditor(uEditor);
     }
 
 
     final List<AnAction> actionList = ActionUtil.getActions(editorComponent);
 
       EditorFactory.getInstance().releaseEditor(uEditor);
     }
 
 
     final List<AnAction> actionList = ActionUtil.getActions(editorComponent);
-    final LightweightHint lightweightHint = new LightweightHint(component);
+    final LightweightHint hint = new LightweightHint(component);
     HintListener closeListener = new HintListener() {
       public void hintHidden(final EventObject event) {
         actionList.remove(rollback);
     HintListener closeListener = new HintListener() {
       public void hintHidden(final EventObject event) {
         actionList.remove(rollback);
@@ -282,14 +286,22 @@ public class LineStatusTrackerDrawing {
         actionList.remove(localShowNextAction);
       }
     };
         actionList.remove(localShowNextAction);
       }
     };
-    lightweightHint.addHintListener(closeListener);
+    hint.addHintListener(closeListener);
+
+    int line = editor.getCaretModel().getLogicalPosition().line;
+    Point point = HintManagerImpl.getHintPosition(hint, editor, new LogicalPosition(line, 0), HintManager.UNDER);
+    if (mousePosition != null) {
+      int lineHeight = editor.getLineHeight();
+      int delta = (point.y - mousePosition.y) % lineHeight;
+      if (delta < 0) delta += lineHeight;
+      point.y = mousePosition.y + delta;
+    }
+    point.x += offsetX;
 
 
-    HintManagerImpl.getInstanceImpl().showEditorHint(lightweightHint, editor, point,
-                                                     HintManagerImpl.HIDE_BY_ANY_KEY | HintManagerImpl.HIDE_BY_TEXT_CHANGE |
-                                                     HintManagerImpl.HIDE_BY_SCROLLING,
-                                                     -1, false, new HintHint(editor, point));
+    int flags = HintManager.HIDE_BY_ANY_KEY | HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_SCROLLING;
+    HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, point, flags, -1, false, new HintHint(editor, point));
 
 
-    if (!lightweightHint.isVisible()) {
+    if (!hint.isVisible()) {
       closeListener.hintHidden(null);
     }
   }
       closeListener.hintHidden(null);
     }
   }
@@ -302,7 +314,8 @@ public class LineStatusTrackerDrawing {
 
   public static void moveToRange(final Range range, final Editor editor, final LineStatusTracker tracker) {
     final Document document = tracker.getDocument();
 
   public static void moveToRange(final Range range, final Editor editor, final LineStatusTracker tracker) {
     final Document document = tracker.getDocument();
-    final int lastOffset = document.getLineStartOffset(Math.min(range.getLine2(), document.getLineCount() - 1));
+    int line = Math.min(range.getType() == Range.DELETED ? range.getLine2() : range.getLine2() - 1, document.getLineCount() - 1);
+    final int lastOffset = document.getLineStartOffset(line);
     editor.getCaretModel().moveToOffset(lastOffset);
     editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
 
     editor.getCaretModel().moveToOffset(lastOffset);
     editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
 
@@ -312,11 +325,7 @@ public class LineStatusTrackerDrawing {
   public static void showHint(final Range range, final Editor editor, final LineStatusTracker tracker) {
     editor.getScrollingModel().runActionOnScrollingFinished(new Runnable() {
       public void run() {
   public static void showHint(final Range range, final Editor editor, final LineStatusTracker tracker) {
     editor.getScrollingModel().runActionOnScrollingFinished(new Runnable() {
       public void run() {
-        Point p = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
-        final JComponent editorComponent = editor.getContentComponent();
-        final JLayeredPane layeredPane = editorComponent.getRootPane().getLayeredPane();
-        p = SwingUtilities.convertPoint(editorComponent, 0, p.y, layeredPane);
-        showActiveHint(range, editor, p, tracker);
+        showActiveHint(range, editor, null, tracker);
       }
     });
   }
       }
     });
   }