diff --git a/src/config.h b/src/config.h index c16939a43..b6e7589db 100644 --- a/src/config.h +++ b/src/config.h @@ -157,6 +157,12 @@ struct Config { }; Completion completion; + struct Extension { + // If true, reference results will include "containerName". + bool referenceContainer = false; + }; + Extension extension; + struct Index { // 0: none, 1: doxygen, 2: all comments // Plugin support for clients: @@ -182,6 +188,7 @@ struct Config { }; MAKE_REFLECT_STRUCT(Config::ClientCapability, snippetSupport); MAKE_REFLECT_STRUCT(Config::Completion, filterAndSort, detailedLabel); +MAKE_REFLECT_STRUCT(Config::Extension, referenceContainer); MAKE_REFLECT_STRUCT(Config::Index, comments, attributeMakeCallsToCtor); MAKE_REFLECT_STRUCT(Config, compilationDatabaseDirectory, @@ -218,6 +225,7 @@ MAKE_REFLECT_STRUCT(Config, client, completion, + extension, index, dumpAST); diff --git a/src/indexer.cc b/src/indexer.cc index 0efd329b6..b72559c63 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -835,23 +835,6 @@ struct FindChildOfKindParam { FindChildOfKindParam(CXCursorKind target_kind) : target_kind(target_kind) {} }; -ClangCursor::VisitResult FindChildOfKindVisitor(ClangCursor cursor, - ClangCursor parent, - FindChildOfKindParam* param) { - if (cursor.get_kind() == param->target_kind) { - param->result = cursor; - return ClangCursor::VisitResult::Break; - } - - return ClangCursor::VisitResult::Recurse; -} - -optional FindChildOfKind(ClangCursor cursor, CXCursorKind kind) { - FindChildOfKindParam param(kind); - cursor.VisitChildren(&FindChildOfKindVisitor, ¶m); - return param.result; -} - ClangCursor::VisitResult FindTypeVisitor(ClangCursor cursor, ClangCursor parent, optional* result) { @@ -873,19 +856,6 @@ optional FindType(ClangCursor cursor) { return result; } -bool IsGlobalContainer(const CXIdxContainerInfo* container) { - if (!container) - return false; - - switch (container->cursor.kind) { - case CXCursor_Namespace: - case CXCursor_TranslationUnit: - return true; - default: - return false; - } -} - bool IsTypeDefinition(const CXIdxContainerInfo* container) { if (!container) return false; diff --git a/src/language_server_api.h b/src/language_server_api.h index 9761ee2c2..4b635268b 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -178,6 +178,12 @@ struct lsLocation { MAKE_HASHABLE(lsLocation, t.uri, t.range); MAKE_REFLECT_STRUCT(lsLocation, uri, range); +// cquery extension +struct lsLocationEx : lsLocation { + optional containerName; +}; +MAKE_REFLECT_STRUCT(lsLocationEx, uri, range, containerName); + enum class lsSymbolKind : int { File = 1, Module = 2, diff --git a/src/messages/text_document_references.cc b/src/messages/text_document_references.cc index 31e9bcec3..f39751a6b 100644 --- a/src/messages/text_document_references.cc +++ b/src/messages/text_document_references.cc @@ -32,7 +32,7 @@ REGISTER_IPC_MESSAGE(Ipc_TextDocumentReferences); struct Out_TextDocumentReferences : public lsOutMessage { lsRequestId id; - std::vector result; + std::vector result; }; MAKE_REFLECT_STRUCT(Out_TextDocumentReferences, jsonrpc, id, result); @@ -58,10 +58,11 @@ struct TextDocumentReferencesHandler db, sym, request->params.context.includeDeclaration); out.result.reserve(uses.size()); for (Use use : uses) { - optional ls_location = - GetLsLocation(db, working_files, use); - if (ls_location) - out.result.push_back(*ls_location); + optional ls_loc = GetLsLocationEx( + db, working_files, use, config->extension.referenceContainer); + if (ls_loc) { + out.result.push_back(*ls_loc); + } } break; } @@ -75,7 +76,7 @@ struct TextDocumentReferencesHandler for (const IndexInclude& include1 : file1.def->includes) if (include1.resolved_path == include.resolved_path) { // Another file |file1| has the same include line. - lsLocation result; + lsLocationEx result; result.uri = lsDocumentUri::FromPath(file1.def->path); result.range.start.line = result.range.end.line = include1.line; diff --git a/src/query_utils.cc b/src/query_utils.cc index a1853daea..98cea0ffd 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -390,6 +390,41 @@ optional GetLsLocation(QueryDatabase* db, return lsLocation(uri, *range); } +optional GetLsLocationEx(QueryDatabase* db, + WorkingFiles* working_files, + Use use, + bool extension) { + optional ls_loc = GetLsLocation(db, working_files, use); + if (!ls_loc) + return nullopt; + lsLocationEx ret; + ret.lsLocation::operator=(*ls_loc); + if (extension) + switch (use.kind) { + default: + break; + case SymbolKind::Func: { + QueryFunc& func = db->GetFunc(use); + if (func.def) + ret.containerName = func.def->detailed_name; + break; + } + case SymbolKind::Type: { + QueryType& type = db->GetType(use); + if (type.def) + ret.containerName = type.def->detailed_name; + break; + } + case SymbolKind::Var: { + QueryVar& var = db->GetVar(use); + if (var.def) + ret.containerName = var.def->detailed_name; + break; + } + } + return ret; +} + std::vector GetLsLocations( QueryDatabase* db, WorkingFiles* working_files, diff --git a/src/query_utils.h b/src/query_utils.h index 213607b17..de2c03da3 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -45,10 +45,13 @@ lsDocumentUri GetLsDocumentUri(QueryDatabase* db, QueryFileId file_id); optional GetLsLocation(QueryDatabase* db, WorkingFiles* working_files, Reference location); -std::vector GetLsLocations( - QueryDatabase* db, - WorkingFiles* working_files, - const std::vector& refs); +optional GetLsLocationEx(QueryDatabase* db, + WorkingFiles* working_files, + Use use, + bool extension); +std::vector GetLsLocations(QueryDatabase* db, + WorkingFiles* working_files, + const std::vector& refs); // Returns a symbol. The symbol will have *NOT* have a location assigned. optional GetSymbolInfo(QueryDatabase* db, WorkingFiles* working_files,