handle_document_symbol.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/language_server/handle.h"
  6. #include "toolchain/lex/lex.h"
  7. #include "toolchain/parse/node_kind.h"
  8. #include "toolchain/parse/parse.h"
  9. #include "toolchain/parse/tree_and_subtrees.h"
  10. #include "toolchain/source/source_buffer.h"
  11. namespace Carbon::LanguageServer {
  12. // Returns the token of first child of kind IdentifierNameBeforeParams or
  13. // IdentifierNameNotBeforeParams.
  14. static auto GetSymbolIdentifier(const Parse::TreeAndSubtrees& tree_and_subtrees,
  15. Parse::NodeId node)
  16. -> std::optional<Lex::TokenIndex> {
  17. const auto& tokens = tree_and_subtrees.tree().tokens();
  18. for (auto child : tree_and_subtrees.children(node)) {
  19. switch (tree_and_subtrees.tree().node_kind(child)) {
  20. case Parse::NodeKind::IdentifierNameBeforeParams:
  21. case Parse::NodeKind::IdentifierNameNotBeforeParams: {
  22. auto token = tree_and_subtrees.tree().node_token(child);
  23. if (tokens.GetKind(token) == Lex::TokenKind::Identifier) {
  24. return token;
  25. }
  26. break;
  27. }
  28. default:
  29. break;
  30. }
  31. }
  32. return std::nullopt;
  33. }
  34. // Constructs a Range from a closed interval of tokens [start, end].
  35. static auto GetTokenRange(const Lex::TokenizedBuffer& tokens,
  36. Lex::TokenIndex start, Lex::TokenIndex end)
  37. -> clang::clangd::Range {
  38. auto start_line = tokens.GetLine(start);
  39. auto start_col = tokens.GetColumnNumber(start);
  40. auto [end_line, end_col] = tokens.GetEndLoc(end);
  41. return clang::clangd::Range{
  42. .start = {.line = start_line.index, .character = start_col - 1},
  43. .end = {.line = end_line.index, .character = end_col - 1},
  44. };
  45. }
  46. // Finds a spanning range for the provided definition / declaration ast node.
  47. // In the case of a definition start, will include the body as well.
  48. static auto GetSymbolRange(const Parse::TreeAndSubtrees& tree_and_subtrees,
  49. const Parse::NodeId& ast_node)
  50. -> clang::clangd::Range {
  51. const auto& tokens = tree_and_subtrees.tree().tokens();
  52. // The left-most node will always be the first node in postorder traversal.
  53. auto start_node = *tree_and_subtrees.postorder(ast_node).begin();
  54. auto start_token = tree_and_subtrees.tree().node_token(start_node);
  55. auto end_token = tree_and_subtrees.tree().node_token(ast_node);
  56. if (tokens.GetKind(end_token).is_opening_symbol()) {
  57. // DefinitionStart nodes use an opening token, so find its closing token to
  58. // span the entire class/function body.
  59. return GetTokenRange(tokens, start_token,
  60. tokens.GetMatchedClosingToken(end_token));
  61. } else {
  62. return GetTokenRange(tokens, start_token, end_token);
  63. }
  64. }
  65. auto HandleDocumentSymbol(
  66. Context& context, const clang::clangd::DocumentSymbolParams& params,
  67. llvm::function_ref<
  68. auto(llvm::Expected<std::vector<clang::clangd::DocumentSymbol>>)->void>
  69. on_done) -> void {
  70. auto* file = context.LookupFile(params.textDocument.uri.file());
  71. if (!file) {
  72. return;
  73. }
  74. const auto& tree_and_subtrees = file->tree_and_subtrees();
  75. const auto& tree = tree_and_subtrees.tree();
  76. const auto& tokens = tree.tokens();
  77. std::vector<clang::clangd::DocumentSymbol> result;
  78. for (const auto& node_id : tree.postorder()) {
  79. clang::clangd::SymbolKind symbol_kind;
  80. switch (tree.node_kind(node_id)) {
  81. case Parse::NodeKind::FunctionDecl:
  82. case Parse::NodeKind::FunctionDefinitionStart:
  83. symbol_kind = clang::clangd::SymbolKind::Function;
  84. break;
  85. case Parse::NodeKind::Namespace:
  86. symbol_kind = clang::clangd::SymbolKind::Namespace;
  87. break;
  88. case Parse::NodeKind::InterfaceDefinitionStart:
  89. case Parse::NodeKind::NamedConstraintDefinitionStart:
  90. symbol_kind = clang::clangd::SymbolKind::Interface;
  91. break;
  92. case Parse::NodeKind::ClassDefinitionStart:
  93. symbol_kind = clang::clangd::SymbolKind::Class;
  94. break;
  95. default:
  96. continue;
  97. }
  98. if (auto identifier = GetSymbolIdentifier(tree_and_subtrees, node_id)) {
  99. clang::clangd::DocumentSymbol symbol{
  100. .name = std::string(tokens.GetTokenText(*identifier)),
  101. .kind = symbol_kind,
  102. .range = GetSymbolRange(tree_and_subtrees, node_id),
  103. .selectionRange = GetTokenRange(tokens, *identifier, *identifier),
  104. };
  105. result.push_back(symbol);
  106. }
  107. }
  108. on_done(result);
  109. }
  110. } // namespace Carbon::LanguageServer