Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions internal/jsonx/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ type Node struct {
LineNumber int
}

type Tombstone struct {
Target *Node
Parent *Node
Prev *Node
Next *Node
EndOf *Node
Index int
HadComma bool
}

// Append ands a node as a child to the current node (body of {...} or [...]).
func (n *Node) Append(child *Node) {
if n.End == nil {
Expand Down Expand Up @@ -367,3 +377,87 @@ func (n *Node) ForEach(cb func(*Node)) {
}
}
}

func (n *Node) GetNodeToDelete() (*Node, bool) {
if n == nil {
return nil, false
}
// Avoid closing bracket nodes (Index == -1 used for brackets)
if n.Index == -1 {
return nil, false
}
parent := n.Parent
if parent == nil { // avoid deleting root
return nil, false
}
// If current points to a wrap placeholder, move to its parent value
node := n
if n.Chunk != "" && n.Value == "" && n.Parent != nil {
node = node.Parent
parent = node.Parent
if parent == nil {
return nil, false
}
}
return node, true
}

func (n *Node) CreateTombstone() Tombstone {
endOf := n
if n.End != nil {
endOf = n.End
} else if n.ChunkEnd != nil {
endOf = n.ChunkEnd
}

t := Tombstone{
Target: n,
EndOf: endOf,
Parent: n.Parent,
Prev: n.Prev,
Next: endOf.Next,
Index: n.Index,
}

if t.Prev != nil && t.Prev != t.Parent {
t.HadComma = t.Prev.Comma
}

return t
}

func (t *Tombstone) DoUndo() {
if t.Prev != nil {
t.Prev.Next = t.Target
}
if t.Next != nil {
t.Next.Prev = t.EndOf
}

// if it was the first child
if t.Parent != nil && t.Parent.Next == t.Next {
t.Parent.Next = t.Target
}

// if DeleteNode cleared a comma
if t.Prev != nil && t.Prev != t.Parent {
t.Prev.Comma = t.HadComma
}

// Reverse Array/Size logic
if t.Parent != nil {
t.Parent.Size++
if t.Parent.Kind == Array {
for it := t.Next; it != nil && it != t.Parent.End; {
if it.Parent == t.Parent && it.Index >= 0 {
it.Index++
}
if it.HasChildren() {
it = it.End.Next
} else {
it = it.Next
}
}
}
}
}
10 changes: 10 additions & 0 deletions keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type KeyMap struct {
Preview key.Binding `category:"Actions"`
Print key.Binding `category:"Actions"`
Open key.Binding `category:"Actions"`
Undo key.Binding `category:"Actions"`
Redo key.Binding `category:"Actions"`
ToggleWrap key.Binding `category:"View"`
ShowSelector key.Binding `category:"View"`
GoBack key.Binding `category:"Navigation"`
Expand Down Expand Up @@ -148,6 +150,14 @@ func init() {
key.WithKeys("d"),
key.WithHelp("", "delete node"),
),
Undo: key.NewBinding(
key.WithKeys("u"),
key.WithHelp("", "undo delete"),
),
Redo: key.NewBinding(
key.WithKeys("ctrl+r"),
key.WithHelp("", "redo delete"),
),
CommandLine: key.NewBinding(
key.WithKeys(":"),
key.WithHelp("", "open command line"),
Expand Down
58 changes: 57 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ type model struct {
printErrorOnExit error
spinner spinner.Model
locationHistory []location
undoStack []Tombstone
redoStack []Tombstone
locationIndex int // position in locationHistory
keysIndex []string
keysIndexNodes []*Node
Expand Down Expand Up @@ -976,7 +978,12 @@ func (m *model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {

case key.Matches(msg, keyMap.Delete):
m.deletePending = true
case key.Matches(msg, keyMap.Undo):
m.undoDelete()
case key.Matches(msg, keyMap.Redo): // Make sure Redo is defined in your keyMap
m.redoDelete()
}

return m, nil
}

Expand Down Expand Up @@ -1035,6 +1042,46 @@ func (m *model) recordHistory() {
m.locationIndex = len(m.locationHistory)
}

func (m *model) recordDeleteHistory(ts Tombstone) {
m.undoStack = append(m.undoStack, ts)
m.redoStack = []Tombstone{}

if len(m.undoStack) > 100 {
m.undoStack = m.undoStack[1:]
}
}

func (m *model) undoDelete() {
if len(m.undoStack) == 0 {
return
}

lastIdx := len(m.undoStack) - 1
t := m.undoStack[lastIdx]
m.undoStack = m.undoStack[:lastIdx]

t.DoUndo()

m.redoStack = append(m.redoStack, t)
m.selectNode(t.Target)
}

func (m *model) redoDelete() {
if len(m.redoStack) == 0 {
return
}

t := m.redoStack[len(m.redoStack)-1]
m.redoStack = m.redoStack[:len(m.redoStack)-1]

if next, ok := DeleteNode(t.Target); ok {
m.selectNode(next)
m.recordHistory()
}

m.undoStack = append(m.undoStack, t)
}

func (m *model) scrollToBottom() {
if m.bottom == nil {
return
Expand Down Expand Up @@ -1467,7 +1514,16 @@ func (m *model) deleteAtCursor() {
if !ok || at == nil {
return
}
if next, ok := DeleteNode(at); ok {

nodeToDelete, ok := at.GetNodeToDelete()
if !ok {
return
}

ts := nodeToDelete.CreateTombstone()
m.recordDeleteHistory(ts)

if next, ok := DeleteNode(nodeToDelete); ok {
m.selectNode(next)
m.recordHistory()
}
Expand Down
32 changes: 32 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,35 @@ func TestCollapseRecursiveWithSizes(t *testing.T) {
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}

func TestUndoRedoInteraction(t *testing.T) {
tm := prepare(t)
targetKey := []byte(`"title"`)

// Leave root, then delete (first key)
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")})
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("d")})
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("d")})

// Verify the node is gone from the output
teatest.WaitFor(t, tm.Output(), func(b []byte) bool {
return !bytes.Contains(b, targetKey)
}, teatest.WithDuration(time.Second))

tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("u")})

// Verify the node reappeared
teatest.WaitFor(t, tm.Output(), func(b []byte) bool {
return bytes.Contains(b, targetKey)
}, teatest.WithDuration(time.Second))

tm.Send(tea.KeyMsg{Type: tea.KeyCtrlR})

// Verify it is gone again
teatest.WaitFor(t, tm.Output(), func(b []byte) bool {
return !bytes.Contains(b, targetKey)
}, teatest.WithDuration(time.Second))

tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t)
}
4 changes: 2 additions & 2 deletions testdata/TestCollapseRecursive.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"tags": […],
"year": 3000,
"funny": true,
"author": {"name":"John Doe",…}
"author": {"name":"John Doe",…},
"hasAvatar": true
}
~
~
Expand All @@ -35,6 +36,5 @@
~
~
~
~
~
1% 
Expand Down
6 changes: 3 additions & 3 deletions testdata/TestCollapseRecursiveWithSizes.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[?25l[?2004h{ (6 keys)
[?25l[?2004h{ (7 keys)
"title": "Lorem ipsum",
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusm
od tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam
Expand All @@ -9,7 +9,8 @@
"tags": […], (3 items)
"year": 3000,
"funny": true,
"author": {"name":"John Doe",…} (2 keys)
"author": {"name":"John Doe",…}, (2 keys)
"hasAvatar": true
}
~
~
Expand All @@ -35,6 +36,5 @@
~
~
~
~
~
1% 
Expand Down
40 changes: 40 additions & 0 deletions testdata/TestDig.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[?25l[?2004h{
"title": "Lorem ipsum",
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusm
od tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam
, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo cons
equat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum d
olore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident
, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"tags": [
"lorem",
"ipsum",
null
],
"year": 3000,
"funny": true,
"author": {
"name": "John Doe",
"email": "john@doe.com"
},
"hasAvatar": true
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
.year 56% 
Expand Down
8 changes: 4 additions & 4 deletions testdata/TestGotoLine.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
11 "author": {
12 "name": "John Doe",
13 "email": "john@doe.com"
14 }
15 }
14 },
15 "hasAvatar": true
16 }
~
~
~
Expand All @@ -36,5 +37,4 @@
~
~
~
~
.tags[0] 33% 
.tags[0] 31% 
Expand Down
8 changes: 4 additions & 4 deletions testdata/TestGotoLineCollapsed.golden
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
 8 ],
 9 "year": 3000,
10 "funny": true,
11 "author": {"name":"John Doe",…}
15 }
11 "author": {"name":"John Doe",…},
15 "hasAvatar": true
16 }
~
~
~
Expand All @@ -36,5 +37,4 @@
~
~
~
~
.tags[0] 33% 
.tags[0] 31% 
Expand Down
6 changes: 3 additions & 3 deletions testdata/TestGotoLineInputGreaterThanTotalLines.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
11 "author": {
12 "name": "John Doe",
13 "email": "john@doe.com"
14 }
15 }
~
14 },
15 "hasAvatar": true
16 }
~
~
~
Expand Down
Loading