Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/formatter_chunks.h (C/C++)

Code before:
  // Adds a parent `Chunk` and returns its `ChunkId`. If `child_chunk_id` isn't
  // `None`, it's added as a child. Must be called before `StartContent`.
  auto AddParent(ChunkId child_chunk_id = None) -> ChunkId;

  // Switches from adding parents to adding content.
  auto StartContent() -> void;

  // Adds a new content `Chunk`. If `parent_chunk_id` is included in
  // output, it'll also include the new chunk. Calls `format` to support adding
  // content to the new chunk. Must be called after `StartContent`.
  auto FormatChildContent(ChunkId parent_chunk_id,
                          llvm::function_ref<auto()->void> format) -> void;

  // Adds `child_chunk_id` to the children of a `FormatChildContent`'s
  // `parent_chunk_id` if called during `format`, or otherwise includes the
  // chunk in output.
  auto AppendChildToCurrentParent(ChunkId child_chunk_id) -> void;

  // Writes included chunks to the given stream.
  auto Write(llvm::raw_ostream& stream) -> void;

  // Returns a stream to write to a content `Chunk`. The returned reference is only valid
  // until the next `Chunk` starts. Must be called after `StartContent`.
  //
  // This adds a new, parentless content `Chunk` if there's not a ready content
  // `Chunk` to write to.
  auto out() -> llvm::raw_ostream& {
    CARBON_CHECK(content_start_id_ != None);
    if (!out_) {
      AddContent(/*include_in_output=*/true);
    }
    return *out_;
  }

  auto size() -> size_t { return chunks_.size(); }

 private:
  // Either a parent or content.
  struct Chunk {
    // Whether this chunk is known to be included in the output.
    bool include_in_output;

    // Either children or content.
    std::variant<llvm::SmallVector<ChunkId>, std::string> data;
  };

  // Adds a `Chunk` that will have `content`, and directs `out_` to it.
  auto AddContent(bool include_in_output) -> ChunkId;

  // Adds `child_chunk_id` to the children of `parent_chunk_id`.
  auto AppendChildToParent(ChunkId child_chunk_id, ChunkId parent_chunk_id)

Review comment (suggestion):
```suggestion
  // If there is a `FormatChildContent` call currently active, this writes to the chunk
  // created by that call. Otherwise it writes to a parentless content `Chunk`, which
  // will be included in the output.
```

Code after:
  // `AppendChildToCurrentParent` must be called to include it.
  auto AddParent(ChunkId child_chunk_id = None) -> ChunkId;

  // Switches from adding parents to adding content.
  auto StartContent() -> void;

  // Calls `format` to add content conditionally included when `parent_chunk_id`
  // is included. Must be called after `StartContent`.
  //
  // During a `FormatChildContent` call where the `parent_chunk_id` is not
  // already included in output, a new content `Chunk` is created, marked as a
  // child of `parent_chunk_id`, and `out` is temporarily directed to it during
  // the duration of `format. Otherwise, `out` lazily creates a content `Chunk`
  // which is always included in output.
  auto FormatChildContent(ChunkId parent_chunk_id,
                          llvm::function_ref<auto()->void> format) -> void;

  // Adds `child_chunk_id` to the children of a `FormatChildContent`'s
  // `parent_chunk_id` if called during `format`, or otherwise includes the
  // chunk in output.
  auto AppendChildToCurrentParent(ChunkId child_chunk_id) -> void;

  // Writes included chunks to the given stream.
  auto Write(llvm::raw_ostream& stream) -> void;

  // Returns a stream to write to a content `Chunk`. The returned reference is
  // only valid until the next `Chunk` starts. Must be called after
  // `StartContent`.
  //
  // See `FormatChildContent` for details of the target content `Chunk`.
  auto out() -> llvm::raw_ostream& {
    CARBON_CHECK(content_start_id_ != None);
    if (!out_) {
      AddContent(/*include_in_output=*/true);
    }
    return *out_;
  }

  auto size() -> size_t { return chunks_.size(); }

 private:
  // Either a parent or content.
  struct Chunk {
    // Whether this chunk is known to be included in the output.
    bool include_in_output;

    // Either children or content.
    std::variant<llvm::SmallVector<ChunkId>, std::string> data;
  };

  // Adds a `Chunk` that will have `content`, and directs `out_` to it.