Repository: carbon-language/carbon-lang
File: toolchain/language_server/handle_text_document.cpp (C++)

Code before:
    // If range is not present, then we replace entire text.
    if (!change.range) {
      source = change.text;
      continue;
    }

    // TODO: Use lexer line number data but avoid re-lexing multiple times.
    auto [start_index, end_index] =
        PositionToIndex(source, change.range->start, change.range->end);

    source.replace(start_index, end_index - start_index, change.text);
  }
}

auto HandleDidChangeTextDocument(
    Context& context, const clang::clangd::DidChangeTextDocumentParams& params)
    -> void {
  llvm::StringRef filename = params.textDocument.uri.file();
  if (!filename.ends_with(".carbon")) {
    // Ignore non-Carbon files.
    return;
  }

  if (auto* file = context.LookupFile(filename)) {
    // we copy the document to a new string, apply changes to the string and
    // set the string as the new text
    std::string source = file->text().str();
    ApplyChanges(source, params.contentChanges);
    file->SetText(context, params.textDocument.version, source);
  }
}

auto HandleDidCloseTextDocument(
    Context& context, const clang::clangd::DidCloseTextDocumentParams& params)
    -> void {
  llvm::StringRef filename = params.textDocument.uri.file();
  if (!filename.ends_with(".carbon")) {
    // Ignore non-Carbon files.
    return;
  }

  if (!context.files().Erase(filename)) {
    CARBON_DIAGNOSTIC(LanguageServerCloseUnknownFile, Warning,
                      "tried closing unknown file; ignoring request");
    context.file_emitter().Emit(filename, LanguageServerCloseUnknownFile);
  }
}

}  // namespace Carbon::LanguageServer


Review comment (suggestion):
```suggestion
    // We copy the document to a new string, apply changes to the string, and
    // set the string as the new text.
```

Capitalization/punctuation.

Code after:
    // If range is not present, then we replace entire text.
    if (!change.range) {
      source = change.text;
      continue;
    }

    // TODO: Use lexer line number data but avoid re-lexing multiple times.
    auto [start_index, end_index] =
        PositionToIndex(source, change.range->start, change.range->end);

    source.replace(start_index, end_index - start_index, change.text);
  }
}

auto HandleDidChangeTextDocument(
    Context& context, const clang::clangd::DidChangeTextDocumentParams& params)
    -> void {
  llvm::StringRef filename = params.textDocument.uri.file();
  if (!filename.ends_with(".carbon")) {
    // Ignore non-Carbon files.
    return;
  }

  if (auto* file = context.LookupFile(filename)) {
    // We copy the document to a new string, apply changes to the string, and
    // set the string as the new text.
    std::string source = file->text().str();
    ApplyChanges(source, params.contentChanges);
    file->SetText(context, params.textDocument.version, source);
  }
}

auto HandleDidCloseTextDocument(
    Context& context, const clang::clangd::DidCloseTextDocumentParams& params)
    -> void {
  llvm::StringRef filename = params.textDocument.uri.file();
  if (!filename.ends_with(".carbon")) {
    // Ignore non-Carbon files.
    return;
  }

  if (!context.files().Erase(filename)) {
    CARBON_DIAGNOSTIC(LanguageServerCloseUnknownFile, Warning,
                      "tried closing unknown file; ignoring request");
    context.file_emitter().Emit(filename, LanguageServerCloseUnknownFile);
  }
}

}  // namespace Carbon::LanguageServer