Add support for json streams in interactive more

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

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

@ -444,14 +444,30 @@ func (m *model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.showCursor = true
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.head = m.top
m.showCursor = true
case key.Matches(msg, keyMap.ExpandAll):
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)
case key.Matches(msg, keyMap.ToggleWrap):
@ -851,7 +867,7 @@ func (m *model) cursorKey() string {
}
func (m *model) selectByPath(path []any) *node {
n := m.top
n := m.currentTopNode()
for _, part := range path {
if n == nil {
return nil
@ -866,6 +882,17 @@ func (m *model) selectByPath(path []any) *node {
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) {
m.search = newSearch()

@ -20,6 +20,7 @@ type node struct {
index int
}
// append ands a node as a child to the current node (body of {...} or [...]).
func (n *node) append(child *node) {
if n.end == nil {
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) {
if n.chunkEnd == nil {
n.insertAfter(chunk)

Loading…
Cancel
Save