Add config->extension.referenceContainer

This commit is contained in:
Fangrui Song 2018-02-10 17:50:44 -08:00
parent d79a7a3fa1
commit 3ba9209eea
6 changed files with 63 additions and 40 deletions

View File

@ -157,6 +157,12 @@ struct Config {
}; };
Completion completion; Completion completion;
struct Extension {
// If true, reference results will include "containerName".
bool referenceContainer = false;
};
Extension extension;
struct Index { struct Index {
// 0: none, 1: doxygen, 2: all comments // 0: none, 1: doxygen, 2: all comments
// Plugin support for clients: // Plugin support for clients:
@ -182,6 +188,7 @@ struct Config {
}; };
MAKE_REFLECT_STRUCT(Config::ClientCapability, snippetSupport); MAKE_REFLECT_STRUCT(Config::ClientCapability, snippetSupport);
MAKE_REFLECT_STRUCT(Config::Completion, filterAndSort, detailedLabel); MAKE_REFLECT_STRUCT(Config::Completion, filterAndSort, detailedLabel);
MAKE_REFLECT_STRUCT(Config::Extension, referenceContainer);
MAKE_REFLECT_STRUCT(Config::Index, comments, attributeMakeCallsToCtor); MAKE_REFLECT_STRUCT(Config::Index, comments, attributeMakeCallsToCtor);
MAKE_REFLECT_STRUCT(Config, MAKE_REFLECT_STRUCT(Config,
compilationDatabaseDirectory, compilationDatabaseDirectory,
@ -218,6 +225,7 @@ MAKE_REFLECT_STRUCT(Config,
client, client,
completion, completion,
extension,
index, index,
dumpAST); dumpAST);

View File

@ -835,23 +835,6 @@ struct FindChildOfKindParam {
FindChildOfKindParam(CXCursorKind target_kind) : target_kind(target_kind) {} 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<ClangCursor> FindChildOfKind(ClangCursor cursor, CXCursorKind kind) {
FindChildOfKindParam param(kind);
cursor.VisitChildren(&FindChildOfKindVisitor, &param);
return param.result;
}
ClangCursor::VisitResult FindTypeVisitor(ClangCursor cursor, ClangCursor::VisitResult FindTypeVisitor(ClangCursor cursor,
ClangCursor parent, ClangCursor parent,
optional<ClangCursor>* result) { optional<ClangCursor>* result) {
@ -873,19 +856,6 @@ optional<ClangCursor> FindType(ClangCursor cursor) {
return result; 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) { bool IsTypeDefinition(const CXIdxContainerInfo* container) {
if (!container) if (!container)
return false; return false;

View File

@ -178,6 +178,12 @@ struct lsLocation {
MAKE_HASHABLE(lsLocation, t.uri, t.range); MAKE_HASHABLE(lsLocation, t.uri, t.range);
MAKE_REFLECT_STRUCT(lsLocation, uri, range); MAKE_REFLECT_STRUCT(lsLocation, uri, range);
// cquery extension
struct lsLocationEx : lsLocation {
optional<std::string> containerName;
};
MAKE_REFLECT_STRUCT(lsLocationEx, uri, range, containerName);
enum class lsSymbolKind : int { enum class lsSymbolKind : int {
File = 1, File = 1,
Module = 2, Module = 2,

View File

@ -32,7 +32,7 @@ REGISTER_IPC_MESSAGE(Ipc_TextDocumentReferences);
struct Out_TextDocumentReferences struct Out_TextDocumentReferences
: public lsOutMessage<Out_TextDocumentReferences> { : public lsOutMessage<Out_TextDocumentReferences> {
lsRequestId id; lsRequestId id;
std::vector<lsLocation> result; std::vector<lsLocationEx> result;
}; };
MAKE_REFLECT_STRUCT(Out_TextDocumentReferences, jsonrpc, id, result); MAKE_REFLECT_STRUCT(Out_TextDocumentReferences, jsonrpc, id, result);
@ -58,10 +58,11 @@ struct TextDocumentReferencesHandler
db, sym, request->params.context.includeDeclaration); db, sym, request->params.context.includeDeclaration);
out.result.reserve(uses.size()); out.result.reserve(uses.size());
for (Use use : uses) { for (Use use : uses) {
optional<lsLocation> ls_location = optional<lsLocationEx> ls_loc = GetLsLocationEx(
GetLsLocation(db, working_files, use); db, working_files, use, config->extension.referenceContainer);
if (ls_location) if (ls_loc) {
out.result.push_back(*ls_location); out.result.push_back(*ls_loc);
}
} }
break; break;
} }
@ -75,7 +76,7 @@ struct TextDocumentReferencesHandler
for (const IndexInclude& include1 : file1.def->includes) for (const IndexInclude& include1 : file1.def->includes)
if (include1.resolved_path == include.resolved_path) { if (include1.resolved_path == include.resolved_path) {
// Another file |file1| has the same include line. // Another file |file1| has the same include line.
lsLocation result; lsLocationEx result;
result.uri = lsDocumentUri::FromPath(file1.def->path); result.uri = lsDocumentUri::FromPath(file1.def->path);
result.range.start.line = result.range.end.line = result.range.start.line = result.range.end.line =
include1.line; include1.line;

View File

@ -390,6 +390,41 @@ optional<lsLocation> GetLsLocation(QueryDatabase* db,
return lsLocation(uri, *range); return lsLocation(uri, *range);
} }
optional<lsLocationEx> GetLsLocationEx(QueryDatabase* db,
WorkingFiles* working_files,
Use use,
bool extension) {
optional<lsLocation> 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<lsLocation> GetLsLocations( std::vector<lsLocation> GetLsLocations(
QueryDatabase* db, QueryDatabase* db,
WorkingFiles* working_files, WorkingFiles* working_files,

View File

@ -45,10 +45,13 @@ lsDocumentUri GetLsDocumentUri(QueryDatabase* db, QueryFileId file_id);
optional<lsLocation> GetLsLocation(QueryDatabase* db, optional<lsLocation> GetLsLocation(QueryDatabase* db,
WorkingFiles* working_files, WorkingFiles* working_files,
Reference location); Reference location);
std::vector<lsLocation> GetLsLocations( optional<lsLocationEx> GetLsLocationEx(QueryDatabase* db,
QueryDatabase* db, WorkingFiles* working_files,
WorkingFiles* working_files, Use use,
const std::vector<Use>& refs); bool extension);
std::vector<lsLocation> GetLsLocations(QueryDatabase* db,
WorkingFiles* working_files,
const std::vector<Use>& refs);
// Returns a symbol. The symbol will have *NOT* have a location assigned. // Returns a symbol. The symbol will have *NOT* have a location assigned.
optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db, optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
WorkingFiles* working_files, WorkingFiles* working_files,