|
| 1 | +package markdown |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + sitter "github.com/smacker/go-tree-sitter" |
| 7 | + tree_sitter_markdown "github.com/smacker/go-tree-sitter/markdown/tree-sitter-markdown" |
| 8 | + tree_sitter_markdown_inline "github.com/smacker/go-tree-sitter/markdown/tree-sitter-markdown-inline" |
| 9 | +) |
| 10 | + |
| 11 | +type MarkdownTree struct { |
| 12 | + blockTree *sitter.Tree |
| 13 | + inlineTrees []*sitter.Tree |
| 14 | + inlineIndices map[uintptr]int |
| 15 | +} |
| 16 | + |
| 17 | +func (t *MarkdownTree) Edit(edit sitter.EditInput) { |
| 18 | + t.blockTree.Edit(edit) |
| 19 | + for _, tree := range t.inlineTrees { |
| 20 | + tree.Edit(edit) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func (t *MarkdownTree) BlockTree() *sitter.Tree { |
| 25 | + return t.blockTree |
| 26 | +} |
| 27 | + |
| 28 | +func (t *MarkdownTree) InlineTree(parent *sitter.Node) *sitter.Tree { |
| 29 | + if parent == nil { |
| 30 | + return nil |
| 31 | + } |
| 32 | + |
| 33 | + index, ok := t.inlineIndices[parent.ID()] |
| 34 | + if ok { |
| 35 | + return t.inlineTrees[index] |
| 36 | + } |
| 37 | + |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +func (t *MarkdownTree) InlineRootNode(parent *sitter.Node) *sitter.Node { |
| 42 | + tree := t.InlineTree(parent) |
| 43 | + if tree == nil { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + return tree.RootNode() |
| 48 | +} |
| 49 | + |
| 50 | +func (t *MarkdownTree) InlineTrees() []*sitter.Tree { |
| 51 | + return t.inlineTrees |
| 52 | +} |
| 53 | + |
| 54 | +func (t *MarkdownTree) Iter(f func(node *Node) bool) { |
| 55 | + root := t.blockTree.RootNode() |
| 56 | + t.iter(&Node{root, t.InlineRootNode(root)}, f) |
| 57 | +} |
| 58 | + |
| 59 | +func (t *MarkdownTree) iter(node *Node, f func(node *Node) bool) (goNext bool) { |
| 60 | + goNext = f(node) |
| 61 | + if !goNext { |
| 62 | + return goNext |
| 63 | + } |
| 64 | + |
| 65 | + childCount := node.NamedChildCount() |
| 66 | + for i := 0; i < int(childCount); i++ { |
| 67 | + child := node.NamedChild(i) |
| 68 | + |
| 69 | + goNext = t.iter(&Node{Node: child, Inline: t.InlineRootNode(child)}, f) |
| 70 | + if !goNext { |
| 71 | + return goNext |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return true |
| 76 | +} |
| 77 | + |
| 78 | +type Node struct { |
| 79 | + *sitter.Node |
| 80 | + Inline *sitter.Node |
| 81 | +} |
| 82 | + |
| 83 | +func ParseCtx(ctx context.Context, oldTree *MarkdownTree, content []byte) (*MarkdownTree, error) { |
| 84 | + p := sitter.NewParser() |
| 85 | + p.SetLanguage(tree_sitter_markdown.GetLanguage()) |
| 86 | + |
| 87 | + var old *sitter.Tree |
| 88 | + if oldTree != nil { |
| 89 | + old = oldTree.blockTree |
| 90 | + } |
| 91 | + tree, err := p.ParseCtx(ctx, old, content) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + res := &MarkdownTree{ |
| 97 | + blockTree: tree, |
| 98 | + inlineTrees: []*sitter.Tree{}, |
| 99 | + inlineIndices: map[uintptr]int{}, |
| 100 | + } |
| 101 | + |
| 102 | + p.SetLanguage(tree_sitter_markdown_inline.GetLanguage()) |
| 103 | + |
| 104 | + q, err := sitter.NewQuery([]byte(`(inline) @inline`), tree_sitter_markdown.GetLanguage()) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + qc := sitter.NewQueryCursor() |
| 110 | + qc.Exec(q, tree.RootNode()) |
| 111 | + |
| 112 | + idx := int(0) |
| 113 | + for { |
| 114 | + match, ok := qc.NextMatch() |
| 115 | + if !ok { |
| 116 | + break |
| 117 | + } |
| 118 | + |
| 119 | + for _, capture := range match.Captures { |
| 120 | + r := capture.Node.Range() |
| 121 | + ranges := []sitter.Range{} |
| 122 | + for i := 0; i < int(capture.Node.NamedChildCount()); i++ { |
| 123 | + child := capture.Node.NamedChild(i) |
| 124 | + childRange := child.Range() |
| 125 | + ranges = append(ranges, sitter.Range{ |
| 126 | + StartPoint: r.StartPoint, |
| 127 | + StartByte: r.StartByte, |
| 128 | + EndPoint: childRange.EndPoint, |
| 129 | + EndByte: childRange.EndByte, |
| 130 | + }) |
| 131 | + |
| 132 | + r.StartPoint = childRange.EndPoint |
| 133 | + r.StartByte = childRange.EndByte |
| 134 | + } |
| 135 | + |
| 136 | + ranges = append(ranges, r) |
| 137 | + p.SetIncludedRanges(ranges) |
| 138 | + var old *sitter.Tree |
| 139 | + if oldTree != nil && idx < len(oldTree.inlineTrees) { |
| 140 | + old = oldTree.inlineTrees[idx] |
| 141 | + } |
| 142 | + |
| 143 | + inlineTree, err := p.ParseCtx(ctx, old, content) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + |
| 148 | + res.inlineTrees = append(res.inlineTrees, inlineTree) |
| 149 | + res.inlineIndices[capture.Node.ID()] = idx |
| 150 | + idx++ |
| 151 | + } |
| 152 | + } |
| 153 | + qc.Close() |
| 154 | + |
| 155 | + return res, nil |
| 156 | +} |
0 commit comments