zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.47k stars 1.16k forks source link

Pressing `Down` when have "visual multiline selection" moves cursor to the second line of selection instead of end #3087

Closed dustdfg closed 2 months ago

dustdfg commented 7 months ago

Description of the problem or steps to reproduce

  1. Open any file that have many lines
  2. Select several lines (not with mouse it looks like it messes start and end of the selection)
  3. Press down

Expected behavior

Cursor is moved to the end. To the line next to end of selection but at least to the last line of selection.

Actual behavior

Even if you selected 1000 lines pressing down will move cursor to the second line of selection. NOTE: Pressing up works correctly and moves cursor up

How it was found and what problem it brought

I needed to select whole lines in custom sidebar on moving up and down. Something like the thing you can see in filemanager plugin. And the following example works while you run micro with disabled softwrap. But when you enable softwrap (and make sure that line you currently selected is softwrapped) pressing down will move cursor to the next "visual" line but it will be the same "actual" line. When you are on the second "visual" line of text the "SelectLine()" will select whole "actual" line. Pressing down will move it to the second "visual" line and now we are in a loop...

Minimal reproducible example, just copy it as plugin to your plugin dir to

function onCursorDown(buffer_pane)
    buffer_pane:SelectLine()
end

function onCursorUp(buffer_pane)
    buffer_pane:SelectLine()
end

Mouse inconsistency

Like mentioned in https://github.com/zyedidia/micro/issues/3055 if you select something with mouse the direction from what you selected text matters. If you selected text from up to bottom, everything works as with keyboard selection but. But if you select line from bottom to up the things will swap: pressing up will result in moving cursor to the one line up before last line and pressing down will work correctly and normally.

About filemanager and workaround

I tried to overcome the bug and found the workaround the same is in filemanager plugin. The pressing up is done via onCursorUp hook because pressing up works good but pressing down is handled in different way. It is done via preCursorDown that handles cursor down movement before it happens and manually moves cursor down and then return false from hook to let micro know that the action was handled and micro mustn't perform it....

https://github.com/micro-editor/updated-plugins/blob/216ec3adaf3adec78665614402ece56cf60ae713/filemanager-plugin/filemanager.lua#L1024-L1037

Possible root of the problem

  1. UpN function that is also used for down don't respect CurrentSelection and just use itself like it is simple 1 char cursor

https://github.com/zyedidia/micro/blob/2d82362a6695a7e898455ce016449167ac439ddd/internal/buffer/cursor.go#L232-L255

  1. I am not sure how important it is but see https://github.com/zyedidia/micro/issues/3055#issuecomment-1850401599

Specifications

Commit hash: https://github.com/zyedidia/micro/commit/68d88b571de6dca9fb8f03e2a3caafa2287c38d4 OS: Debian12 Terminal: foot

dmaluka commented 2 months ago

3091 merged.