handle_document_symbol.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/base/shared_value_stores.h"
  5. #include "toolchain/diagnostics/null_diagnostics.h"
  6. #include "toolchain/language_server/handle.h"
  7. #include "toolchain/lex/lex.h"
  8. #include "toolchain/parse/node_kind.h"
  9. #include "toolchain/parse/parse.h"
  10. #include "toolchain/parse/tree_and_subtrees.h"
  11. #include "toolchain/source/source_buffer.h"
  12. namespace Carbon::LanguageServer {
  13. // Returns the text of first child of kind IdentifierNameBeforeParams or
  14. // IdentifierNameNotBeforeParams.
  15. static auto GetIdentifierName(const Parse::TreeAndSubtrees& tree_and_subtrees,
  16. Parse::NodeId node)
  17. -> std::optional<llvm::StringRef> {
  18. const auto& tokens = tree_and_subtrees.tree().tokens();
  19. for (auto child : tree_and_subtrees.children(node)) {
  20. switch (tree_and_subtrees.tree().node_kind(child)) {
  21. case Parse::NodeKind::IdentifierNameBeforeParams:
  22. case Parse::NodeKind::IdentifierNameNotBeforeParams: {
  23. auto token = tree_and_subtrees.tree().node_token(child);
  24. if (tokens.GetKind(token) == Lex::TokenKind::Identifier) {
  25. return tokens.GetTokenText(token);
  26. }
  27. break;
  28. }
  29. default:
  30. break;
  31. }
  32. }
  33. return std::nullopt;
  34. }
  35. auto HandleDocumentSymbol(
  36. Context& context, const clang::clangd::DocumentSymbolParams& params,
  37. llvm::function_ref<
  38. void(llvm::Expected<std::vector<clang::clangd::DocumentSymbol>>)>
  39. on_done) -> void {
  40. auto* file = context.LookupFile(params.textDocument.uri.file());
  41. if (!file) {
  42. return;
  43. }
  44. const auto& tree_and_subtrees = file->tree_and_subtrees();
  45. const auto& tree = tree_and_subtrees.tree();
  46. const auto& tokens = tree.tokens();
  47. std::vector<clang::clangd::DocumentSymbol> result;
  48. for (const auto& node_id : tree.postorder()) {
  49. clang::clangd::SymbolKind symbol_kind;
  50. switch (tree.node_kind(node_id)) {
  51. case Parse::NodeKind::FunctionDecl:
  52. case Parse::NodeKind::FunctionDefinitionStart:
  53. symbol_kind = clang::clangd::SymbolKind::Function;
  54. break;
  55. case Parse::NodeKind::Namespace:
  56. symbol_kind = clang::clangd::SymbolKind::Namespace;
  57. break;
  58. case Parse::NodeKind::InterfaceDefinitionStart:
  59. case Parse::NodeKind::NamedConstraintDefinitionStart:
  60. symbol_kind = clang::clangd::SymbolKind::Interface;
  61. break;
  62. case Parse::NodeKind::ClassDefinitionStart:
  63. symbol_kind = clang::clangd::SymbolKind::Class;
  64. break;
  65. default:
  66. continue;
  67. }
  68. if (auto name = GetIdentifierName(tree_and_subtrees, node_id)) {
  69. auto token = tree.node_token(node_id);
  70. clang::clangd::Position pos{tokens.GetLineNumber(token) - 1,
  71. tokens.GetColumnNumber(token) - 1};
  72. clang::clangd::DocumentSymbol symbol{
  73. .name = std::string(*name),
  74. .kind = symbol_kind,
  75. .range = {.start = pos, .end = pos},
  76. .selectionRange = {.start = pos, .end = pos},
  77. };
  78. result.push_back(symbol);
  79. }
  80. }
  81. on_done(result);
  82. }
  83. } // namespace Carbon::LanguageServer