Browse Source

Added automatic installation of rust-analyzer through rustup if component is found

pipelines/353213535
eidheim 4 years ago
parent
commit
37341bf12e
  1. 36
      src/notebook.cpp

36
src/notebook.cpp

@ -6,6 +6,7 @@
#include "source_clang.hpp" #include "source_clang.hpp"
#include "source_generic.hpp" #include "source_generic.hpp"
#include "source_language_protocol.hpp" #include "source_language_protocol.hpp"
#include "utility.hpp"
#include <fstream> #include <fstream>
#include <gtksourceview-3.0/gtksourceview/gtksourcemap.h> #include <gtksourceview-3.0/gtksourceview/gtksourcemap.h>
#include <regex> #include <regex>
@ -183,6 +184,41 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position
boost::system::error_code ec; boost::system::error_code ec;
if(boost::filesystem::exists(rust_analyzer, ec)) if(boost::filesystem::exists(rust_analyzer, ec))
source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id, rust_analyzer.string())); source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id, rust_analyzer.string()));
else {
static bool first = true;
std::stringstream stdin_stream, stdout_stream;
if(first && !filesystem::find_executable("rustup").empty()) {
first = false;
if(Terminal::get().process(stdin_stream, stdout_stream, "rustup component list") == 0) {
std::string line;
while(std::getline(stdout_stream, line)) {
if(starts_with(line, "rust-analyzer")) {
Gtk::MessageDialog dialog(*static_cast<Gtk::Window *>(get_toplevel()), "Install rust-analyzer (Rust language server)", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialog.set_default_response(Gtk::RESPONSE_YES);
dialog.set_secondary_text("Would you like to install rust-analyzer through rustup?");
int result = dialog.run();
if(result == Gtk::RESPONSE_YES) {
boost::optional<int> exit_status;
Terminal::get().async_process(std::string("rustup component add rust-src ") + (starts_with(line, "rust-analyzer-preview") ? "rust-analyzer-preview" : "rust-analyzer"), "", [&exit_status](int exit_status_) {
exit_status = exit_status_;
});
while(!exit_status) {
while(Gtk::Main::events_pending())
Gtk::Main::iteration();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if(exit_status == 0) {
Terminal::get().print("\e[32mSuccessfully\e[m installed rust-analyzer.\n");
if(boost::filesystem::exists(rust_analyzer, ec))
source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id, rust_analyzer.string()));
}
}
break;
}
}
}
}
}
} }
} }
} }

Loading…
Cancel
Save