]> git.lizzy.rs Git - micro.git/commitdiff
Better softwrap
authorZachary Yedidia <zyedidia@gmail.com>
Tue, 24 Dec 2019 21:01:08 +0000 (16:01 -0500)
committerZachary Yedidia <zyedidia@gmail.com>
Wed, 25 Dec 2019 22:05:11 +0000 (17:05 -0500)
internal/action/actions.go
internal/action/bufpane.go
internal/action/tab.go
internal/display/bufwindow.go
internal/display/infowindow.go
internal/display/tabwindow.go
internal/display/termwindow.go
internal/display/window.go

index 6d83ae4cd9371b542af9b01d0f29285243ddbe91..c8cd6b5d8ed96d799c7b9505b3cb3c0aba1926fa 100644 (file)
@@ -40,7 +40,7 @@ func (h *BufPane) ScrollDown(n int) {
 func (h *BufPane) MousePress(e *tcell.EventMouse) bool {
        b := h.Buf
        mx, my := e.Position()
-       mouseLoc := h.GetMouseLoc(buffer.Loc{mx, my})
+       mouseLoc := h.LocFromVisual(buffer.Loc{mx, my})
        h.Cursor.Loc = mouseLoc
        if h.mouseReleased {
                if b.NumCursors() > 1 {
@@ -1410,7 +1410,7 @@ func (h *BufPane) SpawnMultiCursorSelect() bool {
 func (h *BufPane) MouseMultiCursor(e *tcell.EventMouse) bool {
        b := h.Buf
        mx, my := e.Position()
-       mouseLoc := h.GetMouseLoc(buffer.Loc{X: mx, Y: my})
+       mouseLoc := h.LocFromVisual(buffer.Loc{X: mx, Y: my})
        c := buffer.NewCursor(b, mouseLoc)
        b.AddCursor(c)
        b.MergeCursors()
index fd6b60195108a3a18ec2cca616c82d4626419a75..17a0220d3c375c86cd518fc5262c7dd7f65cd537 100644 (file)
@@ -241,7 +241,7 @@ func (h *BufPane) HandleEvent(event tcell.Event) {
                                // Mouse was just released
 
                                mx, my := e.Position()
-                               mouseLoc := h.GetMouseLoc(buffer.Loc{X: mx, Y: my})
+                               mouseLoc := h.LocFromVisual(buffer.Loc{X: mx, Y: my})
 
                                // Relocating here isn't really necessary because the cursor will
                                // be in the right place from the last mouse event
index 45a9f3dbe5d7ae2ca3065dd805362ec16ddbdf3b..bf0ffbec860c38b2f2fb275e8476acd4c6aa72f5 100644 (file)
@@ -103,7 +103,7 @@ func (t *TabList) HandleEvent(event tcell.Event) {
                mx, my := e.Position()
                switch e.Buttons() {
                case tcell.Button1:
-                       ind := t.GetMouseLoc(buffer.Loc{mx, my})
+                       ind := t.LocFromVisual(buffer.Loc{mx, my})
                        if ind != -1 {
                                t.SetActive(ind)
                        }
index 5c9581472f2aa3d957bcde1f9cfb06a874be7e91..ac1305a02fe120637934437eabf711830018c72e 100644 (file)
@@ -25,10 +25,8 @@ type BufWindow struct {
 
        sline *StatusLine
 
-       lineHeight    []int
-       hasCalcHeight bool
-       gutterOffset  int
-       drawStatus    bool
+       gutterOffset int
+       drawStatus   bool
 }
 
 // NewBufWindow creates a new window at a location in the screen with a width and height
@@ -36,7 +34,6 @@ 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.lineHeight = make([]int, height)
        w.active = true
 
        w.sline = NewStatusLine(w)
@@ -58,10 +55,6 @@ func (v *View) SetView(view *View) {
 
 func (w *BufWindow) Resize(width, height int) {
        w.Width, w.Height = width, height
-       w.lineHeight = make([]int, height)
-       w.hasCalcHeight = false
-       // This recalculates lineHeight
-       w.GetMouseLoc(buffer.Loc{width, height})
        w.Relocate()
 }
 
@@ -120,8 +113,7 @@ func (w *BufWindow) Clear() {
 // 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 {
-       // TODO: possible non-softwrap optimization
-       if !w.Buf.Settings["softwrap"].(bool) || !w.hasCalcHeight {
+       if !w.Buf.Settings["softwrap"].(bool) {
                h := w.StartLine + w.Height - 1
                if w.drawStatus {
                        h--
@@ -129,15 +121,10 @@ func (w *BufWindow) Bottomline() int {
                return h
        }
 
-       prev := 0
-       for _, l := range w.lineHeight {
-               if l >= prev {
-                       prev = l
-               } else {
-                       break
-               }
-       }
-       return prev
+       l := w.LocFromVisual(buffer.Loc{0, w.Height})
+
+       log.Println("Bottom line:", l.Y)
+       return l.Y
 }
 
 // Relocate moves the view window so that the cursor is in view
@@ -152,7 +139,7 @@ func (w *BufWindow) Relocate() bool {
        if w.drawStatus {
                h--
        }
-       if b.LinesNum() <= h || !w.hasCalcHeight {
+       if b.LinesNum() <= h {
                height = w.Height
        }
        ret := false
@@ -189,15 +176,15 @@ func (w *BufWindow) Relocate() bool {
        return ret
 }
 
-func (w *BufWindow) GetMouseLoc(svloc buffer.Loc) buffer.Loc {
+// LocFromVisual takes a visual location (x and y position) and returns the
+// position in the buffer corresponding to the visual location
+// Computing the buffer location requires essentially drawing the entire screen
+// to account for complications like softwrap, wide characters, and horizontal scrolling
+// If the requested position does not correspond to a buffer location it returns
+// the nearest position
+func (w *BufWindow) LocFromVisual(svloc buffer.Loc) buffer.Loc {
        b := w.Buf
 
-       // TODO: possible non-softwrap optimization
-       // if !b.Settings["softwrap"].(bool) {
-       //      l := b.LineBytes(svloc.Y)
-       //      return buffer.Loc{b.GetActiveCursor().GetCharPosInLine(l, svloc.X), svloc.Y}
-       // }
-
        hasMessage := len(b.Messages) > 0
        bufHeight := w.Height
        if w.drawStatus {
@@ -293,11 +280,12 @@ func (w *BufWindow) GetMouseLoc(svloc buffer.Loc) buffer.Loc {
                        return bloc
                }
 
+               if bloc.Y+1 >= b.LinesNum() || vloc.Y+1 >= bufHeight {
+                       return bloc
+               }
+
                bloc.X = w.StartCol
                bloc.Y++
-               if bloc.Y >= b.LinesNum() {
-                       break
-               }
        }
 
        return buffer.Loc{}
@@ -378,7 +366,6 @@ func (w *BufWindow) displayBuffer() {
                bufWidth--
        }
 
-       w.hasCalcHeight = true
        if b.Settings["syntax"].(bool) && b.SyntaxDef != nil {
                for _, c := range b.GetCursors() {
                        // rehighlight starting from where the cursor is
@@ -544,8 +531,6 @@ func (w *BufWindow) displayBuffer() {
                        nColsBeforeStart--
                }
 
-               w.lineHeight[vloc.Y] = bloc.Y
-
                totalwidth := w.StartCol - nColsBeforeStart
                for len(line) > 0 {
                        r, size := utf8.DecodeRune(line)
@@ -586,7 +571,6 @@ func (w *BufWindow) displayBuffer() {
                                                break
                                        }
                                        vloc.X = 0
-                                       w.lineHeight[vloc.Y] = bloc.Y
                                        // This will draw an empty line number because the current line is wrapped
                                        w.drawLineNum(lineNumStyle, true, maxLineNumLength, &vloc, &bloc)
                                }
index db2f9c08f9797c575def3244d2949c49d5058fc6..b179c81e941978ca808573975866c81a9a8a13df 100644 (file)
@@ -65,7 +65,7 @@ func (i *InfoWindow) SetView(v *View)  {}
 func (i *InfoWindow) SetActive(b bool) {}
 func (i *InfoWindow) IsActive() bool   { return true }
 
-func (i *InfoWindow) GetMouseLoc(vloc buffer.Loc) buffer.Loc {
+func (i *InfoWindow) LocFromVisual(vloc buffer.Loc) buffer.Loc {
        c := i.Buffer.GetActiveCursor()
        l := i.Buffer.LineBytes(0)
        n := utf8.RuneCountInString(i.Msg)
index d3e9553b46f60a43a0b1079a837bf120761462e8..3d25a42caa646b54e927e46049dd7c953727473f 100644 (file)
@@ -25,7 +25,7 @@ func NewTabWindow(w int, y int) *TabWindow {
        return tw
 }
 
-func (w *TabWindow) GetMouseLoc(vloc buffer.Loc) int {
+func (w *TabWindow) LocFromVisual(vloc buffer.Loc) int {
        x := -w.hscroll
 
        for i, n := range w.Names {
index d195d5323feae0c18bff85905045ebd5a0b768db..95a9003764064f5aade804ebbcc2e202b0bb40e1 100644 (file)
@@ -44,7 +44,7 @@ func (w *TermWindow) IsActive() bool {
        return w.active
 }
 
-func (w *TermWindow) GetMouseLoc(vloc buffer.Loc) buffer.Loc {
+func (w *TermWindow) LocFromVisual(vloc buffer.Loc) buffer.Loc {
        return vloc
 }
 
index 4e39bdbd3e46e2fa7c960b91af5450eed44bf4c2..bed538c5f8b4b4deb03d4e5ffc64211470b06f68 100644 (file)
@@ -20,7 +20,7 @@ type Window interface {
        Relocate() bool
        GetView() *View
        SetView(v *View)
-       GetMouseLoc(vloc buffer.Loc) buffer.Loc
+       LocFromVisual(vloc buffer.Loc) buffer.Loc
        Resize(w, h int)
        SetActive(b bool)
        IsActive() bool