From 37341bf12e071b58ab6096b6c8a6f74b3e5163e7 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 8 Jul 2021 10:51:52 +0200 Subject: [PATCH] Added automatic installation of rust-analyzer through rustup if component is found --- src/notebook.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/notebook.cpp b/src/notebook.cpp index 75403f1..f5bd89c 100644 --- a/src/notebook.cpp +++ b/src/notebook.cpp @@ -6,6 +6,7 @@ #include "source_clang.hpp" #include "source_generic.hpp" #include "source_language_protocol.hpp" +#include "utility.hpp" #include #include #include @@ -183,6 +184,41 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position boost::system::error_code 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())); + 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(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 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; + } + } + } + } + } } } }