Parcourir la source

Layout work and plans for tomorrow

Johan Rong il y a 1 mois
Parent
commit
af1126c5c9

+ 3 - 0
cmd/moose/main.go

@@ -7,6 +7,7 @@ import (
 	"github.com/gdamore/tcell/v3"
 	"github.com/gdamore/tcell/v3/color"
 	"golang.design/x/clipboard"
+	"github.com/davecgh/go-spew/spew"
 )
 
 func main() {
@@ -41,6 +42,8 @@ func main() {
 		if maybePanic != nil {
 			panic(maybePanic)
 		}
+
+		spew.Dump(m.LM.Workspaces[0].RootContainer)
 	}
 	defer quit()
 

+ 1 - 0
go.mod

@@ -11,6 +11,7 @@ require (
 require (
 	github.com/clipperhouse/displaywidth v0.11.0 // indirect
 	github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
+	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/ebitengine/purego v0.10.1 // indirect
 	github.com/gdamore/encoding v1.0.1 // indirect
 	github.com/lucasb-eyer/go-colorful v1.4.0 // indirect

+ 2 - 0
go.sum

@@ -2,6 +2,8 @@ github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSE
 github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
 github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
 github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY=
 github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
 github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=

+ 19 - 0
internal/buffer/buffer.go

@@ -5,6 +5,8 @@ import (
 	"unicode"
 	"unicode/utf8"
 	"github.com/zyedidia/rope"
+	"fmt"
+	"os"
 )
 
 type BufferManager struct {
@@ -44,6 +46,23 @@ func NewBuffer() Buffer {
 	}
 }
 
+func NewBufferFromPath(path string) Buffer {
+	b := NewBuffer()
+	content, err := os.ReadFile(path)
+	if err != nil {
+		fmt.Printf("[moose-error] %v", err)
+		os.Exit(1)
+	}
+
+	b.Clear()
+	b.Rope = rope.New(content)
+	b.LI.Rebuild(b.Rope)
+	b.Path = path
+	b.History = UndoStack{}
+
+	return b
+}
+
 func (buf *Buffer) ensureRope() {
 	if buf.Rope == nil {
 		buf.Rope = rope.New([]byte{})

+ 26 - 0
internal/editor/action.go

@@ -362,6 +362,32 @@ func DefaultActionManager() ActionManager {
 					}
 				},
 			},
+			Action{
+				Bindings: []Binding{
+					Binding{
+						Type:   BindingSingle,
+						Single: "enter",
+					},
+				},
+				Commands: []string{"buf"},
+				AskArgs: false,
+				Callback: func(m *Model, args []string) {
+					m.AddBuffer(false)
+				},
+			},
+			Action{
+				Bindings: []Binding{
+					Binding{
+						Type:  BindingChord,
+						Chord: []string{"l", "enter"},
+					},
+				},
+				Commands: []string{"lbuf"},
+				AskArgs: false,
+				Callback: func(m *Model, args []string) {
+					m.AddBuffer(true)
+				},
+			},
 		},
 		Insert:  []Action{
 			Action{

+ 3 - 3
internal/editor/draw.go

@@ -16,7 +16,7 @@ func (m *Model) DrawWorkspace(w *layout.Workspace, rect layout.Rect) {
 	m.DrawContainer(&w.RootContainer, rect)
 }
 
-func (m *Model) DrawContainer(c *layout.Container[layout.ContainerBuffers], rect layout.Rect) {
+func (m *Model) DrawContainer(c *layout.Container, rect layout.Rect) {
 	length := len(c.Children)
 	rect = layout.RectDivide(rect, c.Split, length)
 
@@ -26,8 +26,8 @@ func (m *Model) DrawContainer(c *layout.Container[layout.ContainerBuffers], rect
 			cb := child.(layout.ContainerBuffers)
 			m.DrawContainerBuffers(&cb, layout.RectDisplace(rect, c.Split, i))
 		}
-		case layout.Container[layout.ContainerBuffers]: {
-			c := child.(layout.Container[layout.ContainerBuffers])
+		case layout.Container: {
+			c := child.(layout.Container)
 			m.DrawContainer(&c, layout.RectDisplace(rect, c.Split, i))
 		}
 		}

+ 23 - 0
internal/editor/layout.go

@@ -0,0 +1,23 @@
+package editor
+
+import (
+	"moose/internal/buffer"
+)
+
+func (m *Model) AddBuffer(newNode bool) {
+	b := buffer.NewBuffer()
+	idx := len(m.BM.Buffers)
+	m.BM.Buffers = append(m.BM.Buffers, b)
+	m.BM.CurrentIdx = idx
+
+	m.LM.Workspaces[m.LM.ActiveIdx].RootContainer.InsertBufferInContainer(idx)
+}
+
+func (m *Model) AddBufferFromPath(path string, newNode bool) {
+	b := buffer.NewBufferFromPath(path)
+	idx := len(m.BM.Buffers)
+	m.BM.Buffers = append(m.BM.Buffers, b)
+	m.BM.CurrentIdx = idx
+
+	m.LM.Workspaces[m.LM.ActiveIdx].RootContainer.InsertBufferInContainer(idx)
+}

+ 2 - 2
internal/editor/model.go

@@ -23,8 +23,8 @@ func NewModel(screen tcell.Screen, style tcell.Style) Model {
 		Style:		   style,
 		Mode:		   ModeNormal,
 		BM:            buffer.BufferManager{
-			Buffers:       []buffer.Buffer{buffer.NewBuffer(), buffer.NewBuffer()},
-			CurrentIdx:    1,
+			Buffers:       []buffer.Buffer{buffer.NewBuffer()},
+			CurrentIdx:    0,
 			PaletteBuffer: buffer.NewBuffer(),
 		},
 		AM:            DefaultActionManager(),

+ 46 - 69
internal/layout/layout.go

@@ -1,69 +1,12 @@
 package layout
 
-import (
-	"github.com/gdamore/tcell/v3"
-)
-
-type Rect struct {
-	X      int
-	Y      int
-	Width  int
-	Height int
-}
-
-func RectDivide(rect Rect, split SplitType, num int) Rect {
-	switch split {
-	case SplitHorizontal: return Rect{
-		X: rect.X,
-		Y: rect.Y,
-		Width: rect.Width / num,
-		Height: rect.Height,
-	}
-	case SplitVertical: return Rect{
-		X: rect.X,
-		Y: rect.Y,
-		Width: rect.Width,
-		Height: rect.Height / num,
-	}
-	default: panic("[moose-error] impossible split type")
-	}
-}
-
-func RectDisplace(rect Rect, split SplitType, idx int) Rect {
-	switch split {
-	case SplitHorizontal: return Rect{
-		X: rect.X + (rect.Width * idx),
-		Y: rect.Y,
-		Width: rect.Width,
-		Height: rect.Height,
-	}
-	case SplitVertical: return Rect{
-		X: rect.X,
-		Y: rect.Y + (rect.Height * idx),
-		Width: rect.Width,
-		Height: rect.Height,
-	}
-	default: panic("[moose-error] impossible split type")
-	}
-}
-
-func RectFromScren(s tcell.Screen) Rect {
-	sWidth, sHeight := s.Size()
-	return Rect{
-		X: 0,
-		Y: 0,
-		Width: sWidth,
-		Height: sHeight,
-	}
-}
-
 type LayoutManager struct {
 	Workspaces []Workspace
 	ActiveIdx  int
 }
 
 type Workspace struct {
-	RootContainer Container[ContainerBuffers]
+	RootContainer Container
 }
 
 type SplitType int
@@ -73,7 +16,7 @@ const (
 )
 
 type ContainerNode interface {
-	ContainerBuffers | Container[ContainerBuffers]
+	isContainerNode()
 }
 
 type ContainerBuffers struct {
@@ -81,12 +24,16 @@ type ContainerBuffers struct {
 	ActiveIdx int
 }
 
-type Container[T ContainerNode] struct {
-	Children 	   [2]any
-	Split  		   SplitType
+func (ContainerBuffers) isContainerNode() {}
+
+type Container struct {
+	Children       [2]ContainerNode
+	Split          SplitType
 	ActiveChildIdx int
 }
 
+func (Container) isContainerNode() {}
+
 func NewLayoutManager() LayoutManager {
 	return LayoutManager{
 		Workspaces: []Workspace{NewWorkspace()},
@@ -99,19 +46,49 @@ func NewWorkspace() Workspace {
 	}
 }
 
-func NewContainerEmpty() Container[ContainerBuffers] {
-	return Container[ContainerBuffers]{
-		Children: 	    [2]any{
+func NewContainerEmpty() Container {
+	return Container{
+		Children: 	    [2]ContainerNode{
 			ContainerBuffers{
 				Buffers: []int{0},
 				ActiveIdx: 0,
 			},
-			ContainerBuffers{
-				Buffers: []int{1},
-				ActiveIdx: 0,
-			},
 		},
 		Split:	  	    SplitVertical,
 		ActiveChildIdx: 0,
 	}
+}
+
+// NB: does not set the buffer manager's CurrentIdx, only the layouts active indecies!
+func (c *Container) InsertBufferInContainer(bufferIdx int) {
+	for i := range c.Children {
+		switch child := c.Children[i].(type) {
+		case ContainerBuffers:
+			child.Buffers = append(child.Buffers, bufferIdx)
+			child.ActiveIdx = len(child.Buffers) - 1
+
+			c.Children[i] = child
+			c.ActiveChildIdx = i
+			return
+
+		case Container:
+			child.InsertBufferInContainer(bufferIdx)
+			c.Children[i] = child
+			c.ActiveChildIdx = i
+			return
+		}
+	}
+
+	c.Children[0] = ContainerBuffers{
+		Buffers:   []int{bufferIdx},
+		ActiveIdx: 0,
+	}
+	c.ActiveChildIdx = 0
+}
+
+// ny funksjon som setter in i aktiv
+func (lm *LayoutManager) InsertBuffer(bufferIdx int, newNode bool) {
+	// follow active child trail to find the active buffer container, if not newNode insert in there using InsertBufferInContainer,
+	// else: make new containerbuffers in that current active container, if it's full then split the containerbuffers into a new containernode
+	// and then a new containerbuffers is made and the bufferidx is put there
 }

+ 58 - 0
internal/layout/rect.go

@@ -0,0 +1,58 @@
+package layout
+
+import (
+	"github.com/gdamore/tcell/v3"
+)
+
+type Rect struct {
+	X      int
+	Y      int
+	Width  int
+	Height int
+}
+
+func RectDivide(rect Rect, split SplitType, num int) Rect {
+	switch split {
+	case SplitHorizontal: return Rect{
+		X: rect.X,
+		Y: rect.Y,
+		Width: rect.Width / num,
+		Height: rect.Height,
+	}
+	case SplitVertical: return Rect{
+		X: rect.X,
+		Y: rect.Y,
+		Width: rect.Width,
+		Height: rect.Height / num,
+	}
+	default: panic("[moose-error] impossible split type")
+	}
+}
+
+func RectDisplace(rect Rect, split SplitType, idx int) Rect {
+	switch split {
+	case SplitHorizontal: return Rect{
+		X: rect.X + (rect.Width * idx),
+		Y: rect.Y,
+		Width: rect.Width,
+		Height: rect.Height,
+	}
+	case SplitVertical: return Rect{
+		X: rect.X,
+		Y: rect.Y + (rect.Height * idx),
+		Width: rect.Width,
+		Height: rect.Height,
+	}
+	default: panic("[moose-error] impossible split type")
+	}
+}
+
+func RectFromScren(s tcell.Screen) Rect {
+	sWidth, sHeight := s.Size()
+	return Rect{
+		X: 0,
+		Y: 0,
+		Width: sWidth,
+		Height: sHeight,
+	}
+}