]> git.lizzy.rs Git - micro.git/commitdiff
Fix horizontal scrolling issue after toggling softwrap on/off
authorDmitry Maluka <dmitrymaluka@gmail.com>
Wed, 3 Mar 2021 19:51:06 +0000 (20:51 +0100)
committerDmitry Maluka <dmitrymaluka@gmail.com>
Thu, 8 Apr 2021 21:53:44 +0000 (23:53 +0200)
Fixes #645

internal/buffer/buffer.go
internal/buffer/settings.go
internal/display/bufwindow.go

index 84ab7dc0ad02229fd6f9d92409603a7dda724dbd..718510dd041ed6522b72fd3027a438ac6fa954c0 100644 (file)
@@ -189,6 +189,13 @@ type Buffer struct {
        cursors     []*Cursor
        curCursor   int
        StartCursor Loc
+
+       // OptionCallback is called after a buffer option value is changed.
+       // The display module registers its OptionCallback to ensure the buffer window
+       // is properly updated when needed. This is a workaround for the fact that
+       // the buffer module cannot directly call the display's API (it would mean
+       // a circular dependency between packages).
+       OptionCallback func(option string, nativeValue interface{})
 }
 
 // NewBufferFromFileAtLoc opens a new buffer with a given cursor location
index e04d7061ef3b0878f346c6dea63287f6d42dcb90..c3ff428c59f9f2b5aea6e216b278f86da12f7997 100644 (file)
@@ -41,6 +41,10 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
                b.Type.Readonly = nativeValue.(bool)
        }
 
+       if b.OptionCallback != nil {
+               b.OptionCallback(option, nativeValue)
+       }
+
        return nil
 }
 
index 3c1136b154636844323fb009ca5d1cf1b6c44f4d..0a9e174df056593165f3ab4f28c97fa95f9fe4ef 100644 (file)
@@ -35,7 +35,8 @@ type BufWindow struct {
 func NewBufWindow(x, y, width, height int, buf *buffer.Buffer) *BufWindow {
        w := new(BufWindow)
        w.View = new(View)
-       w.X, w.Y, w.Width, w.Height, w.Buf = x, y, width, height, buf
+       w.X, w.Y, w.Width, w.Height = x, y, width, height
+       w.SetBuffer(buf)
        w.active = true
 
        w.sline = NewStatusLine(w)
@@ -45,6 +46,16 @@ func NewBufWindow(x, y, width, height int, buf *buffer.Buffer) *BufWindow {
 
 func (w *BufWindow) SetBuffer(b *buffer.Buffer) {
        w.Buf = b
+       b.OptionCallback = func(option string, nativeValue interface{}) {
+               if option == "softwrap" {
+                       if nativeValue.(bool) {
+                               w.StartCol = 0
+                       } else {
+                               w.StartLine.Row = 0
+                       }
+                       w.Relocate()
+               }
+       }
 }
 
 func (w *BufWindow) GetView() *View {