Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/formatter.cpp (C++)

Code before:
    // Don't format the scope if no instructions are in a dump range.
    block = block.drop_while(
        [&](InstId inst_id) { return !ShouldFormatInst(inst_id); });
  }

  if (block.empty()) {
    return;
  }

  llvm::SaveAndRestore scope(scope_, scope_id);
  // Note, we don't use OpenBrace() / CloseBrace() here because we always want
  // a newline to avoid misformatting if the first instruction is omitted.
  out_ << "\n" << inst_namer_.GetScopeName(scope_id) << " {\n";
  indent_ += 2;
  for (const InstId inst_id : block) {
    // Unlike code blocks, we don't note when a scope elide entries, because
    // they're less likely to be related to the elided code.
    if (ShouldFormatInst(inst_id)) {
      if (is_tentative) {
        TentativeOutputScope scope(*this,
                                   tentative_inst_chunks_[inst_id.index]);
        FormatInst(inst_id);
      } else {
        FormatInst(inst_id);
      }
    }
  }
  out_ << "}\n";
  indent_ -= 2;
}

auto Formatter::FormatClass(ClassId id) -> void {
  const Class& class_info = sem_ir_->classes().Get(id);
  if (!ShouldFormatEntity(class_info)) {
    return;
  }

  FormatEntityStart("class", class_info, id);

  llvm::SaveAndRestore class_scope(scope_, inst_namer_.GetScopeFor(id));

  if (class_info.scope_id.has_value()) {
    out_ << ' ';
    OpenBrace();
    FormatCodeBlock(class_info.body_block_id);
    Indent();
    out_ << "complete_type_witness = ";
    FormatName(class_info.complete_type_witness_id);
    out_ << "\n";

    FormatNameScope(class_info.scope_id, "!members:\n");

Review comment (suggestion):
I had trouble understanding this comment because I assumed it was explaining the next line or the next block, but I couldn't figure out how they related. Since it's explaining the absence of some code, how about putting it where that code would go?

```suggestion
    if (ShouldFormatInst(inst_id)) {
      if (is_tentative) {
        TentativeOutputScope scope(*this,
                                   tentative_inst_chunks_[inst_id.index]);
        FormatInst(inst_id);
      } else {
        FormatInst(inst_id);
      }
    }
    // Unlike code blocks, we don't note when a scope elide entries, because
    // they're less likely to be related to the elided code.
```

You could even put it inside an empty `else` block, but that may be too cute.

Code after:
  if (!use_tentative_output_scopes && use_dump_sem_ir_ranges_) {
    // Don't format the scope if no instructions are in a dump range.
    block = block.drop_while(
        [&](InstId inst_id) { return !ShouldFormatInst(inst_id); });
  }

  if (block.empty()) {
    return;
  }

  llvm::SaveAndRestore scope(scope_, scope_id);
  // Note, we don't use OpenBrace() / CloseBrace() here because we always want
  // a newline to avoid misformatting if the first instruction is omitted.
  out_ << "\n" << inst_namer_.GetScopeName(scope_id) << " {\n";
  indent_ += 2;
  for (const InstId inst_id : block) {
    // Format instructions when needed, but do nothing for elided entries;
    // unlike normal code blocks, scopes are non-sequential so skipped
    // instructions are assumed to be uninteresting.
    if (use_tentative_output_scopes) {
      // This is for constants and imports. These use tentative logic to
      // determine whether an instruction is printed.
      TentativeOutputScope scope(*this, tentative_inst_chunks_[inst_id.index]);
      FormatInst(inst_id);
    } else if (ShouldFormatInst(inst_id)) {
      // This is for the file scope. It uses only the range-based filtering.
      FormatInst(inst_id);
    }
  }
  out_ << "}\n";
  indent_ -= 2;
}

auto Formatter::FormatClass(ClassId id) -> void {
  const Class& class_info = sem_ir_->classes().Get(id);
  if (!ShouldFormatEntity(class_info)) {
    return;
  }

  FormatEntityStart("class", class_info, id);

  llvm::SaveAndRestore class_scope(scope_, inst_namer_.GetScopeFor(id));

  if (class_info.scope_id.has_value()) {
    out_ << ' ';
    OpenBrace();
    FormatCodeBlock(class_info.body_block_id);
    Indent();
    out_ << "complete_type_witness = ";
    FormatName(class_info.complete_type_witness_id);
    out_ << "\n";