Tokenize and render comments.

This commit is contained in:
Nigel Tao committed 2017-05-05 12:33:38 +10:00
1 parent d192220c3c
commit 94cb8b25cb
2 files changed
+93 -20

No files matched your search

+75 -8
View File
@@ -18,7 +18,7 @@ import (
var newLine = []byte{'\n'}
func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
func RenderFile(w io.Writer, src []token.Token, comments []string, m *token.IDMap) (err error) {
if len(src) == 0 {
return nil
}
@@ -26,6 +26,7 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
const maxIndent = 0xFFFF
indent := 0
buf := make([]byte, 0, 1024)
commentLine := uint32(0)
prevLine := src[0].Line - 1
prevLineHanging := false
@@ -41,6 +42,25 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
lineTokens := src[:i]
src = src[i:]
// Print any previous comments.
for ; commentLine < line; commentLine++ {
buf = buf[:0]
buf = appendComment(buf, comments, commentLine, indent, true)
if len(buf) == 0 {
continue
}
if commentLine > prevLine+1 {
if _, err = w.Write(newLine); err != nil {
return err
}
}
buf = append(buf, '\n')
if _, err = w.Write(buf); err != nil {
return err
}
prevLine = commentLine
}
// Strip any trailing semi-colons.
hanging := prevLineHanging
prevLineHanging = true
@@ -69,13 +89,7 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
} else if hanging {
indentAdjustment++
}
if n := indent + indentAdjustment; n > 0 {
const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
for ; n > len(tabs); n -= len(tabs) {
buf = append(buf, tabs...)
}
buf = append(buf, tabs[:n]...)
}
buf = appendTabs(buf, indent+indentAdjustment)
// Render the lineTokens.
prevID, prevIsTightRight := token.ID(0), false
@@ -119,13 +133,66 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
prevID = t.ID
}
buf = appendComment(buf, comments, line, 0, false)
buf = append(buf, '\n')
if _, err = w.Write(buf); err != nil {
return err
}
commentLine = line + 1
prevLine = line
prevLineHanging = prevLineHanging && lineTokens[len(lineTokens)-1].ID != token.IDOpenCurly
}
// Print any trailing comments.
for ; uint(commentLine) < uint(len(comments)); commentLine++ {
buf = buf[:0]
buf = appendComment(buf, comments, commentLine, indent, true)
if len(buf) > 0 {
if commentLine > prevLine+1 {
if _, err = w.Write(newLine); err != nil {
return err
}
}
buf = append(buf, '\n')
if _, err = w.Write(buf); err != nil {
return err
}
prevLine = commentLine
}
}
return nil
}
func appendComment(buf []byte, comments []string, line uint32, indent int, otherwiseEmpty bool) []byte {
if uint(line) < uint(len(comments)) {
if com := comments[line]; com != "" {
// Strip trailing spaces.
for len(com) > 0 {
if com[len(com)-1] != ' ' {
break
}
com = com[:len(com)-1]
}
if otherwiseEmpty {
buf = appendTabs(buf, indent)
} else {
buf = append(buf, " "...)
}
buf = append(buf, com...)
}
}
return buf
}
func appendTabs(buf []byte, n int) []byte {
if n > 0 {
const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
for ; n > len(tabs); n -= len(tabs) {
buf = append(buf, tabs...)
}
buf = append(buf, tabs[:n]...)
}
return buf
}