mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 16:55:11 +01:00
Tweak rendering indentation of "x=y+\nz".
This commit is contained in:
1 parent
17607c74a5
commit
5169460c2d
1 file changed
+26
-27
+26
-27
@@ -23,12 +23,12 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxOpens = 0xFFFF
|
const maxIndent = 0xFFFF
|
||||||
opens := make([]uint32, 0, 256)
|
indent := 0
|
||||||
indent := uint32(0)
|
|
||||||
buf := make([]byte, 0, 1024)
|
buf := make([]byte, 0, 1024)
|
||||||
prevLine := src[0].Line - 1
|
prevLine := src[0].Line - 1
|
||||||
prevLineEndedWithOpen := false
|
prevLineEndedWithOpen := false
|
||||||
|
prevLineHanging := false
|
||||||
|
|
||||||
for len(src) > 0 {
|
for len(src) > 0 {
|
||||||
// Find the tokens in this line.
|
// Find the tokens in this line.
|
||||||
@@ -43,7 +43,10 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
|
|||||||
src = src[i:]
|
src = src[i:]
|
||||||
|
|
||||||
// Strip any trailing semi-colons.
|
// Strip any trailing semi-colons.
|
||||||
|
hanging := prevLineHanging
|
||||||
|
prevLineHanging = true
|
||||||
for len(lineTokens) > 0 && lineTokens[len(lineTokens)-1].ID == token.IDSemicolon {
|
for len(lineTokens) > 0 && lineTokens[len(lineTokens)-1].ID == token.IDSemicolon {
|
||||||
|
prevLineHanging = false
|
||||||
lineTokens = lineTokens[:len(lineTokens)-1]
|
lineTokens = lineTokens[:len(lineTokens)-1]
|
||||||
}
|
}
|
||||||
if len(lineTokens) == 0 {
|
if len(lineTokens) == 0 {
|
||||||
@@ -60,19 +63,17 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render any leading indentation. If this line starts with a close
|
// Render any leading indentation. If this line starts with a close
|
||||||
// token, indent it so that it aligns vertically with the line
|
// curly, outdent it so that it hopefully aligns vertically with the
|
||||||
// containing the matching open token.
|
// line containing the matching open curly.
|
||||||
buf = buf[:0]
|
buf = buf[:0]
|
||||||
if lineTokens[0].IsClose() {
|
indentAdjustment := 0
|
||||||
if len(opens) > 0 {
|
if lineTokens[0].ID == token.IDCloseCurly {
|
||||||
indent = opens[len(opens)-1]
|
indentAdjustment--
|
||||||
} else {
|
} else if hanging {
|
||||||
indent = 0
|
indentAdjustment++
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if indent != 0 {
|
if n := indent + indentAdjustment; n > 0 {
|
||||||
const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
|
const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
|
||||||
n := int(indent)
|
|
||||||
for ; n > len(tabs); n -= len(tabs) {
|
for ; n > len(tabs); n -= len(tabs) {
|
||||||
buf = append(buf, tabs...)
|
buf = append(buf, tabs...)
|
||||||
}
|
}
|
||||||
@@ -80,7 +81,6 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render the lineTokens.
|
// Render the lineTokens.
|
||||||
numOpens := len(opens)
|
|
||||||
prevID, prevIsTightRight := token.ID(0), false
|
prevID, prevIsTightRight := token.ID(0), false
|
||||||
for _, t := range lineTokens {
|
for _, t := range lineTokens {
|
||||||
const (
|
const (
|
||||||
@@ -98,16 +98,16 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
|
|||||||
|
|
||||||
buf = append(buf, m.ByKey(t.Key())...)
|
buf = append(buf, m.ByKey(t.Key())...)
|
||||||
|
|
||||||
if t.IsOpen() {
|
if t.ID == token.IDOpenCurly {
|
||||||
if len(opens) == maxOpens {
|
if indent == maxIndent {
|
||||||
return errors.New("render: too many open tokens")
|
return errors.New("render: too many \"{\" tokens")
|
||||||
}
|
}
|
||||||
opens = append(opens, indent)
|
indent++
|
||||||
} else if t.IsClose() {
|
} else if t.ID == token.IDCloseCurly {
|
||||||
if len(opens) == 0 {
|
if indent == 0 {
|
||||||
return errors.New("render: too many close tokens")
|
return errors.New("render: too many \"}\" tokens")
|
||||||
}
|
}
|
||||||
opens = opens[:len(opens)-1]
|
indent--
|
||||||
}
|
}
|
||||||
|
|
||||||
prevIsTightRight = t.ID.IsTightRight()
|
prevIsTightRight = t.ID.IsTightRight()
|
||||||
@@ -116,21 +116,20 @@ func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
|
|||||||
if prevID != 0 && t.ID.Flags()&flagsUB == flagsUB {
|
if prevID != 0 && t.ID.Flags()&flagsUB == flagsUB {
|
||||||
// Token-based (not ast.Node-based) heuristic for whether the
|
// Token-based (not ast.Node-based) heuristic for whether the
|
||||||
// operator looks unary instead of binary.
|
// operator looks unary instead of binary.
|
||||||
prevIsTightRight = prevID.Flags() & flagsLIC == 0
|
prevIsTightRight = prevID.Flags()&flagsLIC == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
prevID = t.ID
|
prevID = t.ID
|
||||||
}
|
}
|
||||||
if numOpens != len(opens) {
|
|
||||||
indent++
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
if _, err = w.Write(buf); err != nil {
|
if _, err = w.Write(buf); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
prevLine = line
|
prevLine = line
|
||||||
prevLineEndedWithOpen = lineTokens[len(lineTokens)-1].IsOpen()
|
lastID := lineTokens[len(lineTokens)-1].ID
|
||||||
|
prevLineEndedWithOpen = lastID.IsOpen()
|
||||||
|
prevLineHanging = prevLineHanging && lastID != token.IDOpenCurly
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in new issue
Block a user