1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-07-13 01:10:14 +02:00

Document and add iterator panic recovery.

This commit is contained in:
Alec Thomas
2017-09-20 22:30:25 +10:00
parent cc0e4a59ab
commit e2d6abaa64
10 changed files with 75 additions and 27 deletions

View File

@ -67,8 +67,13 @@ func (h highlightRanges) Len() int { return len(h) }
func (h highlightRanges) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h highlightRanges) Less(i, j int) bool { return h[i][0] < h[j][0] }
func (f *Formatter) Format(w io.Writer, style *chroma.Style, iterator chroma.Iterator) error {
return f.writeHTML(w, style, chroma.Flatten(iterator))
func (f *Formatter) Format(w io.Writer, style *chroma.Style, iterator chroma.Iterator) (err error) {
defer func() {
if perr := recover(); perr != nil {
err = perr.(error)
}
}()
return f.writeHTML(w, style, iterator.Tokens())
}
func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma.Token) error { // nolint: gocyclo