handle_text_document.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/language_server/handle.h"
  5. namespace Carbon::LanguageServer {
  6. auto HandleDidOpenTextDocument(
  7. Context& context, const clang::clangd::DidOpenTextDocumentParams& params)
  8. -> void {
  9. llvm::StringRef filename = params.textDocument.uri.file();
  10. if (!filename.ends_with(".carbon")) {
  11. // Ignore non-Carbon files.
  12. return;
  13. }
  14. auto insert_result = context.files().Insert(
  15. filename, [&] { return Context::File(filename.str()); });
  16. insert_result.value().SetText(context, params.textDocument.text);
  17. if (!insert_result.is_inserted()) {
  18. CARBON_DIAGNOSTIC(LanguageServerOpenDuplicateFile, Warning,
  19. "duplicate open file request; updating content");
  20. context.file_emitter().Emit(filename, LanguageServerOpenDuplicateFile);
  21. }
  22. }
  23. auto HandleDidChangeTextDocument(
  24. Context& context, const clang::clangd::DidChangeTextDocumentParams& params)
  25. -> void {
  26. llvm::StringRef filename = params.textDocument.uri.file();
  27. if (!filename.ends_with(".carbon")) {
  28. // Ignore non-Carbon files.
  29. return;
  30. }
  31. // Full text is sent if full sync is specified in capabilities.
  32. if (params.contentChanges.size() != 1) {
  33. CARBON_DIAGNOSTIC(LanguageServerUnsupportedChanges, Warning,
  34. "received unsupported contentChanges count: {0}", int);
  35. context.file_emitter().Emit(filename, LanguageServerUnsupportedChanges,
  36. params.contentChanges.size());
  37. return;
  38. }
  39. if (auto* file = context.LookupFile(filename)) {
  40. file->SetText(context, params.contentChanges[0].text);
  41. }
  42. }
  43. auto HandleDidCloseTextDocument(
  44. Context& context, const clang::clangd::DidCloseTextDocumentParams& params)
  45. -> void {
  46. llvm::StringRef filename = params.textDocument.uri.file();
  47. if (!filename.ends_with(".carbon")) {
  48. // Ignore non-Carbon files.
  49. return;
  50. }
  51. if (!context.files().Erase(filename)) {
  52. CARBON_DIAGNOSTIC(LanguageServerCloseUnknownFile, Warning,
  53. "tried closing unknown file; ignoring request");
  54. context.file_emitter().Emit(filename, LanguageServerCloseUnknownFile);
  55. }
  56. }
  57. } // namespace Carbon::LanguageServer