From 3f0cd019d7a326cde8440e3b437d586c2739544a Mon Sep 17 00:00:00 2001 From: Dmitry Maluka Date: Sat, 3 Dec 2022 02:49:06 +0100 Subject: [PATCH] Fix incorrect LastVisualX after changing bufWidth w/o resize (#2629) * Fix incorrect LastVisualX after changing bufWidth w/o resize When we resize a buffer pane with softwrap enabled, we update the cursors LastVisualX values to ensure that moving cursor up or down within a wrapped line will move the cursor to the correct location. The problem is that we need to do it also in cases when the visual buffer width within the buffer window is changing without resizing the window itself, e.g. when toggling the ruler on/off. So update LastVisualX whenever the buffer width changes, not neccesarily as a result of resizing the buffer window. * Update LastVisualX and relocate when toggling wordwrap on/off Visual location of a cursor may change not only when softwrap is toggled on or off but also when wordwrap is toggled on or off without changing the softwrap setting. So need to update cursor LastVisualX values and relocate the view if needed, just like when softwrap is toggled, to make sure that moving the cursor up and down will work correctly and that the cursor will not be left out of the view. --- internal/display/bufwindow.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/display/bufwindow.go b/internal/display/bufwindow.go index 5c7c8912..593bf7b7 100644 --- a/internal/display/bufwindow.go +++ b/internal/display/bufwindow.go @@ -53,8 +53,10 @@ func (w *BufWindow) SetBuffer(b *buffer.Buffer) { } else { w.StartLine.Row = 0 } - w.Relocate() + } + if option == "softwrap" || option == "wordwrap" { + w.Relocate() for _, c := range w.Buf.GetCursors() { c.LastVisualX = c.GetVisualX() } @@ -81,12 +83,6 @@ func (w *BufWindow) Resize(width, height int) { w.updateDisplayInfo() w.Relocate() - - if w.Buf.Settings["softwrap"].(bool) { - for _, c := range w.Buf.GetCursors() { - c.LastVisualX = c.GetVisualX() - } - } } // SetActive marks the window as active. @@ -150,10 +146,18 @@ func (w *BufWindow) updateDisplayInfo() { w.gutterOffset += w.maxLineNumLength + 1 } + prevBufWidth := w.bufWidth + w.bufWidth = w.Width - w.gutterOffset if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height { w.bufWidth-- } + + if w.bufWidth != prevBufWidth && w.Buf.Settings["softwrap"].(bool) { + for _, c := range w.Buf.GetCursors() { + c.LastVisualX = c.GetVisualX() + } + } } func (w *BufWindow) getStartInfo(n, lineN int) ([]byte, int, int, *tcell.Style) {