Add support for json streams in interactive more

pull/291/head
Anton Medvedev 7 months ago
parent c699c8c992
commit 2e25c7e794
No known key found for this signature in database

@ -15,7 +15,7 @@ type jsonParser struct {
skipFirstIdent bool skipFirstIdent bool
} }
func parse(data []byte) (line *node, err error) { func parse(data []byte) (head *node, err error) {
p := &jsonParser{ p := &jsonParser{
data: data, data: data,
lineNumber: 1, lineNumber: 1,
@ -27,9 +27,17 @@ func parse(data []byte) (line *node, err error) {
} }
}() }()
p.next() p.next()
line = p.parseValue() var next *node
if p.lastChar != 0 { for p.lastChar != 0 {
panic(fmt.Sprintf("Unexpected character %q after root node", p.lastChar)) value := p.parseValue()
if head == nil {
head = value
next = head
} else {
value.index = -1
next.adjacent(value)
next = value
}
} }
return return
} }

@ -444,14 +444,30 @@ func (m *model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.showCursor = true m.showCursor = true
case key.Matches(msg, keyMap.CollapseAll): case key.Matches(msg, keyMap.CollapseAll):
m.top.collapseRecursively() n := m.top
for n != nil {
n.collapseRecursively()
if n.end == nil {
n = nil
} else {
n = n.end.next
}
}
m.cursor = 0 m.cursor = 0
m.head = m.top m.head = m.top
m.showCursor = true m.showCursor = true
case key.Matches(msg, keyMap.ExpandAll): case key.Matches(msg, keyMap.ExpandAll):
at := m.cursorPointsTo() at := m.cursorPointsTo()
m.top.expandRecursively() n := m.top
for n != nil {
n.expandRecursively()
if n.end == nil {
n = nil
} else {
n = n.end.next
}
}
m.selectNode(at) m.selectNode(at)
case key.Matches(msg, keyMap.ToggleWrap): case key.Matches(msg, keyMap.ToggleWrap):
@ -851,7 +867,7 @@ func (m *model) cursorKey() string {
} }
func (m *model) selectByPath(path []any) *node { func (m *model) selectByPath(path []any) *node {
n := m.top n := m.currentTopNode()
for _, part := range path { for _, part := range path {
if n == nil { if n == nil {
return nil return nil
@ -866,6 +882,17 @@ func (m *model) selectByPath(path []any) *node {
return n return n
} }
func (m *model) currentTopNode() *node {
at := m.cursorPointsTo()
if at == nil {
return nil
}
for at.parent() != nil {
at = at.parent()
}
return at
}
func (m *model) doSearch(s string) { func (m *model) doSearch(s string) {
m.search = newSearch() m.search = newSearch()

@ -20,6 +20,7 @@ type node struct {
index int index int
} }
// append ands a node as a child to the current node (body of {...} or [...]).
func (n *node) append(child *node) { func (n *node) append(child *node) {
if n.end == nil { if n.end == nil {
n.end = n n.end = n
@ -33,6 +34,16 @@ func (n *node) append(child *node) {
} }
} }
// adjacent adds a node as a sibling to the current node ({}{}{} or [][][]).
func (n *node) adjacent(child *node) {
end := n.end
if end == nil {
end = n
}
end.next = child
child.prev = end
}
func (n *node) insertChunk(chunk *node) { func (n *node) insertChunk(chunk *node) {
if n.chunkEnd == nil { if n.chunkEnd == nil {
n.insertAfter(chunk) n.insertAfter(chunk)

Loading…
Cancel
Save