From 411d49951dbb6b6e84f621ce5739cb00d12a66c4 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 23 Feb 2018 15:27:21 -0800 Subject: [PATCH] Reduce structs in language_server_api.h and clarify query_utils.h --- src/clang_complete.h | 2 +- src/code_complete_cache.h | 4 +- src/import_pipeline.cc | 21 +++ src/include_complete.h | 2 +- src/language_server_api.cc | 8 - src/language_server_api.h | 163 ------------------ src/language_server_api_completion.h | 138 +++++++++++++++ src/lex_utils.cc | 25 --- src/lex_utils.h | 3 - src/message_handler.cc | 13 ++ src/messages/cquery_base.cc | 4 +- src/messages/cquery_derived.cc | 4 +- src/messages/cquery_vars.cc | 6 +- src/messages/text_document_code_lens.cc | 8 +- src/messages/text_document_completion.cc | 25 +++ src/messages/text_document_definition.cc | 4 +- .../text_document_document_highlight.cc | 2 +- src/messages/text_document_references.cc | 14 +- src/messages/text_document_rename.cc | 2 +- src/messages/workspace_symbol.cc | 2 +- src/query_utils.cc | 26 +-- src/query_utils.h | 13 +- 22 files changed, 244 insertions(+), 245 deletions(-) create mode 100644 src/language_server_api_completion.h diff --git a/src/clang_complete.h b/src/clang_complete.h index 48124d3e..806a729e 100644 --- a/src/clang_complete.h +++ b/src/clang_complete.h @@ -3,7 +3,7 @@ #include "atomic_object.h" #include "clang_index.h" #include "clang_translation_unit.h" -#include "language_server_api.h" +#include "language_server_api_completion.h" #include "lru_cache.h" #include "project.h" #include "threaded_queue.h" diff --git a/src/code_complete_cache.h b/src/code_complete_cache.h index 716b6704..3ae7bad8 100644 --- a/src/code_complete_cache.h +++ b/src/code_complete_cache.h @@ -1,6 +1,6 @@ #pragma once -#include "language_server_api.h" +#include "language_server_api_completion.h" #include @@ -19,4 +19,4 @@ struct CodeCompleteCache { void WithLock(std::function action); bool IsCacheValid(lsTextDocumentPositionParams position); -}; \ No newline at end of file +}; diff --git a/src/import_pipeline.cc b/src/import_pipeline.cc index a02f82eb..c530524e 100644 --- a/src/import_pipeline.cc +++ b/src/import_pipeline.cc @@ -23,6 +23,27 @@ namespace { +struct Out_Progress : public lsOutMessage { + struct Params { + int indexRequestCount = 0; + int doIdMapCount = 0; + int loadPreviousIndexCount = 0; + int onIdMappedCount = 0; + int onIndexedCount = 0; + int activeThreads = 0; + }; + std::string method = "$cquery/progress"; + Params params; +}; +MAKE_REFLECT_STRUCT(Out_Progress::Params, + indexRequestCount, + doIdMapCount, + loadPreviousIndexCount, + onIdMappedCount, + onIndexedCount, + activeThreads); +MAKE_REFLECT_STRUCT(Out_Progress, jsonrpc, method, params); + struct IModificationTimestampFetcher { virtual ~IModificationTimestampFetcher() = default; virtual optional GetModificationTime(const std::string& path) = 0; diff --git a/src/include_complete.h b/src/include_complete.h index 31db9f2a..5d97ec6f 100644 --- a/src/include_complete.h +++ b/src/include_complete.h @@ -1,6 +1,6 @@ #pragma once -#include "language_server_api.h" +#include "language_server_api_completion.h" #include #include diff --git a/src/language_server_api.cc b/src/language_server_api.cc index 1288b658..f033d474 100644 --- a/src/language_server_api.cc +++ b/src/language_server_api.cc @@ -322,14 +322,6 @@ bool lsTextEdit::operator==(const lsTextEdit& that) { return range == that.range && newText == that.newText; } -const std::string& lsCompletionItem::InsertedContent() const { - if (textEdit) - return textEdit->newText; - if (!insertText.empty()) - return insertText; - return label; -} - std::string Out_ShowLogMessage::method() { if (display_type == DisplayType::Log) return "window/logMessage"; diff --git a/src/language_server_api.h b/src/language_server_api.h index 09a79e68..35e60877 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -247,136 +247,6 @@ struct lsTextEdit { }; MAKE_REFLECT_STRUCT(lsTextEdit, range, newText); -// Defines whether the insert text in a completion item should be interpreted as -// plain text or a snippet. -enum class lsInsertTextFormat { - // The primary text to be inserted is treated as a plain string. - PlainText = 1, - - // The primary text to be inserted is treated as a snippet. - // - // A snippet can define tab stops and placeholders with `$1`, `$2` - // and `${3:foo}`. `$0` defines the final tab stop, it defaults to - // the end of the snippet. Placeholders with equal identifiers are linked, - // that is typing in one will update others too. - // - // See also: - // https://github.com/Microsoft/vscode/blob/master/src/vs/editor/contrib/snippet/common/snippet.md - Snippet = 2 -}; -MAKE_REFLECT_TYPE_PROXY(lsInsertTextFormat); - -// The kind of a completion entry. -enum class lsCompletionItemKind { - Text = 1, - Method = 2, - Function = 3, - Constructor = 4, - Field = 5, - Variable = 6, - Class = 7, - Interface = 8, - Module = 9, - Property = 10, - Unit = 11, - Value = 12, - Enum = 13, - Keyword = 14, - Snippet = 15, - Color = 16, - File = 17, - Reference = 18, - Folder = 19, - EnumMember = 20, - Constant = 21, - Struct = 22, - Event = 23, - Operator = 24, - TypeParameter = 25, -}; -MAKE_REFLECT_TYPE_PROXY(lsCompletionItemKind); - -struct lsCompletionItem { - // A set of function parameters. Used internally for signature help. Not sent - // to vscode. - std::vector parameters_; - - // The label of this completion item. By default - // also the text that is inserted when selecting - // this completion. - std::string label; - - // The kind of this completion item. Based of the kind - // an icon is chosen by the editor. - lsCompletionItemKind kind = lsCompletionItemKind::Text; - - // A human-readable string with additional information - // about this item, like type or symbol information. - std::string detail; - - // A human-readable string that represents a doc-comment. - optional documentation; - - // Internal information to order candidates. - bool found_; - std::string::size_type skip_; - unsigned priority_; - - // Use <> or "" by default as include path. - bool use_angle_brackets_ = false; - - // A string that shoud be used when comparing this item - // with other items. When `falsy` the label is used. - std::string sortText; - - // A string that should be used when filtering a set of - // completion items. When `falsy` the label is used. - optional filterText; - - // A string that should be inserted a document when selecting - // this completion. When `falsy` the label is used. - std::string insertText; - - // The format of the insert text. The format applies to both the `insertText` - // property and the `newText` property of a provided `textEdit`. - lsInsertTextFormat insertTextFormat = lsInsertTextFormat::PlainText; - - // An edit which is applied to a document when selecting this completion. When - // an edit is provided the value of `insertText` is ignored. - // - // *Note:* The range of the edit must be a single line range and it must - // contain the position at which completion has been requested. - optional textEdit; - - // An optional array of additional text edits that are applied when - // selecting this completion. Edits must not overlap with the main edit - // nor with themselves. - // std::vector additionalTextEdits; - - // An optional command that is executed *after* inserting this completion. - // *Note* that additional modifications to the current document should be - // described with the additionalTextEdits-property. Command command; - - // An data entry field that is preserved on a completion item between - // a completion and a completion resolve request. - // data ? : any - - // Use this helper to figure out what content the completion item will insert - // into the document, as it could live in either |textEdit|, |insertText|, or - // |label|. - const std::string& InsertedContent() const; -}; -MAKE_REFLECT_STRUCT(lsCompletionItem, - label, - kind, - detail, - documentation, - sortText, - insertText, - filterText, - insertTextFormat, - textEdit); - struct lsTextDocumentItem { // The text document's URI. lsDocumentUri uri; @@ -589,39 +459,6 @@ void Reflect(TVisitor& visitor, Out_ShowLogMessage& value) { REFLECT_MEMBER_END(); } -struct Out_Progress : public lsOutMessage { - struct Params { - int indexRequestCount = 0; - int doIdMapCount = 0; - int loadPreviousIndexCount = 0; - int onIdMappedCount = 0; - int onIndexedCount = 0; - int activeThreads = 0; - }; - std::string method = "$cquery/progress"; - Params params; -}; -MAKE_REFLECT_STRUCT(Out_Progress::Params, - indexRequestCount, - doIdMapCount, - loadPreviousIndexCount, - onIdMappedCount, - onIndexedCount, - activeThreads); -MAKE_REFLECT_STRUCT(Out_Progress, jsonrpc, method, params); - -struct Out_CquerySetInactiveRegion - : public lsOutMessage { - struct Params { - lsDocumentUri uri; - std::vector inactiveRegions; - }; - std::string method = "$cquery/setInactiveRegions"; - Params params; -}; -MAKE_REFLECT_STRUCT(Out_CquerySetInactiveRegion::Params, uri, inactiveRegions); -MAKE_REFLECT_STRUCT(Out_CquerySetInactiveRegion, jsonrpc, method, params); - struct Out_LocationList : public lsOutMessage { lsRequestId id; std::vector result; diff --git a/src/language_server_api_completion.h b/src/language_server_api_completion.h new file mode 100644 index 00000000..93aac29a --- /dev/null +++ b/src/language_server_api_completion.h @@ -0,0 +1,138 @@ +#pragma once +#include "language_server_api.h" + +// The kind of a completion entry. +enum class lsCompletionItemKind { + Text = 1, + Method = 2, + Function = 3, + Constructor = 4, + Field = 5, + Variable = 6, + Class = 7, + Interface = 8, + Module = 9, + Property = 10, + Unit = 11, + Value = 12, + Enum = 13, + Keyword = 14, + Snippet = 15, + Color = 16, + File = 17, + Reference = 18, + Folder = 19, + EnumMember = 20, + Constant = 21, + Struct = 22, + Event = 23, + Operator = 24, + TypeParameter = 25, +}; +MAKE_REFLECT_TYPE_PROXY(lsCompletionItemKind); + +// Defines whether the insert text in a completion item should be interpreted as +// plain text or a snippet. +enum class lsInsertTextFormat { + // The primary text to be inserted is treated as a plain string. + PlainText = 1, + + // The primary text to be inserted is treated as a snippet. + // + // A snippet can define tab stops and placeholders with `$1`, `$2` + // and `${3:foo}`. `$0` defines the final tab stop, it defaults to + // the end of the snippet. Placeholders with equal identifiers are linked, + // that is typing in one will update others too. + // + // See also: + // https://github.com/Microsoft/vscode/blob/master/src/vs/editor/contrib/snippet/common/snippet.md + Snippet = 2 +}; +MAKE_REFLECT_TYPE_PROXY(lsInsertTextFormat); + +struct lsCompletionItem { + // A set of function parameters. Used internally for signature help. Not sent + // to vscode. + std::vector parameters_; + + // The label of this completion item. By default + // also the text that is inserted when selecting + // this completion. + std::string label; + + // The kind of this completion item. Based of the kind + // an icon is chosen by the editor. + lsCompletionItemKind kind = lsCompletionItemKind::Text; + + // A human-readable string with additional information + // about this item, like type or symbol information. + std::string detail; + + // A human-readable string that represents a doc-comment. + optional documentation; + + // Internal information to order candidates. + bool found_; + std::string::size_type skip_; + unsigned priority_; + + // Use <> or "" by default as include path. + bool use_angle_brackets_ = false; + + // A string that shoud be used when comparing this item + // with other items. When `falsy` the label is used. + std::string sortText; + + // A string that should be used when filtering a set of + // completion items. When `falsy` the label is used. + optional filterText; + + // A string that should be inserted a document when selecting + // this completion. When `falsy` the label is used. + std::string insertText; + + // The format of the insert text. The format applies to both the `insertText` + // property and the `newText` property of a provided `textEdit`. + lsInsertTextFormat insertTextFormat = lsInsertTextFormat::PlainText; + + // An edit which is applied to a document when selecting this completion. When + // an edit is provided the value of `insertText` is ignored. + // + // *Note:* The range of the edit must be a single line range and it must + // contain the position at which completion has been requested. + optional textEdit; + + // An optional array of additional text edits that are applied when + // selecting this completion. Edits must not overlap with the main edit + // nor with themselves. + // std::vector additionalTextEdits; + + // An optional command that is executed *after* inserting this completion. + // *Note* that additional modifications to the current document should be + // described with the additionalTextEdits-property. Command command; + + // An data entry field that is preserved on a completion item between + // a completion and a completion resolve request. + // data ? : any + + // Use this helper to figure out what content the completion item will insert + // into the document, as it could live in either |textEdit|, |insertText|, or + // |label|. + const std::string& InsertedContent() const { + if (textEdit) + return textEdit->newText; + if (!insertText.empty()) + return insertText; + return label; + } +}; +MAKE_REFLECT_STRUCT(lsCompletionItem, + label, + kind, + detail, + documentation, + sortText, + insertText, + filterText, + insertTextFormat, + textEdit); diff --git a/src/lex_utils.cc b/src/lex_utils.cc index 591d6904..298a3f41 100644 --- a/src/lex_utils.cc +++ b/src/lex_utils.cc @@ -63,31 +63,6 @@ ParseIncludeLineResult ParseIncludeLine(const std::string& line) { return {ok, text, match}; } -void DecorateIncludePaths(const std::smatch& match, - std::vector* items) { - std::string spaces_after_include = " "; - if (match[3].compare("include") == 0 && match[5].length()) - spaces_after_include = match[4].str(); - - std::string prefix = - match[1].str() + '#' + match[2].str() + "include" + spaces_after_include; - std::string suffix = match[7].str(); - - for (lsCompletionItem& item : *items) { - char quote0, quote1; - if (match[5].compare("<") == 0 || - (match[5].length() == 0 && item.use_angle_brackets_)) - quote0 = '<', quote1 = '>'; - else - quote0 = quote1 = '"'; - - item.textEdit->newText = - prefix + quote0 + item.textEdit->newText + quote1 + suffix; - item.label = prefix + quote0 + item.label + quote1 + suffix; - item.filterText = nullopt; - } -} - // TODO: eliminate |line_number| param. optional ExtractQuotedRange(int line_number, const std::string& line) { // Find starting and ending quote. diff --git a/src/lex_utils.h b/src/lex_utils.h index ac5ccc20..09192763 100644 --- a/src/lex_utils.h +++ b/src/lex_utils.h @@ -23,9 +23,6 @@ struct ParseIncludeLineResult { ParseIncludeLineResult ParseIncludeLine(const std::string& line); -void DecorateIncludePaths(const std::smatch& match, - std::vector* items); - // TODO: eliminate |line_number| param. optional ExtractQuotedRange(int line_number, const std::string& line); diff --git a/src/message_handler.cc b/src/message_handler.cc index 16b04b67..4cf6e718 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -11,6 +11,19 @@ #include namespace { + +struct Out_CquerySetInactiveRegion + : public lsOutMessage { + struct Params { + lsDocumentUri uri; + std::vector inactiveRegions; + }; + std::string method = "$cquery/setInactiveRegions"; + Params params; +}; +MAKE_REFLECT_STRUCT(Out_CquerySetInactiveRegion::Params, uri, inactiveRegions); +MAKE_REFLECT_STRUCT(Out_CquerySetInactiveRegion, jsonrpc, method, params); + struct ScanLineEvent { lsPosition pos; lsPosition end_pos; // Second key when there is a tie for insertion events. diff --git a/src/messages/cquery_base.cc b/src/messages/cquery_base.cc index 0b8d784a..94588cc1 100644 --- a/src/messages/cquery_base.cc +++ b/src/messages/cquery_base.cc @@ -28,13 +28,13 @@ struct CqueryBaseHandler : BaseMessageHandler { if (sym.kind == SymbolKind::Type) { if (const auto* def = db->GetType(sym).AnyDef()) out.result = - GetLsLocationExs(db, working_files, ToUses(db, def->parents), + GetLsLocationExs(db, working_files, GetDeclarations(db, def->parents), config->xref.container, config->xref.maxNum); break; } else if (sym.kind == SymbolKind::Func) { if (const auto* def = db->GetFunc(sym).AnyDef()) out.result = - GetLsLocationExs(db, working_files, ToUses(db, def->base), + GetLsLocationExs(db, working_files, GetDeclarations(db, def->base), config->xref.container, config->xref.maxNum); break; } diff --git a/src/messages/cquery_derived.cc b/src/messages/cquery_derived.cc index 65aee1b1..6e056584 100644 --- a/src/messages/cquery_derived.cc +++ b/src/messages/cquery_derived.cc @@ -28,13 +28,13 @@ struct CqueryDerivedHandler : BaseMessageHandler { if (sym.kind == SymbolKind::Type) { QueryType& type = db->GetType(sym); out.result = - GetLsLocationExs(db, working_files, ToUses(db, type.derived), + GetLsLocationExs(db, working_files, GetDeclarations(db, type.derived), config->xref.container, config->xref.maxNum); break; } else if (sym.kind == SymbolKind::Func) { QueryFunc& func = db->GetFunc(sym); out.result = - GetLsLocationExs(db, working_files, ToUses(db, func.derived), + GetLsLocationExs(db, working_files, GetDeclarations(db, func.derived), config->xref.container, config->xref.maxNum); break; } diff --git a/src/messages/cquery_vars.cc b/src/messages/cquery_vars.cc index 99657fad..1f6b9c56 100644 --- a/src/messages/cquery_vars.cc +++ b/src/messages/cquery_vars.cc @@ -38,9 +38,9 @@ struct CqueryVarsHandler : BaseMessageHandler { // fallthrough case SymbolKind::Type: { QueryType& type = db->types[id.id]; - out.result = - GetLsLocationExs(db, working_files, ToUses(db, type.instances), - config->xref.container, config->xref.maxNum); + out.result = GetLsLocationExs( + db, working_files, GetDeclarations(db, type.instances), + config->xref.container, config->xref.maxNum); break; } } diff --git a/src/messages/text_document_code_lens.cc b/src/messages/text_document_code_lens.cc index 05231e86..a6aefa88 100644 --- a/src/messages/text_document_code_lens.cc +++ b/src/messages/text_document_code_lens.cc @@ -160,9 +160,9 @@ struct TextDocumentCodeLensHandler AddCodeLens("ref", "refs", &common, OffsetStartColumn(use, 0), type.uses, true /*force_display*/); AddCodeLens("derived", "derived", &common, OffsetStartColumn(use, 1), - ToUses(db, type.derived), false /*force_display*/); + GetDeclarations(db, type.derived), false /*force_display*/); AddCodeLens("var", "vars", &common, OffsetStartColumn(use, 2), - ToUses(db, type.instances), false /*force_display*/); + GetDeclarations(db, type.instances), false /*force_display*/); break; } case SymbolKind::Func: { @@ -210,7 +210,7 @@ struct TextDocumentCodeLensHandler AddCodeLens("derived", "derived", &common, OffsetStartColumn(use, offset++), - ToUses(db, func.derived), false /*force_display*/); + GetDeclarations(db, func.derived), false /*force_display*/); // "Base" if (def->base.size() == 1) { @@ -237,7 +237,7 @@ struct TextDocumentCodeLensHandler } } else { AddCodeLens("base", "base", &common, OffsetStartColumn(use, 1), - ToUses(db, def->base), false /*force_display*/); + GetDeclarations(db, def->base), false /*force_display*/); } break; diff --git a/src/messages/text_document_completion.cc b/src/messages/text_document_completion.cc index 5159f91b..5a2706ff 100644 --- a/src/messages/text_document_completion.cc +++ b/src/messages/text_document_completion.cc @@ -81,6 +81,31 @@ bool CompareLsCompletionItem(const lsCompletionItem& lhs, return *lhs.filterText < *rhs.filterText; } +void DecorateIncludePaths(const std::smatch& match, + std::vector* items) { + std::string spaces_after_include = " "; + if (match[3].compare("include") == 0 && match[5].length()) + spaces_after_include = match[4].str(); + + std::string prefix = + match[1].str() + '#' + match[2].str() + "include" + spaces_after_include; + std::string suffix = match[7].str(); + + for (lsCompletionItem& item : *items) { + char quote0, quote1; + if (match[5].compare("<") == 0 || + (match[5].length() == 0 && item.use_angle_brackets_)) + quote0 = '<', quote1 = '>'; + else + quote0 = quote1 = '"'; + + item.textEdit->newText = + prefix + quote0 + item.textEdit->newText + quote1 + suffix; + item.label = prefix + quote0 + item.label + quote1 + suffix; + item.filterText = nullopt; + } +} + template char* tofixedbase64(T input, char* out) { const char* digits = diff --git a/src/messages/text_document_definition.cc b/src/messages/text_document_definition.cc index 9a0b4447..825a0c77 100644 --- a/src/messages/text_document_definition.cc +++ b/src/messages/text_document_definition.cc @@ -27,7 +27,7 @@ MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result); std::vector GetGotoDefinitionTargets(QueryDatabase* db, SymbolRef sym) { switch (sym.kind) { case SymbolKind::Var: { - std::vector ret = GetDeclarationsOfSymbolForGotoDefinition(db, sym); + std::vector ret = GetNonDefDeclarations(db, sym); // If there is no declaration, jump the its type. if (ret.empty()) { for (auto& def : db->GetVar(sym).def) @@ -42,7 +42,7 @@ std::vector GetGotoDefinitionTargets(QueryDatabase* db, SymbolRef sym) { return ret; } default: - return GetDeclarationsOfSymbolForGotoDefinition(db, sym); + return GetNonDefDeclarations(db, sym); } } diff --git a/src/messages/text_document_document_highlight.cc b/src/messages/text_document_document_highlight.cc index 3f6609c7..1183e1d5 100644 --- a/src/messages/text_document_document_highlight.cc +++ b/src/messages/text_document_document_highlight.cc @@ -39,7 +39,7 @@ struct TextDocumentDocumentHighlightHandler for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { // Found symbol. Return references to highlight. - EachUse(db, sym, true, [&](Use use) { + EachOccurrence(db, sym, true, [&](Use use) { if (use.file != file_id) return; if (optional ls_loc = diff --git a/src/messages/text_document_references.cc b/src/messages/text_document_references.cc index 72e1d5ca..8ad45adc 100644 --- a/src/messages/text_document_references.cc +++ b/src/messages/text_document_references.cc @@ -57,13 +57,13 @@ struct TextDocumentReferencesHandler for (const SymbolRef& sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { // Found symbol. Return references. - EachUse(db, sym, request->params.context.includeDeclaration, - [&](Use use) { - if (use.role & request->params.context.role) - if (optional ls_loc = GetLsLocationEx( - db, working_files, use, config->xref.container)) - out.result.push_back(*ls_loc); - }); + EachOccurrence( + db, sym, request->params.context.includeDeclaration, [&](Use use) { + if (use.role & request->params.context.role) + if (optional ls_loc = GetLsLocationEx( + db, working_files, use, config->xref.container)) + out.result.push_back(*ls_loc); + }); break; } diff --git a/src/messages/text_document_rename.cc b/src/messages/text_document_rename.cc index ecc54cf8..719eddfe 100644 --- a/src/messages/text_document_rename.cc +++ b/src/messages/text_document_rename.cc @@ -10,7 +10,7 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, const std::string& new_text) { std::unordered_map path_to_edit; - EachUse(db, sym, true, [&](Use use) { + EachOccurrence(db, sym, true, [&](Use use) { optional ls_location = GetLsLocation(db, working_files, use); if (!ls_location) return; diff --git a/src/messages/workspace_symbol.cc b/src/messages/workspace_symbol.cc index c194dd3f..2e0ba211 100644 --- a/src/messages/workspace_symbol.cc +++ b/src/messages/workspace_symbol.cc @@ -28,7 +28,7 @@ bool InsertSymbolIntoResult(QueryDatabase* db, if (location) loc = *location; else { - auto decls = GetDeclarationsOfSymbolForGotoDefinition(db, symbol); + auto decls = GetNonDefDeclarations(db, symbol); if (decls.empty()) return false; loc = decls[0]; diff --git a/src/query_utils.cc b/src/query_utils.cc index 86667a12..f0ff6a77 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -19,8 +19,8 @@ int ComputeRangeSize(const Range& range) { } template -std::vector ToUsesHelper(std::vector& entities, - const std::vector>& ids) { +std::vector GetDeclarations(std::vector& entities, + const std::vector>& ids) { std::vector ret; ret.reserve(ids.size()); for (auto id : ids) { @@ -87,22 +87,23 @@ Maybe GetDeclarationFileForSymbol(QueryDatabase* db, return nullopt; } -std::vector ToUses(QueryDatabase* db, - const std::vector& ids) { - return ToUsesHelper(db->funcs, ids); +std::vector GetDeclarations(QueryDatabase* db, + const std::vector& ids) { + return GetDeclarations(db->funcs, ids); } -std::vector ToUses(QueryDatabase* db, - const std::vector& ids) { - return ToUsesHelper(db->types, ids); +std::vector GetDeclarations(QueryDatabase* db, + const std::vector& ids) { + return GetDeclarations(db->types, ids); } -std::vector ToUses(QueryDatabase* db, const std::vector& ids) { - return ToUsesHelper(db->vars, ids); +std::vector GetDeclarations(QueryDatabase* db, + const std::vector& ids) { + return GetDeclarations(db->vars, ids); } -std::vector GetDeclarationsOfSymbolForGotoDefinition(QueryDatabase* db, - SymbolIdx sym) { +std::vector GetNonDefDeclarations(QueryDatabase* db, + SymbolIdx sym) { switch (sym.kind) { case SymbolKind::Func: return db->GetFunc(sym).declarations; @@ -334,7 +335,6 @@ optional GetSymbolInfo(QueryDatabase* db, return nullopt; } -// TODO Sort only by range length, not |kind| or |idx| std::vector FindSymbolsAtLocation(WorkingFile* working_file, QueryFile* file, lsPosition position) { diff --git a/src/query_utils.h b/src/query_utils.h index 3053dcae..fc107d87 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -10,12 +10,13 @@ Maybe GetDefinitionExtentOfSymbol(QueryDatabase* db, SymbolIdx sym); Maybe GetDeclarationFileForSymbol(QueryDatabase* db, SymbolIdx sym); -std::vector ToUses(QueryDatabase* db, const std::vector& ids); -std::vector ToUses(QueryDatabase* db, const std::vector& ids); -std::vector ToUses(QueryDatabase* db, const std::vector& ids); +// Get defining declaration (if exists) or an arbitrary declaration (otherwise) for each id. +std::vector GetDeclarations(QueryDatabase* db, const std::vector& ids); +std::vector GetDeclarations(QueryDatabase* db, const std::vector& ids); +std::vector GetDeclarations(QueryDatabase* db, const std::vector& ids); -std::vector GetDeclarationsOfSymbolForGotoDefinition(QueryDatabase* db, - SymbolIdx sym); +// Get non-defining declarations. +std::vector GetNonDefDeclarations(QueryDatabase* db, SymbolIdx sym); bool HasCallersOnSelfOrBaseOrDerived(QueryDatabase* db, QueryFunc& root); std::vector GetCallersForAllBaseFunctions(QueryDatabase* db, @@ -84,7 +85,7 @@ void EachEntityDef(QueryDatabase* db, SymbolIdx sym, Fn&& fn) { } template -void EachUse(QueryDatabase* db, SymbolIdx sym, bool include_decl, Fn&& fn) { +void EachOccurrence(QueryDatabase* db, SymbolIdx sym, bool include_decl, Fn&& fn) { WithEntity(db, sym, [&](const auto& entity) { for (Use use : entity.uses) fn(use);