diff --git a/cmd/viva/components_gen.go b/cmd/viva/components_gen.go index 95d3c8a00..16cef0ca2 100644 --- a/cmd/viva/components_gen.go +++ b/cmd/viva/components_gen.go @@ -63,6 +63,7 @@ var componentFiles = map[string]string{ "label": "label.go.txt", "markdown": "markdown.go.txt", "markdown-anchor": "markdown_anchor.go.txt", + "markdown-index": "markdown_index.go.txt", "marker": "marker.go.txt", "menu-typeahead": "menu_typeahead.go.txt", "menubar": "menubar.go.txt", @@ -145,8 +146,9 @@ var componentRequires = map[string][]string{ "input-group": {"button", "input", "spinner", "textarea"}, "input-otp": {"input"}, "item": {"button"}, - "markdown": {"markdown-anchor"}, + "markdown": {"markdown-anchor", "markdown-index"}, "markdown-anchor": {"markdown"}, + "markdown-index": {"markdown", "markdown-anchor"}, "marker": {"spinner"}, "menubar": {"dialog", "direction", "dropdown-menu", "hover-card", "popover"}, "metric-card": {"card"}, diff --git a/cmd/viva/templates/markdown.go.txt b/cmd/viva/templates/markdown.go.txt index e58982cae..1b08a477c 100644 --- a/cmd/viva/templates/markdown.go.txt +++ b/cmd/viva/templates/markdown.go.txt @@ -143,29 +143,35 @@ var _ gfx.TextPlacement = (*MarkdownView)(nil) // Layout measures text and is the expensive half: do it when the text or the // width changed, and call Paint every frame. type MarkdownView struct { - pieces []mdPiece - runs []mdCodeRun - hl []MarkdownCodeRun - scratch []gfx.TextRun - selScratch []gfx.TextSpan - boxes []mdBox - links []mdLink - codes []MarkdownCode - images []mdImage - anchors []MarkdownAnchor - atoms []mdAtom - numbers []byte - tabs []byte // code lines with their tabs spread into blanks - tabBound []int32 // where each byte of a spread code line came from - plain []byte // the rendered text, for selection and copying - spanBase []int32 // where each span of the current inline block starts in plain - measure *gfx.Canvas - paintX float32 // where the last Paint put the document - paintY float32 - painted bool - bars [markdownMaxQuote + 1]int32 - face gfx.MonoFace - height float32 + pieces []mdPiece + runs []mdCodeRun + hl []MarkdownCodeRun + scratch []gfx.TextRun + selScratch []gfx.TextSpan + boxes []mdBox + links []mdLink + codes []MarkdownCode + images []mdImage + anchors []MarkdownAnchor + atoms []mdAtom + numbers []byte + tabs []byte // code lines with their tabs spread into blanks + tabBound []int32 // where each byte of a spread code line came from + plain []byte // the rendered text, for selection and copying + spanBase []int32 // where each span of the current inline block starts in plain + blockSpans []mdBlockSpan + rowSpans []mdRowSpan + cellSpans []mdCellSpan + tableRows []mdTableRowSpan + quoteBarIdx [markdownMaxQuote][]int32 + plainPieces []int32 + measure *gfx.Canvas + paintX float32 // where the last Paint put the document + paintY float32 + painted bool + bars [markdownMaxQuote + 1]int32 + face gfx.MonoFace + height float32 } // Height is the height of the laid-out document. @@ -190,53 +196,7 @@ func (v *MarkdownView) OffsetAt(point gfx.Point) (offset int, ok bool) { if !v.painted || v.measure == nil || len(v.pieces) == 0 { return 0, false } - x, y := point.X-v.paintX, point.Y-v.paintY - best, bestScore := -1, float32(0) - var previous *mdPiece - for index := range v.pieces { - piece := &v.pieces[index] - if piece.plainLo < 0 { - continue - } - if previous != nil && previous.y == piece.y && previous.plainHi < piece.plainLo && y >= piece.y && y <= piece.y+piece.h { - start := previous.x + v.caretAdvance(previous, int(previous.plainHi-previous.plainLo)) - end := piece.x + v.caretAdvance(piece, 0) - if end > start && x >= start && x <= end { - gap := int(piece.plainLo - previous.plainHi) - position := int(float32(gap)*(x-start)/(end-start) + 0.5) - return gfx.GraphemeStart(unsafe.String(unsafe.SliceData(v.plain), len(v.plain)), int(previous.plainHi)+min(max(position, 0), gap)), true - } - } - previous = piece - dx, dy := float32(0), float32(0) - if x < piece.x { - dx = piece.x - x - } else if x > piece.x+piece.w { - dx = x - piece.x - piece.w - } - if y < piece.y { - dy = piece.y - y - } else if y > piece.y+piece.h { - dy = y - piece.y - piece.h - } - score := dy*1_000_000 + dx - if best < 0 || score < bestScore { - best, bestScore = index, score - } - } - if best < 0 { - return 0, false - } - piece := &v.pieces[best] - localX := min(max(x-piece.x, 0), piece.w) - var local int - if piece.mono { - local = v.codeOffsetAt(piece, localX) - } else { - local = v.measure.TextOffsetAtStyle(piece.text, gfx.TextStyle{Size: piece.size, Weight: piece.weight, Italic: piece.italic}, localX) - } - local = min(max(local, 0), int(piece.plainHi-piece.plainLo)) - return int(piece.plainLo) + local, true + return v.offsetAtIndexed(point.X-v.paintX, point.Y-v.paintY) } // CaretAt returns the window rectangle for a rendered byte offset after Paint. @@ -245,50 +205,7 @@ func (v *MarkdownView) CaretAt(offset int) (gfx.Rect, bool) { if !v.painted || v.measure == nil || len(v.pieces) == 0 { return gfx.Rect{}, false } - value := unsafe.String(unsafe.SliceData(v.plain), len(v.plain)) - offset = gfx.GraphemeStart(value, offset) - var chosen, before, after *mdPiece - for index := range v.pieces { - piece := &v.pieces[index] - if piece.plainLo < 0 { - continue - } - switch { - case offset >= int(piece.plainLo) && offset <= int(piece.plainHi): - if chosen == nil || piece.plainLo == int32(offset) && chosen.plainLo < int32(offset) { - chosen = piece - } - case int(piece.plainHi) < offset: - if before == nil || piece.plainHi > before.plainHi { - before = piece - } - case int(piece.plainLo) > offset: - if after == nil || piece.plainLo < after.plainLo { - after = piece - } - } - } - if chosen == nil { - if before != nil && after != nil && before.y == after.y { - start := before.x + v.caretAdvance(before, int(before.plainHi-before.plainLo)) - end := after.x + v.caretAdvance(after, 0) - fraction := float32(offset-int(before.plainHi)) / float32(after.plainLo-before.plainHi) - return gfx.Rect{X: v.paintX + start + (end-start)*fraction, Y: v.paintY + before.y, Width: 1, Height: before.h}, true - } - if before == nil && after == nil { - return gfx.Rect{}, false - } - if before == nil || after != nil && int(after.plainLo)-offset <= offset-int(before.plainHi) { - chosen = after - offset = int(after.plainLo) - } else { - chosen = before - offset = int(before.plainHi) - } - } - local := min(max(offset-int(chosen.plainLo), 0), int(chosen.plainHi-chosen.plainLo)) - advance := v.caretAdvance(chosen, local) - return gfx.Rect{X: v.paintX + chosen.x + advance, Y: v.paintY + chosen.y, Width: 1, Height: chosen.h}, true + return v.caretAtIndexed(offset) } func (v *MarkdownView) caretAdvance(piece *mdPiece, local int) float32 { @@ -410,6 +327,7 @@ const ( markdownCodePadY = 8 markdownCellPadX = 8 markdownCellPadY = 5 + markdownLabelGap = 12 markdownMaxLines = 20000 // the most lines one layout produces ) @@ -520,6 +438,10 @@ func (v *MarkdownView) Layout(canvas *gfx.Canvas, style MarkdownStyle, doc *mark } v.images = v.images[:0] v.pieces, v.boxes, v.links, v.codes, v.anchors, v.numbers, v.runs, v.tabs, v.tabBound, v.plain, v.spanBase = v.pieces[:0], v.boxes[:0], v.links[:0], v.codes[:0], v.anchors[:0], v.numbers[:0], v.runs[:0], v.tabs[:0], v.tabBound[:0], v.plain[:0], v.spanBase[:0] + v.blockSpans, v.rowSpans, v.cellSpans, v.tableRows, v.plainPieces = v.blockSpans[:0], v.rowSpans[:0], v.cellSpans[:0], v.tableRows[:0], v.plainPieces[:0] + for qi := range v.quoteBarIdx { + v.quoteBarIdx[qi] = v.quoteBarIdx[qi][:0] + } v.bars = [markdownMaxQuote + 1]int32{} v.face = style.MonoFace v.measure = canvas @@ -530,6 +452,8 @@ func (v *MarkdownView) Layout(canvas *gfx.Canvas, style MarkdownStyle, doc *mark body := mdFont{size: style.Size, line: style.LineHeight, weight: gfx.WeightRegular, color: mdColText} y := float32(0) var prev *markdown.Block + labeledEnd := -1 + labeledColW := float32(0) for index := range doc.Blocks { if l.lines >= markdownMaxLines { break @@ -544,35 +468,76 @@ func (v *MarkdownView) Layout(canvas *gfx.Canvas, style MarkdownStyle, doc *mark t.color = mdColMuted } l.blockBreak() + blkSpan := mdBlockSpan{kind: b.Kind, y: top, blockBox: -1, markerPiece: -1} switch b.Kind { case markdown.Paragraph: indent := float32(0) if b.Level > 0 { indent = float32(b.Level-1)*markdownListStep + markdownMarkerW } + blkSpan.rowLo = int32(len(v.rowSpans)) l.appendSpans(doc.Spans(b)) y = l.inline(doc.Spans(b), x0+indent, w-indent, y, t, markdown.AlignLeft) + blkSpan.rowHi = int32(len(v.rowSpans)) case markdown.Heading: + blkSpan.rowLo = int32(len(v.rowSpans)) l.appendSpans(doc.Spans(b)) y = l.inline(doc.Spans(b), x0, w, y, l.heading(b.Level, t), markdown.AlignLeft) + blkSpan.rowHi = int32(len(v.rowSpans)) case markdown.Item: indent := float32(b.Level)*markdownListStep + markdownMarkerW + blkSpan.markerPiece = int32(len(v.pieces)) l.marker(b, x0+float32(b.Level)*markdownListStep, y, t) + blkSpan.rowLo = int32(len(v.rowSpans)) l.appendSpans(doc.Spans(b)) y = l.inline(doc.Spans(b), x0+indent, w-indent, y, t, markdown.AlignLeft) + blkSpan.rowHi = int32(len(v.rowSpans)) y = max(y, top+t.line) case markdown.Code: + blkSpan.rowLo = int32(len(v.rowSpans)) y = l.code(b, x0, w, y) + blkSpan.rowHi = int32(len(v.rowSpans)) + if len(v.boxes) > 0 && v.boxes[len(v.boxes)-1].kind == mdBoxCode { + blkSpan.blockBox = int32(len(v.boxes) - 1) + } case markdown.Rule: + blkSpan.blockBox = int32(len(v.boxes)) v.boxes = append(v.boxes, mdBox{kind: mdBoxLine, x: x0, y: y + 6, w: w, h: 1}) y += 13 case markdown.Table: + blkSpan.tableRowLo = int32(len(v.tableRows)) y = l.table(b, x0, w, y, t) + blkSpan.tableRowHi = int32(len(v.tableRows)) + case markdown.Labeled: + if index >= labeledEnd { + labelFont := t + labelFont.color = mdColMuted + maxLabel := float32(0) + end := index + for end < len(doc.Blocks) && doc.Blocks[end].Kind == markdown.Labeled && doc.Blocks[end].Quote == b.Quote { + if ls := doc.LabelSpans(&doc.Blocks[end]); len(ls) > 0 { + maxLabel = max(maxLabel, l.natural(ls, labelFont)) + } + end++ + } + labeledEnd = end + labeledColW = min(maxLabel, max(w*0.28, 48)) + } + blkSpan.cellLo = int32(len(v.cellSpans)) + y = l.labeled(b, x0, w, y, t, labeledColW) + blkSpan.cellHi = int32(len(v.cellSpans)) } + blkSpan.bottom = y + v.blockSpans = append(v.blockSpans, blkSpan) l.quoteBars(b.Quote, top, y) v.anchors = append(v.anchors, MarkdownAnchor{Start: b.Start, End: b.End, Top: top, Bottom: y}) prev = b } + for i := range v.pieces { + if v.pieces[i].plainLo >= 0 { + v.plainPieces = append(v.plainPieces, int32(i)) + } + } v.height = y return y } @@ -628,6 +593,8 @@ func (l *mdLayout) gap(prev, cur *markdown.Block) float32 { switch { case prev.Kind == markdown.Item && (cur.Kind == markdown.Item || cur.Kind == markdown.Paragraph && cur.Level > 0): return float32(int(lh * 0.25)) + case prev.Kind == markdown.Labeled && cur.Kind == markdown.Labeled: + return float32(int(lh * 0.25)) case cur.Kind == markdown.Heading: return float32(int(lh * 0.75)) case prev.Kind == markdown.Heading: @@ -636,6 +603,44 @@ func (l *mdLayout) gap(prev, cur *markdown.Block) float32 { return float32(int(lh * 0.45)) } +// labeled lays out one borderless two-column label–content block. +func (l *mdLayout) labeled(b *markdown.Block, x, width, y float32, t mdFont, labelW float32) float32 { + v, doc := l.v, l.doc + labelSpans := doc.LabelSpans(b) + valueSpans := doc.Spans(b) + labelFont := t + labelFont.color = mdColMuted + top := y + + lRowLo := int32(len(v.rowSpans)) + labelEnd := top + if len(labelSpans) > 0 && labelW > 0 { + l.appendSpans(labelSpans) + labelEnd = l.inline(labelSpans, x, labelW, top, labelFont, markdown.AlignLeft) + } + v.cellSpans = append(v.cellSpans, mdCellSpan{rowLo: lRowLo, rowHi: int32(len(v.rowSpans))}) + + if len(labelSpans) > 0 && len(valueSpans) > 0 { + v.plain = append(v.plain, ' ') + } + + valX := x + valW := width + if labelW > 0 { + valX = x + labelW + markdownLabelGap + valW = max(width-labelW-markdownLabelGap, 40) + } + vRowLo := int32(len(v.rowSpans)) + valEnd := top + if len(valueSpans) > 0 { + l.appendSpans(valueSpans) + valEnd = l.inline(valueSpans, valX, valW, top, t, markdown.AlignLeft) + } + v.cellSpans = append(v.cellSpans, mdCellSpan{rowLo: vRowLo, rowHi: int32(len(v.rowSpans))}) + + return max(max(labelEnd, valEnd), top+t.line) +} + // quoteBars keeps one bar per quote level running through the blocks inside it. func (l *mdLayout) quoteBars(quote int, top, bottom float32) { v := l.v @@ -649,8 +654,10 @@ func (l *mdLayout) quoteBars(quote int, top, bottom float32) { box.h = bottom - box.y continue } + boxIdx := int32(len(v.boxes)) v.boxes = append(v.boxes, mdBox{kind: mdBoxBar, x: float32(level-1)*markdownQuoteStep + 2, y: top, w: 3, h: bottom - top}) - v.bars[level] = int32(len(v.boxes)) + v.quoteBarIdx[level-1] = append(v.quoteBarIdx[level-1], boxIdx) + v.bars[level] = boxIdx + 1 } } @@ -704,6 +711,7 @@ func (l *mdLayout) code(b *markdown.Block, x, width, y float32) float32 { end, done := monoBreak(line[start:], cols) end += start end = codeBreak(line, bound, start, end) + pieceLo := int32(len(v.pieces)) if end > start { chunk := line[start:end] plainLo := int32(len(v.plain)) @@ -723,6 +731,10 @@ func (l *mdLayout) code(b *markdown.Block, x, width, y float32) float32 { v.pieces = append(v.pieces, piece) column += monoCells(chunk) } + v.rowSpans = append(v.rowSpans, mdRowSpan{ + y: y, bottom: y + height, + pieceLo: pieceLo, pieceHi: int32(len(v.pieces)), + }) l.lines++ y += height start = end @@ -986,6 +998,7 @@ func (l *mdLayout) table(b *markdown.Block, x, width, y float32, t mdFont) float } rowTop := y rowBottom := y + 2*markdownCellPadY + t.line + cellLo := int32(len(v.cellSpans)) cx := x for c := 0; c < columns; c++ { cell := cells[r*columns+c] @@ -996,16 +1009,24 @@ func (l *mdLayout) table(b *markdown.Block, x, width, y float32, t mdFont) float if c > 0 { v.plain = append(v.plain, ' ') } + cRowLo := int32(len(v.rowSpans)) l.appendSpans(doc.CellSpans(cell)) end := l.inline(doc.CellSpans(cell), cx+markdownCellPadX, widths[c]-2*markdownCellPadX, rowTop+markdownCellPadY, style, cell.Align) + v.cellSpans = append(v.cellSpans, mdCellSpan{rowLo: cRowLo, rowHi: int32(len(v.rowSpans))}) rowBottom = max(rowBottom, end+markdownCellPadY) cx += widths[c] } y = rowBottom + boxLo := int32(len(v.boxes)) if r == 0 { v.boxes = append(v.boxes, mdBox{kind: mdBoxHead, x: x, y: rowTop, w: tableW, h: rowBottom - rowTop}) } v.boxes = append(v.boxes, mdBox{kind: mdBoxLine, x: x, y: y - 1, w: tableW, h: 1}) + v.tableRows = append(v.tableRows, mdTableRowSpan{ + y: rowTop, bottom: rowBottom, + cellLo: cellLo, cellHi: int32(len(v.cellSpans)), + boxLo: boxLo, boxHi: int32(len(v.boxes)), + }) } return y } @@ -1597,16 +1618,27 @@ func (m *mdLine) endLine() { } // A line with an image is as tall as the tallest image, with the text along its // top and a little air below the pixels so that stacked images do not touch. + lineTop := m.y m.y += max(m.t.line, m.tall+4) + v.rowSpans = append(v.rowSpans, mdRowSpan{ + y: lineTop, bottom: m.y, + pieceLo: int32(m.pieceP), pieceHi: int32(len(v.pieces)), + boxLo: int32(m.pieceB), boxHi: int32(len(v.boxes)), + linkLo: int32(m.pieceL), linkHi: int32(len(v.links)), + imageLo: int32(m.pieceI), imageHi: int32(len(v.images)), + }) m.l.lines++ m.reset() } -// Paint draws the laid-out document. +// Paint draws the laid-out document, querying the spatial index so only +// visible blocks, quote bars, table rows, and cell visual rows are visited. func (v *MarkdownView) Paint(canvas *gfx.Canvas, p MarkdownPaint) { v.paintX, v.paintY, v.painted = p.X, p.Y, true look := p.Look clipped := p.Clip.Height > 0 + clipTop := p.Clip.Y - p.Y + clipBottom := p.Clip.Y + p.Clip.Height - p.Y visible := func(y, h float32) bool { return !clipped || (y+h >= p.Clip.Y && y <= p.Clip.Y+p.Clip.Height) } @@ -1616,10 +1648,12 @@ func (v *MarkdownView) Paint(canvas *gfx.Canvas, p MarkdownPaint) { }) defer canvas.Unclip() } - for _, box := range v.boxes { + firstBlk, lastBlk := v.visibleBlockRange(clipped, clipTop, clipBottom) + + paintBox := func(box mdBox) { rect := gfx.Rect{X: p.X + box.x, Y: p.Y + box.y, Width: box.w, Height: box.h} if !visible(rect.Y, rect.Height) { - continue + return } switch box.kind { case mdBoxCode: @@ -1634,54 +1668,92 @@ func (v *MarkdownView) Paint(canvas *gfx.Canvas, p MarkdownPaint) { canvas.Rounded(rect, 4, look.InlineCodeFill) } } - for _, image := range v.images { - rect := gfx.Rect{X: p.X + image.x, Y: p.Y + image.y, Width: image.w, Height: image.h} - if visible(rect.Y, rect.Height) { - canvas.DrawBitmap(image.bitmap, rect) + + // Pass 1: quote bars, block/table backgrounds, and inline-code pills. + for level := 0; level < markdownMaxQuote; level++ { + for _, bi := range v.visibleQuoteBars(level, clipped, clipTop, clipBottom) { + paintBox(v.boxes[bi]) } } - start, end := p.Selection.Range(v.plain) - if start < end && look.Selection.Alpha > 0 { - for index := range v.pieces { - piece := &v.pieces[index] - if piece.plainLo < 0 { - continue + for bi := firstBlk; bi < lastBlk; bi++ { + blk := &v.blockSpans[bi] + if blk.blockBox >= 0 { + paintBox(v.boxes[blk.blockBox]) + } + if blk.kind == markdown.Table { + tRows := v.tableRows[blk.tableRowLo:blk.tableRowHi] + rLo, rHi := visibleTableRowRange(tRows, clipped, clipTop, clipBottom) + for ri := rLo; ri < rHi; ri++ { + tr := &tRows[ri] + for k := tr.boxLo; k < tr.boxHi; k++ { + paintBox(v.boxes[k]) + } } - lo, hi := max(int(piece.plainLo), start), min(int(piece.plainHi), end) - if lo >= hi || !visible(p.Y+piece.y, piece.h) { - continue + } + } + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for k := row.boxLo; k < row.boxHi; k++ { + paintBox(v.boxes[k]) + } + }) + + // Pass 2: images. + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for k := row.imageLo; k < row.imageHi; k++ { + image := &v.images[k] + rect := gfx.Rect{X: p.X + image.x, Y: p.Y + image.y, Width: image.w, Height: image.h} + if visible(rect.Y, rect.Height) { + canvas.DrawBitmap(image.bitmap, rect) } - if piece.mono { - from, to := lo-int(piece.plainLo), hi-int(piece.plainLo) - if piece.spread { - from, to = v.codeDisplayRange(piece, from, to) + } + }) + + // Pass 3: selection highlights. + start, end := p.Selection.Range(v.plain) + if start < end && look.Selection.Alpha > 0 { + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for index := row.pieceLo; index < row.pieceHi; index++ { + piece := &v.pieces[index] + if piece.plainLo < 0 { + continue } - if to > from { - cell := piece.cell - if cell <= 0 { - cell = 1 + lo, hi := max(int(piece.plainLo), start), min(int(piece.plainHi), end) + if lo >= hi || !visible(p.Y+piece.y, piece.h) { + continue + } + if piece.mono { + from, to := lo-int(piece.plainLo), hi-int(piece.plainLo) + if piece.spread { + from, to = v.codeDisplayRange(piece, from, to) + } + if to > from { + cell := piece.cell + if cell <= 0 { + cell = 1 + } + x := p.X + piece.x + piece.padX + float32(monoCells(piece.text[:from]))*cell + width := float32(monoCells(piece.text[from:to])) * cell + canvas.Fill(gfx.Rect{X: x, Y: p.Y + piece.y, Width: width, Height: piece.h}, look.Selection) } - x := p.X + piece.x + piece.padX + float32(monoCells(piece.text[:from]))*cell - width := float32(monoCells(piece.text[from:to])) * cell - canvas.Fill(gfx.Rect{X: x, Y: p.Y + piece.y, Width: width, Height: piece.h}, look.Selection) + continue + } + v.selScratch = canvas.TextSelectionStyle(v.selScratch[:0], piece.text, gfx.TextStyle{Size: piece.size, Weight: piece.weight, Italic: piece.italic}, + lo-int(piece.plainLo), hi-int(piece.plainLo)) + for _, span := range v.selScratch { + canvas.Fill(gfx.Rect{X: p.X + piece.x + span.X, Y: p.Y + piece.y, Width: span.Width, Height: piece.h}, look.Selection) } - continue - } - v.selScratch = canvas.TextSelectionStyle(v.selScratch[:0], piece.text, gfx.TextStyle{Size: piece.size, Weight: piece.weight, Italic: piece.italic}, - lo-int(piece.plainLo), hi-int(piece.plainLo)) - for _, span := range v.selScratch { - canvas.Fill(gfx.Rect{X: p.X + piece.x + span.X, Y: p.Y + piece.y, Width: span.Width, Height: piece.h}, look.Selection) } - } + }) } - for index := range v.pieces { - piece := &v.pieces[index] + + // Pass 4: text pieces (and list markers). + paintPiece := func(piece *mdPiece) { rect := gfx.Rect{ X: p.X + piece.x + piece.padX, Y: p.Y + piece.y, Width: piece.w - 2*piece.padX, Height: piece.h, } if !visible(rect.Y, rect.Height) { - continue + return } color := look.Text switch piece.color { @@ -1713,16 +1785,32 @@ func (v *MarkdownView) Paint(canvas *gfx.Canvas, p MarkdownPaint) { canvas.Fill(gfx.Rect{X: rect.X, Y: rect.Y + rect.Height/2, Width: rect.Width, Height: 1}, color) } } - for index := range v.links { - link := &v.links[index] - rect := gfx.Rect{X: p.X + link.x, Y: p.Y + link.y, Width: link.w, Height: link.h} - if !visible(rect.Y, rect.Height) || p.Links.IsZero() { - continue + for bi := firstBlk; bi < lastBlk; bi++ { + if mp := v.blockSpans[bi].markerPiece; mp >= 0 { + paintPiece(&v.pieces[mp]) } - id := p.Links.At(p.Base + index) - canvas.HitWithOptions(id, rect, link.text, gfx.HitOptions{Role: gfx.RoleLink}) - if canvas.Hovered() == id { - canvas.Fill(gfx.Rect{X: rect.X, Y: rect.Y + link.underline, Width: rect.Width, Height: 1}, look.Link) + } + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for index := row.pieceLo; index < row.pieceHi; index++ { + paintPiece(&v.pieces[index]) } + }) + + // Pass 5: clickable link hits & hover underlines. + if !p.Links.IsZero() { + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for index := int(row.linkLo); index < int(row.linkHi); index++ { + link := &v.links[index] + rect := gfx.Rect{X: p.X + link.x, Y: p.Y + link.y, Width: link.w, Height: link.h} + if !visible(rect.Y, rect.Height) { + continue + } + id := p.Links.At(p.Base + index) + canvas.HitWithOptions(id, rect, link.text, gfx.HitOptions{Role: gfx.RoleLink}) + if canvas.Hovered() == id { + canvas.Fill(gfx.Rect{X: rect.X, Y: rect.Y + link.underline, Width: rect.Width, Height: 1}, look.Link) + } + } + }) } } diff --git a/cmd/viva/templates/markdown_index.go.txt b/cmd/viva/templates/markdown_index.go.txt new file mode 100644 index 000000000..6b7dc0988 --- /dev/null +++ b/cmd/viva/templates/markdown_index.go.txt @@ -0,0 +1,414 @@ +package {{PACKAGE}} + +import ( + "sort" + "unsafe" + + "github.com/vivaui/viva/gfx" + "github.com/vivaui/viva/markdown" +) + +// Four-level hierarchical spatial index and independent text-offset index for +// MarkdownView. Original pieces, boxes, images, links and plain text stay in +// reading order so selection and copy semantics never change; Paint, OffsetAt +// and CaretAt query these flat reusable indices in O(log N + visible) time +// without steady-state allocations. + +// mdRowSpan indexes one laid-out visual line of a block, table cell, or +// Labeled column. Ranges refer to contiguous slices of MarkdownView's backing +// arrays in exact emission order. +type mdRowSpan struct { + y, bottom float32 + pieceLo, pieceHi int32 + boxLo, boxHi int32 + linkLo, linkHi int32 + imageLo, imageHi int32 +} + +// mdCellSpan indexes the monotonic visual rows of one table cell or one column +// of a Labeled block. +type mdCellSpan struct { + rowLo, rowHi int32 +} + +// mdTableRowSpan indexes one row of a Table block: its cells (at most 24) and +// row-level boxes (header fill and horizontal divider). +type mdTableRowSpan struct { + y, bottom float32 + cellLo, cellHi int32 + boxLo, boxHi int32 +} + +// mdBlockSpan indexes one top-level block of the laid-out document. Blocks are +// strictly disjoint and monotonically ordered by y. +type mdBlockSpan struct { + kind markdown.Kind + y, bottom float32 + rowLo, rowHi int32 + tableRowLo, tableRowHi int32 + cellLo, cellHi int32 + blockBox int32 // mdBoxCode or mdBoxLine, or -1 + markerPiece int32 // Item bullet/number piece, or -1 +} + +// AllocatedBytes reports a conservative estimate of retained heap memory held +// by v, including backing slice capacities so callers can enforce strict +// per-document and global memory budgets. +func (v *MarkdownView) AllocatedBytes() int { + total := cap(v.pieces)*int(unsafe.Sizeof(mdPiece{})) + + cap(v.runs)*int(unsafe.Sizeof(mdCodeRun{})) + + cap(v.hl)*int(unsafe.Sizeof(MarkdownCodeRun{})) + + cap(v.scratch)*int(unsafe.Sizeof(gfx.TextRun{})) + + cap(v.selScratch)*int(unsafe.Sizeof(gfx.TextSpan{})) + + cap(v.boxes)*int(unsafe.Sizeof(mdBox{})) + + cap(v.links)*int(unsafe.Sizeof(mdLink{})) + + cap(v.codes)*int(unsafe.Sizeof(MarkdownCode{})) + + cap(v.images)*int(unsafe.Sizeof(mdImage{})) + + cap(v.anchors)*int(unsafe.Sizeof(MarkdownAnchor{})) + + cap(v.atoms)*int(unsafe.Sizeof(mdAtom{})) + + cap(v.numbers) + + cap(v.tabs) + + cap(v.tabBound)*int(unsafe.Sizeof(int32(0))) + + cap(v.plain) + + cap(v.spanBase)*int(unsafe.Sizeof(int32(0))) + + cap(v.blockSpans)*int(unsafe.Sizeof(mdBlockSpan{})) + + cap(v.rowSpans)*int(unsafe.Sizeof(mdRowSpan{})) + + cap(v.cellSpans)*int(unsafe.Sizeof(mdCellSpan{})) + + cap(v.tableRows)*int(unsafe.Sizeof(mdTableRowSpan{})) + + cap(v.plainPieces)*int(unsafe.Sizeof(int32(0))) + for i := range v.quoteBarIdx { + total += cap(v.quoteBarIdx[i]) * int(unsafe.Sizeof(int32(0))) + } + return total +} + +// BlockAnchorTop returns the laid-out vertical top coordinate of top-level +// block index, or (0, false) if index is out of range. +func (v *MarkdownView) BlockAnchorTop(index int) (float32, bool) { + if index < 0 || index >= len(v.blockSpans) { + return 0, false + } + return v.blockSpans[index].y, true +} + +func (v *MarkdownView) visibleBlockRange(clipped bool, clipTop, clipBottom float32) (int, int) { + n := len(v.blockSpans) + if !clipped || n == 0 { + return 0, n + } + lo := sort.Search(n, func(i int) bool { return v.blockSpans[i].bottom >= clipTop }) + hi := sort.Search(n, func(i int) bool { return v.blockSpans[i].y > clipBottom }) + if hi < lo { + hi = lo + } + return lo, hi +} + +func visibleRowRange(rows []mdRowSpan, clipped bool, clipTop, clipBottom float32) (int, int) { + n := len(rows) + if !clipped || n == 0 { + return 0, n + } + lo := sort.Search(n, func(i int) bool { return rows[i].bottom >= clipTop }) + hi := sort.Search(n, func(i int) bool { return rows[i].y > clipBottom }) + if hi < lo { + hi = lo + } + return lo, hi +} + +func visibleTableRowRange(rows []mdTableRowSpan, clipped bool, clipTop, clipBottom float32) (int, int) { + n := len(rows) + if !clipped || n == 0 { + return 0, n + } + lo := sort.Search(n, func(i int) bool { return rows[i].bottom >= clipTop }) + hi := sort.Search(n, func(i int) bool { return rows[i].y > clipBottom }) + if hi < lo { + hi = lo + } + return lo, hi +} + +func (v *MarkdownView) visibleQuoteBars(level int, clipped bool, clipTop, clipBottom float32) []int32 { + bars := v.quoteBarIdx[level] + n := len(bars) + if !clipped || n == 0 { + return bars + } + lo := sort.Search(n, func(i int) bool { + b := v.boxes[bars[i]] + return b.y+b.h >= clipTop + }) + hi := sort.Search(n, func(i int) bool { + return v.boxes[bars[i]].y > clipBottom + }) + if hi < lo { + hi = lo + } + return bars[lo:hi] +} + +// visitVisibleRows invokes fn for every laid-out visual row intersecting +// [clipTop, clipBottom] inside blocks [firstBlk, lastBlk). For Table blocks it +// descends Table -> TableRow -> Cell -> CellVisualRow so a single very tall +// cell row only visits its visible lines. +func (v *MarkdownView) visitVisibleRows(firstBlk, lastBlk int, clipped bool, clipTop, clipBottom float32, fn func(row *mdRowSpan)) { + for bi := firstBlk; bi < lastBlk; bi++ { + blk := &v.blockSpans[bi] + switch blk.kind { + case markdown.Table: + tRows := v.tableRows[blk.tableRowLo:blk.tableRowHi] + rLo, rHi := visibleTableRowRange(tRows, clipped, clipTop, clipBottom) + for ri := rLo; ri < rHi; ri++ { + tr := &tRows[ri] + for ci := tr.cellLo; ci < tr.cellHi; ci++ { + cell := &v.cellSpans[ci] + cRows := v.rowSpans[cell.rowLo:cell.rowHi] + lo, hi := visibleRowRange(cRows, clipped, clipTop, clipBottom) + for k := lo; k < hi; k++ { + fn(&cRows[k]) + } + } + } + case markdown.Labeled: + for ci := blk.cellLo; ci < blk.cellHi; ci++ { + cell := &v.cellSpans[ci] + cRows := v.rowSpans[cell.rowLo:cell.rowHi] + lo, hi := visibleRowRange(cRows, clipped, clipTop, clipBottom) + for k := lo; k < hi; k++ { + fn(&cRows[k]) + } + } + default: + rows := v.rowSpans[blk.rowLo:blk.rowHi] + lo, hi := visibleRowRange(rows, clipped, clipTop, clipBottom) + for k := lo; k < hi; k++ { + fn(&rows[k]) + } + } + } +} + +// offsetAtIndexed locates the rendered byte offset nearest (x, y) in local +// coordinates using the hierarchical spatial index. +func (v *MarkdownView) offsetAtIndexed(x, y float32) (int, bool) { + if len(v.blockSpans) == 0 { + return 0, false + } + // Find the block containing y, or the closest vertical neighbor(s) when y + // sits in a gap between blocks. + n := len(v.blockSpans) + bi := sort.Search(n, func(i int) bool { return v.blockSpans[i].bottom >= y }) + loBlk, hiBlk := bi, bi+1 + if bi >= n { + loBlk, hiBlk = n-1, n + } else if bi > 0 && y < v.blockSpans[bi].y { + // y is in the vertical gap between block bi-1 and block bi; check both. + loBlk = bi - 1 + } + + best, bestScore := -1, float32(0) + checkRow := func(row *mdRowSpan) (int, bool) { + var previous *mdPiece + for pi := row.pieceLo; pi < row.pieceHi; pi++ { + piece := &v.pieces[pi] + if piece.plainLo < 0 { + continue + } + if previous != nil && previous.y == piece.y && previous.plainHi < piece.plainLo && y >= piece.y && y <= piece.y+piece.h { + start := previous.x + v.caretAdvance(previous, int(previous.plainHi-previous.plainLo)) + end := piece.x + v.caretAdvance(piece, 0) + if end > start && x >= start && x <= end { + gap := int(piece.plainLo - previous.plainHi) + position := int(float32(gap)*(x-start)/(end-start) + 0.5) + return gfx.GraphemeStart(unsafe.String(unsafe.SliceData(v.plain), len(v.plain)), int(previous.plainHi)+min(max(position, 0), gap)), true + } + } + previous = piece + dx, dy := float32(0), float32(0) + if x < piece.x { + dx = piece.x - x + } else if x > piece.x+piece.w { + dx = x - piece.x - piece.w + } + if y < piece.y { + dy = piece.y - y + } else if y > piece.y+piece.h { + dy = y - piece.y - piece.h + } + score := dy*1_000_000 + dx + if best < 0 || score < bestScore { + best, bestScore = int(pi), score + } + } + return 0, false + } + + checkRowsNearY := func(rows []mdRowSpan) (int, bool) { + rn := len(rows) + if rn == 0 { + return 0, false + } + ri := sort.Search(rn, func(i int) bool { return rows[i].bottom >= y }) + rLo, rHi := ri, ri+1 + if ri >= rn { + rLo, rHi = rn-1, rn + } else if ri > 0 && y < rows[ri].y { + rLo = ri - 1 + } + for k := rLo; k < rHi; k++ { + if off, hit := checkRow(&rows[k]); hit { + return off, true + } + } + return 0, false + } + + // Expand outward if the nearest block(s) had no selectable text (e.g. a Rule). + for expand := 0; expand < n; expand++ { + if expand > 0 { + foundAny := false + if loBlk > 0 { + loBlk-- + foundAny = true + } + if hiBlk < n { + hiBlk++ + foundAny = true + } + if !foundAny { + break + } + } + for idx := loBlk; idx < hiBlk; idx++ { + blk := &v.blockSpans[idx] + switch blk.kind { + case markdown.Table: + tRows := v.tableRows[blk.tableRowLo:blk.tableRowHi] + trn := len(tRows) + if trn == 0 { + continue + } + tri := sort.Search(trn, func(i int) bool { return tRows[i].bottom >= y }) + tLo, tHi := tri, tri+1 + if tri >= trn { + tLo, tHi = trn-1, trn + } else if tri > 0 && y < tRows[tri].y { + tLo = tri - 1 + } + for k := tLo; k < tHi; k++ { + tr := &tRows[k] + for ci := tr.cellLo; ci < tr.cellHi; ci++ { + cell := &v.cellSpans[ci] + if off, hit := checkRowsNearY(v.rowSpans[cell.rowLo:cell.rowHi]); hit { + return off, true + } + } + } + case markdown.Labeled: + for ci := blk.cellLo; ci < blk.cellHi; ci++ { + cell := &v.cellSpans[ci] + if off, hit := checkRowsNearY(v.rowSpans[cell.rowLo:cell.rowHi]); hit { + return off, true + } + } + default: + if off, hit := checkRowsNearY(v.rowSpans[blk.rowLo:blk.rowHi]); hit { + return off, true + } + } + } + if best >= 0 { + break + } + } + + if best < 0 { + return 0, false + } + piece := &v.pieces[best] + localX := min(max(x-piece.x, 0), piece.w) + var local int + if piece.mono { + local = v.codeOffsetAt(piece, localX) + } else { + local = v.measure.TextOffsetAtStyle(piece.text, gfx.TextStyle{Size: piece.size, Weight: piece.weight, Italic: piece.italic}, localX) + } + local = min(max(local, 0), int(piece.plainHi-piece.plainLo)) + return int(piece.plainLo) + local, true +} + +// caretAtIndexed resolves a rendered byte offset using the monotonic +// plainPieces index in O(log P) time, preserving exact boundary affinity and +// same-line gap interpolation semantics. +func (v *MarkdownView) caretAtIndexed(offset int) (gfx.Rect, bool) { + n := len(v.plainPieces) + if n == 0 { + return gfx.Rect{}, false + } + value := unsafe.String(unsafe.SliceData(v.plain), len(v.plain)) + offset = gfx.GraphemeStart(value, offset) + + // Find the first text-bearing piece whose plainHi >= offset. + i := sort.Search(n, func(k int) bool { + return int(v.pieces[v.plainPieces[k]].plainHi) >= offset + }) + + var chosen, before, after *mdPiece + for i < n && v.pieces[v.plainPieces[i]].plainLo < 0 { + i++ + } + if i < n { + cand := &v.pieces[v.plainPieces[i]] + if offset >= int(cand.plainLo) && offset <= int(cand.plainHi) { + chosen = cand + // When offset sits on the exact seam between two adjacent pieces + // (prev.plainHi == offset == next.plainLo), prefer the piece that + // starts at offset, matching the linear scan's affinity rule. + if i+1 < n { + next := &v.pieces[v.plainPieces[i+1]] + if next.plainLo >= 0 && int(next.plainLo) == offset && int(chosen.plainLo) < offset { + chosen = next + } + } + } else { + after = cand + for j := i - 1; j >= 0; j-- { + if v.pieces[v.plainPieces[j]].plainLo >= 0 { + before = &v.pieces[v.plainPieces[j]] + break + } + } + } + } else { + for j := n - 1; j >= 0; j-- { + if v.pieces[v.plainPieces[j]].plainLo >= 0 { + before = &v.pieces[v.plainPieces[j]] + break + } + } + } + + if chosen == nil { + if before != nil && after != nil && before.y == after.y { + start := before.x + v.caretAdvance(before, int(before.plainHi-before.plainLo)) + end := after.x + v.caretAdvance(after, 0) + fraction := float32(offset-int(before.plainHi)) / float32(after.plainLo-before.plainHi) + return gfx.Rect{X: v.paintX + start + (end-start)*fraction, Y: v.paintY + before.y, Width: 1, Height: before.h}, true + } + if before == nil && after == nil { + return gfx.Rect{}, false + } + if before == nil || after != nil && int(after.plainLo)-offset <= offset-int(before.plainHi) { + chosen = after + offset = int(after.plainLo) + } else { + chosen = before + offset = int(before.plainHi) + } + } + local := min(max(offset-int(chosen.plainLo), 0), int(chosen.plainHi-chosen.plainLo)) + advance := v.caretAdvance(chosen, local) + return gfx.Rect{X: v.paintX + chosen.x + advance, Y: v.paintY + chosen.y, Width: 1, Height: chosen.h}, true +} diff --git a/components/doc_test.go b/components/doc_test.go index c16552bc9..aaf06f12e 100644 --- a/components/doc_test.go +++ b/components/doc_test.go @@ -36,6 +36,7 @@ var docPrimaryEntries = map[string][]string{ "kbd.go": {"Kbd"}, "markdown.go": {"MarkdownView"}, "markdown_anchor.go": {"MarkdownView"}, + "markdown_index.go": {"MarkdownView"}, "marker.go": {"Marker"}, "menu_typeahead.go": {"MenuTypeahead"}, "message.go": {"Message"}, diff --git a/components/markdown.go b/components/markdown.go index 2f24d1783..1ee0613d0 100644 --- a/components/markdown.go +++ b/components/markdown.go @@ -143,29 +143,35 @@ var _ gfx.TextPlacement = (*MarkdownView)(nil) // Layout measures text and is the expensive half: do it when the text or the // width changed, and call Paint every frame. type MarkdownView struct { - pieces []mdPiece - runs []mdCodeRun - hl []MarkdownCodeRun - scratch []gfx.TextRun - selScratch []gfx.TextSpan - boxes []mdBox - links []mdLink - codes []MarkdownCode - images []mdImage - anchors []MarkdownAnchor - atoms []mdAtom - numbers []byte - tabs []byte // code lines with their tabs spread into blanks - tabBound []int32 // where each byte of a spread code line came from - plain []byte // the rendered text, for selection and copying - spanBase []int32 // where each span of the current inline block starts in plain - measure *gfx.Canvas - paintX float32 // where the last Paint put the document - paintY float32 - painted bool - bars [markdownMaxQuote + 1]int32 - face gfx.MonoFace - height float32 + pieces []mdPiece + runs []mdCodeRun + hl []MarkdownCodeRun + scratch []gfx.TextRun + selScratch []gfx.TextSpan + boxes []mdBox + links []mdLink + codes []MarkdownCode + images []mdImage + anchors []MarkdownAnchor + atoms []mdAtom + numbers []byte + tabs []byte // code lines with their tabs spread into blanks + tabBound []int32 // where each byte of a spread code line came from + plain []byte // the rendered text, for selection and copying + spanBase []int32 // where each span of the current inline block starts in plain + blockSpans []mdBlockSpan + rowSpans []mdRowSpan + cellSpans []mdCellSpan + tableRows []mdTableRowSpan + quoteBarIdx [markdownMaxQuote][]int32 + plainPieces []int32 + measure *gfx.Canvas + paintX float32 // where the last Paint put the document + paintY float32 + painted bool + bars [markdownMaxQuote + 1]int32 + face gfx.MonoFace + height float32 } // Height is the height of the laid-out document. @@ -190,53 +196,7 @@ func (v *MarkdownView) OffsetAt(point gfx.Point) (offset int, ok bool) { if !v.painted || v.measure == nil || len(v.pieces) == 0 { return 0, false } - x, y := point.X-v.paintX, point.Y-v.paintY - best, bestScore := -1, float32(0) - var previous *mdPiece - for index := range v.pieces { - piece := &v.pieces[index] - if piece.plainLo < 0 { - continue - } - if previous != nil && previous.y == piece.y && previous.plainHi < piece.plainLo && y >= piece.y && y <= piece.y+piece.h { - start := previous.x + v.caretAdvance(previous, int(previous.plainHi-previous.plainLo)) - end := piece.x + v.caretAdvance(piece, 0) - if end > start && x >= start && x <= end { - gap := int(piece.plainLo - previous.plainHi) - position := int(float32(gap)*(x-start)/(end-start) + 0.5) - return gfx.GraphemeStart(unsafe.String(unsafe.SliceData(v.plain), len(v.plain)), int(previous.plainHi)+min(max(position, 0), gap)), true - } - } - previous = piece - dx, dy := float32(0), float32(0) - if x < piece.x { - dx = piece.x - x - } else if x > piece.x+piece.w { - dx = x - piece.x - piece.w - } - if y < piece.y { - dy = piece.y - y - } else if y > piece.y+piece.h { - dy = y - piece.y - piece.h - } - score := dy*1_000_000 + dx - if best < 0 || score < bestScore { - best, bestScore = index, score - } - } - if best < 0 { - return 0, false - } - piece := &v.pieces[best] - localX := min(max(x-piece.x, 0), piece.w) - var local int - if piece.mono { - local = v.codeOffsetAt(piece, localX) - } else { - local = v.measure.TextOffsetAtStyle(piece.text, gfx.TextStyle{Size: piece.size, Weight: piece.weight, Italic: piece.italic}, localX) - } - local = min(max(local, 0), int(piece.plainHi-piece.plainLo)) - return int(piece.plainLo) + local, true + return v.offsetAtIndexed(point.X-v.paintX, point.Y-v.paintY) } // CaretAt returns the window rectangle for a rendered byte offset after Paint. @@ -245,50 +205,7 @@ func (v *MarkdownView) CaretAt(offset int) (gfx.Rect, bool) { if !v.painted || v.measure == nil || len(v.pieces) == 0 { return gfx.Rect{}, false } - value := unsafe.String(unsafe.SliceData(v.plain), len(v.plain)) - offset = gfx.GraphemeStart(value, offset) - var chosen, before, after *mdPiece - for index := range v.pieces { - piece := &v.pieces[index] - if piece.plainLo < 0 { - continue - } - switch { - case offset >= int(piece.plainLo) && offset <= int(piece.plainHi): - if chosen == nil || piece.plainLo == int32(offset) && chosen.plainLo < int32(offset) { - chosen = piece - } - case int(piece.plainHi) < offset: - if before == nil || piece.plainHi > before.plainHi { - before = piece - } - case int(piece.plainLo) > offset: - if after == nil || piece.plainLo < after.plainLo { - after = piece - } - } - } - if chosen == nil { - if before != nil && after != nil && before.y == after.y { - start := before.x + v.caretAdvance(before, int(before.plainHi-before.plainLo)) - end := after.x + v.caretAdvance(after, 0) - fraction := float32(offset-int(before.plainHi)) / float32(after.plainLo-before.plainHi) - return gfx.Rect{X: v.paintX + start + (end-start)*fraction, Y: v.paintY + before.y, Width: 1, Height: before.h}, true - } - if before == nil && after == nil { - return gfx.Rect{}, false - } - if before == nil || after != nil && int(after.plainLo)-offset <= offset-int(before.plainHi) { - chosen = after - offset = int(after.plainLo) - } else { - chosen = before - offset = int(before.plainHi) - } - } - local := min(max(offset-int(chosen.plainLo), 0), int(chosen.plainHi-chosen.plainLo)) - advance := v.caretAdvance(chosen, local) - return gfx.Rect{X: v.paintX + chosen.x + advance, Y: v.paintY + chosen.y, Width: 1, Height: chosen.h}, true + return v.caretAtIndexed(offset) } func (v *MarkdownView) caretAdvance(piece *mdPiece, local int) float32 { @@ -410,6 +327,7 @@ const ( markdownCodePadY = 8 markdownCellPadX = 8 markdownCellPadY = 5 + markdownLabelGap = 12 markdownMaxLines = 20000 // the most lines one layout produces ) @@ -520,6 +438,10 @@ func (v *MarkdownView) Layout(canvas *gfx.Canvas, style MarkdownStyle, doc *mark } v.images = v.images[:0] v.pieces, v.boxes, v.links, v.codes, v.anchors, v.numbers, v.runs, v.tabs, v.tabBound, v.plain, v.spanBase = v.pieces[:0], v.boxes[:0], v.links[:0], v.codes[:0], v.anchors[:0], v.numbers[:0], v.runs[:0], v.tabs[:0], v.tabBound[:0], v.plain[:0], v.spanBase[:0] + v.blockSpans, v.rowSpans, v.cellSpans, v.tableRows, v.plainPieces = v.blockSpans[:0], v.rowSpans[:0], v.cellSpans[:0], v.tableRows[:0], v.plainPieces[:0] + for qi := range v.quoteBarIdx { + v.quoteBarIdx[qi] = v.quoteBarIdx[qi][:0] + } v.bars = [markdownMaxQuote + 1]int32{} v.face = style.MonoFace v.measure = canvas @@ -530,6 +452,8 @@ func (v *MarkdownView) Layout(canvas *gfx.Canvas, style MarkdownStyle, doc *mark body := mdFont{size: style.Size, line: style.LineHeight, weight: gfx.WeightRegular, color: mdColText} y := float32(0) var prev *markdown.Block + labeledEnd := -1 + labeledColW := float32(0) for index := range doc.Blocks { if l.lines >= markdownMaxLines { break @@ -544,35 +468,76 @@ func (v *MarkdownView) Layout(canvas *gfx.Canvas, style MarkdownStyle, doc *mark t.color = mdColMuted } l.blockBreak() + blkSpan := mdBlockSpan{kind: b.Kind, y: top, blockBox: -1, markerPiece: -1} switch b.Kind { case markdown.Paragraph: indent := float32(0) if b.Level > 0 { indent = float32(b.Level-1)*markdownListStep + markdownMarkerW } + blkSpan.rowLo = int32(len(v.rowSpans)) l.appendSpans(doc.Spans(b)) y = l.inline(doc.Spans(b), x0+indent, w-indent, y, t, markdown.AlignLeft) + blkSpan.rowHi = int32(len(v.rowSpans)) case markdown.Heading: + blkSpan.rowLo = int32(len(v.rowSpans)) l.appendSpans(doc.Spans(b)) y = l.inline(doc.Spans(b), x0, w, y, l.heading(b.Level, t), markdown.AlignLeft) + blkSpan.rowHi = int32(len(v.rowSpans)) case markdown.Item: indent := float32(b.Level)*markdownListStep + markdownMarkerW + blkSpan.markerPiece = int32(len(v.pieces)) l.marker(b, x0+float32(b.Level)*markdownListStep, y, t) + blkSpan.rowLo = int32(len(v.rowSpans)) l.appendSpans(doc.Spans(b)) y = l.inline(doc.Spans(b), x0+indent, w-indent, y, t, markdown.AlignLeft) + blkSpan.rowHi = int32(len(v.rowSpans)) y = max(y, top+t.line) case markdown.Code: + blkSpan.rowLo = int32(len(v.rowSpans)) y = l.code(b, x0, w, y) + blkSpan.rowHi = int32(len(v.rowSpans)) + if len(v.boxes) > 0 && v.boxes[len(v.boxes)-1].kind == mdBoxCode { + blkSpan.blockBox = int32(len(v.boxes) - 1) + } case markdown.Rule: + blkSpan.blockBox = int32(len(v.boxes)) v.boxes = append(v.boxes, mdBox{kind: mdBoxLine, x: x0, y: y + 6, w: w, h: 1}) y += 13 case markdown.Table: + blkSpan.tableRowLo = int32(len(v.tableRows)) y = l.table(b, x0, w, y, t) + blkSpan.tableRowHi = int32(len(v.tableRows)) + case markdown.Labeled: + if index >= labeledEnd { + labelFont := t + labelFont.color = mdColMuted + maxLabel := float32(0) + end := index + for end < len(doc.Blocks) && doc.Blocks[end].Kind == markdown.Labeled && doc.Blocks[end].Quote == b.Quote { + if ls := doc.LabelSpans(&doc.Blocks[end]); len(ls) > 0 { + maxLabel = max(maxLabel, l.natural(ls, labelFont)) + } + end++ + } + labeledEnd = end + labeledColW = min(maxLabel, max(w*0.28, 48)) + } + blkSpan.cellLo = int32(len(v.cellSpans)) + y = l.labeled(b, x0, w, y, t, labeledColW) + blkSpan.cellHi = int32(len(v.cellSpans)) } + blkSpan.bottom = y + v.blockSpans = append(v.blockSpans, blkSpan) l.quoteBars(b.Quote, top, y) v.anchors = append(v.anchors, MarkdownAnchor{Start: b.Start, End: b.End, Top: top, Bottom: y}) prev = b } + for i := range v.pieces { + if v.pieces[i].plainLo >= 0 { + v.plainPieces = append(v.plainPieces, int32(i)) + } + } v.height = y return y } @@ -628,6 +593,8 @@ func (l *mdLayout) gap(prev, cur *markdown.Block) float32 { switch { case prev.Kind == markdown.Item && (cur.Kind == markdown.Item || cur.Kind == markdown.Paragraph && cur.Level > 0): return float32(int(lh * 0.25)) + case prev.Kind == markdown.Labeled && cur.Kind == markdown.Labeled: + return float32(int(lh * 0.25)) case cur.Kind == markdown.Heading: return float32(int(lh * 0.75)) case prev.Kind == markdown.Heading: @@ -636,6 +603,44 @@ func (l *mdLayout) gap(prev, cur *markdown.Block) float32 { return float32(int(lh * 0.45)) } +// labeled lays out one borderless two-column label–content block. +func (l *mdLayout) labeled(b *markdown.Block, x, width, y float32, t mdFont, labelW float32) float32 { + v, doc := l.v, l.doc + labelSpans := doc.LabelSpans(b) + valueSpans := doc.Spans(b) + labelFont := t + labelFont.color = mdColMuted + top := y + + lRowLo := int32(len(v.rowSpans)) + labelEnd := top + if len(labelSpans) > 0 && labelW > 0 { + l.appendSpans(labelSpans) + labelEnd = l.inline(labelSpans, x, labelW, top, labelFont, markdown.AlignLeft) + } + v.cellSpans = append(v.cellSpans, mdCellSpan{rowLo: lRowLo, rowHi: int32(len(v.rowSpans))}) + + if len(labelSpans) > 0 && len(valueSpans) > 0 { + v.plain = append(v.plain, ' ') + } + + valX := x + valW := width + if labelW > 0 { + valX = x + labelW + markdownLabelGap + valW = max(width-labelW-markdownLabelGap, 40) + } + vRowLo := int32(len(v.rowSpans)) + valEnd := top + if len(valueSpans) > 0 { + l.appendSpans(valueSpans) + valEnd = l.inline(valueSpans, valX, valW, top, t, markdown.AlignLeft) + } + v.cellSpans = append(v.cellSpans, mdCellSpan{rowLo: vRowLo, rowHi: int32(len(v.rowSpans))}) + + return max(max(labelEnd, valEnd), top+t.line) +} + // quoteBars keeps one bar per quote level running through the blocks inside it. func (l *mdLayout) quoteBars(quote int, top, bottom float32) { v := l.v @@ -649,8 +654,10 @@ func (l *mdLayout) quoteBars(quote int, top, bottom float32) { box.h = bottom - box.y continue } + boxIdx := int32(len(v.boxes)) v.boxes = append(v.boxes, mdBox{kind: mdBoxBar, x: float32(level-1)*markdownQuoteStep + 2, y: top, w: 3, h: bottom - top}) - v.bars[level] = int32(len(v.boxes)) + v.quoteBarIdx[level-1] = append(v.quoteBarIdx[level-1], boxIdx) + v.bars[level] = boxIdx + 1 } } @@ -704,6 +711,7 @@ func (l *mdLayout) code(b *markdown.Block, x, width, y float32) float32 { end, done := monoBreak(line[start:], cols) end += start end = codeBreak(line, bound, start, end) + pieceLo := int32(len(v.pieces)) if end > start { chunk := line[start:end] plainLo := int32(len(v.plain)) @@ -723,6 +731,10 @@ func (l *mdLayout) code(b *markdown.Block, x, width, y float32) float32 { v.pieces = append(v.pieces, piece) column += monoCells(chunk) } + v.rowSpans = append(v.rowSpans, mdRowSpan{ + y: y, bottom: y + height, + pieceLo: pieceLo, pieceHi: int32(len(v.pieces)), + }) l.lines++ y += height start = end @@ -986,6 +998,7 @@ func (l *mdLayout) table(b *markdown.Block, x, width, y float32, t mdFont) float } rowTop := y rowBottom := y + 2*markdownCellPadY + t.line + cellLo := int32(len(v.cellSpans)) cx := x for c := 0; c < columns; c++ { cell := cells[r*columns+c] @@ -996,16 +1009,24 @@ func (l *mdLayout) table(b *markdown.Block, x, width, y float32, t mdFont) float if c > 0 { v.plain = append(v.plain, ' ') } + cRowLo := int32(len(v.rowSpans)) l.appendSpans(doc.CellSpans(cell)) end := l.inline(doc.CellSpans(cell), cx+markdownCellPadX, widths[c]-2*markdownCellPadX, rowTop+markdownCellPadY, style, cell.Align) + v.cellSpans = append(v.cellSpans, mdCellSpan{rowLo: cRowLo, rowHi: int32(len(v.rowSpans))}) rowBottom = max(rowBottom, end+markdownCellPadY) cx += widths[c] } y = rowBottom + boxLo := int32(len(v.boxes)) if r == 0 { v.boxes = append(v.boxes, mdBox{kind: mdBoxHead, x: x, y: rowTop, w: tableW, h: rowBottom - rowTop}) } v.boxes = append(v.boxes, mdBox{kind: mdBoxLine, x: x, y: y - 1, w: tableW, h: 1}) + v.tableRows = append(v.tableRows, mdTableRowSpan{ + y: rowTop, bottom: rowBottom, + cellLo: cellLo, cellHi: int32(len(v.cellSpans)), + boxLo: boxLo, boxHi: int32(len(v.boxes)), + }) } return y } @@ -1597,16 +1618,27 @@ func (m *mdLine) endLine() { } // A line with an image is as tall as the tallest image, with the text along its // top and a little air below the pixels so that stacked images do not touch. + lineTop := m.y m.y += max(m.t.line, m.tall+4) + v.rowSpans = append(v.rowSpans, mdRowSpan{ + y: lineTop, bottom: m.y, + pieceLo: int32(m.pieceP), pieceHi: int32(len(v.pieces)), + boxLo: int32(m.pieceB), boxHi: int32(len(v.boxes)), + linkLo: int32(m.pieceL), linkHi: int32(len(v.links)), + imageLo: int32(m.pieceI), imageHi: int32(len(v.images)), + }) m.l.lines++ m.reset() } -// Paint draws the laid-out document. +// Paint draws the laid-out document, querying the spatial index so only +// visible blocks, quote bars, table rows, and cell visual rows are visited. func (v *MarkdownView) Paint(canvas *gfx.Canvas, p MarkdownPaint) { v.paintX, v.paintY, v.painted = p.X, p.Y, true look := p.Look clipped := p.Clip.Height > 0 + clipTop := p.Clip.Y - p.Y + clipBottom := p.Clip.Y + p.Clip.Height - p.Y visible := func(y, h float32) bool { return !clipped || (y+h >= p.Clip.Y && y <= p.Clip.Y+p.Clip.Height) } @@ -1616,10 +1648,12 @@ func (v *MarkdownView) Paint(canvas *gfx.Canvas, p MarkdownPaint) { }) defer canvas.Unclip() } - for _, box := range v.boxes { + firstBlk, lastBlk := v.visibleBlockRange(clipped, clipTop, clipBottom) + + paintBox := func(box mdBox) { rect := gfx.Rect{X: p.X + box.x, Y: p.Y + box.y, Width: box.w, Height: box.h} if !visible(rect.Y, rect.Height) { - continue + return } switch box.kind { case mdBoxCode: @@ -1634,54 +1668,92 @@ func (v *MarkdownView) Paint(canvas *gfx.Canvas, p MarkdownPaint) { canvas.Rounded(rect, 4, look.InlineCodeFill) } } - for _, image := range v.images { - rect := gfx.Rect{X: p.X + image.x, Y: p.Y + image.y, Width: image.w, Height: image.h} - if visible(rect.Y, rect.Height) { - canvas.DrawBitmap(image.bitmap, rect) + + // Pass 1: quote bars, block/table backgrounds, and inline-code pills. + for level := 0; level < markdownMaxQuote; level++ { + for _, bi := range v.visibleQuoteBars(level, clipped, clipTop, clipBottom) { + paintBox(v.boxes[bi]) } } - start, end := p.Selection.Range(v.plain) - if start < end && look.Selection.Alpha > 0 { - for index := range v.pieces { - piece := &v.pieces[index] - if piece.plainLo < 0 { - continue + for bi := firstBlk; bi < lastBlk; bi++ { + blk := &v.blockSpans[bi] + if blk.blockBox >= 0 { + paintBox(v.boxes[blk.blockBox]) + } + if blk.kind == markdown.Table { + tRows := v.tableRows[blk.tableRowLo:blk.tableRowHi] + rLo, rHi := visibleTableRowRange(tRows, clipped, clipTop, clipBottom) + for ri := rLo; ri < rHi; ri++ { + tr := &tRows[ri] + for k := tr.boxLo; k < tr.boxHi; k++ { + paintBox(v.boxes[k]) + } } - lo, hi := max(int(piece.plainLo), start), min(int(piece.plainHi), end) - if lo >= hi || !visible(p.Y+piece.y, piece.h) { - continue + } + } + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for k := row.boxLo; k < row.boxHi; k++ { + paintBox(v.boxes[k]) + } + }) + + // Pass 2: images. + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for k := row.imageLo; k < row.imageHi; k++ { + image := &v.images[k] + rect := gfx.Rect{X: p.X + image.x, Y: p.Y + image.y, Width: image.w, Height: image.h} + if visible(rect.Y, rect.Height) { + canvas.DrawBitmap(image.bitmap, rect) } - if piece.mono { - from, to := lo-int(piece.plainLo), hi-int(piece.plainLo) - if piece.spread { - from, to = v.codeDisplayRange(piece, from, to) + } + }) + + // Pass 3: selection highlights. + start, end := p.Selection.Range(v.plain) + if start < end && look.Selection.Alpha > 0 { + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for index := row.pieceLo; index < row.pieceHi; index++ { + piece := &v.pieces[index] + if piece.plainLo < 0 { + continue } - if to > from { - cell := piece.cell - if cell <= 0 { - cell = 1 + lo, hi := max(int(piece.plainLo), start), min(int(piece.plainHi), end) + if lo >= hi || !visible(p.Y+piece.y, piece.h) { + continue + } + if piece.mono { + from, to := lo-int(piece.plainLo), hi-int(piece.plainLo) + if piece.spread { + from, to = v.codeDisplayRange(piece, from, to) + } + if to > from { + cell := piece.cell + if cell <= 0 { + cell = 1 + } + x := p.X + piece.x + piece.padX + float32(monoCells(piece.text[:from]))*cell + width := float32(monoCells(piece.text[from:to])) * cell + canvas.Fill(gfx.Rect{X: x, Y: p.Y + piece.y, Width: width, Height: piece.h}, look.Selection) } - x := p.X + piece.x + piece.padX + float32(monoCells(piece.text[:from]))*cell - width := float32(monoCells(piece.text[from:to])) * cell - canvas.Fill(gfx.Rect{X: x, Y: p.Y + piece.y, Width: width, Height: piece.h}, look.Selection) + continue + } + v.selScratch = canvas.TextSelectionStyle(v.selScratch[:0], piece.text, gfx.TextStyle{Size: piece.size, Weight: piece.weight, Italic: piece.italic}, + lo-int(piece.plainLo), hi-int(piece.plainLo)) + for _, span := range v.selScratch { + canvas.Fill(gfx.Rect{X: p.X + piece.x + span.X, Y: p.Y + piece.y, Width: span.Width, Height: piece.h}, look.Selection) } - continue - } - v.selScratch = canvas.TextSelectionStyle(v.selScratch[:0], piece.text, gfx.TextStyle{Size: piece.size, Weight: piece.weight, Italic: piece.italic}, - lo-int(piece.plainLo), hi-int(piece.plainLo)) - for _, span := range v.selScratch { - canvas.Fill(gfx.Rect{X: p.X + piece.x + span.X, Y: p.Y + piece.y, Width: span.Width, Height: piece.h}, look.Selection) } - } + }) } - for index := range v.pieces { - piece := &v.pieces[index] + + // Pass 4: text pieces (and list markers). + paintPiece := func(piece *mdPiece) { rect := gfx.Rect{ X: p.X + piece.x + piece.padX, Y: p.Y + piece.y, Width: piece.w - 2*piece.padX, Height: piece.h, } if !visible(rect.Y, rect.Height) { - continue + return } color := look.Text switch piece.color { @@ -1713,16 +1785,32 @@ func (v *MarkdownView) Paint(canvas *gfx.Canvas, p MarkdownPaint) { canvas.Fill(gfx.Rect{X: rect.X, Y: rect.Y + rect.Height/2, Width: rect.Width, Height: 1}, color) } } - for index := range v.links { - link := &v.links[index] - rect := gfx.Rect{X: p.X + link.x, Y: p.Y + link.y, Width: link.w, Height: link.h} - if !visible(rect.Y, rect.Height) || p.Links.IsZero() { - continue + for bi := firstBlk; bi < lastBlk; bi++ { + if mp := v.blockSpans[bi].markerPiece; mp >= 0 { + paintPiece(&v.pieces[mp]) } - id := p.Links.At(p.Base + index) - canvas.HitWithOptions(id, rect, link.text, gfx.HitOptions{Role: gfx.RoleLink}) - if canvas.Hovered() == id { - canvas.Fill(gfx.Rect{X: rect.X, Y: rect.Y + link.underline, Width: rect.Width, Height: 1}, look.Link) + } + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for index := row.pieceLo; index < row.pieceHi; index++ { + paintPiece(&v.pieces[index]) } + }) + + // Pass 5: clickable link hits & hover underlines. + if !p.Links.IsZero() { + v.visitVisibleRows(firstBlk, lastBlk, clipped, clipTop, clipBottom, func(row *mdRowSpan) { + for index := int(row.linkLo); index < int(row.linkHi); index++ { + link := &v.links[index] + rect := gfx.Rect{X: p.X + link.x, Y: p.Y + link.y, Width: link.w, Height: link.h} + if !visible(rect.Y, rect.Height) { + continue + } + id := p.Links.At(p.Base + index) + canvas.HitWithOptions(id, rect, link.text, gfx.HitOptions{Role: gfx.RoleLink}) + if canvas.Hovered() == id { + canvas.Fill(gfx.Rect{X: rect.X, Y: rect.Y + link.underline, Width: rect.Width, Height: 1}, look.Link) + } + } + }) } } diff --git a/components/markdown_index.go b/components/markdown_index.go new file mode 100644 index 000000000..b294e4d5d --- /dev/null +++ b/components/markdown_index.go @@ -0,0 +1,414 @@ +package components + +import ( + "sort" + "unsafe" + + "github.com/vivaui/viva/gfx" + "github.com/vivaui/viva/markdown" +) + +// Four-level hierarchical spatial index and independent text-offset index for +// MarkdownView. Original pieces, boxes, images, links and plain text stay in +// reading order so selection and copy semantics never change; Paint, OffsetAt +// and CaretAt query these flat reusable indices in O(log N + visible) time +// without steady-state allocations. + +// mdRowSpan indexes one laid-out visual line of a block, table cell, or +// Labeled column. Ranges refer to contiguous slices of MarkdownView's backing +// arrays in exact emission order. +type mdRowSpan struct { + y, bottom float32 + pieceLo, pieceHi int32 + boxLo, boxHi int32 + linkLo, linkHi int32 + imageLo, imageHi int32 +} + +// mdCellSpan indexes the monotonic visual rows of one table cell or one column +// of a Labeled block. +type mdCellSpan struct { + rowLo, rowHi int32 +} + +// mdTableRowSpan indexes one row of a Table block: its cells (at most 24) and +// row-level boxes (header fill and horizontal divider). +type mdTableRowSpan struct { + y, bottom float32 + cellLo, cellHi int32 + boxLo, boxHi int32 +} + +// mdBlockSpan indexes one top-level block of the laid-out document. Blocks are +// strictly disjoint and monotonically ordered by y. +type mdBlockSpan struct { + kind markdown.Kind + y, bottom float32 + rowLo, rowHi int32 + tableRowLo, tableRowHi int32 + cellLo, cellHi int32 + blockBox int32 // mdBoxCode or mdBoxLine, or -1 + markerPiece int32 // Item bullet/number piece, or -1 +} + +// AllocatedBytes reports a conservative estimate of retained heap memory held +// by v, including backing slice capacities so callers can enforce strict +// per-document and global memory budgets. +func (v *MarkdownView) AllocatedBytes() int { + total := cap(v.pieces)*int(unsafe.Sizeof(mdPiece{})) + + cap(v.runs)*int(unsafe.Sizeof(mdCodeRun{})) + + cap(v.hl)*int(unsafe.Sizeof(MarkdownCodeRun{})) + + cap(v.scratch)*int(unsafe.Sizeof(gfx.TextRun{})) + + cap(v.selScratch)*int(unsafe.Sizeof(gfx.TextSpan{})) + + cap(v.boxes)*int(unsafe.Sizeof(mdBox{})) + + cap(v.links)*int(unsafe.Sizeof(mdLink{})) + + cap(v.codes)*int(unsafe.Sizeof(MarkdownCode{})) + + cap(v.images)*int(unsafe.Sizeof(mdImage{})) + + cap(v.anchors)*int(unsafe.Sizeof(MarkdownAnchor{})) + + cap(v.atoms)*int(unsafe.Sizeof(mdAtom{})) + + cap(v.numbers) + + cap(v.tabs) + + cap(v.tabBound)*int(unsafe.Sizeof(int32(0))) + + cap(v.plain) + + cap(v.spanBase)*int(unsafe.Sizeof(int32(0))) + + cap(v.blockSpans)*int(unsafe.Sizeof(mdBlockSpan{})) + + cap(v.rowSpans)*int(unsafe.Sizeof(mdRowSpan{})) + + cap(v.cellSpans)*int(unsafe.Sizeof(mdCellSpan{})) + + cap(v.tableRows)*int(unsafe.Sizeof(mdTableRowSpan{})) + + cap(v.plainPieces)*int(unsafe.Sizeof(int32(0))) + for i := range v.quoteBarIdx { + total += cap(v.quoteBarIdx[i]) * int(unsafe.Sizeof(int32(0))) + } + return total +} + +// BlockAnchorTop returns the laid-out vertical top coordinate of top-level +// block index, or (0, false) if index is out of range. +func (v *MarkdownView) BlockAnchorTop(index int) (float32, bool) { + if index < 0 || index >= len(v.blockSpans) { + return 0, false + } + return v.blockSpans[index].y, true +} + +func (v *MarkdownView) visibleBlockRange(clipped bool, clipTop, clipBottom float32) (int, int) { + n := len(v.blockSpans) + if !clipped || n == 0 { + return 0, n + } + lo := sort.Search(n, func(i int) bool { return v.blockSpans[i].bottom >= clipTop }) + hi := sort.Search(n, func(i int) bool { return v.blockSpans[i].y > clipBottom }) + if hi < lo { + hi = lo + } + return lo, hi +} + +func visibleRowRange(rows []mdRowSpan, clipped bool, clipTop, clipBottom float32) (int, int) { + n := len(rows) + if !clipped || n == 0 { + return 0, n + } + lo := sort.Search(n, func(i int) bool { return rows[i].bottom >= clipTop }) + hi := sort.Search(n, func(i int) bool { return rows[i].y > clipBottom }) + if hi < lo { + hi = lo + } + return lo, hi +} + +func visibleTableRowRange(rows []mdTableRowSpan, clipped bool, clipTop, clipBottom float32) (int, int) { + n := len(rows) + if !clipped || n == 0 { + return 0, n + } + lo := sort.Search(n, func(i int) bool { return rows[i].bottom >= clipTop }) + hi := sort.Search(n, func(i int) bool { return rows[i].y > clipBottom }) + if hi < lo { + hi = lo + } + return lo, hi +} + +func (v *MarkdownView) visibleQuoteBars(level int, clipped bool, clipTop, clipBottom float32) []int32 { + bars := v.quoteBarIdx[level] + n := len(bars) + if !clipped || n == 0 { + return bars + } + lo := sort.Search(n, func(i int) bool { + b := v.boxes[bars[i]] + return b.y+b.h >= clipTop + }) + hi := sort.Search(n, func(i int) bool { + return v.boxes[bars[i]].y > clipBottom + }) + if hi < lo { + hi = lo + } + return bars[lo:hi] +} + +// visitVisibleRows invokes fn for every laid-out visual row intersecting +// [clipTop, clipBottom] inside blocks [firstBlk, lastBlk). For Table blocks it +// descends Table -> TableRow -> Cell -> CellVisualRow so a single very tall +// cell row only visits its visible lines. +func (v *MarkdownView) visitVisibleRows(firstBlk, lastBlk int, clipped bool, clipTop, clipBottom float32, fn func(row *mdRowSpan)) { + for bi := firstBlk; bi < lastBlk; bi++ { + blk := &v.blockSpans[bi] + switch blk.kind { + case markdown.Table: + tRows := v.tableRows[blk.tableRowLo:blk.tableRowHi] + rLo, rHi := visibleTableRowRange(tRows, clipped, clipTop, clipBottom) + for ri := rLo; ri < rHi; ri++ { + tr := &tRows[ri] + for ci := tr.cellLo; ci < tr.cellHi; ci++ { + cell := &v.cellSpans[ci] + cRows := v.rowSpans[cell.rowLo:cell.rowHi] + lo, hi := visibleRowRange(cRows, clipped, clipTop, clipBottom) + for k := lo; k < hi; k++ { + fn(&cRows[k]) + } + } + } + case markdown.Labeled: + for ci := blk.cellLo; ci < blk.cellHi; ci++ { + cell := &v.cellSpans[ci] + cRows := v.rowSpans[cell.rowLo:cell.rowHi] + lo, hi := visibleRowRange(cRows, clipped, clipTop, clipBottom) + for k := lo; k < hi; k++ { + fn(&cRows[k]) + } + } + default: + rows := v.rowSpans[blk.rowLo:blk.rowHi] + lo, hi := visibleRowRange(rows, clipped, clipTop, clipBottom) + for k := lo; k < hi; k++ { + fn(&rows[k]) + } + } + } +} + +// offsetAtIndexed locates the rendered byte offset nearest (x, y) in local +// coordinates using the hierarchical spatial index. +func (v *MarkdownView) offsetAtIndexed(x, y float32) (int, bool) { + if len(v.blockSpans) == 0 { + return 0, false + } + // Find the block containing y, or the closest vertical neighbor(s) when y + // sits in a gap between blocks. + n := len(v.blockSpans) + bi := sort.Search(n, func(i int) bool { return v.blockSpans[i].bottom >= y }) + loBlk, hiBlk := bi, bi+1 + if bi >= n { + loBlk, hiBlk = n-1, n + } else if bi > 0 && y < v.blockSpans[bi].y { + // y is in the vertical gap between block bi-1 and block bi; check both. + loBlk = bi - 1 + } + + best, bestScore := -1, float32(0) + checkRow := func(row *mdRowSpan) (int, bool) { + var previous *mdPiece + for pi := row.pieceLo; pi < row.pieceHi; pi++ { + piece := &v.pieces[pi] + if piece.plainLo < 0 { + continue + } + if previous != nil && previous.y == piece.y && previous.plainHi < piece.plainLo && y >= piece.y && y <= piece.y+piece.h { + start := previous.x + v.caretAdvance(previous, int(previous.plainHi-previous.plainLo)) + end := piece.x + v.caretAdvance(piece, 0) + if end > start && x >= start && x <= end { + gap := int(piece.plainLo - previous.plainHi) + position := int(float32(gap)*(x-start)/(end-start) + 0.5) + return gfx.GraphemeStart(unsafe.String(unsafe.SliceData(v.plain), len(v.plain)), int(previous.plainHi)+min(max(position, 0), gap)), true + } + } + previous = piece + dx, dy := float32(0), float32(0) + if x < piece.x { + dx = piece.x - x + } else if x > piece.x+piece.w { + dx = x - piece.x - piece.w + } + if y < piece.y { + dy = piece.y - y + } else if y > piece.y+piece.h { + dy = y - piece.y - piece.h + } + score := dy*1_000_000 + dx + if best < 0 || score < bestScore { + best, bestScore = int(pi), score + } + } + return 0, false + } + + checkRowsNearY := func(rows []mdRowSpan) (int, bool) { + rn := len(rows) + if rn == 0 { + return 0, false + } + ri := sort.Search(rn, func(i int) bool { return rows[i].bottom >= y }) + rLo, rHi := ri, ri+1 + if ri >= rn { + rLo, rHi = rn-1, rn + } else if ri > 0 && y < rows[ri].y { + rLo = ri - 1 + } + for k := rLo; k < rHi; k++ { + if off, hit := checkRow(&rows[k]); hit { + return off, true + } + } + return 0, false + } + + // Expand outward if the nearest block(s) had no selectable text (e.g. a Rule). + for expand := 0; expand < n; expand++ { + if expand > 0 { + foundAny := false + if loBlk > 0 { + loBlk-- + foundAny = true + } + if hiBlk < n { + hiBlk++ + foundAny = true + } + if !foundAny { + break + } + } + for idx := loBlk; idx < hiBlk; idx++ { + blk := &v.blockSpans[idx] + switch blk.kind { + case markdown.Table: + tRows := v.tableRows[blk.tableRowLo:blk.tableRowHi] + trn := len(tRows) + if trn == 0 { + continue + } + tri := sort.Search(trn, func(i int) bool { return tRows[i].bottom >= y }) + tLo, tHi := tri, tri+1 + if tri >= trn { + tLo, tHi = trn-1, trn + } else if tri > 0 && y < tRows[tri].y { + tLo = tri - 1 + } + for k := tLo; k < tHi; k++ { + tr := &tRows[k] + for ci := tr.cellLo; ci < tr.cellHi; ci++ { + cell := &v.cellSpans[ci] + if off, hit := checkRowsNearY(v.rowSpans[cell.rowLo:cell.rowHi]); hit { + return off, true + } + } + } + case markdown.Labeled: + for ci := blk.cellLo; ci < blk.cellHi; ci++ { + cell := &v.cellSpans[ci] + if off, hit := checkRowsNearY(v.rowSpans[cell.rowLo:cell.rowHi]); hit { + return off, true + } + } + default: + if off, hit := checkRowsNearY(v.rowSpans[blk.rowLo:blk.rowHi]); hit { + return off, true + } + } + } + if best >= 0 { + break + } + } + + if best < 0 { + return 0, false + } + piece := &v.pieces[best] + localX := min(max(x-piece.x, 0), piece.w) + var local int + if piece.mono { + local = v.codeOffsetAt(piece, localX) + } else { + local = v.measure.TextOffsetAtStyle(piece.text, gfx.TextStyle{Size: piece.size, Weight: piece.weight, Italic: piece.italic}, localX) + } + local = min(max(local, 0), int(piece.plainHi-piece.plainLo)) + return int(piece.plainLo) + local, true +} + +// caretAtIndexed resolves a rendered byte offset using the monotonic +// plainPieces index in O(log P) time, preserving exact boundary affinity and +// same-line gap interpolation semantics. +func (v *MarkdownView) caretAtIndexed(offset int) (gfx.Rect, bool) { + n := len(v.plainPieces) + if n == 0 { + return gfx.Rect{}, false + } + value := unsafe.String(unsafe.SliceData(v.plain), len(v.plain)) + offset = gfx.GraphemeStart(value, offset) + + // Find the first text-bearing piece whose plainHi >= offset. + i := sort.Search(n, func(k int) bool { + return int(v.pieces[v.plainPieces[k]].plainHi) >= offset + }) + + var chosen, before, after *mdPiece + for i < n && v.pieces[v.plainPieces[i]].plainLo < 0 { + i++ + } + if i < n { + cand := &v.pieces[v.plainPieces[i]] + if offset >= int(cand.plainLo) && offset <= int(cand.plainHi) { + chosen = cand + // When offset sits on the exact seam between two adjacent pieces + // (prev.plainHi == offset == next.plainLo), prefer the piece that + // starts at offset, matching the linear scan's affinity rule. + if i+1 < n { + next := &v.pieces[v.plainPieces[i+1]] + if next.plainLo >= 0 && int(next.plainLo) == offset && int(chosen.plainLo) < offset { + chosen = next + } + } + } else { + after = cand + for j := i - 1; j >= 0; j-- { + if v.pieces[v.plainPieces[j]].plainLo >= 0 { + before = &v.pieces[v.plainPieces[j]] + break + } + } + } + } else { + for j := n - 1; j >= 0; j-- { + if v.pieces[v.plainPieces[j]].plainLo >= 0 { + before = &v.pieces[v.plainPieces[j]] + break + } + } + } + + if chosen == nil { + if before != nil && after != nil && before.y == after.y { + start := before.x + v.caretAdvance(before, int(before.plainHi-before.plainLo)) + end := after.x + v.caretAdvance(after, 0) + fraction := float32(offset-int(before.plainHi)) / float32(after.plainLo-before.plainHi) + return gfx.Rect{X: v.paintX + start + (end-start)*fraction, Y: v.paintY + before.y, Width: 1, Height: before.h}, true + } + if before == nil && after == nil { + return gfx.Rect{}, false + } + if before == nil || after != nil && int(after.plainLo)-offset <= offset-int(before.plainHi) { + chosen = after + offset = int(after.plainLo) + } else { + chosen = before + offset = int(before.plainHi) + } + } + local := min(max(offset-int(chosen.plainLo), 0), int(chosen.plainHi-chosen.plainLo)) + advance := v.caretAdvance(chosen, local) + return gfx.Rect{X: v.paintX + chosen.x + advance, Y: v.paintY + chosen.y, Width: 1, Height: chosen.h}, true +} diff --git a/components/markdown_index_test.go b/components/markdown_index_test.go new file mode 100644 index 000000000..736b24417 --- /dev/null +++ b/components/markdown_index_test.go @@ -0,0 +1,189 @@ +package components + +import ( + "fmt" + "testing" + + "github.com/vivaui/viva/gfx" + "github.com/vivaui/viva/markdown" +) + +func TestMarkdownBuilderLabeledAndOpaqueLinks(t *testing.T) { + var doc markdown.Document + doc.Reset() + doc.AppendParagraph(0, 0, []markdown.Span{ + {Text: "Iterators returned by "}, + {Text: "iterator", Style: markdown.Mono, Link: "symbol:#iterator()"}, + {Text: " are "}, + {Text: "fail-fast", Style: markdown.Italic}, + {Text: "."}, + }) + doc.AppendLabeled(0, []markdown.Span{{Text: "Since:"}}, []markdown.Span{{Text: "1.2"}}) + doc.AppendLabeled(0, []markdown.Span{{Text: "See also:"}}, []markdown.Span{ + {Text: "Collection", Style: markdown.Mono, Link: "symbol:Collection"}, + {Text: ", "}, + {Text: "List", Style: markdown.Mono, Link: "symbol:List"}, + }) + doc.AppendLabeled(0, []markdown.Span{{Text: "Type parameters:"}}, []markdown.Span{ + {Text: "", Style: markdown.Mono}, + {Text: " – the type of elements"}, + }) + + canvas := markdownCanvas() + var view MarkdownView + h := view.Layout(canvas, MarkdownStyle{Size: 13, LineHeight: 20}, &doc, 460) + if h <= 0 { + t.Fatalf("expected positive height, got %v", h) + } + if view.LinkCount() != 3 { + t.Fatalf("LinkCount = %d, want 3", view.LinkCount()) + } + if got := view.Link(0); got != "symbol:#iterator()" { + t.Fatalf("Link(0) = %q, want opaque symbol target", got) + } + if view.AllocatedBytes() <= 0 || doc.AllocatedBytes() <= 0 { + t.Fatalf("expected positive AllocatedBytes: view=%d doc=%d", view.AllocatedBytes(), doc.AllocatedBytes()) + } + // Verify aligned Labeled value columns start at the same X. + var labelEndX, valStartX []float32 + for _, blk := range view.blockSpans { + if blk.kind != markdown.Labeled { + continue + } + lCell := view.cellSpans[blk.cellLo] + vCell := view.cellSpans[blk.cellLo+1] + if lCell.rowLo < lCell.rowHi && vCell.rowLo < vCell.rowHi { + lp := view.pieces[view.rowSpans[lCell.rowLo].pieceLo] + vp := view.pieces[view.rowSpans[vCell.rowLo].pieceLo] + labelEndX = append(labelEndX, lp.x+lp.w) + valStartX = append(valStartX, vp.x) + } + } + if len(valStartX) != 3 { + t.Fatalf("expected 3 labeled rows, got %d", len(valStartX)) + } + for i := 1; i < len(valStartX); i++ { + if valStartX[i] != valStartX[0] { + t.Fatalf("labeled value column misaligned: %v (labels end at %v)", valStartX, labelEndX) + } + } +} + +func TestMarkdownViewTallTableCellFourLevelIndex(t *testing.T) { + // Single-row table where one cell wraps into >1000 visual lines: clipped + // Paint and OffsetAt must visit only visible cell lines, not all 1000. + tallSpans := make([]markdown.Span, 0, 2200) + for i := 0; i < 1100; i++ { + if i > 0 { + tallSpans = append(tallSpans, markdown.Span{Text: markdown.Newline}) + } + tallSpans = append(tallSpans, markdown.Span{Text: fmt.Sprintf("cell-line-%04d", i)}) + } + var doc markdown.Document + doc.AppendTable(0, 2, [][]markdown.TableCell{ + { + {Spans: []markdown.Span{{Text: "Header A"}}}, + {Spans: []markdown.Span{{Text: "Header B"}}}, + }, + { + {Spans: []markdown.Span{{Text: "Short"}}}, + {Spans: tallSpans}, + }, + }) + + canvas := markdownCanvas() + var view MarkdownView + h := view.Layout(canvas, MarkdownStyle{Size: 13, LineHeight: 20}, &doc, 400) + if h < 15000 { + t.Fatalf("expected tall table height >= 15000, got %v", h) + } + + // Count rows visited for a 200px viewport slice in the middle of the tall cell. + clipTop, clipBottom := float32(10000), float32(10200) + firstBlk, lastBlk := view.visibleBlockRange(true, clipTop, clipBottom) + visited := 0 + view.visitVisibleRows(firstBlk, lastBlk, true, clipTop, clipBottom, func(*mdRowSpan) { + visited++ + }) + if visited == 0 || visited > 20 { + t.Fatalf("visited %d rows for 200px slice of 1100-line cell, want 1..20", visited) + } + + // Paint the slice and verify OffsetAt + CaretAt round-trip inside the middle of the cell. + canvas.Reset() + view.Paint(canvas, MarkdownPaint{ + Look: MarkdownLookFor(gfx.Theme{}), + X: 0, + Y: -clipTop, + Clip: gfx.Rect{X: 0, Y: 0, Width: 400, Height: 200}, + }) + off, ok := view.OffsetAt(gfx.Point{X: 220, Y: 100}) + if !ok { + t.Fatal("OffsetAt in tall cell returned ok=false") + } + rect, ok := view.CaretAt(off) + if !ok { + t.Fatalf("CaretAt(%d) returned ok=false", off) + } + if rect.Y < -25 || rect.Y > 225 { + t.Fatalf("CaretAt(%d) Y=%v outside painted slice [0, 200]", off, rect.Y) + } +} + +func TestMarkdownViewLongQuoteBarAndShortBlocksIndex(t *testing.T) { + // Counterexample check: one quote bar spanning many blocks does not force + // visiting off-screen blocks or off-screen quote bars. + var doc markdown.Document + for i := 0; i < 600; i++ { + doc.AppendParagraph(1, 0, []markdown.Span{{Text: fmt.Sprintf("Quoted paragraph %d", i)}}) + } + canvas := markdownCanvas() + var view MarkdownView + h := view.Layout(canvas, MarkdownStyle{Size: 13, LineHeight: 20}, &doc, 420) + if h < 10000 { + t.Fatalf("expected tall quoted doc, got %v", h) + } + + clipTop, clipBottom := float32(5000), float32(5200) + firstBlk, lastBlk := view.visibleBlockRange(true, clipTop, clipBottom) + if count := lastBlk - firstBlk; count == 0 || count > 20 { + t.Fatalf("visible blocks = %d, want 1..20", count) + } + bars := view.visibleQuoteBars(0, true, clipTop, clipBottom) + if len(bars) != 1 { + t.Fatalf("visible quote bars at depth 0 = %d, want 1", len(bars)) + } +} + +func BenchmarkMarkdownViewClippedPaintSteady(b *testing.B) { + var doc markdown.Document + for i := 0; i < 400; i++ { + doc.AppendParagraph(1, 0, []markdown.Span{ + {Text: "Iterators returned by "}, + {Text: "iterator", Style: markdown.Mono, Link: "symbol:#iterator()"}, + {Text: " are "}, + {Text: "fail-fast", Style: markdown.Italic}, + {Text: " in paragraph."}, + }) + } + canvas := markdownCanvas() + var view MarkdownView + view.Layout(canvas, MarkdownStyle{Size: 13, LineHeight: 20}, &doc, 500) + look := MarkdownLookFor(gfx.Theme{}) + paint := MarkdownPaint{ + Look: look, + X: 0, + Y: -4000, + Clip: gfx.Rect{X: 0, Y: 0, Width: 500, Height: 300}, + } + // Warm canvas command buffer and view scratch slices. + canvas.Reset() + view.Paint(canvas, paint) + + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + canvas.Reset() + view.Paint(canvas, paint) + } +} diff --git a/markdown/builder.go b/markdown/builder.go new file mode 100644 index 000000000..da83b49d9 --- /dev/null +++ b/markdown/builder.go @@ -0,0 +1,156 @@ +package markdown + +import "unsafe" + +// TableCell describes one cell when constructing a Table block programmatically +// with Document.AppendTable. +type TableCell struct { + Align Align + Spans []Span +} + +// Reset clears the blocks and spans of d while keeping its backing storage for +// reuse by subsequent Append calls or Parse. +func (d *Document) Reset() { + d.Blocks = d.Blocks[:0] + d.spans = d.spans[:0] + d.cells = d.cells[:0] + d.stack = d.stack[:0] + d.code = d.code[:0] +} + +// AppendParagraph adds a Paragraph block with caller-supplied spans. Unlike +// Parse, spans are preserved as given, including opaque caller link targets. +func (d *Document) AppendParagraph(quote, level int, spans []Span) { + lo := int32(len(d.spans)) + d.spans = append(d.spans, spans...) + d.Blocks = append(d.Blocks, Block{ + Kind: Paragraph, + Quote: clampInt(quote, 0, maxQuote), + Level: clampInt(level, 0, maxLevel), + lo: lo, + hi: int32(len(d.spans)), + }) +} + +// AppendHeading adds a Heading block at heading level 1..6. +func (d *Document) AppendHeading(quote, level int, spans []Span) { + lo := int32(len(d.spans)) + d.spans = append(d.spans, spans...) + d.Blocks = append(d.Blocks, Block{ + Kind: Heading, + Quote: clampInt(quote, 0, maxQuote), + Level: clampInt(level, 1, 6), + lo: lo, + hi: int32(len(d.spans)), + }) +} + +// AppendItem adds a list item block. number == 0 renders a bullet; number > 0 +// renders an ordered marker. +func (d *Document) AppendItem(quote, level, number int, spans []Span) { + lo := int32(len(d.spans)) + d.spans = append(d.spans, spans...) + if number < 0 { + number = 0 + } + d.Blocks = append(d.Blocks, Block{ + Kind: Item, + Quote: clampInt(quote, 0, maxQuote), + Level: clampInt(level, 0, maxLevel), + Number: number, + lo: lo, + hi: int32(len(d.spans)), + }) +} + +// AppendCode adds a preformatted code block. +func (d *Document) AppendCode(quote int, info, text string) { + d.Blocks = append(d.Blocks, Block{ + Kind: Code, + Quote: clampInt(quote, 0, maxQuote), + Info: info, + Text: text, + }) +} + +// AppendRule adds a horizontal divider rule. +func (d *Document) AppendRule(quote int) { + d.Blocks = append(d.Blocks, Block{ + Kind: Rule, + Quote: clampInt(quote, 0, maxQuote), + }) +} + +// AppendLabeled adds a borderless two-column label–content block. +func (d *Document) AppendLabeled(quote int, label, value []Span) { + labelLo := int32(len(d.spans)) + d.spans = append(d.spans, label...) + labelHi := int32(len(d.spans)) + lo := labelHi + d.spans = append(d.spans, value...) + d.Blocks = append(d.Blocks, Block{ + Kind: Labeled, + Quote: clampInt(quote, 0, maxQuote), + labelLo: labelLo, + labelHi: labelHi, + lo: lo, + hi: int32(len(d.spans)), + }) +} + +// AppendTable adds a Table block with the given column count and row-major +// cells. Rows shorter than columns are padded with empty cells; extra columns +// past maxColumns are ignored. +func (d *Document) AppendTable(quote, columns int, rows [][]TableCell) { + columns = clampInt(columns, 0, maxColumns) + if columns == 0 || len(rows) == 0 { + return + } + cellLo := int32(len(d.cells)) + for _, row := range rows { + for c := 0; c < columns; c++ { + var align Align + spanLo := int32(len(d.spans)) + if c < len(row) { + align = row[c].Align + d.spans = append(d.spans, row[c].Spans...) + } + d.cells = append(d.cells, Cell{Align: align, lo: spanLo, hi: int32(len(d.spans))}) + } + } + d.Blocks = append(d.Blocks, Block{ + Kind: Table, + Quote: clampInt(quote, 0, maxQuote), + Columns: columns, + lo: cellLo, + hi: int32(len(d.cells)), + }) +} + +// AllocatedBytes reports a conservative estimate of retained heap memory held +// by d, including backing slice capacities and referenced string payloads. +func (d *Document) AllocatedBytes() int { + total := cap(d.Blocks)*int(unsafe.Sizeof(Block{})) + + cap(d.spans)*int(unsafe.Sizeof(Span{})) + + cap(d.cells)*int(unsafe.Sizeof(Cell{})) + + cap(d.stack)*int(unsafe.Sizeof(int(0))) + + cap(d.code) + for i := range d.Blocks { + total += len(d.Blocks[i].Info) + len(d.Blocks[i].Text) + } + for i := range d.spans { + total += len(d.spans[i].Text) + len(d.spans[i].Link) + len(d.spans[i].Image) + } + return total +} + +func clampInt(v, lo, hi int) int { + if v < lo { + return lo + } + if v > hi { + return hi + } + return v +} diff --git a/markdown/markdown.go b/markdown/markdown.go index c9dfa7ad7..864f3b35f 100644 --- a/markdown/markdown.go +++ b/markdown/markdown.go @@ -35,6 +35,9 @@ const ( Rule // Table is a pipe table; its first row is the header. Table + // Labeled is a borderless two-column label–content block. Consecutive + // Labeled blocks share one aligned label column. + Labeled ) // Style is the inline formatting of a Span. @@ -106,7 +109,8 @@ type Block struct { // fences; for a quoted or indented block, the quote markers and the indent. Start, End int - lo, hi int32 // inline spans, or table cells + lo, hi int32 // inline spans, or table cells + labelLo, labelHi int32 // inline spans of a Labeled block's label column } // Document is a parsed text. The zero value is empty and ready for Parse. @@ -120,8 +124,8 @@ type Document struct { work int // inline scanning left, see inline.go } -// Spans returns the inline content of a Paragraph, Heading or Item. It is valid -// until the next call to Parse. +// Spans returns the inline content of a Paragraph, Heading, Item or Labeled +// block's value column. It is valid until the next call to Parse or Reset. func (d *Document) Spans(b *Block) []Span { if b.Kind == Table || b.Kind == Code || b.Kind == Rule { return nil @@ -129,6 +133,14 @@ func (d *Document) Spans(b *Block) []Span { return d.spans[b.lo:b.hi] } +// LabelSpans returns the inline content of a Labeled block's label column. +func (d *Document) LabelSpans(b *Block) []Span { + if b.Kind != Labeled { + return nil + } + return d.spans[b.labelLo:b.labelHi] +} + // Cells returns the cells of a Table, row by row: Columns cells make a row and // the first row is the header. func (d *Document) Cells(b *Block) []Cell { diff --git a/tools/docs/catalog.go b/tools/docs/catalog.go index 8709dfdfe..41e293e03 100644 --- a/tools/docs/catalog.go +++ b/tools/docs/catalog.go @@ -224,7 +224,7 @@ func loadPages(root string) ([]Page, Manifest, error) { if file.Name() == "doc.go" || file.Name() == "direction.go" || file.Name() == "menu_typeahead.go" || file.Name() == "code_diff_model.go" || file.Name() == "code_diff_inline.go" || file.Name() == "code_editor_layout.go" || file.Name() == "code_editor_decorations.go" || file.Name() == "code_editor_shaped.go" || file.Name() == "caret_blink.go" || file.Name() == "find_bar.go" || file.Name() == "go_to_line.go" || file.Name() == "hierarchy_menu.go" || - file.Name() == "graph_sim.go" || file.Name() == "markdown_anchor.go" || file.Name() == "text_selection.go" { + file.Name() == "graph_sim.go" || file.Name() == "markdown_anchor.go" || file.Name() == "markdown_index.go" || file.Name() == "text_selection.go" { continue } id := strings.ReplaceAll(strings.TrimSuffix(file.Name(), ".go"), "_", "-")