From 9579c16193d86a5beed6d5ffc84f2086e3bbcfc1 Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 24 May 2021 09:52:16 +0200 Subject: [PATCH] Added menu item File, New Project, Rust --- docs/language_servers.md | 2 +- src/menu.cpp | 4 ++++ src/notebook.cpp | 4 ++-- src/project.cpp | 5 ++--- src/project_build.cpp | 9 +++++++++ src/project_build.hpp | 2 +- src/source_base.cpp | 7 ++++--- src/source_language_protocol.cpp | 14 ++++++++++++++ src/window.cpp | 27 +++++++++++++++++++++++++++ 9 files changed, 64 insertions(+), 10 deletions(-) diff --git a/docs/language_servers.md b/docs/language_servers.md index 2ce6596..a3294f4 100644 --- a/docs/language_servers.md +++ b/docs/language_servers.md @@ -85,7 +85,7 @@ ln -s ~/.cargo/bin/rust-analyzer /usr/local/bin/rust-language-server ``` - Additional setup within a Rust project: - - Add an empty `.rust-format` file to enable style format on save + - Add an empty `.rustfmt.toml` file to enable style format on save ## GLSL diff --git a/src/menu.cpp b/src/menu.cpp index 575466d..19aea24 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -135,6 +135,10 @@ const Glib::ustring menu_xml = R"RAW( C++ app.file_new_project_cpp + + Rust + app.file_new_project_rust +
diff --git a/src/notebook.cpp b/src/notebook.cpp index c3b4678..19b432e 100644 --- a/src/notebook.cpp +++ b/src/notebook.cpp @@ -175,10 +175,10 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position else if(language && !language_protocol_language_id.empty() && !filesystem::find_executable(language_protocol_language_id + "-language-server").empty()) source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id, language_protocol_language_id + "-language-server")); else if(language && language_protocol_language_id == "rust" && !filesystem::get_rust_sysroot_path().empty()) { - auto rust_analyzer = filesystem::get_rust_sysroot_path().string() + "/bin/rust-analyzer"; + auto rust_analyzer = filesystem::get_rust_sysroot_path() / "bin" / "rust-analyzer"; 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)); + source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id, rust_analyzer.string())); else source_views.emplace_back(new Source::GenericView(file_path, language)); } diff --git a/src/project.cpp b/src/project.cpp index 0a03cdf..8a89889 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -908,7 +908,7 @@ std::pair Project::Rust::get_run_arguments() { arguments = run_arguments_it->second; if(arguments.empty()) - arguments = filesystem::get_short_path(build->get_executable(project_path)).string(); + arguments = filesystem::escape_argument(filesystem::get_short_path(build->get_executable(project_path)).string()); return {project_path, arguments}; } @@ -921,8 +921,7 @@ void Project::Rust::compile() { Terminal::get().print("\e[2mCompiling project " + filesystem::get_short_path(build->project_path).string() + "\e[m\n"); - auto command = build->get_compile_command(); - Terminal::get().async_process(command, build->project_path, [](int exit_status) { + Terminal::get().async_process(build->get_compile_command(), build->project_path, [](int exit_status) { compiling = false; }); } diff --git a/src/project_build.cpp b/src/project_build.cpp index d767121..a73cccb 100644 --- a/src/project_build.cpp +++ b/src/project_build.cpp @@ -227,3 +227,12 @@ bool Project::CargoBuild::update_debug(bool force) { std::string Project::CargoBuild::get_compile_command() { return Config::get().project.cargo_command + " build"; } + +boost::filesystem::path Project::CargoBuild::get_executable(const boost::filesystem::path &path) { + auto project_name = project_path.filename().string(); + for(auto &chr : project_name) { + if(chr == ' ') + chr = '_'; + } + return get_debug_path() / project_name; +} diff --git a/src/project_build.hpp b/src/project_build.hpp index 0ddacf1..a69add0 100644 --- a/src/project_build.hpp +++ b/src/project_build.hpp @@ -68,7 +68,7 @@ namespace Project { bool update_debug(bool force = false) override; std::string get_compile_command() override; - boost::filesystem::path get_executable(const boost::filesystem::path &path) override { return get_debug_path() / project_path.filename(); } + boost::filesystem::path get_executable(const boost::filesystem::path &path) override; }; class NpmBuild : public Build { diff --git a/src/source_base.cpp b/src/source_base.cpp index 075712b..0fd282c 100644 --- a/src/source_base.cpp +++ b/src/source_base.cpp @@ -169,6 +169,10 @@ Source::BaseView::BaseView(const boost::filesystem::path &file_path, const Glib: #endif tab_char = Config::get().source.default_tab_char; tab_size = Config::get().source.default_tab_size; + if(language && (language->get_id() == "python" || language->get_id() == "rust")) { + tab_char = ' '; + tab_size = 4; + } if(Config::get().source.auto_tab_char_and_size) { auto tab_char_and_size = find_tab_char_and_size(); if(tab_char_and_size.second != 0) { @@ -408,9 +412,6 @@ void Source::BaseView::check_last_write_time(boost::optional last_w } std::pair Source::BaseView::find_tab_char_and_size() { - if(language && language->get_id() == "python") - return {' ', 4}; - std::map tab_chars; std::map tab_sizes; auto iter = get_buffer()->begin(); diff --git a/src/source_language_protocol.cpp b/src/source_language_protocol.cpp index f76131e..cc4e588 100644 --- a/src/source_language_protocol.cpp +++ b/src/source_language_protocol.cpp @@ -568,6 +568,20 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() { style_file_search_path = style_file_search_path.parent_path(); } + if(!has_style_file && language && language->get_id() == "rust") { + auto style_file_search_path = file_path.parent_path(); + while(true) { + if(boost::filesystem::exists(style_file_search_path / "rustfmt.toml", ec) || + boost::filesystem::exists(style_file_search_path / ".rustfmt.toml", ec)) { + has_style_file = true; + break; + } + if(style_file_search_path == style_file_search_path.root_directory()) + break; + style_file_search_path = style_file_search_path.parent_path(); + } + } + if(!has_style_file && !continue_without_style_file) return; } diff --git a/src/window.cpp b/src/window.cpp index a0869c6..f6a8463 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -429,6 +429,33 @@ void Window::set_menu_actions() { Terminal::get().print("\e[31mError\e[m: Could not create project " + filesystem::get_short_path(project_path).string() + "\n", true); } }); + menu.add_action("file_new_project_rust", []() { + auto sysroot = filesystem::get_rust_sysroot_path(); + if(sysroot.empty()) { + Terminal::get().print("\e[33mWarning\e[m: could not find Rust.\n"); + Terminal::get().print("For installation instructions please visit: https://gitlab.com/cppit/jucipp/-/blob/master/docs/language_servers.md#rust.\n"); + return; + } + boost::filesystem::path project_path = Dialog::new_folder(Project::get_preferably_directory_folder()); + if(!project_path.empty()) { + auto project_name = project_path.filename().string(); + for(auto &chr : project_name) { + if(chr == ' ') + chr = '_'; + } + if(Terminal::get().process("cargo init " + filesystem::escape_argument(project_path.string()) + " --bin --vcs none --name " + project_name) == 0) { + filesystem::write(project_path / ".rustfmt.toml", ""); + Directories::get().open(project_path); + Notebook::get().open(project_path / "src" / "main.rs"); + Directories::get().update(); + Terminal::get().print("Rust project "); + Terminal::get().print(project_path.filename().string(), true); + Terminal::get().print(" \e[32mcreated\e[m\n"); + } + else + Terminal::get().print("\e[31mError\e[m: Could not create project " + filesystem::get_short_path(project_path).string() + "\n", true); + } + }); menu.add_action("file_open_file", []() { auto path = Dialog::open_file(Project::get_preferably_view_folder());