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

Code before:
  }
  return uri.drop_front(FilePrefix.size());
}

static auto AutoFillDidOpenParams(llvm::json::Object* params,
                                  llvm::ArrayRef<TestFile::Split> splits)
    -> ErrorOr<Success> {
  auto* text_document = params->getObject("textDocument");
  if (text_document == nullptr) {
    return Success{};
  }

  auto attr_it = text_document->find("text");
  if (attr_it == text_document->end() || attr_it->second != "FROM_FILE_SPLIT") {
    return Success{};
  }

  auto uri = text_document->getString("uri");
  if (!uri) {
    return ErrorBuilder() << "missing uri in params.textDocument";
  }

  auto file_path = ExtractFilePathFromUri(*uri);
  if (!file_path) {
    return ErrorBuilder() << "uri `" << *uri << "` is not a file uri";
  }
  const auto* split_it =
      llvm::find_if(splits, [&](const TestFile::Split& split) {
        return split.filename == file_path;
      });
  if (split_it == splits.end()) {
    return ErrorBuilder() << "No split found for uri: " << *uri;
  }
  attr_it->second = split_it->content;
  return Success{};
}

// Reformats `[[@LSP:` and similar keyword as an LSP call with headers.
static auto ReplaceLspKeywordAt(std::string* content, size_t keyword_pos,
                                int& lsp_call_id,
                                llvm::ArrayRef<TestFile::Split> splits)
    -> ErrorOr<size_t> {
  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) << "`";
  }


Review comment (refactor):
```suggestion
  CARBON_ASSIGN_OR_RETURN(auto file_path, ExtractFilePathFromUri(*uri));
```

(see above)

Code after:
  if (!uri.starts_with(FilePrefix)) {
    return ErrorBuilder("uri `") << uri << "` is not a file uri";
  }
  return uri.drop_front(FilePrefix.size());
}

// When `FROM_FILE_SPLIT` is used in path `textDocument.text`, populate the
// value from the split matching the `uri`. Only used for
// `textDocument/didOpen`.
static auto AutoFillDidOpenParams(llvm::json::Object* params,
                                  llvm::ArrayRef<TestFile::Split> splits)
    -> ErrorOr<Success> {
  auto* text_document = params->getObject("textDocument");
  if (text_document == nullptr) {
    return Success();
  }

  auto attr_it = text_document->find("text");
  if (attr_it == text_document->end() || attr_it->second != "FROM_FILE_SPLIT") {
    return Success();
  }

  auto uri = text_document->getString("uri");
  if (!uri) {
    return Error("missing uri in params.textDocument");
  }

  CARBON_ASSIGN_OR_RETURN(auto file_path, ExtractFilePathFromUri(*uri));
  const auto* split_it =
      llvm::find_if(splits, [&](const TestFile::Split& split) {
        return split.filename == file_path;
      });
  if (split_it == splits.end()) {
    return ErrorBuilder() << "No split found for uri: " << *uri;
  }
  attr_it->second = split_it->content;
  return Success();
}

// Reformats `[[@LSP:` and similar keyword as an LSP call with headers.
static auto ReplaceLspKeywordAt(std::string* content, size_t keyword_pos,
                                int& lsp_call_id,
                                llvm::ArrayRef<TestFile::Split> splits)
    -> ErrorOr<size_t> {
  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) << "`";