Menu

#2518 SCI_VERTICALCENTRECARET doesn't clamp topLine to MaxScrollPos()

Bug
open-fixed
nobody
scintilla (613)
5
22 hours ago
1 day ago
No

Editor::VerticalCentreCaret() in Editor.cxx computes newTop by
centering the caret, but only clamps its lower bound.

Other scrolling paths clamp their target line with
std::clamp<sci::line>(..., 0, MaxScrollPos()). This function does not,
and SetTopLine() also accepts a value above MaxScrollPos().</sci::line>

Fix: clamp newTop like the neighbouring scrolling code:

--- a/src/Editor.cxx
+++ b/src/Editor.cxx
@@ -1021,11 +1021,11 @@ void Editor::VerticalCentreCaret() {
 void Editor::VerticalCentreCaret() {
    const Sci::Line lineDoc =
        pdoc->SciLineFromPosition(sel.IsRectangular() ? sel.Rectangular().caret.Position() : sel.MainCaret());
    const Sci::Line lineDisplay = pcs->DisplayFromDoc(lineDoc);

-   const Sci::Line newTop = lineDisplay - (LinesOnScreen() / 2);
+   const Sci::Line newTop = std::clamp<Sci::Line>(lineDisplay - (LinesOnScreen() / 2), 0, MaxScrollPos());
    if (topLine != newTop) {
-       SetTopLine(newTop > 0 ? newTop : 0);
+       SetTopLine(newTop);
        SetVerticalScrollPos();
        RedrawRect(GetClientRectangle());
    }
 }

Discussion

  • Neil Hodgson

    Neil Hodgson - 22 hours ago

    Committed with [fce5e5].

     

    Related

    Commit: [fce5e5]

  • Neil Hodgson

    Neil Hodgson - 22 hours ago
    • labels: --> scintilla
    • status: open --> open-fixed
     

Log in to post a comment.