Skip to content

Commit 74c2d82

Browse files
committed
proper newlines for comments
The logic is still messy, but it seems to work.
1 parent 3072608 commit 74c2d82

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/lib_json/json_writer.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,10 @@ void StyledStreamWriter::write(std::ostream& out, const Value& root) {
466466
document_ = &out;
467467
addChildValues_ = false;
468468
indentString_ = "";
469-
indented_ = false;
469+
indented_ = true;
470470
writeCommentBeforeValue(root);
471+
if (!indented_) writeIndent();
472+
indented_ = true;
471473
writeValue(root);
472474
writeCommentAfterValueOnSameLine(root);
473475
*document_ << "\n";
@@ -631,32 +633,29 @@ void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
631633
if (!root.hasComment(commentBefore))
632634
return;
633635

634-
*document_ << "\n";
635-
writeIndent();
636+
if (!indented_) writeIndent();
636637
const std::string& comment = root.getComment(commentBefore);
637638
std::string::const_iterator iter = comment.begin();
638639
while (iter != comment.end()) {
639640
*document_ << *iter;
640641
if (*iter == '\n' &&
641642
(iter != comment.end() && *(iter + 1) == '/'))
642-
writeIndent();
643+
// writeIndent(); // would include newline
644+
*document_ << indentString_;
643645
++iter;
644646
}
645-
646-
// Comments are stripped of trailing newlines, so add one here
647-
*document_ << "\n";
648647
indented_ = false;
649648
}
650649

651650
void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
652651
if (root.hasComment(commentAfterOnSameLine))
653-
*document_ << " " + root.getComment(commentAfterOnSameLine);
652+
*document_ << root.getComment(commentAfterOnSameLine);
654653

655654
if (root.hasComment(commentAfter)) {
656-
*document_ << "\n";
655+
writeIndent();
657656
*document_ << root.getComment(commentAfter);
658-
*document_ << "\n";
659657
}
658+
indented_ = false;
660659
}
661660

662661
bool StyledStreamWriter::hasCommentForValue(const Value& value) {

0 commit comments

Comments
 (0)