handle_text_document.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(params.textDocument.uri); });
  16. insert_result.value().SetText(context, params.textDocument.version,
  17. params.textDocument.text);
  18. if (!insert_result.is_inserted()) {
  19. CARBON_DIAGNOSTIC(LanguageServerOpenDuplicateFile, Warning,
  20. "duplicate open file request; updating content");
  21. context.file_emitter().Emit(filename, LanguageServerOpenDuplicateFile);
  22. }
  23. }
  24. auto HandleDidChangeTextDocument(
  25. Context& context, const clang::clangd::DidChangeTextDocumentParams& params)
  26. -> void {
  27. llvm::StringRef filename = params.textDocument.uri.file();
  28. if (!filename.ends_with(".carbon")) {
  29. // Ignore non-Carbon files.
  30. return;
  31. }
  32. // Full text is sent if full sync is specified in capabilities.
  33. if (params.contentChanges.size() != 1) {
  34. CARBON_DIAGNOSTIC(LanguageServerUnsupportedChanges, Warning,
  35. "received unsupported contentChanges count: {0}", int);
  36. context.file_emitter().Emit(filename, LanguageServerUnsupportedChanges,
  37. params.contentChanges.size());
  38. return;
  39. }
  40. if (auto* file = context.LookupFile(filename)) {
  41. file->SetText(context, params.textDocument.version,
  42. params.contentChanges[0].text);
  43. }
  44. }
  45. auto HandleDidCloseTextDocument(
  46. Context& context, const clang::clangd::DidCloseTextDocumentParams& params)
  47. -> void {
  48. llvm::StringRef filename = params.textDocument.uri.file();
  49. if (!filename.ends_with(".carbon")) {
  50. // Ignore non-Carbon files.
  51. return;
  52. }
  53. if (!context.files().Erase(filename)) {
  54. CARBON_DIAGNOSTIC(LanguageServerCloseUnknownFile, Warning,
  55. "tried closing unknown file; ignoring request");
  56. context.file_emitter().Emit(filename, LanguageServerCloseUnknownFile);
  57. }
  58. }
  59. } // namespace Carbon::LanguageServer