Browse Source

Language protocol: renamed uri to file where appropriate

merge-requests/389/head
eidheim 7 years ago
parent
commit
1cb311de02
  1. 8
      src/project.cc
  2. 52
      src/source_language_protocol.cc
  3. 4
      src/source_language_protocol.h

8
src/project.cc

@ -719,7 +719,7 @@ void Project::LanguageProtocol::show_symbols() {
for(auto it = result.begin(); it != result.end(); ++it) { for(auto it = result.begin(); it != result.end(); ++it) {
try { try {
::LanguageProtocol::Location location(it->second.get_child("location")); ::LanguageProtocol::Location location(it->second.get_child("location"));
if(filesystem::file_in_path(location.uri, *project_path)) { if(filesystem::file_in_path(location.file, *project_path)) {
locations->emplace_back(std::move(location)); locations->emplace_back(std::move(location));
names.emplace_back(it->second.get<std::string>("name")); names.emplace_back(it->second.get<std::string>("name"));
} }
@ -732,7 +732,7 @@ void Project::LanguageProtocol::show_symbols() {
}); });
result_processed.get_future().get(); result_processed.get_future().get();
for(size_t c = 0; c < locations->size() && c < names.size(); ++c) for(size_t c = 0; c < locations->size() && c < names.size(); ++c)
SelectionDialog::get()->add_row(filesystem::get_relative_path((*locations)[c].uri, *project_path).string() + ':' + std::to_string((*locations)[c].range.start.line + 1) + ':' + std::to_string((*locations)[c].range.start.character + 1) + ": " + names[c]); SelectionDialog::get()->add_row(filesystem::get_relative_path((*locations)[c].file, *project_path).string() + ':' + std::to_string((*locations)[c].range.start.line + 1) + ':' + std::to_string((*locations)[c].range.start.character + 1) + ": " + names[c]);
}; };
} }
else { else {
@ -758,9 +758,9 @@ void Project::LanguageProtocol::show_symbols() {
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) {
auto &offset = (*locations)[index]; auto &offset = (*locations)[index];
if(!boost::filesystem::is_regular_file(offset.uri)) if(!boost::filesystem::is_regular_file(offset.file))
return; return;
Notebook::get().open(offset.uri); Notebook::get().open(offset.file);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(offset.range.start.line, offset.range.start.character); view->place_cursor_at_line_offset(offset.range.start.line, offset.range.start.character);
view->scroll_to_cursor_delayed(view, true, false); view->scroll_to_cursor_delayed(view, true, false);

52
src/source_language_protocol.cc

@ -20,13 +20,13 @@ LanguageProtocol::Offset::Offset(const boost::property_tree::ptree &pt) : line(p
LanguageProtocol::Range::Range(const boost::property_tree::ptree &pt) : start(pt.get_child("start")), end(pt.get_child("end")) {} LanguageProtocol::Range::Range(const boost::property_tree::ptree &pt) : start(pt.get_child("start")), end(pt.get_child("end")) {}
LanguageProtocol::Location::Location(const boost::property_tree::ptree &pt, std::string uri_) : range(pt.get_child("range")) { LanguageProtocol::Location::Location(const boost::property_tree::ptree &pt, std::string file_) : range(pt.get_child("range")) {
if(uri_.empty()) { if(file_.empty()) {
uri = pt.get<std::string>("uri"); file = pt.get<std::string>("uri");
uri.erase(0, 7); file.erase(0, 7);
} }
else else
uri = std::move(uri_); file = std::move(file_);
} }
LanguageProtocol::Diagnostic::RelatedInformation::RelatedInformation(const boost::property_tree::ptree &pt) : message(pt.get<std::string>("message")), location(pt.get_child("location")) {} LanguageProtocol::Diagnostic::RelatedInformation::RelatedInformation(const boost::property_tree::ptree &pt) : message(pt.get<std::string>("message")), location(pt.get_child("location")) {}
@ -619,11 +619,11 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
auto c = static_cast<size_t>(-1); auto c = static_cast<size_t>(-1);
for(auto &location : locations) { for(auto &location : locations) {
++c; ++c;
usages.emplace_back(Offset(location.range.start.line, location.range.start.character, location.uri), std::string()); usages.emplace_back(Offset(location.range.start.line, location.range.start.character, location.file), std::string());
auto &usage = usages.back(); auto &usage = usages.back();
auto view_it = views.end(); auto view_it = views.end();
for(auto it = views.begin(); it != views.end(); ++it) { for(auto it = views.begin(); it != views.end(); ++it) {
if(location.uri == (*it)->file_path) { if(location.file == (*it)->file_path) {
view_it = it; view_it = it;
break; break;
} }
@ -638,9 +638,9 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
} }
} }
else { else {
auto it = file_lines.find(location.uri); auto it = file_lines.find(location.file);
if(it == file_lines.end()) { if(it == file_lines.end()) {
std::ifstream ifs(location.uri); std::ifstream ifs(location.file);
if(ifs) { if(ifs) {
std::vector<std::string> lines; std::vector<std::string> lines;
std::string line; std::string line;
@ -649,11 +649,11 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
line.pop_back(); line.pop_back();
lines.emplace_back(line); lines.emplace_back(line);
} }
auto pair = file_lines.emplace(location.uri, lines); auto pair = file_lines.emplace(location.file, lines);
it = pair.first; it = pair.first;
} }
else { else {
auto pair = file_lines.emplace(location.uri, std::vector<std::string>()); auto pair = file_lines.emplace(location.file, std::vector<std::string>());
it = pair.first; it = pair.first;
} }
} }
@ -693,7 +693,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
rename_similar_tokens = [this](const std::string &text) { rename_similar_tokens = [this](const std::string &text) {
class Changes { class Changes {
public: public:
std::string uri; std::string file;
std::vector<LanguageProtocol::TextEdit> text_edits; std::vector<LanguageProtocol::TextEdit> text_edits;
}; };
@ -717,13 +717,13 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
auto changes_it = result.find("changes"); auto changes_it = result.find("changes");
if(changes_it != result.not_found()) { if(changes_it != result.not_found()) {
for(auto file_it = changes_it->second.begin(); file_it != changes_it->second.end(); ++file_it) { for(auto file_it = changes_it->second.begin(); file_it != changes_it->second.end(); ++file_it) {
auto uri = file_it->first; auto file = file_it->first;
uri.erase(0, 7); file.erase(0, 7);
if(filesystem::file_in_path(uri, project_path)) { if(filesystem::file_in_path(file, project_path)) {
std::vector<LanguageProtocol::TextEdit> edits; std::vector<LanguageProtocol::TextEdit> edits;
for(auto edit_it = file_it->second.begin(); edit_it != file_it->second.end(); ++edit_it) for(auto edit_it = file_it->second.begin(); edit_it != file_it->second.end(); ++edit_it)
edits.emplace_back(edit_it->second); edits.emplace_back(edit_it->second);
changes.emplace_back(Changes{std::move(uri), std::move(edits)}); changes.emplace_back(Changes{std::move(file), std::move(edits)});
} }
} }
} }
@ -732,14 +732,14 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
for(auto change_it = changes_pt.begin(); change_it != changes_pt.end(); ++change_it) { for(auto change_it = changes_pt.begin(); change_it != changes_pt.end(); ++change_it) {
auto document_it = change_it->second.find("textDocument"); auto document_it = change_it->second.find("textDocument");
if(document_it != change_it->second.not_found()) { if(document_it != change_it->second.not_found()) {
auto uri = document_it->second.get<std::string>("uri", ""); auto file = document_it->second.get<std::string>("uri", "");
uri.erase(0, 7); file.erase(0, 7);
if(filesystem::file_in_path(uri, project_path)) { if(filesystem::file_in_path(file, project_path)) {
std::vector<LanguageProtocol::TextEdit> edits; std::vector<LanguageProtocol::TextEdit> edits;
auto edits_pt = change_it->second.get_child("edits", boost::property_tree::ptree()); auto edits_pt = change_it->second.get_child("edits", boost::property_tree::ptree());
for(auto edit_it = edits_pt.begin(); edit_it != edits_pt.end(); ++edit_it) for(auto edit_it = edits_pt.begin(); edit_it != edits_pt.end(); ++edit_it)
edits.emplace_back(edit_it->second); edits.emplace_back(edit_it->second);
changes.emplace_back(Changes{std::move(uri), std::move(edits)}); changes.emplace_back(Changes{std::move(file), std::move(edits)});
} }
} }
} }
@ -774,7 +774,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
for(auto &change : changes) { for(auto &change : changes) {
auto view_it = views.end(); auto view_it = views.end();
for(auto it = views.begin(); it != views.end(); ++it) { for(auto it = views.begin(); it != views.end(); ++it) {
if((*it)->file_path == change.uri) { if((*it)->file_path == change.file) {
view_it = it; view_it = it;
break; break;
} }
@ -807,11 +807,11 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
else { else {
Glib::ustring buffer; Glib::ustring buffer;
{ {
std::ifstream stream(change.uri, std::ifstream::binary); std::ifstream stream(change.file, std::ifstream::binary);
if(stream) if(stream)
buffer.assign(std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>()); buffer.assign(std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>());
} }
std::ofstream stream(change.uri, std::ifstream::binary); std::ofstream stream(change.file, std::ifstream::binary);
if(!buffer.empty() && stream) { if(!buffer.empty() && stream) {
std::vector<size_t> lines_start_pos = {0}; std::vector<size_t> lines_start_pos = {0};
for(size_t c = 0; c < buffer.size(); ++c) { for(size_t c = 0; c < buffer.size(); ++c) {
@ -837,7 +837,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
changes_renamed.emplace_back(&change); changes_renamed.emplace_back(&change);
} }
else else
Terminal::get().print("Error: could not write to file " + change.uri + '\n', true); Terminal::get().print("Error: could not write to file " + change.file + '\n', true);
} }
} }
@ -910,7 +910,7 @@ void Source::LanguageProtocolView::update_diagnostics(std::vector<LanguageProtoc
buffer->insert_at_cursor(diagnostic.message); buffer->insert_at_cursor(diagnostic.message);
for(size_t i = 0; i < diagnostic.related_informations.size(); ++i) { for(size_t i = 0; i < diagnostic.related_informations.size(); ++i) {
auto link = filesystem::get_relative_path(diagnostic.related_informations[i].location.uri, file_path.parent_path()).string(); auto link = filesystem::get_relative_path(diagnostic.related_informations[i].location.file, file_path.parent_path()).string();
link += ':' + std::to_string(diagnostic.related_informations[i].location.range.start.line + 1); link += ':' + std::to_string(diagnostic.related_informations[i].location.range.start.line + 1);
link += ':' + std::to_string(diagnostic.related_informations[i].location.range.start.character + 1); link += ':' + std::to_string(diagnostic.related_informations[i].location.range.start.character + 1);
@ -1115,7 +1115,7 @@ Source::Offset Source::LanguageProtocolView::get_declaration(const Gtk::TextIter
for(auto it = result.begin(); it != result.end(); ++it) { for(auto it = result.begin(); it != result.end(); ++it) {
try { try {
LanguageProtocol::Location location(it->second); LanguageProtocol::Location location(it->second);
offset->file_path = std::move(location.uri); offset->file_path = std::move(location.file);
offset->line = location.range.start.line; offset->line = location.range.start.line;
offset->index = location.range.start.character; offset->index = location.range.start.character;
break; // TODO: can a language server return several definitions? break; // TODO: can a language server return several definitions?

4
src/source_language_protocol.h

@ -30,8 +30,8 @@ namespace LanguageProtocol {
class Location { class Location {
public: public:
Location(const boost::property_tree::ptree &pt, std::string uri_ = {}); Location(const boost::property_tree::ptree &pt, std::string file_ = {});
std::string uri; std::string file;
Range range; Range range;
}; };

Loading…
Cancel
Save