Browse Source

Language client: improved parsing of textDocument/documentSymbol

pipelines/235045657
eidheim 6 years ago
parent
commit
2630e3001f
  1. 32
      src/project.cc
  2. 24
      src/source_language_protocol.cc
  3. 1
      src/source_language_protocol.h

32
src/project.cc

@ -758,22 +758,38 @@ void Project::LanguageProtocol::show_symbols() {
else { else {
std::map<::LanguageProtocol::Location, std::string> locations_rows; std::map<::LanguageProtocol::Location, std::string> locations_rows;
std::promise<void> result_processed; std::promise<void> result_processed;
client->write_request(language_protocol_view, "textDocument/documentSymbol", R"("textDocument":{"uri":")" + language_protocol_view->uri + "\"}", [&result_processed, &locations_rows, locations](const boost::property_tree::ptree &result, bool error) { client->write_request(language_protocol_view, "textDocument/documentSymbol", R"("textDocument":{"uri":")" + language_protocol_view->uri + "\"}", [&result_processed, &locations_rows, locations, language_protocol_view](const boost::property_tree::ptree &result, bool error) {
if(!error) { if(!error) {
for(auto it = result.begin(); it != result.end(); ++it) { std::function<void(const boost::property_tree::ptree &ptee, const std::string &container)> parse_result = [&locations_rows, &parse_result, language_protocol_view](const boost::property_tree::ptree &pt, const std::string &container) {
for(auto it = pt.begin(); it != pt.end(); ++it) {
try { try {
::LanguageProtocol::Location location(it->second.get_child("location")); std::unique_ptr<::LanguageProtocol::Location> location;
std::string prefix;
auto container = it->second.get<std::string>("containerName", ""); auto location_pt = it->second.get_child_optional("location");
if(location_pt) {
location = std::make_unique<::LanguageProtocol::Location>(*location_pt);
std::string container = it->second.get<std::string>("containerName", "");
if(container == "null") if(container == "null")
container.clear(); container.clear();
auto row = std::to_string(location.range.start.line + 1) + ": " + (!container.empty() ? container + "::" : "") + "<b>" + it->second.get<std::string>("name") + "</b>"; if(!container.empty())
prefix = container + "::";
locations_rows.emplace(std::move(location), std::move(row)); }
else {
location = std::make_unique<::LanguageProtocol::Location>(language_protocol_view->file_path.string(), ::LanguageProtocol::Range(it->second.get_child("range")));
if(!container.empty())
prefix = container + "::";
}
auto row = std::to_string(location->range.start.line + 1) + ": " + prefix + "<b>" + it->second.get<std::string>("name") + "</b>";
locations_rows.emplace(std::move(*location), std::move(row));
auto children = it->second.get_child_optional("children");
if(children)
parse_result(*children, (!container.empty() ? container + "::" : "") + it->second.get<std::string>("name"));
} }
catch(...) { catch(...) {
} }
} }
};
parse_result(result, "");
} }
result_processed.set_value(); result_processed.set_value();
}); });

24
src/source_language_protocol.cc

@ -846,29 +846,39 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
std::promise<void> result_processed; std::promise<void> result_processed;
client->write_request(this, "textDocument/documentSymbol", R"("textDocument":{"uri":")" + uri + "\"}", [&result_processed, &methods](const boost::property_tree::ptree &result, bool error) { client->write_request(this, "textDocument/documentSymbol", R"("textDocument":{"uri":")" + uri + "\"}", [&result_processed, &methods](const boost::property_tree::ptree &result, bool error) {
if(!error) { if(!error) {
for(auto it = result.begin(); it != result.end(); ++it) { std::function<void(const boost::property_tree::ptree &ptee, const std::string &container)> parse_result = [&methods, &parse_result](const boost::property_tree::ptree &pt, const std::string &container) {
for(auto it = pt.begin(); it != pt.end(); ++it) {
try { try {
auto kind = it->second.get<int>("kind"); auto kind = it->second.get<int>("kind");
if(kind == 6 || kind == 9 || kind == 12) { if(kind == 6 || kind == 9 || kind == 12) {
std::unique_ptr<LanguageProtocol::Range> range; std::unique_ptr<LanguageProtocol::Range> range;
std::string container; std::string prefix;
auto location_pt = it->second.get_child_optional("location"); auto location_pt = it->second.get_child_optional("location");
if(location_pt) { if(location_pt) {
LanguageProtocol::Location location(*location_pt); LanguageProtocol::Location location(*location_pt);
container = it->second.get<std::string>("containerName", ""); range = std::make_unique<LanguageProtocol::Range>(location.range);
std::string container = it->second.get<std::string>("containerName", "");
if(container == "null") if(container == "null")
container.clear(); container.clear();
range = std::make_unique<LanguageProtocol::Range>(location.range); if(!container.empty())
prefix = container + "::";
} }
else else {
range = std::make_unique<LanguageProtocol::Range>(it->second.get_child("range")); range = std::make_unique<LanguageProtocol::Range>(it->second.get_child("range"));
if(!container.empty())
methods.emplace_back(Offset(range->start.line, range->start.character), std::to_string(range->start.line + 1) + ": " + (!container.empty() ? container + "::" : "") + "<b>" + it->second.get<std::string>("name") + "</b>"); prefix = container + "::";
} }
methods.emplace_back(Offset(range->start.line, range->start.character), std::to_string(range->start.line + 1) + ": " + prefix + "<b>" + it->second.get<std::string>("name") + "</b>");
}
auto children = it->second.get_child_optional("children");
if(children)
parse_result(*children, (!container.empty() ? container + "::" : "") + it->second.get<std::string>("name"));
} }
catch(...) { catch(...) {
} }
} }
};
parse_result(result, "");
} }
result_processed.set_value(); result_processed.set_value();
}); });

1
src/source_language_protocol.h

@ -44,6 +44,7 @@ namespace LanguageProtocol {
class Location { class Location {
public: public:
Location(const boost::property_tree::ptree &pt, std::string file_ = {}); Location(const boost::property_tree::ptree &pt, std::string file_ = {});
Location(std::string _file, Range _range) : file(std::move(_file)), range(std::move(_range)) {}
std::string file; std::string file;
Range range; Range range;

Loading…
Cancel
Save