Browse Source

Find Symbol now always uses ctags

pipelines/280567345
eidheim 5 years ago
parent
commit
cd6af96e0e
  1. 140
      src/project.cpp
  2. 3
      src/project.hpp

140
src/project.cpp

@ -15,7 +15,6 @@
#include "info.hpp"
#include "snippets.hpp"
#include "source_clang.hpp"
#include "source_language_protocol.hpp"
#include "usages_clang.hpp"
#include <future>
@ -675,145 +674,6 @@ void Project::LLDB::debug_write(const std::string &buffer) {
}
#endif
void Project::LanguageProtocol::show_symbols() {
auto project_path = std::make_shared<boost::filesystem::path>(build->project_path);
auto view = Notebook::get().get_current_view();
auto language_protocol_view = dynamic_cast<Source::LanguageProtocolView *>(view);
if(project_path->empty()) {
if(language_protocol_view)
*project_path = language_protocol_view->file_path.parent_path();
else {
Info::get().print("Could not find project folder");
return;
}
}
auto language_id = get_language_id();
auto executable_name = language_id + "-language-server";
if(filesystem::find_executable(executable_name).empty())
return Base::show_symbols();
auto client = ::LanguageProtocol::Client::get(language_protocol_view ? language_protocol_view->file_path : *project_path, language_id);
auto capabilities = client->initialize(language_protocol_view);
if(!capabilities.workspace_symbol && !(capabilities.document_symbol && language_protocol_view))
return Base::show_symbols();
if(view)
SelectionDialog::create(view, true, true);
else
SelectionDialog::create(true, true);
SelectionDialog::get()->on_hide = [] {
SelectionDialog::get()->on_search_entry_changed = nullptr; // To delete client object
};
auto locations = std::make_shared<std::vector<std::pair<::LanguageProtocol::Location, std::string>>>();
if(capabilities.workspace_symbol) {
SelectionDialog::get()->on_search_entry_changed = [client, project_path, locations](const std::string &text) {
if(text.size() > 1)
return;
else {
locations->clear();
SelectionDialog::get()->erase_rows();
if(text.empty())
return;
}
std::promise<void> result_processed;
client->write_request(nullptr, "workspace/symbol", R"("query":")" + text + '"', [&result_processed, locations, project_path](const boost::property_tree::ptree &result, bool error) {
if(!error) {
for(auto it = result.begin(); it != result.end(); ++it) {
try {
::LanguageProtocol::Location location(it->second.get_child("location"));
if(filesystem::file_in_path(location.file, *project_path)) {
auto container = it->second.get<std::string>("containerName", "");
if(container == "null")
container.clear();
auto row = filesystem::get_relative_path(location.file, *project_path).string() + ':' + std::to_string(location.range.start.line + 1) + ": " + (!container.empty() ? Glib::Markup::escape_text(container) + "::" : "") + "<b>" + Glib::Markup::escape_text(it->second.get<std::string>("name")) + "</b>";
locations->emplace_back(std::make_pair(std::move(location), std::move(row)));
}
}
catch(...) {
}
}
}
result_processed.set_value();
});
result_processed.get_future().get();
std::sort(locations->begin(), locations->end());
for(auto &location : *locations) {
SelectionDialog::get()->add_row(location.second);
location.second.clear();
}
};
}
else {
std::promise<void> result_processed;
client->write_request(language_protocol_view, "textDocument/documentSymbol", R"("textDocument":{"uri":")" + language_protocol_view->uri + "\"}", [&result_processed, locations, language_protocol_view](const boost::property_tree::ptree &result, bool error) {
if(!error) {
std::function<void(const boost::property_tree::ptree &ptee, const std::string &container)> parse_result = [locations, &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 {
std::unique_ptr<::LanguageProtocol::Location> location;
std::string prefix;
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")
container.clear();
if(!container.empty())
prefix = container + "::";
}
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) + ": " + Glib::Markup::escape_text(prefix) + "<b>" + Glib::Markup::escape_text(it->second.get<std::string>("name")) + "</b>";
locations->emplace_back(std::make_pair(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(...) {
}
}
};
parse_result(result, "");
}
result_processed.set_value();
});
result_processed.get_future().get();
std::sort(locations->begin(), locations->end());
for(auto &location : *locations) {
SelectionDialog::get()->add_row(location.second);
location.second.clear();
}
}
SelectionDialog::get()->on_select = [locations](unsigned int index, const std::string &text, bool hide_window) {
auto &location = (*locations)[index].first;
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file, ec))
return;
if(Notebook::get().open(location.file)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(location.range.start.line, location.range.start.character);
view->scroll_to_cursor_delayed(true, false);
}
};
if(view)
view->hide_tooltips();
SelectionDialog::get()->show();
}
std::pair<std::string, std::string> Project::Clang::get_run_arguments() {
auto build_path = build->get_default_path();
if(build_path.empty())

3
src/project.hpp

@ -59,7 +59,7 @@ namespace Project {
virtual void compile_and_run();
virtual void recreate_build();
virtual void show_symbols();
void show_symbols();
virtual std::pair<std::string, std::string> debug_get_run_arguments();
virtual Project::DebugOptions *debug_get_options() { return nullptr; }
@ -105,7 +105,6 @@ namespace Project {
class LanguageProtocol : public virtual Base {
public:
virtual std::string get_language_id() = 0;
void show_symbols() override;
};
class Clang : public LLDB {

Loading…
Cancel
Save