fix scrolling to caret at the end of soft wrapped line

This commit is contained in:
Dmitry Batrak
2016-03-16 16:55:11 +03:00
parent 4ced9feddc
commit 1682b9b79e
2 changed files with 30 additions and 7 deletions
@@ -30,6 +30,7 @@ import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.editor.VisualPosition;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.VisibleAreaEvent;
@@ -135,9 +136,23 @@ public class ScrollingModelImpl implements ScrollingModelEx {
@Override
public void scrollToCaret(@NotNull ScrollType scrollType) {
assertIsDispatchThread();
LogicalPosition caretPosition = myEditor.getCaretModel().getLogicalPosition();
myEditor.validateSize();
scrollTo(caretPosition, scrollType);
if (myEditor.myUseNewRendering) {
VisualPosition caretPosition = myEditor.getCaretModel().getVisualPosition();
scrollTo(caretPosition, scrollType);
}
else {
LogicalPosition caretPosition = myEditor.getCaretModel().getLogicalPosition();
scrollTo(caretPosition, scrollType);
}
}
private void scrollTo(@NotNull VisualPosition pos, @NotNull ScrollType scrollType) {
AnimatedScrollingRunnable canceledThread = cancelAnimatedScrolling(false);
Rectangle viewRect = canceledThread != null ? canceledThread.getTargetVisibleArea() : getVisibleArea();
Point targetLocation = myEditor.visualPositionToXY(pos);
Point p = calcOffsetsToScroll(targetLocation, scrollType, viewRect);
scrollToOffsets(p.x, p.y);
}
@Override
@@ -146,8 +161,8 @@ public class ScrollingModelImpl implements ScrollingModelEx {
AnimatedScrollingRunnable canceledThread = cancelAnimatedScrolling(false);
Rectangle viewRect = canceledThread != null ? canceledThread.getTargetVisibleArea() : getVisibleArea();
Point p = calcOffsetsToScroll(pos, scrollType, viewRect);
Point targetLocation = myEditor.logicalPositionToXY(pos);
Point p = calcOffsetsToScroll(targetLocation, scrollType, viewRect);
scrollToOffsets(p.x, p.y);
}
@@ -177,9 +192,7 @@ public class ScrollingModelImpl implements ScrollingModelEx {
myAnimationDisabled = false;
}
private Point calcOffsetsToScroll(LogicalPosition pos, ScrollType scrollType, Rectangle viewRect) {
Point targetLocation = myEditor.logicalPositionToXY(pos);
private Point calcOffsetsToScroll(Point targetLocation, ScrollType scrollType, Rectangle viewRect) {
if (myEditor.getSettings().isRefrainFromScrolling() && viewRect.contains(targetLocation)) {
if (scrollType == ScrollType.CENTER ||
scrollType == ScrollType.CENTER_DOWN ||
@@ -319,4 +319,14 @@ public class EditorImplTest extends AbstractEditorTest {
up();
checkResultByText("abc\nd<caret>ef\nghi");
}
public void testScrollingToCaretAtSoftWrap() throws Exception {
initText("looooooooooooooooong wooooooooooooooooords");
configureSoftWraps(10);
end();
VisualPosition caretPosition = myEditor.getCaretModel().getCurrentCaret().getVisualPosition();
Point caretPoint = myEditor.visualPositionToXY(caretPosition);
Rectangle visibleArea = myEditor.getScrollingModel().getVisibleArea();
assertTrue(visibleArea.contains(caretPoint));
}
}