Menu

#2457 [Patch] Fix moving down when the last line is empty

Bug
closed-fixed
nobody
scintilla (610)
5
2024-12-18
2024-11-24
No

Hi. This is a fix to an issue reported in Notepad++ repo. All tests passed :) See the comments for more info.

--- a/src/Editor.cxx    Sun Nov 24 15:19:50 2024 +0100
+++ b/src/Editor.cxx    Sun Nov 24 15:44:34 2024 +0100
@@ -999,21 +999,25 @@
    // if selection doesn't end at the beginning of a line greater than that of the start,
    // then set it at the beginning of the next one
    Sci::Position selectionEnd = SelectionEnd().Position();

-   const Sci::Line endLine = pdoc->SciLineFromPosition(selectionEnd);
+   Sci::Line endLine = pdoc->SciLineFromPosition(selectionEnd);
    const Sci::Position beginningOfEndLine = pdoc->LineStart(endLine);
    bool appendEol = false;
    if (selectionEnd > beginningOfEndLine
        || selectionStart == selectionEnd) {
        selectionEnd = pdoc->LineStart(endLine + 1);
        appendEol = (selectionEnd == pdoc->Length() && pdoc->SciLineFromPosition(selectionEnd) == endLine);
+       endLine = pdoc->SciLineFromPosition(selectionEnd);
    }

    // if there's nowhere for the selection to move
    // (i.e. at the beginning going up or at the end going down),
    // stop it right there!

+   bool docEndLineEmpty = pdoc->LineStart(endLine) == pdoc->Length();
    if ((selectionStart == 0 && lineDelta < 0)
-       || (selectionEnd == pdoc->Length() && lineDelta > 0)
-           || selectionStart == selectionEnd) {
+       || (selectionEnd == pdoc->Length() && lineDelta > 0
+           && !docEndLineEmpty) // allow moving when end line of document is empty
+       || ((selectionStart == selectionEnd)
+           && !(lineDelta < 0 && docEndLineEmpty && selectionEnd == pdoc->Length()))) { // allow moving-up last empty line
        return;
    }
1 Attachments

Discussion

  • pawelzwronek

    pawelzwronek - 2024-11-25

    Btw, I've noticed the date in ScintillaHistory.html for Release 5.5.4 is the same as for 5.5.3 :)

     
  • Neil Hodgson

    Neil Hodgson - 2024-12-02

    Committed as [352da4] with an added const. The expression to exit is ugly but I didn't find a good way to simplify it.

    Dates in the histories are updated when the release is scheduled.

     

    Related

    Commit: [352da4]

  • Neil Hodgson

    Neil Hodgson - 2024-12-02
    • labels: --> scintilla
    • status: open --> open-fixed
     
  • pawelzwronek

    pawelzwronek - 2024-12-03

    Thanks!

    Handling the edge cases most of the time uglify the code IMO 🤷‍♀️
    Maybe changing assumptions or even the definition of some core functions would simplify this whole function but it's risky 😈

     
  • Zufu Liu

    Zufu Liu - 2024-12-04

    following change can save extra SciLineFromPosition() call:

    -       appendEol = (selectionEnd == pdoc->Length() && pdoc->SciLineFromPosition(selectionEnd) == endLine);
    -       endLine = pdoc->SciLineFromPosition(selectionEnd);
    +       const Sci::Line line = pdoc->SciLineFromPosition(selectionEnd);
    +       appendEol = (line == endLine && selectionEnd == pdoc->Length());
    +       endLine = line;
    
     

    Last edit: Zufu Liu 2024-12-04
  • Neil Hodgson

    Neil Hodgson - 2024-12-04

    This doesn't appear to be a measurable performance improvement since the extra call only occurs at the document end and the whole move lines command is large compared to a single LineFromPosition. Is the code shorter or neater? Not really.

     
  • Neil Hodgson

    Neil Hodgson - 2024-12-18
    • status: open-fixed --> closed-fixed
     

Log in to post a comment.

MongoDB Logo MongoDB