Browse Source

Language protocol: now sorts locations received from server, and added some support for newer lsp specification

merge-requests/398/head
eidheim 7 years ago
parent
commit
232e9b4876
  1. 30
      src/project.cc
  2. 13
      src/source_language_protocol.cc
  3. 25
      src/source_language_protocol.h

30
src/project.cc

@ -715,9 +715,9 @@ void Project::LanguageProtocol::show_symbols() {
if(text.empty()) if(text.empty())
return; return;
} }
std::vector<std::string> rows; std::map<::LanguageProtocol::Location, std::string> locations_rows;
std::promise<void> result_processed; std::promise<void> result_processed;
client->write_request(nullptr, "workspace/symbol", R"("query":")" + text + '"', [&result_processed, &rows, locations, project_path](const boost::property_tree::ptree &result, bool error) { client->write_request(nullptr, "workspace/symbol", R"("query":")" + text + '"', [&result_processed, &locations_rows, locations, project_path](const boost::property_tree::ptree &result, bool error) {
if(!error) { if(!error) {
for(auto it = result.begin(); it != result.end(); ++it) { for(auto it = result.begin(); it != result.end(); ++it) {
try { try {
@ -731,8 +731,7 @@ void Project::LanguageProtocol::show_symbols() {
row += container_name + ':'; row += container_name + ':';
row += std::to_string(location.range.start.line + 1) + ": <b>" + it->second.get<std::string>("name") + "</b>"; row += std::to_string(location.range.start.line + 1) + ": <b>" + it->second.get<std::string>("name") + "</b>";
locations->emplace_back(std::move(location)); locations_rows.emplace(std::move(location), std::move(row));
rows.emplace_back(std::move(row));
} }
} }
catch(...) { catch(...) {
@ -742,14 +741,18 @@ void Project::LanguageProtocol::show_symbols() {
result_processed.set_value(); result_processed.set_value();
}); });
result_processed.get_future().get(); result_processed.get_future().get();
for(auto &row : rows)
SelectionDialog::get()->add_row(row); locations->reserve(locations_rows.size());
for(auto &location_row : locations_rows) {
locations->emplace_back(std::move(location_row.first));
SelectionDialog::get()->add_row(location_row.second);
}
}; };
} }
else { else {
std::vector<std::string> 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, &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](const boost::property_tree::ptree &result, bool error) {
if(!error) { if(!error) {
for(auto it = result.begin(); it != result.end(); ++it) { for(auto it = result.begin(); it != result.end(); ++it) {
try { try {
@ -761,8 +764,7 @@ void Project::LanguageProtocol::show_symbols() {
row += container_name + ':'; row += container_name + ':';
row += std::to_string(location.range.start.line + 1) + ": <b>" + it->second.get<std::string>("name") + "</b>"; row += std::to_string(location.range.start.line + 1) + ": <b>" + it->second.get<std::string>("name") + "</b>";
locations->emplace_back(std::move(location)); locations_rows.emplace(std::move(location), std::move(row));
rows.emplace_back(std::move(row));
} }
catch(...) { catch(...) {
} }
@ -771,8 +773,12 @@ void Project::LanguageProtocol::show_symbols() {
result_processed.set_value(); result_processed.set_value();
}); });
result_processed.get_future().get(); result_processed.get_future().get();
for(auto &row : rows)
SelectionDialog::get()->add_row(row); locations->reserve(locations_rows.size());
for(auto &location_row : locations_rows) {
locations->emplace_back(std::move(location_row.first));
SelectionDialog::get()->add_row(location_row.second);
}
} }
SelectionDialog::get()->on_select = [locations](unsigned int index, const std::string &text, bool hide_window) { SelectionDialog::get()->on_select = [locations](unsigned int index, const std::string &text, bool hide_window) {

13
src/source_language_protocol.cc

@ -116,7 +116,12 @@ LanguageProtocol::Capabilities LanguageProtocol::Client::initialize(Source::Lang
if(!error) { if(!error) {
auto capabilities_pt = result.find("capabilities"); auto capabilities_pt = result.find("capabilities");
if(capabilities_pt != result.not_found()) { if(capabilities_pt != result.not_found()) {
capabilities.text_document_sync = static_cast<LanguageProtocol::Capabilities::TextDocumentSync>(capabilities_pt->second.get<int>("textDocumentSync", 0)); try {
capabilities.text_document_sync = static_cast<LanguageProtocol::Capabilities::TextDocumentSync>(capabilities_pt->second.get<int>("textDocumentSync"));
}
catch(...) {
capabilities.text_document_sync = static_cast<LanguageProtocol::Capabilities::TextDocumentSync>(capabilities_pt->second.get<int>("textDocumentSync.change", 0));
}
capabilities.hover = capabilities_pt->second.get<bool>("hoverProvider", false); capabilities.hover = capabilities_pt->second.get<bool>("hoverProvider", false);
capabilities.completion = capabilities_pt->second.find("completionProvider") != capabilities_pt->second.not_found() ? true : false; capabilities.completion = capabilities_pt->second.find("completionProvider") != capabilities_pt->second.not_found() ? true : false;
capabilities.signature_help = capabilities_pt->second.find("signatureHelpProvider") != capabilities_pt->second.not_found() ? true : false; capabilities.signature_help = capabilities_pt->second.find("signatureHelpProvider") != capabilities_pt->second.not_found() ? true : false;
@ -579,7 +584,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
if(capabilities.references || capabilities.document_highlight) { if(capabilities.references || capabilities.document_highlight) {
get_usages = [this] { get_usages = [this] {
auto iter = get_buffer()->get_insert()->get_iter(); auto iter = get_buffer()->get_insert()->get_iter();
std::vector<LanguageProtocol::Location> locations; std::set<LanguageProtocol::Location> locations;
std::promise<void> result_processed; std::promise<void> result_processed;
std::string method; std::string method;
@ -592,7 +597,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
if(!error) { if(!error) {
try { try {
for(auto it = result.begin(); it != result.end(); ++it) for(auto it = result.begin(); it != result.end(); ++it)
locations.emplace_back(it->second, !capabilities.references ? file_path.string() : std::string()); locations.emplace(it->second, !capabilities.references ? file_path.string() : std::string());
} }
catch(...) { catch(...) {
locations.clear(); locations.clear();
@ -1379,6 +1384,8 @@ void Source::LanguageProtocolView::setup_autocomplete() {
auto detail = it->second.get<std::string>("detail", ""); auto detail = it->second.get<std::string>("detail", "");
auto documentation = it->second.get<std::string>("documentation", ""); auto documentation = it->second.get<std::string>("documentation", "");
auto insert = it->second.get<std::string>("insertText", ""); auto insert = it->second.get<std::string>("insertText", "");
if(insert.empty())
insert = it->second.get<std::string>("textEdit.newText", "");
if(!insert.empty()) { if(!insert.empty()) {
// In case ( is missing in insert but is present in label // In case ( is missing in insert but is present in label
if(label.size() > insert.size() && label.back() == ')' && insert.find('(') == std::string::npos) { if(label.size() > insert.size() && label.back() == ')' && insert.find('(') == std::string::npos) {

25
src/source_language_protocol.h

@ -18,15 +18,27 @@ namespace LanguageProtocol {
class Offset { class Offset {
public: public:
Offset(const boost::property_tree::ptree &pt); Offset(const boost::property_tree::ptree &pt);
int line; int line, character;
int character;
bool operator<(const Offset &rhs) const {
return line < rhs.line || (line == rhs.line && character < rhs.character);
}
bool operator==(const Offset &rhs) const {
return line == rhs.line && character == rhs.character;
}
}; };
class Range { class Range {
public: public:
Range(const boost::property_tree::ptree &pt); Range(const boost::property_tree::ptree &pt);
Range() = default;
Offset start, end; Offset start, end;
bool operator<(const Range &rhs) const {
return start < rhs.start || (start == rhs.start && end < rhs.end);
}
bool operator==(const Range &rhs) const {
return start == rhs.start && end == rhs.end;
}
}; };
class Location { class Location {
@ -34,6 +46,13 @@ namespace LanguageProtocol {
Location(const boost::property_tree::ptree &pt, std::string file_ = {}); Location(const boost::property_tree::ptree &pt, std::string file_ = {});
std::string file; std::string file;
Range range; Range range;
bool operator<(const Location &rhs) const {
return file < rhs.file || (file == rhs.file && range < rhs.range);
}
bool operator==(const Location &rhs) const {
return file == rhs.file && range == rhs.range;
}
}; };
class Diagnostic { class Diagnostic {

Loading…
Cancel
Save