Browse Source

Language protocol: rename and showing usages now fallsback to document_highlight if no better capability is supported. This for instance enables rename and showing usages when flow-language-server is used.

merge-requests/389/head
eidheim 7 years ago
parent
commit
61fbdfd8a8
  1. 53
      src/source_language_protocol.cc

53
src/source_language_protocol.cc

@ -546,19 +546,33 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
}; };
} }
if(capabilities.references) { 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<std::pair<Offset, std::string>> usages; std::vector<std::pair<Offset, std::string>> usages;
std::vector<Offset> end_offsets; std::vector<Offset> end_offsets;
std::promise<void> result_processed; std::promise<void> result_processed;
client->write_request(this, "textDocument/references", R"("textDocument":{"uri":")" + uri + R"("}, "position": {"line": )" + std::to_string(iter.get_line()) + ", \"character\": " + std::to_string(iter.get_line_offset()) + R"(}, "context": {"includeDeclaration": true})", [&usages, &end_offsets, &result_processed](const boost::property_tree::ptree &result, bool error) {
std::string method;
if(capabilities.document_highlight)
method = "textDocument/documentHighlight";
else
method = "textDocument/references";
client->write_request(this, method, R"("textDocument":{"uri":")" + uri + R"("}, "position": {"line": )" + std::to_string(iter.get_line()) + ", \"character\": " + std::to_string(iter.get_line_offset()) + R"(}, "context": {"includeDeclaration": true})", [this, &usages, &end_offsets, &result_processed](const boost::property_tree::ptree &result, bool error) {
if(!error) { if(!error) {
try { try {
for(auto it = result.begin(); it != result.end(); ++it) { for(auto it = result.begin(); it != result.end(); ++it) {
auto path = it->second.get<std::string>("uri", ""); std::string path;
if(path.size() >= 7) { if(capabilities.document_highlight)
path = file_path.string();
else {
path = it->second.get<std::string>("uri", "");
if(path.size() >= 7)
path.erase(0, 7); path.erase(0, 7);
else
continue;
}
auto range_it = it->second.find("range"); auto range_it = it->second.find("range");
if(range_it != it->second.not_found()) { if(range_it != it->second.not_found()) {
auto start_it = range_it->second.find("start"); auto start_it = range_it->second.find("start");
@ -570,7 +584,6 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
} }
} }
} }
}
catch(...) { catch(...) {
usages.clear(); usages.clear();
end_offsets.clear(); end_offsets.clear();
@ -582,7 +595,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
auto embolden_token = [](std::string &line_, unsigned token_start_pos, unsigned token_end_pos) { auto embolden_token = [](std::string &line_, unsigned token_start_pos, unsigned token_end_pos) {
Glib::ustring line = line_; Glib::ustring line = line_;
if(token_start_pos >= line.size() || token_end_pos >= line.size()) if(token_start_pos > line.size() || token_end_pos > line.size())
return; return;
//markup token as bold //markup token as bold
@ -683,7 +696,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
return get_buffer()->get_text(start, end); return get_buffer()->get_text(start, end);
}; };
if(capabilities.rename) { if(capabilities.rename || capabilities.document_highlight) {
rename_similar_tokens = [this](const std::string &text) { rename_similar_tokens = [this](const std::string &text) {
class Usages { class Usages {
public: public:
@ -699,6 +712,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
auto iter = get_buffer()->get_insert()->get_iter(); auto iter = get_buffer()->get_insert()->get_iter();
std::vector<Usages> usages; std::vector<Usages> usages;
std::promise<void> result_processed; std::promise<void> result_processed;
if(capabilities.rename) {
client->write_request(this, "textDocument/rename", R"("textDocument":{"uri":")" + uri + R"("}, "position": {"line": )" + std::to_string(iter.get_line()) + ", \"character\": " + std::to_string(iter.get_line_offset()) + R"(}, "newName": ")" + text + "\"", [this, &usages, &result_processed](const boost::property_tree::ptree &result, bool error) { client->write_request(this, "textDocument/rename", R"("textDocument":{"uri":")" + uri + R"("}, "position": {"line": )" + std::to_string(iter.get_line()) + ", \"character\": " + std::to_string(iter.get_line_offset()) + R"(}, "newName": ")" + text + "\"", [this, &usages, &result_processed](const boost::property_tree::ptree &result, bool error) {
if(!error) { if(!error) {
boost::filesystem::path project_path; boost::filesystem::path project_path;
@ -771,6 +785,31 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
} }
result_processed.set_value(); result_processed.set_value();
}); });
}
else {
client->write_request(this, "textDocument/documentHighlight", R"("textDocument":{"uri":")" + uri + R"("}, "position": {"line": )" + std::to_string(iter.get_line()) + ", \"character\": " + std::to_string(iter.get_line_offset()) + R"(}, "context": {"includeDeclaration": true})", [this, &usages, &result_processed](const boost::property_tree::ptree &result, bool error) {
if(!error) {
try {
usages.emplace_back(Usages{file_path, nullptr, std::vector<std::pair<Offset, Offset>>()});
for(auto it = result.begin(); it != result.end(); ++it) {
auto range_it = it->second.find("range");
if(range_it != it->second.not_found()) {
auto start_it = range_it->second.find("start");
auto end_it = range_it->second.find("end");
if(start_it != range_it->second.not_found() && end_it != range_it->second.not_found()) {
usages.back().offsets.emplace_back(std::make_pair(Offset(start_it->second.get<unsigned>("line"), start_it->second.get<unsigned>("character")),
Offset(end_it->second.get<unsigned>("line"), end_it->second.get<unsigned>("character"))));
}
}
}
}
catch(...) {
usages.clear();
}
}
result_processed.set_value();
});
}
result_processed.get_future().get(); result_processed.get_future().get();
std::vector<Usages *> usages_renamed; std::vector<Usages *> usages_renamed;

Loading…
Cancel
Save