From b71cf25186270f9d251e0698d4352c63771e5902 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sun, 11 Feb 2018 13:42:48 -0800 Subject: [PATCH] Query GetFileId returns Maybe --- src/maybe.h | 4 ++++ src/messages/cquery_member_hierarchy.cc | 6 +++--- src/messages/text_document_code_action.cc | 4 ++-- src/messages/text_document_code_lens.cc | 6 ++++-- src/messages/text_document_rename.cc | 16 ++++++++-------- src/query.cc | 6 +++--- src/query.h | 2 +- src/query_utils.cc | 6 +++--- 8 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/maybe.h b/src/maybe.h index b05fabbf..eb482e50 100644 --- a/src/maybe.h +++ b/src/maybe.h @@ -44,4 +44,8 @@ public: bool operator==(const Maybe& o) const { return storage == o.storage; } + bool operator!=(const Maybe& o) const { + return !(*this == o); + } + }; diff --git a/src/messages/cquery_member_hierarchy.cc b/src/messages/cquery_member_hierarchy.cc index 34b0e5e7..804fcb2b 100644 --- a/src/messages/cquery_member_hierarchy.cc +++ b/src/messages/cquery_member_hierarchy.cc @@ -15,7 +15,7 @@ struct Ipc_CqueryMemberHierarchyExpand : public RequestMessage { const static IpcId kIpcId = IpcId::CqueryMemberHierarchyExpand; struct Params { - QueryTypeId type_id; + Maybe type_id; }; Params params; }; @@ -114,8 +114,8 @@ struct CqueryMemberHierarchyExpandHandler Out_CqueryMemberHierarchy out; out.id = request->id; // |ExpandNode| uses -1 to indicate invalid |type_id|. - if (request->params.type_id.HasValue()) - out.result = ExpandNode(db, working_files, request->params.type_id); + if (request->params.type_id.has_value()) + out.result = ExpandNode(db, working_files, *request->params.type_id); QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyExpand, out); } diff --git a/src/messages/text_document_code_action.cc b/src/messages/text_document_code_action.cc index 59efd9fd..1dd62ca4 100644 --- a/src/messages/text_document_code_action.cc +++ b/src/messages/text_document_code_action.cc @@ -73,7 +73,7 @@ optional GetImplementationFile(QueryDatabase* db, // Note: we ignore the definition if it is in the same file (ie, // possibly a header). if (func.def && func.def->extent) { - QueryFileId t = db->GetFileId(*func.def->extent); + Maybe t = db->GetFileId(*func.def->extent); if (t != file_id) return t; } @@ -84,7 +84,7 @@ optional GetImplementationFile(QueryDatabase* db, // Note: we ignore the definition if it is in the same file (ie, // possibly a header). if (var.def && var.def->extent) { - QueryFileId t = db->GetFileId(*var.def->extent); + Maybe t = db->GetFileId(*var.def->extent); if (t != file_id) return t; } diff --git a/src/messages/text_document_code_lens.cc b/src/messages/text_document_code_lens.cc index 94e0b084..529bb569 100644 --- a/src/messages/text_document_code_lens.cc +++ b/src/messages/text_document_code_lens.cc @@ -91,11 +91,13 @@ void AddCodeLens(const char* singular, optional range = GetLsRange(common->working_file, loc.range); if (!range) return; + Maybe file_id = common->db->GetFileId(loc); + if (!file_id) + return; code_lens.range = *range; code_lens.command = lsCommand(); code_lens.command->command = "cquery.showReferences"; - code_lens.command->arguments.uri = - GetLsDocumentUri(common->db, common->db->GetFileId(loc)); + code_lens.command->arguments.uri = GetLsDocumentUri(common->db, *file_id); code_lens.command->arguments.position = code_lens.range.start; // Add unique uses. diff --git a/src/messages/text_document_rename.cc b/src/messages/text_document_rename.cc index d39b4eab..f0a9e2a1 100644 --- a/src/messages/text_document_rename.cc +++ b/src/messages/text_document_rename.cc @@ -16,23 +16,23 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, if (!ls_location) continue; - QueryFileId file_id = db->GetFileId(use); - if (!file_id.HasValue()) + Maybe file_id = db->GetFileId(use); + if (!file_id.has_value()) continue; - if (path_to_edit.find(file_id) == path_to_edit.end()) { - path_to_edit[file_id] = lsTextDocumentEdit(); + if (path_to_edit.find(*file_id) == path_to_edit.end()) { + path_to_edit[*file_id] = lsTextDocumentEdit(); - QueryFile& file = db->files[file_id.id]; + QueryFile& file = db->files[file_id->id]; if (!file.def) continue; const std::string& path = file.def->path; - path_to_edit[file_id].textDocument.uri = + path_to_edit[*file_id].textDocument.uri = lsDocumentUri::FromPath(path); WorkingFile* working_file = working_files->GetFileByFilename(path); if (working_file) - path_to_edit[file_id].textDocument.version = + path_to_edit[*file_id].textDocument.version = working_file->version; } @@ -41,7 +41,7 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, edit.newText = new_text; // vscode complains if we submit overlapping text edits. - auto& edits = path_to_edit[file_id].edits; + auto& edits = path_to_edit[*file_id].edits; if (std::find(edits.begin(), edits.end(), edit) == edits.end()) edits.push_back(edit); } diff --git a/src/query.cc b/src/query.cc index 09d176d1..028b1a8d 100644 --- a/src/query.cc +++ b/src/query.cc @@ -129,9 +129,9 @@ void AddMergeableRange( } } -// Compares |previous| and |current|, adding all elements that are -// in |previous| but not |current| to |removed|, and all elements -// that are in |current| but not |previous| to |added|. +// Compares |previous| and |current|, adding all elements that are in |previous| +// but not |current| to |removed|, and all elements that are in |current| but +// not |previous| to |added|. // // Returns true iff |removed| or |added| are non-empty. template diff --git a/src/query.h b/src/query.h index 0bd1619c..a43c0b00 100644 --- a/src/query.h +++ b/src/query.h @@ -290,7 +290,7 @@ struct QueryDatabase { Maybe GetQueryFuncIdFromUsr(Usr usr); Maybe GetQueryVarIdFromUsr(Usr usr); - QueryFileId GetFileId(SymbolIdx ref) { + Maybe GetFileId(SymbolIdx ref) { switch (ref.kind) { case SymbolKind::Invalid: break; diff --git a/src/query_utils.cc b/src/query_utils.cc index d40b79a9..59fe3c45 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -379,10 +379,10 @@ optional GetLsLocation(QueryDatabase* db, WorkingFiles* working_files, Reference ref) { std::string path; - QueryFileId file_id = db->GetFileId(ref); - if (!file_id.HasValue()) + Maybe file_id = db->GetFileId(ref); + if (!file_id.has_value()) return nullopt; - lsDocumentUri uri = GetLsDocumentUri(db, file_id, &path); + lsDocumentUri uri = GetLsDocumentUri(db, *file_id, &path); optional range = GetLsRange(working_files->GetFileByFilename(path), ref.range); if (!range)