Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dustdfg opened this issue Dec 11, 2023 · 1 comment

Comments

@dustdfg
Copy link
Contributor

dustdfg commented Dec 11, 2023

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 #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

// UpN moves the cursor up N lines (if possible)
func (c *Cursor) UpN(amount int) {
proposedY := c.Y - amount
if proposedY < 0 {
proposedY = 0
} else if proposedY >= len(c.buf.lines) {
proposedY = len(c.buf.lines) - 1
}
bytes := c.buf.LineBytes(proposedY)
c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
if c.X > util.CharacterCount(bytes) || (amount < 0 && proposedY == c.Y) {
c.X = util.CharacterCount(bytes)
c.StoreVisualX()
}
if c.X < 0 || (amount > 0 && proposedY == c.Y) {
c.X = 0
c.StoreVisualX()
}
c.Y = proposedY
}

  1. I am not sure how important it is but see Inconsistent cursor up/down moving after selecting with keyboard/mouse #3055 (comment)

Specifications

Commit hash: 68d88b5
OS: Debian12
Terminal: foot

dustdfg added a commit to dustdfg/micro that referenced this issue Dec 13, 2023
Previously `CursorDown` function called `Deselect` with a wrong
argument which lead to the situation when cursor was moved to the
start instead of the end of the selection

Signed-off-by: Yevhen Babiichuk (DustDFG) <dfgdust@gmail.com>
@dustdfg dustdfg changed the title Pressing Down when have "multiline selection" moves cursor to the second line of selection instead of end Pressing Down when have "visual multiline selection" moves cursor to the second line of selection instead of end Dec 18, 2023
dmaluka pushed a commit that referenced this issue Apr 25, 2024
Previously `CursorDown` function called `Deselect` with a wrong
argument which lead to the situation when cursor was moved to the
start instead of the end of the selection

Signed-off-by: Yevhen Babiichuk (DustDFG) <dfgdust@gmail.com>
@dmaluka
Copy link
Collaborator

dmaluka commented Apr 25, 2024

#3091 merged.

@dmaluka dmaluka closed this as completed Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants