handle_document_symbol.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Parse::NodeKind::IdentifierName.
  14. static auto GetIdentifierName(const SharedValueStores& value_stores,
  15. const Lex::TokenizedBuffer& tokens,
  16. const Parse::TreeAndSubtrees& tree_and_subtrees,
  17. Parse::NodeId node)
  18. -> std::optional<llvm::StringRef> {
  19. for (auto child : tree_and_subtrees.children(node)) {
  20. if (tree_and_subtrees.tree().node_kind(child) ==
  21. Parse::NodeKind::IdentifierName) {
  22. auto token = tree_and_subtrees.tree().node_token(child);
  23. if (tokens.GetKind(token) == Lex::TokenKind::Identifier) {
  24. return value_stores.identifiers().Get(tokens.GetIdentifier(token));
  25. }
  26. }
  27. }
  28. return std::nullopt;
  29. }
  30. auto HandleDocumentSymbol(
  31. Context& context, const clang::clangd::DocumentSymbolParams& params,
  32. llvm::function_ref<
  33. void(llvm::Expected<std::vector<clang::clangd::DocumentSymbol>>)>
  34. on_done) -> void {
  35. SharedValueStores value_stores;
  36. llvm::vfs::InMemoryFileSystem vfs;
  37. auto lookup = context.files().Lookup(params.textDocument.uri.file());
  38. CARBON_CHECK(lookup);
  39. vfs.addFile(lookup.key(), /*mtime=*/0,
  40. llvm::MemoryBuffer::getMemBufferCopy(lookup.value()));
  41. auto source =
  42. SourceBuffer::MakeFromFile(vfs, lookup.key(), NullDiagnosticConsumer());
  43. auto tokens = Lex::Lex(value_stores, *source, NullDiagnosticConsumer());
  44. auto tree = Parse::Parse(tokens, NullDiagnosticConsumer(), nullptr);
  45. Parse::TreeAndSubtrees tree_and_subtrees(tokens, tree);
  46. std::vector<clang::clangd::DocumentSymbol> result;
  47. for (const auto& node : tree.postorder()) {
  48. clang::clangd::SymbolKind symbol_kind;
  49. switch (tree.node_kind(node)) {
  50. case Parse::NodeKind::FunctionDecl:
  51. case Parse::NodeKind::FunctionDefinitionStart:
  52. symbol_kind = clang::clangd::SymbolKind::Function;
  53. break;
  54. case Parse::NodeKind::Namespace:
  55. symbol_kind = clang::clangd::SymbolKind::Namespace;
  56. break;
  57. case Parse::NodeKind::InterfaceDefinitionStart:
  58. case Parse::NodeKind::NamedConstraintDefinitionStart:
  59. symbol_kind = clang::clangd::SymbolKind::Interface;
  60. break;
  61. case Parse::NodeKind::ClassDefinitionStart:
  62. symbol_kind = clang::clangd::SymbolKind::Class;
  63. break;
  64. default:
  65. continue;
  66. }
  67. if (auto name =
  68. GetIdentifierName(value_stores, tokens, tree_and_subtrees, node)) {
  69. auto token = tree.node_token(node);
  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