Fix softwrap scrolling issues#1981
Conversation
Softwrap implementation enhanced to fix various issues with scrolling, centering, relocating etc. The main idea is simple: work not with simple line numbers but with (Line, Row) pairs, where Line is a line number in the buffer and Row is a visual line (a row) number within this line. The logic remains mostly the same, but simple arithmetic operations on line numbers are replaced with corresponding operations on (Line, Row) pairs. Fixes micro-editor#632, micro-editor#1657
| v.StartLine = h.Buf.LinesNum() - v.Height | ||
| h.SetView(v) | ||
| } | ||
| v.StartLine = h.Scroll(h.SLocFromLoc(h.Buf.End()), -v.Height+1) |
There was a problem hiding this comment.
The use of v.Height here (and in other functions in this file) is buggy, since the actual buffer height is v.Height-1 if statusline is on. This is a separate issue.
| v := LogBufPane.GetView() | ||
| endY := buffer.LogBuf.End().Y | ||
|
|
||
| if endY > v.StartLine+v.Height { |
There was a problem hiding this comment.
This check is redundant since StartLine has been already properly updated by CursorEnd.
| // You might think that this is obviously just v.StartLine + v.Height | ||
| // but if softwrap is enabled things get complicated since one buffer | ||
| // line can take up multiple lines in the view | ||
| func (w *BufWindow) Bottomline() int { |
There was a problem hiding this comment.
Bottomline removed because its use is buggy anyway: if the bottom line is wrapped, Bottomline doesn't tell which row is at the bottom.
Instead of w.Bottomline() use w.Scroll(v.StartLine, height-1).
There was a problem hiding this comment.
BTW this Bottomline implementation is inconsistent: if we've scrolled past the last line, it returns different values with softwrap=off and softwrap=on (which results in different behavior).
| ret = true | ||
| } else if cy >= b.LinesNum()-scrollmargin && cy >= height { | ||
| w.StartLine = b.LinesNum() - height | ||
| } else if c.GreaterThan(w.Scroll(bEnd, -scrollmargin)) && c.GreaterThan(w.Scroll(w.StartLine, height-1)) { |
There was a problem hiding this comment.
Note that I've changed cy >= height to essentially cy >= w.StartLine + height. This, together with the removal of Bottomline, fixes some bugs with scrolling past the last line.
|
Cool! This has been a sore point for a while, so it will be good to fix it. Does |
Not quite yet. It tracks the vertical position only. The visual y can be easily obtained from a sloc (it equals However I've been already thinking about adding one more struct: and functions |
This would be awesome! |
|
On my softwrap-improvement-dev branch I've implemented a bunch of further softwrap improvements on top of this PR, including cursor up/down movement within wrapped line, word wrapping, and fixes for issues #645 and #1979. |
For reference, those changes are now in my PR #2076. |
Softwrap implementation enhanced to fix various issues with scrolling, centering, relocating etc.
The main idea is simple: work not with simple line numbers but with
(Line, Row)pairs, whereLineis a line number in the buffer andRowis a visual line (a row) number within this line. The logic remains mostly the same, but simple arithmetic operations on line numbers are replaced with corresponding operations on(Line, Row)pairs.Fixes #632, #1657
This is the first step in improving the soft wrapping functionality. I think the next steps should be:
CursorUpandCursorDownto move between visual lines within a wrapped line (Unable to move up and down within a paragraph #1598)