Wip split printer

This commit is contained in:
Mike Farah 2021-10-29 14:14:39 +11:00
parent 65fd001575
commit 0289128d2a

View File

@ -105,45 +105,7 @@ func (p *resultsPrinter) safelyFlush(writer *bufio.Writer) {
} }
} }
func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error { func (p *resultsPrinter) processLeadingContent(mappedDoc *CandidateNode, writer io.Writer) error {
log.Debug("PrintResults for %v matches", matchingNodes.Len())
if p.outputFormat != YamlOutputFormat {
explodeOp := Operation{OperationType: explodeOpType}
explodeNode := ExpressionNode{Operation: &explodeOp}
context, err := p.treeNavigator.GetMatchingNodes(Context{MatchingNodes: matchingNodes}, &explodeNode)
if err != nil {
return err
}
matchingNodes = context.MatchingNodes
}
bufferedWriter := bufio.NewWriter(p.writer)
defer p.safelyFlush(bufferedWriter)
if matchingNodes.Len() == 0 {
log.Debug("no matching results, nothing to print")
return nil
}
if p.firstTimePrinting {
node := matchingNodes.Front().Value.(*CandidateNode)
p.previousDocIndex = node.Document
p.previousFileIndex = node.FileIndex
p.firstTimePrinting = false
}
for el := matchingNodes.Front(); el != nil; el = el.Next() {
mappedDoc := el.Value.(*CandidateNode)
log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators)
commentStartsWithSeparator := strings.Contains(mappedDoc.Node.HeadComment, "$yqLeadingContent$\n$yqDocSeperator$")
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators && !commentStartsWithSeparator {
log.Debug("-- writing doc sep")
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
return err
}
}
if strings.Contains(mappedDoc.Node.HeadComment, "$yqLeadingContent$") { if strings.Contains(mappedDoc.Node.HeadComment, "$yqLeadingContent$") {
log.Debug("headcommentwas %v", mappedDoc.Node.HeadComment) log.Debug("headcommentwas %v", mappedDoc.Node.HeadComment)
log.Debug("finished headcomment") log.Debug("finished headcomment")
@ -161,12 +123,12 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
} else if strings.Contains(readline, "$yqDocSeperator$") { } else if strings.Contains(readline, "$yqDocSeperator$") {
if p.printDocSeparators { if p.printDocSeparators {
if err := p.writeString(bufferedWriter, "---\n"); err != nil { if err := p.writeString(writer, "---\n"); err != nil {
return err return err
} }
} }
} else if p.outputFormat == YamlOutputFormat { } else if p.outputFormat == YamlOutputFormat {
if err := p.writeString(bufferedWriter, readline); err != nil { if err := p.writeString(writer, readline); err != nil {
return err return err
} }
} }
@ -174,7 +136,7 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
if errReading == io.EOF { if errReading == io.EOF {
if readline != "" { if readline != "" {
// the last comment we read didn't have a new line, put one in // the last comment we read didn't have a new line, put one in
if err := p.writeString(bufferedWriter, "\n"); err != nil { if err := p.writeString(writer, "\n"); err != nil {
return err return err
} }
} }
@ -183,6 +145,51 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
} }
} }
return nil
}
func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
log.Debug("PrintResults for %v matches", matchingNodes.Len())
if p.outputFormat != YamlOutputFormat {
explodeOp := Operation{OperationType: explodeOpType}
explodeNode := ExpressionNode{Operation: &explodeOp}
context, err := p.treeNavigator.GetMatchingNodes(Context{MatchingNodes: matchingNodes}, &explodeNode)
if err != nil {
return err
}
matchingNodes = context.MatchingNodes
}
if matchingNodes.Len() == 0 {
log.Debug("no matching results, nothing to print")
return nil
}
if p.firstTimePrinting {
node := matchingNodes.Front().Value.(*CandidateNode)
p.previousDocIndex = node.Document
p.previousFileIndex = node.FileIndex
p.firstTimePrinting = false
}
bufferedWriter := bufio.NewWriter(p.writer)
defer p.safelyFlush(bufferedWriter)
for el := matchingNodes.Front(); el != nil; el = el.Next() {
mappedDoc := el.Value.(*CandidateNode)
log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators)
commentStartsWithSeparator := strings.Contains(mappedDoc.Node.HeadComment, "$yqLeadingContent$\n$yqDocSeperator$")
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators && !commentStartsWithSeparator {
log.Debug("-- writing doc sep")
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
return err
}
}
if err := p.processLeadingContent(mappedDoc, bufferedWriter); err != nil {
return err
}
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil { if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {
return err return err