Repository: carbon-language/carbon-lang
File: testing/file_test/file_test_base.cpp (C++)

Code before:
  llvm::StringRef content_at_keyword =
      llvm::StringRef(*content).substr(keyword_pos);

  auto [keyword, body_start] = content_at_keyword.split(":");
  if (body_start.empty()) {
    return ErrorBuilder() << "Missing `:` for `"
                          << content_at_keyword.take_front(10) << "`";
  }

  // Whether the first param is a method or id.
  llvm::StringRef method_or_id_label = "method";
  // Whether to attach the `lsp_call_id`.
  bool use_call_id = false;
  // The JSON label for extra content.
  llvm::StringRef extra_content_label;
  if (keyword == "[[@LSP-CALL") {
    use_call_id = true;
    extra_content_label = "params";
  } else if (keyword == "[[@LSP-NOTIFY") {
    extra_content_label = "params";
  } else if (keyword == "[[@LSP-REPLY") {
    method_or_id_label = "id";
    extra_content_label = "result";
  } else if (keyword != "[[@LSP") {
    return ErrorBuilder() << "Unrecognized @LSP keyword at `"
                          << content_at_keyword.take_front(10) << "`";
  }

  static constexpr llvm::StringLiteral LspEnd = "]]";
  auto body_end = body_start.find(LspEnd);
  if (body_end == std::string::npos) {
    return ErrorBuilder() << "Missing `" << LspEnd << "` after `" << keyword
                          << "`";
  }
  llvm::StringRef body = body_start.take_front(body_end);
  auto [method_or_id, extra_content] = body.split(":");

  // Form the JSON.
  std::string json = llvm::formatv(R"({{"jsonrpc": "2.0", "{0}": "{1}")",
                                   method_or_id_label, method_or_id);
  if (use_call_id) {
    json += llvm::formatv(R"(, "id": {0})", ++lsp_call_id);
  }
  if (!extra_content.empty()) {
    json += ",";
    if (extra_content_label.empty()) {
      if (!extra_content.starts_with("\n")) {
        json += " ";
      }
      json += extra_content;
    } else {

Review comment (refactor):
Seems like this should stop at the colon
```suggestion
    return ErrorBuilder() << "Unrecognized @LSP keyword `"
                          << keyword.drop_front(2) << "`";
```
or if you are really worried about it being too long, use a longer limit since "@LSP-NOTIFY" is more than 10 chars.
```suggestion
    return ErrorBuilder() << "Unrecognized @LSP keyword `"
                          << keyword.drop_front(2).take_front(16) << "`";
```


Code after:
  llvm::StringRef content_at_keyword =
      llvm::StringRef(*content).substr(keyword_pos);

  auto [keyword, body_start] = content_at_keyword.split(":");
  if (body_start.empty()) {
    return ErrorBuilder() << "Missing `:` for `"
                          << content_at_keyword.take_front(10) << "`";
  }

  // Whether the first param is a method or id.
  llvm::StringRef method_or_id_label = "method";
  // Whether to attach the `lsp_call_id`.
  bool use_call_id = false;
  // The JSON label for extra content.
  llvm::StringRef extra_content_label;
  if (keyword == "[[@LSP-CALL") {
    use_call_id = true;
    extra_content_label = "params";
  } else if (keyword == "[[@LSP-NOTIFY") {
    extra_content_label = "params";
  } else if (keyword == "[[@LSP-REPLY") {
    method_or_id_label = "id";
    extra_content_label = "result";
  } else if (keyword != "[[@LSP") {
    return ErrorBuilder() << "Unrecognized @LSP keyword at `"
                          << keyword.take_front(10) << "`";
  }

  static constexpr llvm::StringLiteral LspEnd = "]]";
  auto body_end = body_start.find(LspEnd);
  if (body_end == std::string::npos) {
    return ErrorBuilder() << "Missing `" << LspEnd << "` after `" << keyword
                          << "`";
  }
  llvm::StringRef body = body_start.take_front(body_end);
  auto [method_or_id, extra_content] = body.split(":");

  // Form the JSON.
  std::string json = llvm::formatv(R"({{"jsonrpc": "2.0", "{0}": "{1}")",
                                   method_or_id_label, method_or_id);
  if (use_call_id) {
    // Omit quotes on the ID because we know it's an integer.
    json += llvm::formatv(R"(, "id": {0})", ++lsp_call_id);
  }
  if (!extra_content.empty()) {
    json += ",";
    if (extra_content_label.empty()) {
      if (!extra_content.starts_with("\n")) {
        json += " ";
      }
      json += extra_content;