diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp --- a/clang-tools-extra/clangd/FindSymbols.cpp +++ b/clang-tools-extra/clangd/FindSymbols.cpp @@ -172,6 +172,31 @@ } namespace { + +std::string getSymbolDetail(ASTContext &Ctx, const NamedDecl &ND) { + PrintingPolicy P(Ctx.getPrintingPolicy()); + P.SuppressScope = true; + P.SuppressUnwrittenScope = true; + P.AnonymousTagLocations = false; + P.PolishForDeclaration = true; + std::string Detail; + llvm::raw_string_ostream OS(Detail); + if (const auto *TP = ND.getDescribedTemplateParams()) { + TP->print(OS, Ctx, P); + } + if (const auto *VD = dyn_cast(&ND)) { + VD->getType().print(OS, P); + } else if (const auto *TD = dyn_cast(&ND)) { + OS << TD->getKindName(); + } else if (const auto *TD = dyn_cast(&ND)) { + OS << "type alias"; + } else if (const auto *CD = dyn_cast(&ND)) { + CD->getTemplateParameters()->print(OS, Ctx, P); + OS << "concept"; + } + return Detail; +} + llvm::Optional declToSym(ASTContext &Ctx, const NamedDecl &ND) { auto &SM = Ctx.getSourceManager(); @@ -193,6 +218,7 @@ SI.deprecated = ND.isDeprecated(); SI.range = Range{sourceLocToPosition(SM, SymbolRange->getBegin()), sourceLocToPosition(SM, SymbolRange->getEnd())}; + SI.detail = getSymbolDetail(Ctx, ND); SourceLocation NameLoc = ND.getLocation(); SourceLocation FallbackNameLoc; diff --git a/clang-tools-extra/clangd/test/symbols.test b/clang-tools-extra/clangd/test/symbols.test --- a/clang-tools-extra/clangd/test/symbols.test +++ b/clang-tools-extra/clangd/test/symbols.test @@ -1,5 +1,5 @@ # RUN: clangd --index-file=%S/Inputs/symbols.test.yaml -lit-test < %s | FileCheck %s -{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"workspace":{"symbol":{"symbolKind":{"valueSet": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}}}},"trace":"off"}} +{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument":{"documentSymbol":{"hierarchicalDocumentSymbolSupport":true}},"workspace":{"symbol":{"symbolKind":{"valueSet": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}}}},"trace":"off"}} --- {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///main.cpp","languageId":"cpp","version":1,"text":"void foo(); int main() { foo(); }\n"}}} --- @@ -34,40 +34,54 @@ # CHECK-NEXT: "jsonrpc": "2.0", # CHECK-NEXT: "result": [ # CHECK-NEXT: { -# CHECK-NEXT: "containerName": "", +# CHECK-NEXT: "detail": "void ()", # CHECK-NEXT: "kind": 12, -# CHECK-NEXT: "location": { -# CHECK-NEXT: "range": { -# CHECK-NEXT: "end": { -# CHECK-NEXT: "character": {{.*}}, -# CHECK-NEXT: "line": {{.*}} -# CHECK-NEXT: }, -# CHECK-NEXT: "start": { -# CHECK-NEXT: "character": {{.*}}, -# CHECK-NEXT: "line": {{.*}} -# CHECK-NEXT: } +# CHECK-NEXT: "name": "foo", +# CHECK-NEXT: "range": { +# CHECK-NEXT: "end": { +# CHECK-NEXT: "character": {{.*}}, +# CHECK-NEXT: "line": {{.*}} # CHECK-NEXT: }, -# CHECK-NEXT: "uri": "file://{{.*}}/main.cpp" +# CHECK-NEXT: "start": { +# CHECK-NEXT: "character": {{.*}}, +# CHECK-NEXT: "line": {{.*}} +# CHECK-NEXT: } # CHECK-NEXT: }, -# CHECK-NEXT: "name": "foo" -# CHECK-NEXT: } +# CHECK-NEXT: "selectionRange": { +# CHECK-NEXT: "end": { +# CHECK-NEXT: "character": {{.*}}, +# CHECK-NEXT: "line": {{.*}} +# CHECK-NEXT: }, +# CHECK-NEXT: "start": { +# CHECK-NEXT: "character": {{.*}}, +# CHECK-NEXT: "line": {{.*}} +# CHECK-NEXT: } +# CHECK-NEXT: } +# CHECK-NEXT: }, # CHECK-NEXT: { -# CHECK-NEXT: "containerName": "", +# CHECK-NEXT: "detail": "int ()", # CHECK-NEXT: "kind": 12, -# CHECK-NEXT: "location": { -# CHECK-NEXT: "range": { -# CHECK-NEXT: "end": { -# CHECK-NEXT: "character": {{.*}}, -# CHECK-NEXT: "line": {{.*}} -# CHECK-NEXT: }, -# CHECK-NEXT: "start": { -# CHECK-NEXT: "character": {{.*}}, -# CHECK-NEXT: "line": {{.*}} -# CHECK-NEXT: } +# CHECK-NEXT: "name": "main", +# CHECK-NEXT: "range": { +# CHECK-NEXT: "end": { +# CHECK-NEXT: "character": {{.*}}, +# CHECK-NEXT: "line": {{.*}} # CHECK-NEXT: }, -# CHECK-NEXT: "uri": "file://{{.*}}/main.cpp" +# CHECK-NEXT: "start": { +# CHECK-NEXT: "character": {{.*}}, +# CHECK-NEXT: "line": {{.*}} +# CHECK-NEXT: } # CHECK-NEXT: }, -# CHECK-NEXT: "name": "main" +# CHECK-NEXT: "selectionRange": { +# CHECK-NEXT: "end": { +# CHECK-NEXT: "character": {{.*}}, +# CHECK-NEXT: "line": {{.*}} +# CHECK-NEXT: }, +# CHECK-NEXT: "start": { +# CHECK-NEXT: "character": {{.*}}, +# CHECK-NEXT: "line": {{.*}} +# CHECK-NEXT: } +# CHECK-NEXT: } # CHECK-NEXT: } # CHECK-NEXT: ] # CHECK-NEXT:}