handle_document_symbol.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 SharedValueStores& value_stores,
  16. const Lex::TokenizedBuffer& tokens,
  17. const Parse::TreeAndSubtrees& tree_and_subtrees,
  18. Parse::NodeId node)
  19. -> std::optional<llvm::StringRef> {
  20. for (auto child : tree_and_subtrees.children(node)) {
  21. switch (tree_and_subtrees.tree().node_kind(child)) {
  22. case Parse::NodeKind::IdentifierNameBeforeParams:
  23. case Parse::NodeKind::IdentifierNameNotBeforeParams: {
  24. auto token = tree_and_subtrees.tree().node_token(child);
  25. if (tokens.GetKind(token) == Lex::TokenKind::Identifier) {
  26. return value_stores.identifiers().Get(tokens.GetIdentifier(token));
  27. }
  28. break;
  29. }
  30. default:
  31. break;
  32. }
  33. }
  34. return std::nullopt;
  35. }
  36. auto HandleDocumentSymbol(
  37. Context& context, const clang::clangd::DocumentSymbolParams& params,
  38. llvm::function_ref<
  39. void(llvm::Expected<std::vector<clang::clangd::DocumentSymbol>>)>
  40. on_done) -> void {
  41. SharedValueStores value_stores;
  42. llvm::vfs::InMemoryFileSystem vfs;
  43. auto lookup = context.files().Lookup(params.textDocument.uri.file());
  44. CARBON_CHECK(lookup);
  45. vfs.addFile(lookup.key(), /*mtime=*/0,
  46. llvm::MemoryBuffer::getMemBufferCopy(lookup.value()));
  47. auto source =
  48. SourceBuffer::MakeFromFile(vfs, lookup.key(), NullDiagnosticConsumer());
  49. auto tokens = Lex::Lex(value_stores, *source, NullDiagnosticConsumer());
  50. auto tree = Parse::Parse(tokens, NullDiagnosticConsumer(), nullptr);
  51. Parse::TreeAndSubtrees tree_and_subtrees(tokens, tree);
  52. std::vector<clang::clangd::DocumentSymbol> result;
  53. for (const auto& node : tree.postorder()) {
  54. clang::clangd::SymbolKind symbol_kind;
  55. switch (tree.node_kind(node)) {
  56. case Parse::NodeKind::FunctionDecl:
  57. case Parse::NodeKind::FunctionDefinitionStart:
  58. symbol_kind = clang::clangd::SymbolKind::Function;
  59. break;
  60. case Parse::NodeKind::Namespace:
  61. symbol_kind = clang::clangd::SymbolKind::Namespace;
  62. break;
  63. case Parse::NodeKind::InterfaceDefinitionStart:
  64. case Parse::NodeKind::NamedConstraintDefinitionStart:
  65. symbol_kind = clang::clangd::SymbolKind::Interface;
  66. break;
  67. case Parse::NodeKind::ClassDefinitionStart:
  68. symbol_kind = clang::clangd::SymbolKind::Class;
  69. break;
  70. default:
  71. continue;
  72. }
  73. if (auto name =
  74. GetIdentifierName(value_stores, tokens, tree_and_subtrees, node)) {
  75. auto token = tree.node_token(node);
  76. clang::clangd::Position pos{tokens.GetLineNumber(token) - 1,
  77. tokens.GetColumnNumber(token) - 1};
  78. clang::clangd::DocumentSymbol symbol{
  79. .name = std::string(*name),
  80. .kind = symbol_kind,
  81. .range = {.start = pos, .end = pos},
  82. .selectionRange = {.start = pos, .end = pos},
  83. };
  84. result.push_back(symbol);
  85. }
  86. }
  87. on_done(result);
  88. }
  89. } // namespace Carbon::LanguageServer