Browse Source

Cleanup: added ec argument to filesystem calls, and added and made use of Project::get_view_folder and Project::get_directory_folder

pipelines/143601543
eidheim 6 years ago
parent
commit
42341f98f3
  1. 19
      src/cmake.cc
  2. 58
      src/config.cc
  3. 1
      src/config.h
  4. 71
      src/directories.cc
  5. 3
      src/filesystem.cc
  6. 9
      src/juci.cc
  7. 19
      src/meson.cc
  8. 18
      src/notebook.cc
  9. 1
      src/notebook.h
  10. 83
      src/project.cc
  11. 12
      src/project.h
  12. 24
      src/project_build.cc
  13. 3
      src/source_generic.cc
  14. 3
      src/source_language_protocol.cc
  15. 10
      src/terminal.cc
  16. 3
      src/tooltips.cc
  17. 10
      src/usages_clang.cc
  18. 159
      src/window.cc

19
src/cmake.cc

@ -17,10 +17,11 @@ CMake::CMake(const boost::filesystem::path &path) {
return false; return false;
}; };
auto search_path = boost::filesystem::is_directory(path) ? path : path.parent_path(); boost::system::error_code ec;
auto search_path = boost::filesystem::is_directory(path, ec) ? path : path.parent_path();
while(true) { while(true) {
auto search_cmake_path = search_path / "CMakeLists.txt"; auto search_cmake_path = search_path / "CMakeLists.txt";
if(boost::filesystem::exists(search_cmake_path)) { if(boost::filesystem::exists(search_cmake_path, ec)) {
paths.emplace(paths.begin(), search_cmake_path); paths.emplace(paths.begin(), search_cmake_path);
if(find_cmake_project(search_cmake_path)) { if(find_cmake_project(search_cmake_path)) {
project_path = search_path; project_path = search_path;
@ -34,10 +35,11 @@ CMake::CMake(const boost::filesystem::path &path) {
} }
bool CMake::update_default_build(const boost::filesystem::path &default_build_path, bool force) { bool CMake::update_default_build(const boost::filesystem::path &default_build_path, bool force) {
if(project_path.empty() || !boost::filesystem::exists(project_path / "CMakeLists.txt") || default_build_path.empty()) boost::system::error_code ec;
if(project_path.empty() || !boost::filesystem::exists(project_path / "CMakeLists.txt", ec) || default_build_path.empty())
return false; return false;
if(!boost::filesystem::exists(default_build_path)) { if(!boost::filesystem::exists(default_build_path, ec)) {
boost::system::error_code ec; boost::system::error_code ec;
boost::filesystem::create_directories(default_build_path, ec); boost::filesystem::create_directories(default_build_path, ec);
if(ec) { if(ec) {
@ -46,7 +48,7 @@ bool CMake::update_default_build(const boost::filesystem::path &default_build_pa
} }
} }
if(!force && boost::filesystem::exists(default_build_path / "compile_commands.json")) if(!force && boost::filesystem::exists(default_build_path / "compile_commands.json", ec))
return true; return true;
auto compile_commands_path = default_build_path / "compile_commands.json"; auto compile_commands_path = default_build_path / "compile_commands.json";
@ -77,10 +79,11 @@ bool CMake::update_default_build(const boost::filesystem::path &default_build_pa
} }
bool CMake::update_debug_build(const boost::filesystem::path &debug_build_path, bool force) { bool CMake::update_debug_build(const boost::filesystem::path &debug_build_path, bool force) {
if(project_path.empty() || !boost::filesystem::exists(project_path / "CMakeLists.txt") || debug_build_path.empty()) boost::system::error_code ec;
if(project_path.empty() || !boost::filesystem::exists(project_path / "CMakeLists.txt", ec) || debug_build_path.empty())
return false; return false;
if(!boost::filesystem::exists(debug_build_path)) { if(!boost::filesystem::exists(debug_build_path, ec)) {
boost::system::error_code ec; boost::system::error_code ec;
boost::filesystem::create_directories(debug_build_path, ec); boost::filesystem::create_directories(debug_build_path, ec);
if(ec) { if(ec) {
@ -89,7 +92,7 @@ bool CMake::update_debug_build(const boost::filesystem::path &debug_build_path,
} }
} }
if(!force && boost::filesystem::exists(debug_build_path / "CMakeCache.txt")) if(!force && boost::filesystem::exists(debug_build_path / "CMakeCache.txt", ec))
return true; return true;
Dialog::Message message("Creating/updating debug build"); Dialog::Message message("Creating/updating debug build");

58
src/config.cc

@ -14,50 +14,46 @@ Config::Config() {
} }
void Config::load() { void Config::load() {
auto config_json = (home_juci_path / "config" / "config.json").string(); // This causes some redundant copies, but assures windows support auto config_dir = home_juci_path / "config";
boost::property_tree::ptree cfg; auto config_json = config_dir / "config.json";
try { try {
find_or_create_config_files(); boost::filesystem::create_directories(config_dir);
boost::property_tree::json_parser::read_json(config_json, cfg);
if(!boost::filesystem::exists(config_json))
filesystem::write(config_json, default_config_file);
auto juci_style_path = home_juci_path / "styles";
boost::filesystem::create_directories(juci_style_path);
juci_style_path /= "juci-light.xml";
if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_light_style);
juci_style_path = juci_style_path.parent_path();
juci_style_path /= "juci-dark.xml";
if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_dark_style);
juci_style_path = juci_style_path.parent_path();
juci_style_path /= "juci-dark-blue.xml";
if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_dark_blue_style);
boost::property_tree::ptree cfg;
boost::property_tree::json_parser::read_json(config_json.string(), cfg);
update(cfg); update(cfg);
read(cfg); read(cfg);
} }
catch(const std::exception &e) { catch(const std::exception &e) {
dispatcher.post([config_json, e_what = std::string(e.what())] { dispatcher.post([config_json = std::move(config_json), e_what = std::string(e.what())] {
::Terminal::get().print("Error: could not parse " + config_json + ": " + e_what + "\n", true); ::Terminal::get().print("Error: could not parse " + config_json.string() + ": " + e_what + "\n", true);
}); });
std::stringstream ss; std::stringstream ss;
ss << default_config_file; ss << default_config_file;
boost::property_tree::ptree cfg;
boost::property_tree::read_json(ss, cfg); boost::property_tree::read_json(ss, cfg);
read(cfg); read(cfg);
} }
} }
void Config::find_or_create_config_files() {
auto config_dir = home_juci_path / "config";
auto config_json = config_dir / "config.json";
boost::filesystem::create_directories(config_dir); // io exp captured by calling method
if(!boost::filesystem::exists(config_json))
filesystem::write(config_json, default_config_file);
auto juci_style_path = home_juci_path / "styles";
boost::filesystem::create_directories(juci_style_path); // io exp captured by calling method
juci_style_path /= "juci-light.xml";
if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_light_style);
juci_style_path = juci_style_path.parent_path();
juci_style_path /= "juci-dark.xml";
if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_dark_style);
juci_style_path = juci_style_path.parent_path();
juci_style_path /= "juci-dark-blue.xml";
if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_dark_blue_style);
}
void Config::update(boost::property_tree::ptree &cfg) { void Config::update(boost::property_tree::ptree &cfg) {
boost::property_tree::ptree default_cfg; boost::property_tree::ptree default_cfg;
bool cfg_ok = true; bool cfg_ok = true;

1
src/config.h

@ -132,7 +132,6 @@ private:
/// Used to dispatch Terminal outputs after juCi++ GUI setup and configuration /// Used to dispatch Terminal outputs after juCi++ GUI setup and configuration
Dispatcher dispatcher; Dispatcher dispatcher;
void find_or_create_config_files();
void update(boost::property_tree::ptree &cfg); void update(boost::property_tree::ptree &cfg);
void make_version_dependent_corrections(boost::property_tree::ptree &cfg, const boost::property_tree::ptree &default_cfg, const std::string &version); void make_version_dependent_corrections(boost::property_tree::ptree &cfg, const boost::property_tree::ptree &default_cfg, const std::string &version);
bool add_missing_nodes(boost::property_tree::ptree &cfg, const boost::property_tree::ptree &default_cfg, std::string parent_path = ""); bool add_missing_nodes(boost::property_tree::ptree &cfg, const boost::property_tree::ptree &default_cfg, std::string parent_path = "");

71
src/directories.cc

@ -54,17 +54,16 @@ bool Directories::TreeStore::drag_data_received_vfunc(const TreeModel::Path &pat
if(source_path == target_path) if(source_path == target_path)
return false; return false;
if(boost::filesystem::exists(target_path)) { boost::system::error_code ec;
if(boost::filesystem::exists(target_path, ec)) {
Terminal::get().print("Error: could not move file: " + target_path.string() + " already exists\n", true); Terminal::get().print("Error: could not move file: " + target_path.string() + " already exists\n", true);
return false; return false;
} }
bool is_directory = boost::filesystem::is_directory(source_path); bool is_directory = boost::filesystem::is_directory(source_path, ec);
if(is_directory) if(is_directory)
Directories::get().remove_path(source_path); Directories::get().remove_path(source_path);
boost::system::error_code ec;
boost::filesystem::rename(source_path, target_path, ec); boost::filesystem::rename(source_path, target_path, ec);
if(ec) { if(ec) {
Terminal::get().print("Error: could not move file: " + ec.message() + '\n', true); Terminal::get().print("Error: could not move file: " + ec.message() + '\n', true);
@ -251,7 +250,8 @@ Directories::Directories() : Gtk::ListViewText(1) {
if(iter) { if(iter) {
auto filesystem_path = iter->get_value(column_record.path); auto filesystem_path = iter->get_value(column_record.path);
if(filesystem_path != "") { if(filesystem_path != "") {
if(boost::filesystem::is_directory(boost::filesystem::path(filesystem_path))) boost::system::error_code ec;
if(boost::filesystem::is_directory(boost::filesystem::path(filesystem_path), ec))
row_expanded(path) ? collapse_row(path) : expand_row(path, false); row_expanded(path) ? collapse_row(path) : expand_row(path, false);
else else
Notebook::get().open(filesystem_path); Notebook::get().open(filesystem_path);
@ -277,9 +277,10 @@ Directories::Directories() : Gtk::ListViewText(1) {
return; return;
EntryBox::get().clear(); EntryBox::get().clear();
EntryBox::get().entries.emplace_back("", [this, source_path = menu_popup_row_path](const std::string &content) { EntryBox::get().entries.emplace_back("", [this, source_path = menu_popup_row_path](const std::string &content) {
bool is_directory = boost::filesystem::is_directory(source_path); boost::system::error_code ec;
bool is_directory = boost::filesystem::is_directory(source_path, ec);
auto target_path = (is_directory ? source_path : source_path.parent_path()) / content; auto target_path = (is_directory ? source_path : source_path.parent_path()) / content;
if(!boost::filesystem::exists(target_path)) { if(!boost::filesystem::exists(target_path, ec)) {
if(filesystem::write(target_path, "")) { if(filesystem::write(target_path, "")) {
update(); update();
Notebook::get().open(target_path); Notebook::get().open(target_path);
@ -319,9 +320,10 @@ Directories::Directories() : Gtk::ListViewText(1) {
return; return;
EntryBox::get().clear(); EntryBox::get().clear();
EntryBox::get().entries.emplace_back("", [this, source_path = menu_popup_row_path](const std::string &content) { EntryBox::get().entries.emplace_back("", [this, source_path = menu_popup_row_path](const std::string &content) {
bool is_directory = boost::filesystem::is_directory(source_path); boost::system::error_code ec;
bool is_directory = boost::filesystem::is_directory(source_path, ec);
auto target_path = (is_directory ? source_path : source_path.parent_path()) / content; auto target_path = (is_directory ? source_path : source_path.parent_path()) / content;
if(!boost::filesystem::exists(target_path)) { if(!boost::filesystem::exists(target_path, ec)) {
boost::system::error_code ec; boost::system::error_code ec;
boost::filesystem::create_directory(target_path, ec); boost::filesystem::create_directory(target_path, ec);
if(!ec) { if(!ec) {
@ -364,11 +366,12 @@ Directories::Directories() : Gtk::ListViewText(1) {
return; return;
EntryBox::get().clear(); EntryBox::get().clear();
EntryBox::get().entries.emplace_back(menu_popup_row_path.filename().string(), [this, source_path = menu_popup_row_path](const std::string &content) { EntryBox::get().entries.emplace_back(menu_popup_row_path.filename().string(), [this, source_path = menu_popup_row_path](const std::string &content) {
bool is_directory = boost::filesystem::is_directory(source_path); boost::system::error_code ec;
bool is_directory = boost::filesystem::is_directory(source_path, ec);
auto target_path = source_path.parent_path() / content; auto target_path = source_path.parent_path() / content;
if(boost::filesystem::exists(target_path)) { if(boost::filesystem::exists(target_path, ec)) {
Terminal::get().print("Error: could not rename to " + target_path.string() + ": already exists\n", true); Terminal::get().print("Error: could not rename to " + target_path.string() + ": already exists\n", true);
return; return;
} }
@ -376,7 +379,6 @@ Directories::Directories() : Gtk::ListViewText(1) {
if(is_directory) if(is_directory)
this->remove_path(source_path); this->remove_path(source_path);
boost::system::error_code ec;
boost::filesystem::rename(source_path, target_path, ec); boost::filesystem::rename(source_path, target_path, ec);
if(ec) { if(ec) {
Terminal::get().print("Error: could not rename " + source_path.string() + ": " + ec.message() + '\n', true); Terminal::get().print("Error: could not rename " + source_path.string() + ": " + ec.message() + '\n', true);
@ -441,25 +443,26 @@ Directories::Directories() : Gtk::ListViewText(1) {
dialog.set_secondary_text("Are you sure you want to delete " + menu_popup_row_path.string() + "?"); dialog.set_secondary_text("Are you sure you want to delete " + menu_popup_row_path.string() + "?");
int result = dialog.run(); int result = dialog.run();
if(result == Gtk::RESPONSE_YES) { if(result == Gtk::RESPONSE_YES) {
bool is_directory = boost::filesystem::is_directory(menu_popup_row_path);
boost::system::error_code ec; boost::system::error_code ec;
bool is_directory = boost::filesystem::is_directory(menu_popup_row_path, ec);
boost::filesystem::remove_all(menu_popup_row_path, ec); boost::filesystem::remove_all(menu_popup_row_path, ec);
if(ec) if(ec) {
Terminal::get().print("Error: could not delete " + menu_popup_row_path.string() + ": " + ec.message() + "\n", true); Terminal::get().print("Error: could not delete " + menu_popup_row_path.string() + ": " + ec.message() + "\n", true);
else { return;
update(); }
for(size_t c = 0; c < Notebook::get().size(); c++) { update();
auto view = Notebook::get().get_view(c);
if(is_directory) { for(size_t c = 0; c < Notebook::get().size(); c++) {
if(filesystem::file_in_path(view->file_path, menu_popup_row_path)) auto view = Notebook::get().get_view(c);
view->get_buffer()->set_modified();
} if(is_directory) {
else if(view->file_path == menu_popup_row_path) if(filesystem::file_in_path(view->file_path, menu_popup_row_path))
view->get_buffer()->set_modified(); view->get_buffer()->set_modified();
} }
else if(view->file_path == menu_popup_row_path)
view->get_buffer()->set_modified();
} }
} }
}); });
@ -491,8 +494,10 @@ Directories::~Directories() {
void Directories::open(const boost::filesystem::path &dir_path) { void Directories::open(const boost::filesystem::path &dir_path) {
boost::system::error_code ec; boost::system::error_code ec;
if(dir_path.empty() || !boost::filesystem::exists(dir_path, ec) || ec) if(dir_path.empty() || !boost::filesystem::is_directory(dir_path, ec)) {
Terminal::get().print("Error: could not open " + dir_path.string() + '\n', true);
return; return;
}
tree_store->clear(); tree_store->clear();
@ -549,7 +554,8 @@ void Directories::select(const boost::filesystem::path &select_path) {
std::list<boost::filesystem::path> paths; std::list<boost::filesystem::path> paths;
boost::filesystem::path parent_path; boost::filesystem::path parent_path;
if(boost::filesystem::is_directory(select_path)) boost::system::error_code ec;
if(boost::filesystem::is_directory(select_path, ec))
parent_path = select_path; parent_path = select_path;
else else
parent_path = select_path.parent_path(); parent_path = select_path.parent_path();
@ -629,7 +635,8 @@ bool Directories::on_button_press_event(GdkEventButton *event) {
void Directories::add_or_update_path(const boost::filesystem::path &dir_path, const Gtk::TreeModel::Row &row, bool include_parent_paths) { void Directories::add_or_update_path(const boost::filesystem::path &dir_path, const Gtk::TreeModel::Row &row, bool include_parent_paths) {
auto path_it = directories.find(dir_path.string()); auto path_it = directories.find(dir_path.string());
if(!boost::filesystem::exists(dir_path)) { boost::system::error_code ec;
if(!boost::filesystem::exists(dir_path, ec)) {
if(path_it != directories.end()) if(path_it != directories.end())
directories.erase(path_it); directories.erase(path_it);
return; return;
@ -693,8 +700,7 @@ void Directories::add_or_update_path(const boost::filesystem::path &dir_path, co
} }
std::unordered_map<std::string, boost::filesystem::path> filenames; std::unordered_map<std::string, boost::filesystem::path> filenames;
boost::filesystem::directory_iterator end_it; for(boost::filesystem::directory_iterator it(dir_path, ec), end; it != end; it++) {
for(boost::filesystem::directory_iterator it(dir_path); it != end_it; it++) {
auto path = it->path(); auto path = it->path();
filenames.emplace(path.filename().string(), path); filenames.emplace(path.filename().string(), path);
} }
@ -713,7 +719,8 @@ void Directories::add_or_update_path(const boost::filesystem::path &dir_path, co
for(auto &filename : filenames) { for(auto &filename : filenames) {
if(already_added.find(filename.first) == already_added.end()) { if(already_added.find(filename.first) == already_added.end()) {
auto child = tree_store->append(children); auto child = tree_store->append(children);
auto is_directory = boost::filesystem::is_directory(filename.second); boost::system::error_code ec;
auto is_directory = boost::filesystem::is_directory(filename.second, ec);
child->set_value(column_record.is_directory, is_directory); child->set_value(column_record.is_directory, is_directory);
child->set_value(column_record.name, filename.first); child->set_value(column_record.name, filename.first);
child->set_value(column_record.markup, Glib::Markup::escape_text(filename.first)); child->set_value(column_record.markup, Glib::Markup::escape_text(filename.first));
@ -806,6 +813,7 @@ void Directories::colorize_path(boost::filesystem::path dir_path_, bool include_
green.set_green(normal_color.get_green() + factor * (green.get_green() - normal_color.get_green())); green.set_green(normal_color.get_green() + factor * (green.get_green() - normal_color.get_green()));
green.set_blue(normal_color.get_blue() + factor * (green.get_blue() - normal_color.get_blue())); green.set_blue(normal_color.get_blue() + factor * (green.get_blue() - normal_color.get_blue()));
boost::system::error_code ec;
do { do {
Gtk::TreeNodeChildren children(it->second.row ? it->second.row.children() : tree_store->children()); Gtk::TreeNodeChildren children(it->second.row ? it->second.row.children() : tree_store->children());
if(!children) if(!children)
@ -815,7 +823,6 @@ void Directories::colorize_path(boost::filesystem::path dir_path_, bool include_
auto name = Glib::Markup::escape_text(child.get_value(column_record.name)); auto name = Glib::Markup::escape_text(child.get_value(column_record.name));
auto path = child.get_value(column_record.path); auto path = child.get_value(column_record.path);
// Use canonical path to follow symbolic links // Use canonical path to follow symbolic links
boost::system::error_code ec;
auto canonical_path = boost::filesystem::canonical(path, ec); auto canonical_path = boost::filesystem::canonical(path, ec);
if(ec) if(ec)
canonical_path = path; canonical_path = path;
@ -844,7 +851,7 @@ void Directories::colorize_path(boost::filesystem::path dir_path_, bool include_
break; break;
auto path = boost::filesystem::path(it->first); auto path = boost::filesystem::path(it->first);
if(boost::filesystem::exists(path / ".git")) if(boost::filesystem::exists(path / ".git", ec))
break; break;
if(path == path.root_directory()) if(path == path.root_directory())
break; break;

3
src/filesystem.cc

@ -147,9 +147,10 @@ bool filesystem::file_in_path(const boost::filesystem::path &file_path, const bo
boost::filesystem::path filesystem::find_file_in_path_parents(const std::string &file_name, const boost::filesystem::path &path) { boost::filesystem::path filesystem::find_file_in_path_parents(const std::string &file_name, const boost::filesystem::path &path) {
auto current_path = path; auto current_path = path;
boost::system::error_code ec;
while(true) { while(true) {
auto test_path = current_path / file_name; auto test_path = current_path / file_name;
if(boost::filesystem::exists(test_path)) if(boost::filesystem::exists(test_path, ec))
return test_path; return test_path;
if(current_path == current_path.root_directory()) if(current_path == current_path.root_directory())
return boost::filesystem::path(); return boost::filesystem::path();

9
src/juci.cc

@ -27,15 +27,16 @@ int Application::on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>
boost::filesystem::path path(argv[c]); boost::filesystem::path path(argv[c]);
if(path.is_relative() && !current_path_ec) if(path.is_relative() && !current_path_ec)
path = current_path / path; path = current_path / path;
if(boost::filesystem::exists(path)) { boost::system::error_code ec;
if(boost::filesystem::is_regular_file(path)) if(boost::filesystem::exists(path, ec)) {
if(boost::filesystem::is_regular_file(path, ec))
files.emplace_back(path, 0); files.emplace_back(path, 0);
else if(boost::filesystem::is_directory(path)) else if(boost::filesystem::is_directory(path, ec))
directories.emplace_back(path); directories.emplace_back(path);
} }
//Open new file if parent path exists //Open new file if parent path exists
else { else {
if(path.is_absolute() && boost::filesystem::is_directory(path.parent_path())) if(path.is_absolute() && boost::filesystem::is_directory(path.parent_path(), ec))
files.emplace_back(path, 0); files.emplace_back(path, 0);
else else
errors.emplace_back("Error: could not create " + path.string() + ".\n"); errors.emplace_back("Error: could not create " + path.string() + ".\n");

19
src/meson.cc

@ -17,10 +17,11 @@ Meson::Meson(const boost::filesystem::path &path) {
return false; return false;
}; };
auto search_path = boost::filesystem::is_directory(path) ? path : path.parent_path(); boost::system::error_code ec;
auto search_path = boost::filesystem::is_directory(path, ec) ? path : path.parent_path();
while(true) { while(true) {
auto search_file = search_path / "meson.build"; auto search_file = search_path / "meson.build";
if(boost::filesystem::exists(search_file)) { if(boost::filesystem::exists(search_file, ec)) {
if(find_project(search_file)) { if(find_project(search_file)) {
project_path = search_path; project_path = search_path;
break; break;
@ -33,10 +34,11 @@ Meson::Meson(const boost::filesystem::path &path) {
} }
bool Meson::update_default_build(const boost::filesystem::path &default_build_path, bool force) { bool Meson::update_default_build(const boost::filesystem::path &default_build_path, bool force) {
if(project_path.empty() || !boost::filesystem::exists(project_path / "meson.build") || default_build_path.empty()) boost::system::error_code ec;
if(project_path.empty() || !boost::filesystem::exists(project_path / "meson.build", ec) || default_build_path.empty())
return false; return false;
if(!boost::filesystem::exists(default_build_path)) { if(!boost::filesystem::exists(default_build_path, ec)) {
boost::system::error_code ec; boost::system::error_code ec;
boost::filesystem::create_directories(default_build_path, ec); boost::filesystem::create_directories(default_build_path, ec);
if(ec) { if(ec) {
@ -46,7 +48,7 @@ bool Meson::update_default_build(const boost::filesystem::path &default_build_pa
} }
auto compile_commands_path = default_build_path / "compile_commands.json"; auto compile_commands_path = default_build_path / "compile_commands.json";
bool compile_commands_exists = boost::filesystem::exists(compile_commands_path); bool compile_commands_exists = boost::filesystem::exists(compile_commands_path, ec);
if(!force && compile_commands_exists) if(!force && compile_commands_exists)
return true; return true;
@ -61,10 +63,11 @@ bool Meson::update_default_build(const boost::filesystem::path &default_build_pa
} }
bool Meson::update_debug_build(const boost::filesystem::path &debug_build_path, bool force) { bool Meson::update_debug_build(const boost::filesystem::path &debug_build_path, bool force) {
if(project_path.empty() || !boost::filesystem::exists(project_path / "meson.build") || debug_build_path.empty()) boost::system::error_code ec;
if(project_path.empty() || !boost::filesystem::exists(project_path / "meson.build", ec) || debug_build_path.empty())
return false; return false;
if(!boost::filesystem::exists(debug_build_path)) { if(!boost::filesystem::exists(debug_build_path, ec)) {
boost::system::error_code ec; boost::system::error_code ec;
boost::filesystem::create_directories(debug_build_path, ec); boost::filesystem::create_directories(debug_build_path, ec);
if(ec) { if(ec) {
@ -73,7 +76,7 @@ bool Meson::update_debug_build(const boost::filesystem::path &debug_build_path,
} }
} }
bool compile_commands_exists = boost::filesystem::exists(debug_build_path / "compile_commands.json"); bool compile_commands_exists = boost::filesystem::exists(debug_build_path / "compile_commands.json", ec);
if(!force && compile_commands_exists) if(!force && compile_commands_exists)
return true; return true;

18
src/notebook.cc

@ -1,6 +1,5 @@
#include "notebook.h" #include "notebook.h"
#include "config.h" #include "config.h"
#include "directories.h"
#include "filesystem.h" #include "filesystem.h"
#include "gtksourceview-3.0/gtksourceview/gtksourcemap.h" #include "gtksourceview-3.0/gtksourceview/gtksourcemap.h"
#include "project.h" #include "project.h"
@ -104,6 +103,12 @@ std::vector<Source::View *> &Notebook::get_views() {
} }
void Notebook::open(const boost::filesystem::path &file_path_, Position position) { void Notebook::open(const boost::filesystem::path &file_path_, Position position) {
boost::system::error_code ec;
if(file_path_.empty() || (boost::filesystem::exists(file_path_, ec) && !boost::filesystem::is_regular_file(file_path_, ec))) {
Terminal::get().print("Error: could not open " + file_path_.string() + "\n", true);
return;
}
auto file_path = filesystem::get_normal_path(file_path_); auto file_path = filesystem::get_normal_path(file_path_);
if((position == Position::right || position == Position::split) && !split) if((position == Position::right || position == Position::split) && !split)
@ -130,7 +135,7 @@ void Notebook::open(const boost::filesystem::path &file_path_, Position position
} }
} }
if(boost::filesystem::exists(file_path)) { if(boost::filesystem::exists(file_path, ec)) {
std::ifstream can_read(file_path.string()); std::ifstream can_read(file_path.string());
if(!can_read) { if(!can_read) {
Terminal::get().print("Error: could not open " + file_path.string() + "\n", true); Terminal::get().print("Error: could not open " + file_path.string() + "\n", true);
@ -615,15 +620,6 @@ void Notebook::toggle_tabs() {
notebook.set_show_tabs(!notebook.get_show_tabs()); notebook.set_show_tabs(!notebook.get_show_tabs());
} }
boost::filesystem::path Notebook::get_current_folder() {
if(!Directories::get().path.empty())
return Directories::get().path;
else if(auto view = get_current_view())
return view->file_path.parent_path();
else
return boost::filesystem::path();
}
std::vector<std::pair<size_t, Source::View *>> Notebook::get_notebook_views() { std::vector<std::pair<size_t, Source::View *>> Notebook::get_notebook_views() {
std::vector<std::pair<size_t, Source::View *>> notebook_views; std::vector<std::pair<size_t, Source::View *>> notebook_views;
for(size_t notebook_index = 0; notebook_index < notebooks.size(); ++notebook_index) { for(size_t notebook_index = 0; notebook_index < notebooks.size(); ++notebook_index) {

1
src/notebook.h

@ -47,7 +47,6 @@ public:
void toggle_split(); void toggle_split();
/// Hide/Show tabs. /// Hide/Show tabs.
void toggle_tabs(); void toggle_tabs();
boost::filesystem::path get_current_folder();
std::vector<std::pair<size_t, Source::View *>> get_notebook_views(); std::vector<std::pair<size_t, Source::View *>> get_notebook_views();
Gtk::Label status_location; Gtk::Label status_location;

83
src/project.cc

@ -29,9 +29,33 @@ std::string Project::debug_status;
std::shared_ptr<Project::Base> Project::current; std::shared_ptr<Project::Base> Project::current;
std::unique_ptr<Project::DebugOptions> Project::Base::debug_options; std::unique_ptr<Project::DebugOptions> Project::Base::debug_options;
Gtk::Label &Project::debug_status_label() { boost::filesystem::path Project::get_preferably_view_folder() {
static Gtk::Label label; boost::filesystem::path view_folder;
return label; if(auto view = Notebook::get().get_current_view())
return view->file_path.parent_path();
else if(!Directories::get().path.empty())
return Directories::get().path;
else {
boost::system::error_code ec;
auto current_path = boost::filesystem::current_path(ec);
if(ec)
return boost::filesystem::path();
return current_path;
}
}
boost::filesystem::path Project::get_preferably_directory_folder() {
if(!Directories::get().path.empty())
return Directories::get().path;
else if(auto view = Notebook::get().get_current_view())
return view->file_path.parent_path();
else {
boost::system::error_code ec;
auto current_path = boost::filesystem::current_path(ec);
if(ec)
return boost::filesystem::path();
return current_path;
}
} }
void Project::save_files(const boost::filesystem::path &path) { void Project::save_files(const boost::filesystem::path &path) {
@ -89,6 +113,11 @@ void Project::on_save(size_t index) {
} }
} }
Gtk::Label &Project::debug_status_label() {
static Gtk::Label label;
return label;
}
void Project::debug_update_status(const std::string &new_debug_status) { void Project::debug_update_status(const std::string &new_debug_status) {
debug_status = new_debug_status; debug_status = new_debug_status;
if(debug_status.empty()) if(debug_status.empty())
@ -190,22 +219,8 @@ void Project::Base::recreate_build() {
} }
void Project::Base::show_symbols() { void Project::Base::show_symbols() {
auto view = Notebook::get().get_current_view(); auto view_folder = get_preferably_view_folder();
auto pair = Ctags::get_result(view_folder);
boost::filesystem::path search_path;
if(view)
search_path = view->file_path.parent_path();
else if(!Directories::get().path.empty())
search_path = Directories::get().path;
else {
boost::system::error_code ec;
search_path = boost::filesystem::current_path(ec);
if(ec) {
Terminal::get().print("Error: could not find current path\n", true);
return;
}
}
auto pair = Ctags::get_result(search_path);
auto path = std::move(pair.first); auto path = std::move(pair.first);
auto stream = std::move(pair.second); auto stream = std::move(pair.second);
@ -216,6 +231,7 @@ void Project::Base::show_symbols() {
} }
stream->seekg(0, std::ios::beg); stream->seekg(0, std::ios::beg);
auto view = Notebook::get().get_current_view();
if(view) if(view)
SelectionDialog::create(view, true, true); SelectionDialog::create(view, true, true);
else else
@ -239,7 +255,8 @@ void Project::Base::show_symbols() {
return; return;
auto offset = rows[index]; auto offset = rows[index];
auto full_path = path / offset.file_path; auto full_path = path / offset.file_path;
if(!boost::filesystem::is_regular_file(full_path)) boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(full_path, ec))
return; return;
Notebook::get().open(full_path); Notebook::get().open(full_path);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
@ -780,7 +797,8 @@ 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.file)) boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(offset.file, ec))
return; return;
Notebook::get().open(offset.file); Notebook::get().open(offset.file);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
@ -880,8 +898,9 @@ void Project::Clang::recreate_build() {
return; return;
auto debug_build_path = build->get_debug_path(); auto debug_build_path = build->get_debug_path();
bool has_default_build = boost::filesystem::exists(default_build_path); boost::system::error_code ec;
bool has_debug_build = !debug_build_path.empty() && boost::filesystem::exists(debug_build_path); bool has_default_build = boost::filesystem::exists(default_build_path, ec);
bool has_debug_build = !debug_build_path.empty() && boost::filesystem::exists(debug_build_path, ec);
if(has_default_build || has_debug_build) { if(has_default_build || has_debug_build) {
Gtk::MessageDialog dialog(*static_cast<Gtk::Window *>(Notebook::get().get_toplevel()), "Recreate Build", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); Gtk::MessageDialog dialog(*static_cast<Gtk::Window *>(Notebook::get().get_toplevel()), "Recreate Build", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
@ -899,10 +918,20 @@ void Project::Clang::recreate_build() {
return; return;
Usages::Clang::erase_all_caches_for_project(build->project_path, default_build_path); Usages::Clang::erase_all_caches_for_project(build->project_path, default_build_path);
try { try {
if(has_default_build) if(has_default_build) {
boost::filesystem::remove_all(default_build_path); std::vector<boost::filesystem::path> paths;
if(has_debug_build) for(boost::filesystem::directory_iterator it(default_build_path), end; it != end; ++it)
boost::filesystem::remove_all(debug_build_path); paths.emplace_back(*it);
for(auto &path : paths)
boost::filesystem::remove_all(path);
}
if(has_debug_build && boost::filesystem::exists(debug_build_path)) {
std::vector<boost::filesystem::path> paths;
for(boost::filesystem::directory_iterator it(debug_build_path), end; it != end; ++it)
paths.emplace_back(*it);
for(auto &path : paths)
boost::filesystem::remove_all(path);
}
} }
catch(const std::exception &e) { catch(const std::exception &e) {
Terminal::get().print(std::string("Error: could not remove build: ") + e.what() + "\n", true); Terminal::get().print(std::string("Error: could not remove build: ") + e.what() + "\n", true);

12
src/project.h

@ -9,6 +9,13 @@
#include <unordered_map> #include <unordered_map>
namespace Project { namespace Project {
/// Returns folder of current view if any. Otherwise returns directory folder if any, or if not, working directory.
boost::filesystem::path get_preferably_view_folder();
/// Returns directory folder if any. Otherwise returns folder of current view if any, or if not, working directory.
boost::filesystem::path get_preferably_directory_folder();
void save_files(const boost::filesystem::path &path);
void on_save(size_t index);
class DebugRunArguments { class DebugRunArguments {
public: public:
std::string arguments; std::string arguments;
@ -22,10 +29,6 @@ namespace Project {
Gtk::Box vbox; Gtk::Box vbox;
}; };
Gtk::Label &debug_status_label();
void save_files(const boost::filesystem::path &path);
void on_save(size_t index);
extern boost::filesystem::path debug_last_stop_file_path; extern boost::filesystem::path debug_last_stop_file_path;
extern std::unordered_map<std::string, std::string> run_arguments; extern std::unordered_map<std::string, std::string> run_arguments;
extern std::unordered_map<std::string, DebugRunArguments> debug_run_arguments; extern std::unordered_map<std::string, DebugRunArguments> debug_run_arguments;
@ -33,6 +36,7 @@ namespace Project {
extern std::atomic<bool> debugging; extern std::atomic<bool> debugging;
extern std::pair<boost::filesystem::path, std::pair<int, int>> debug_stop; extern std::pair<boost::filesystem::path, std::pair<int, int>> debug_stop;
extern std::string debug_status; extern std::string debug_status;
Gtk::Label &debug_status_label();
void debug_update_status(const std::string &new_debug_status); void debug_update_status(const std::string &new_debug_status);
void debug_activate_menu_items(); void debug_activate_menu_items();
void debug_update_stop(); void debug_update_stop();

24
src/project_build.cc

@ -4,10 +4,14 @@
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
std::unique_ptr<Project::Build> Project::Build::create(const boost::filesystem::path &path) { std::unique_ptr<Project::Build> Project::Build::create(const boost::filesystem::path &path) {
auto search_path = boost::filesystem::is_directory(path) ? path : path.parent_path(); if(path.empty())
return std::make_unique<Project::Build>();
boost::system::error_code ec;
auto search_path = boost::filesystem::is_directory(path, ec) ? path : path.parent_path();
while(true) { while(true) {
if(boost::filesystem::exists(search_path / "CMakeLists.txt")) { if(boost::filesystem::exists(search_path / "CMakeLists.txt", ec)) {
std::unique_ptr<Project::Build> build(new CMakeBuild(path)); std::unique_ptr<Project::Build> build(new CMakeBuild(path));
if(!build->project_path.empty()) if(!build->project_path.empty())
return build; return build;
@ -15,30 +19,30 @@ std::unique_ptr<Project::Build> Project::Build::create(const boost::filesystem::
return std::make_unique<Project::Build>(); return std::make_unique<Project::Build>();
} }
if(boost::filesystem::exists(search_path / "meson.build")) { if(boost::filesystem::exists(search_path / "meson.build"), ec) {
std::unique_ptr<Project::Build> build(new MesonBuild(path)); std::unique_ptr<Project::Build> build(new MesonBuild(path));
if(!build->project_path.empty()) if(!build->project_path.empty())
return build; return build;
} }
if(boost::filesystem::exists(search_path / Config::get().project.default_build_path / "compile_commands.json")) { if(boost::filesystem::exists(search_path / Config::get().project.default_build_path / "compile_commands.json", ec)) {
std::unique_ptr<Project::Build> build(new CompileCommandsBuild(search_path)); std::unique_ptr<Project::Build> build(new CompileCommandsBuild(search_path));
return build; return build;
} }
if(boost::filesystem::exists(search_path / "Cargo.toml")) { if(boost::filesystem::exists(search_path / "Cargo.toml", ec)) {
std::unique_ptr<Project::Build> build(new CargoBuild()); std::unique_ptr<Project::Build> build(new CargoBuild());
build->project_path = search_path; build->project_path = search_path;
return build; return build;
} }
if(boost::filesystem::exists(search_path / "package.json")) { if(boost::filesystem::exists(search_path / "package.json", ec)) {
std::unique_ptr<Project::Build> build(new NpmBuild()); std::unique_ptr<Project::Build> build(new NpmBuild());
build->project_path = search_path; build->project_path = search_path;
return build; return build;
} }
if(boost::filesystem::exists(search_path / "__main__.py")) { if(boost::filesystem::exists(search_path / "__main__.py", ec)) {
std::unique_ptr<Project::Build> build(new PythonMain()); std::unique_ptr<Project::Build> build(new PythonMain());
build->project_path = search_path; build->project_path = search_path;
return build; return build;
@ -107,7 +111,8 @@ boost::filesystem::path Project::CMakeBuild::get_executable(const boost::filesys
auto executable = cmake.get_executable(default_path, path); auto executable = cmake.get_executable(default_path, path);
if(executable.empty()) { if(executable.empty()) {
auto src_path = project_path / "src"; auto src_path = project_path / "src";
if(boost::filesystem::is_directory(src_path)) { boost::system::error_code ec;
if(boost::filesystem::is_directory(src_path, ec)) {
auto cmake = CMake(src_path); // ignore cache in this->cmake auto cmake = CMake(src_path); // ignore cache in this->cmake
executable = cmake.get_executable(default_path, src_path); executable = cmake.get_executable(default_path, src_path);
} }
@ -136,7 +141,8 @@ boost::filesystem::path Project::MesonBuild::get_executable(const boost::filesys
auto executable = meson.get_executable(default_path, path); auto executable = meson.get_executable(default_path, path);
if(executable.empty()) { if(executable.empty()) {
auto src_path = project_path / "src"; auto src_path = project_path / "src";
if(boost::filesystem::is_directory(src_path)) boost::system::error_code ec;
if(boost::filesystem::is_directory(src_path, ec))
executable = meson.get_executable(default_path, src_path); executable = meson.get_executable(default_path, src_path);
} }
return executable; return executable;

3
src/source_generic.cc

@ -28,9 +28,10 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const
auto search_paths = language_manager->get_search_path(); auto search_paths = language_manager->get_search_path();
bool found_language_file = false; bool found_language_file = false;
boost::filesystem::path language_file; boost::filesystem::path language_file;
boost::system::error_code ec;
for(auto &search_path : search_paths) { for(auto &search_path : search_paths) {
boost::filesystem::path p(static_cast<std::string>(search_path) + '/' + static_cast<std::string>(language->get_id()) + ".lang"); boost::filesystem::path p(static_cast<std::string>(search_path) + '/' + static_cast<std::string>(language->get_id()) + ".lang");
if(boost::filesystem::exists(p) && boost::filesystem::is_regular_file(p)) { if(boost::filesystem::exists(p, ec) && boost::filesystem::is_regular_file(p, ec)) {
language_file = p; language_file = p;
found_language_file = true; found_language_file = true;
break; break;

3
src/source_language_protocol.cc

@ -490,8 +490,9 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() {
auto style_file_search_path = file_path.parent_path(); auto style_file_search_path = file_path.parent_path();
auto style_file = '.' + language_id + "-format"; auto style_file = '.' + language_id + "-format";
boost::system::error_code ec;
while(true) { while(true) {
if(boost::filesystem::exists(style_file_search_path / style_file)) { if(boost::filesystem::exists(style_file_search_path / style_file, ec)) {
has_style_file = true; has_style_file = true;
break; break;
} }

10
src/terminal.cc

@ -347,11 +347,12 @@ bool Terminal::on_button_press_event(GdkEventButton *button_event) {
if(path.is_relative()) { if(path.is_relative()) {
if(Project::current) { if(Project::current) {
if(boost::filesystem::exists(Project::current->build->get_default_path() / path)) boost::system::error_code ec;
if(boost::filesystem::exists(Project::current->build->get_default_path() / path, ec))
path = Project::current->build->get_default_path() / path; path = Project::current->build->get_default_path() / path;
else if(boost::filesystem::exists(Project::current->build->get_debug_path() / path)) else if(boost::filesystem::exists(Project::current->build->get_debug_path() / path, ec))
path = Project::current->build->get_debug_path() / path; path = Project::current->build->get_debug_path() / path;
else if(boost::filesystem::exists(Project::current->build->project_path / path)) else if(boost::filesystem::exists(Project::current->build->project_path / path, ec))
path = Project::current->build->project_path / path; path = Project::current->build->project_path / path;
else else
return Gtk::TextView::on_button_press_event(button_event); return Gtk::TextView::on_button_press_event(button_event);
@ -359,7 +360,8 @@ bool Terminal::on_button_press_event(GdkEventButton *button_event) {
else else
return Gtk::TextView::on_button_press_event(button_event); return Gtk::TextView::on_button_press_event(button_event);
} }
if(boost::filesystem::is_regular_file(path)) { boost::system::error_code ec;
if(boost::filesystem::is_regular_file(path, ec)) {
Notebook::get().open(path); Notebook::get().open(path);
if(auto view = Notebook::get().get_current_view()) { if(auto view = Notebook::get().get_current_view()) {
try { try {

3
src/tooltips.cc

@ -129,7 +129,8 @@ void Tooltip::show(bool disregard_drawn, const std::function<void()> &on_motion)
if(auto view = dynamic_cast<Source::View *>(text_view)) if(auto view = dynamic_cast<Source::View *>(text_view))
path = filesystem::get_normal_path(view->file_path.parent_path() / path); path = filesystem::get_normal_path(view->file_path.parent_path() / path);
if(boost::filesystem::is_regular_file(path)) { boost::system::error_code ec;
if(boost::filesystem::is_regular_file(path, ec)) {
Notebook::get().open(path); Notebook::get().open(path);
if(auto view = Notebook::get().get_current_view()) { if(auto view = Notebook::get().get_current_view()) {
try { try {

10
src/usages_clang.cc

@ -372,7 +372,7 @@ void Usages::Clang::erase_all_caches_for_project(const boost::filesystem::path &
boost::system::error_code ec; boost::system::error_code ec;
auto usages_clang_path = build_path / cache_folder; auto usages_clang_path = build_path / cache_folder;
if(boost::filesystem::exists(usages_clang_path, ec) && boost::filesystem::is_directory(usages_clang_path, ec)) { if(boost::filesystem::exists(usages_clang_path, ec) && boost::filesystem::is_directory(usages_clang_path, ec)) {
for(boost::filesystem::directory_iterator it(usages_clang_path), end; it != end; ++it) { for(boost::filesystem::directory_iterator it(usages_clang_path, ec), end; it != end; ++it) {
if(it->path().extension() == ".usages") if(it->path().extension() == ".usages")
boost::filesystem::remove(it->path(), ec); boost::filesystem::remove(it->path(), ec);
} }
@ -515,9 +515,11 @@ Usages::Clang::PathSet Usages::Clang::find_paths(const boost::filesystem::path &
CompileCommands compile_commands(build_path); CompileCommands compile_commands(build_path);
for(boost::filesystem::recursive_directory_iterator it(project_path), end; it != end; ++it) { boost::system::error_code ec;
for(boost::filesystem::recursive_directory_iterator it(project_path, ec), end; it != end; ++it) {
auto &path = it->path(); auto &path = it->path();
if(!boost::filesystem::is_regular_file(path)) { boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(path, ec)) {
if(path == build_path || path == debug_path || path.filename() == ".git") if(path == build_path || path == debug_path || path.filename() == ".git")
it.no_push(); it.no_push();
continue; continue;
@ -677,7 +679,7 @@ void Usages::Clang::write_cache(const boost::filesystem::path &path, const Clang
if(ec) if(ec)
return; return;
} }
else if(!boost::filesystem::is_directory(cache_path, ec) || ec) else if(!boost::filesystem::is_directory(cache_path, ec))
return; return;
auto path_str = filesystem::get_relative_path(path, cache.project_path).string(); auto path_str = filesystem::get_relative_path(path, cache.project_path).string();

159
src/window.cc

@ -270,17 +270,18 @@ void Window::set_menu_actions() {
}); });
menu.add_action("file_new_file", []() { menu.add_action("file_new_file", []() {
boost::filesystem::path path = Dialog::new_file(Notebook::get().get_current_folder()); boost::filesystem::path path = Dialog::new_file(Project::get_preferably_view_folder());
if(path != "") { if(!path.empty()) {
if(boost::filesystem::exists(path)) { boost::system::error_code ec;
if(boost::filesystem::exists(path, ec)) {
Terminal::get().print("Error: " + path.string() + " already exists.\n", true); Terminal::get().print("Error: " + path.string() + " already exists.\n", true);
} }
else { else {
if(filesystem::write(path)) { if(filesystem::write(path)) {
if(Directories::get().path != "") if(!Directories::get().path.empty())
Directories::get().update(); Directories::get().update();
Notebook::get().open(path); Notebook::get().open(path);
if(Directories::get().path != "") if(!Directories::get().path.empty())
Directories::get().on_save_file(path); Directories::get().on_save_file(path);
Terminal::get().print("New file " + path.string() + " created.\n"); Terminal::get().print("New file " + path.string() + " created.\n");
} }
@ -291,12 +292,12 @@ void Window::set_menu_actions() {
}); });
menu.add_action("file_new_folder", []() { menu.add_action("file_new_folder", []() {
auto time_now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); auto time_now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
boost::filesystem::path path = Dialog::new_folder(Notebook::get().get_current_folder()); boost::filesystem::path path = Dialog::new_folder(Project::get_preferably_directory_folder());
if(path != "" && boost::filesystem::exists(path)) { boost::system::error_code ec;
boost::system::error_code ec; if(!path.empty() && boost::filesystem::exists(path, ec)) {
auto last_write_time = boost::filesystem::last_write_time(path, ec); auto last_write_time = boost::filesystem::last_write_time(path, ec);
if(!ec && last_write_time >= time_now) { if(!ec && last_write_time >= time_now) {
if(Directories::get().path != "") if(!Directories::get().path.empty())
Directories::get().update(); Directories::get().update();
Terminal::get().print("New folder " + path.string() + " created.\n"); Terminal::get().print("New folder " + path.string() + " created.\n");
} }
@ -306,8 +307,8 @@ void Window::set_menu_actions() {
} }
}); });
menu.add_action("file_new_project_c", []() { menu.add_action("file_new_project_c", []() {
boost::filesystem::path project_path = Dialog::new_folder(Notebook::get().get_current_folder()); boost::filesystem::path project_path = Dialog::new_folder(Project::get_preferably_directory_folder());
if(project_path != "") { if(!project_path.empty()) {
auto project_name = project_path.filename().string(); auto project_name = project_path.filename().string();
for(auto &chr : project_name) { for(auto &chr : project_name) {
if(chr == ' ') if(chr == ' ')
@ -330,15 +331,16 @@ void Window::set_menu_actions() {
} }
auto c_main_path = project_path / "main.c"; auto c_main_path = project_path / "main.c";
auto clang_format_path = project_path / ".clang-format"; auto clang_format_path = project_path / ".clang-format";
if(boost::filesystem::exists(build_config_path)) { boost::system::error_code ec;
if(boost::filesystem::exists(build_config_path, ec)) {
Terminal::get().print("Error: " + build_config_path.string() + " already exists.\n", true); Terminal::get().print("Error: " + build_config_path.string() + " already exists.\n", true);
return; return;
} }
if(boost::filesystem::exists(c_main_path)) { if(boost::filesystem::exists(c_main_path, ec)) {
Terminal::get().print("Error: " + c_main_path.string() + " already exists.\n", true); Terminal::get().print("Error: " + c_main_path.string() + " already exists.\n", true);
return; return;
} }
if(boost::filesystem::exists(clang_format_path)) { if(boost::filesystem::exists(clang_format_path, ec)) {
Terminal::get().print("Error: " + clang_format_path.string() + " already exists.\n", true); Terminal::get().print("Error: " + clang_format_path.string() + " already exists.\n", true);
return; return;
} }
@ -355,8 +357,8 @@ void Window::set_menu_actions() {
} }
}); });
menu.add_action("file_new_project_cpp", []() { menu.add_action("file_new_project_cpp", []() {
boost::filesystem::path project_path = Dialog::new_folder(Notebook::get().get_current_folder()); boost::filesystem::path project_path = Dialog::new_folder(Project::get_preferably_directory_folder());
if(project_path != "") { if(!project_path.empty()) {
auto project_name = project_path.filename().string(); auto project_name = project_path.filename().string();
for(auto &chr : project_name) { for(auto &chr : project_name) {
if(chr == ' ') if(chr == ' ')
@ -379,15 +381,16 @@ void Window::set_menu_actions() {
} }
auto cpp_main_path = project_path / "main.cpp"; auto cpp_main_path = project_path / "main.cpp";
auto clang_format_path = project_path / ".clang-format"; auto clang_format_path = project_path / ".clang-format";
if(boost::filesystem::exists(build_config_path)) { boost::system::error_code ec;
if(boost::filesystem::exists(build_config_path, ec)) {
Terminal::get().print("Error: " + build_config_path.string() + " already exists.\n", true); Terminal::get().print("Error: " + build_config_path.string() + " already exists.\n", true);
return; return;
} }
if(boost::filesystem::exists(cpp_main_path)) { if(boost::filesystem::exists(cpp_main_path, ec)) {
Terminal::get().print("Error: " + cpp_main_path.string() + " already exists.\n", true); Terminal::get().print("Error: " + cpp_main_path.string() + " already exists.\n", true);
return; return;
} }
if(boost::filesystem::exists(clang_format_path)) { if(boost::filesystem::exists(clang_format_path, ec)) {
Terminal::get().print("Error: " + clang_format_path.string() + " already exists.\n", true); Terminal::get().print("Error: " + clang_format_path.string() + " already exists.\n", true);
return; return;
} }
@ -405,24 +408,20 @@ void Window::set_menu_actions() {
}); });
menu.add_action("file_open_file", []() { menu.add_action("file_open_file", []() {
auto folder_path = Notebook::get().get_current_folder(); auto path = Dialog::open_file(Project::get_preferably_view_folder());
if(auto view = Notebook::get().get_current_view()) { if(!path.empty())
if(!Directories::get().path.empty() && !filesystem::file_in_path(view->file_path, Directories::get().path))
folder_path = view->file_path.parent_path();
}
auto path = Dialog::open_file(folder_path);
if(path != "")
Notebook::get().open(path); Notebook::get().open(path);
}); });
menu.add_action("file_open_folder", []() { menu.add_action("file_open_folder", []() {
auto path = Dialog::open_folder(Notebook::get().get_current_folder()); auto path = Dialog::open_folder(Project::get_preferably_directory_folder());
if(path != "" && boost::filesystem::exists(path)) if(!path.empty())
Directories::get().open(path); Directories::get().open(path);
}); });
menu.add_action("file_reload_file", []() { menu.add_action("file_reload_file", []() {
if(auto view = Notebook::get().get_current_view()) { if(auto view = Notebook::get().get_current_view()) {
if(boost::filesystem::exists(view->file_path)) { boost::system::error_code ec;
if(boost::filesystem::exists(view->file_path, ec)) {
std::ifstream can_read(view->file_path.string()); std::ifstream can_read(view->file_path.string());
if(!can_read) { if(!can_read) {
Terminal::get().print("Error: could not read " + view->file_path.string() + "\n", true); Terminal::get().print("Error: could not read " + view->file_path.string() + "\n", true);
@ -456,12 +455,12 @@ void Window::set_menu_actions() {
menu.add_action("file_save_as", []() { menu.add_action("file_save_as", []() {
if(auto view = Notebook::get().get_current_view()) { if(auto view = Notebook::get().get_current_view()) {
auto path = Dialog::save_file_as(view->file_path); auto path = Dialog::save_file_as(view->file_path);
if(path != "") { if(!path.empty()) {
std::ofstream file(path, std::ofstream::binary); std::ofstream file(path, std::ofstream::binary);
if(file) { if(file) {
file << view->get_buffer()->get_text().raw(); file << view->get_buffer()->get_text().raw();
file.close(); file.close();
if(Directories::get().path != "") if(!Directories::get().path.empty())
Directories::get().update(); Directories::get().update();
Notebook::get().open(path); Notebook::get().open(path);
Terminal::get().print("File saved to: " + Notebook::get().get_current_view()->file_path.string() + "\n"); Terminal::get().print("File saved to: " + Notebook::get().get_current_view()->file_path.string() + "\n");
@ -785,35 +784,20 @@ void Window::set_menu_actions() {
}); });
menu.add_action("source_find_pattern", [this]() { menu.add_action("source_find_pattern", [this]() {
auto view = Notebook::get().get_current_view();
std::string excludes = "--exclude-dir=node_modules"; std::string excludes = "--exclude-dir=node_modules";
boost::filesystem::path search_path; auto view_folder = Project::get_preferably_view_folder();
if(view) auto build = Project::Build::create(view_folder);
search_path = view->file_path.parent_path();
else if(!Directories::get().path.empty())
search_path = Directories::get().path;
else {
boost::system::error_code ec;
search_path = boost::filesystem::current_path(ec);
if(ec) {
Terminal::get().print("Error: could not find current path\n", true);
return;
}
}
auto build = Project::Build::create(search_path);
auto project_path = build->project_path;
boost::filesystem::path default_path, debug_path; boost::filesystem::path default_path, debug_path;
if(!project_path.empty()) { if(!build->project_path.empty()) {
search_path = project_path; view_folder = build->project_path;
excludes += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_default_path(), project_path).string()); excludes += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_default_path(), build->project_path).string());
excludes += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_debug_path(), project_path).string()); excludes += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_debug_path(), build->project_path).string());
} }
EntryBox::get().clear(); EntryBox::get().clear();
EntryBox::get().entries.emplace_back(last_find_pattern, [this, search_path = std::move(search_path), excludes = std::move(excludes)](const std::string &pattern) { EntryBox::get().entries.emplace_back(last_find_pattern, [this, view_folder = std::move(view_folder), excludes = std::move(excludes)](const std::string &pattern) {
if(!pattern.empty()) { if(!pattern.empty()) {
std::stringstream stdin_stream; std::stringstream stdin_stream;
std::string flags; std::string flags;
@ -831,7 +815,7 @@ void Window::set_menu_actions() {
std::string command = Config::get().project.grep_command + " -R " + flags + " --color=always --binary-files=without-match " + excludes + " -n " + escaped_pattern + " *"; std::string command = Config::get().project.grep_command + " -R " + flags + " --color=always --binary-files=without-match " + excludes + " -n " + escaped_pattern + " *";
//TODO: when debian stable gets newer g++ version that supports move on streams, remove unique_ptr below //TODO: when debian stable gets newer g++ version that supports move on streams, remove unique_ptr below
auto stdout_stream = std::make_unique<std::stringstream>(); auto stdout_stream = std::make_unique<std::stringstream>();
Terminal::get().process(stdin_stream, *stdout_stream, command, search_path); Terminal::get().process(stdin_stream, *stdout_stream, command, view_folder);
stdout_stream->seekg(0, std::ios::end); stdout_stream->seekg(0, std::ios::end);
if(stdout_stream->tellg() == 0) { if(stdout_stream->tellg() == 0) {
Info::get().print("Pattern not found"); Info::get().print("Pattern not found");
@ -862,7 +846,7 @@ void Window::set_menu_actions() {
} }
SelectionDialog::get()->add_row(line_markup); SelectionDialog::get()->add_row(line_markup);
} }
SelectionDialog::get()->on_select = [search_path = std::move(search_path)](unsigned int index, const std::string &text, bool hide_window) { SelectionDialog::get()->on_select = [view_folder = std::move(view_folder)](unsigned int index, const std::string &text, bool hide_window) {
auto remove_markup = [](std::string &markup) { auto remove_markup = [](std::string &markup) {
auto start = markup.end(); auto start = markup.end();
for(auto it = markup.begin(); it != markup.end();) { for(auto it = markup.begin(); it != markup.end();) {
@ -888,7 +872,7 @@ void Window::set_menu_actions() {
auto line_str = text.substr(file_end + 1, line_end - file_end); auto line_str = text.substr(file_end + 1, line_end - file_end);
remove_markup(line_str); remove_markup(line_str);
auto line = std::stoi(line_str); auto line = std::stoi(line_str);
Notebook::get().open(search_path / file); Notebook::get().open(view_folder / file);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_pos(line - 1, 0); view->place_cursor_at_line_pos(line - 1, 0);
view->scroll_to_cursor_delayed(true, false); view->scroll_to_cursor_delayed(true, false);
@ -930,30 +914,16 @@ void Window::set_menu_actions() {
}); });
menu.add_action("source_find_file", []() { menu.add_action("source_find_file", []() {
auto view = Notebook::get().get_current_view(); auto view_folder = Project::get_preferably_view_folder();
auto build = Project::Build::create(view_folder);
boost::filesystem::path search_path;
if(view)
search_path = view->file_path.parent_path();
else if(!Directories::get().path.empty())
search_path = Directories::get().path;
else {
boost::system::error_code ec;
search_path = boost::filesystem::current_path(ec);
if(ec) {
Terminal::get().print("Error: could not find current path\n", true);
return;
}
}
auto build = Project::Build::create(search_path);
auto project_path = build->project_path;
boost::filesystem::path default_path, debug_path; boost::filesystem::path default_path, debug_path;
if(!project_path.empty()) { if(!build->project_path.empty()) {
search_path = project_path; view_folder = build->project_path;
default_path = build->get_default_path(); default_path = build->get_default_path();
debug_path = build->get_debug_path(); debug_path = build->get_debug_path();
} }
auto view = Notebook::get().get_current_view();
if(view) if(view)
SelectionDialog::create(view, true, true); SelectionDialog::create(view, true, true);
else else
@ -965,17 +935,18 @@ void Window::set_menu_actions() {
std::vector<boost::filesystem::path> paths; std::vector<boost::filesystem::path> paths;
// populate with all files in search_path // populate with all files in search_path
for(boost::filesystem::recursive_directory_iterator iter(search_path), end; iter != end; iter++) { boost::system::error_code ec;
for(boost::filesystem::recursive_directory_iterator iter(view_folder, ec), end; iter != end; iter++) {
auto path = iter->path(); auto path = iter->path();
// ignore folders // ignore folders
if(!boost::filesystem::is_regular_file(path)) { if(!boost::filesystem::is_regular_file(path, ec)) {
if(path == default_path || path == debug_path || path.filename() == ".git") if(path == default_path || path == debug_path || path.filename() == ".git")
iter.no_push(); iter.no_push();
continue; continue;
} }
// remove project base path // remove project base path
auto row_str = filesystem::get_relative_path(path, search_path).string(); auto row_str = filesystem::get_relative_path(path, view_folder).string();
if(buffer_paths.count(path.string())) if(buffer_paths.count(path.string()))
row_str = "<b>" + row_str + "</b>"; row_str = "<b>" + row_str + "</b>";
paths.emplace_back(path); paths.emplace_back(path);
@ -1013,7 +984,8 @@ void Window::set_menu_actions() {
auto documentation_template = view->get_documentation_template(); auto documentation_template = view->get_documentation_template();
auto offset = std::get<0>(documentation_template); auto offset = std::get<0>(documentation_template);
if(offset) { if(offset) {
if(!boost::filesystem::is_regular_file(offset.file_path)) boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(offset.file_path, ec))
return; return;
Notebook::get().open(offset.file_path); Notebook::get().open(offset.file_path);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
@ -1071,7 +1043,8 @@ void Window::set_menu_actions() {
if(view->get_declaration_location) { if(view->get_declaration_location) {
auto location = view->get_declaration_location(); auto location = view->get_declaration_location();
if(location) { if(location) {
if(!boost::filesystem::is_regular_file(location.file_path)) boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file_path, ec))
return; return;
Notebook::get().open(location.file_path); Notebook::get().open(location.file_path);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
@ -1088,7 +1061,8 @@ void Window::set_menu_actions() {
if(view->get_type_declaration_location) { if(view->get_type_declaration_location) {
auto location = view->get_type_declaration_location(); auto location = view->get_type_declaration_location();
if(location) { if(location) {
if(!boost::filesystem::is_regular_file(location.file_path)) boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file_path, ec))
return; return;
Notebook::get().open(location.file_path); Notebook::get().open(location.file_path);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
@ -1122,7 +1096,8 @@ void Window::set_menu_actions() {
return; return;
else if(rows.size() == 1) { else if(rows.size() == 1) {
auto location = *rows.begin(); auto location = *rows.begin();
if(!boost::filesystem::is_regular_file(location.file_path)) boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file_path, ec))
return; return;
Notebook::get().open(location.file_path); Notebook::get().open(location.file_path);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
@ -1136,7 +1111,8 @@ void Window::set_menu_actions() {
if(index >= rows.size()) if(index >= rows.size())
return; return;
auto location = rows[index]; auto location = rows[index];
if(!boost::filesystem::is_regular_file(location.file_path)) boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file_path, ec))
return; return;
Notebook::get().open(location.file_path); Notebook::get().open(location.file_path);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
@ -1194,7 +1170,8 @@ void Window::set_menu_actions() {
if(index >= rows.size()) if(index >= rows.size())
return; return;
auto offset = rows[index]; auto offset = rows[index];
if(!boost::filesystem::is_regular_file(offset.file_path)) boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(offset.file_path, ec))
return; return;
Notebook::get().open(offset.file_path); Notebook::get().open(offset.file_path);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
@ -1379,12 +1356,12 @@ void Window::set_menu_actions() {
}; };
label_it->update(0, ""); label_it->update(0, "");
EntryBox::get().entries.emplace_back(last_run_command, [this](const std::string &content) { EntryBox::get().entries.emplace_back(last_run_command, [this](const std::string &content) {
if(content != "") { if(!content.empty()) {
last_run_command = content; last_run_command = content;
auto run_path = Notebook::get().get_current_folder(); auto directory_folder = Project::get_preferably_directory_folder();
Terminal::get().async_print("Running: " + content + '\n'); Terminal::get().async_print("Running: " + content + '\n');
Terminal::get().async_process(content, run_path, [content](int exit_status) { Terminal::get().async_process(content, directory_folder, [content](int exit_status) {
Terminal::get().async_print(content + " returned: " + std::to_string(exit_status) + '\n'); Terminal::get().async_print(content + " returned: " + std::to_string(exit_status) + '\n');
}); });
} }
@ -1489,7 +1466,7 @@ void Window::set_menu_actions() {
menu.add_action("debug_run_command", [this]() { menu.add_action("debug_run_command", [this]() {
EntryBox::get().clear(); EntryBox::get().clear();
EntryBox::get().entries.emplace_back(last_run_debug_command, [this](const std::string &content) { EntryBox::get().entries.emplace_back(last_run_debug_command, [this](const std::string &content) {
if(content != "") { if(!content.empty()) {
if(Project::current) if(Project::current)
Project::current->debug_run_command(content); Project::current->debug_run_command(content);
last_run_debug_command = content; last_run_debug_command = content;
@ -2066,8 +2043,7 @@ void Window::save_session() {
for(auto &run_argument : Project::run_arguments) { for(auto &run_argument : Project::run_arguments) {
if(run_argument.second.empty()) if(run_argument.second.empty())
continue; continue;
boost::system::error_code ec; if(boost::filesystem::exists(run_argument.first) && boost::filesystem::is_directory(run_argument.first)) {
if(boost::filesystem::exists(run_argument.first, ec) && boost::filesystem::is_directory(run_argument.first, ec)) {
boost::property_tree::ptree run_argument_pt; boost::property_tree::ptree run_argument_pt;
run_argument_pt.put("path", run_argument.first); run_argument_pt.put("path", run_argument.first);
run_argument_pt.put("arguments", run_argument.second); run_argument_pt.put("arguments", run_argument.second);
@ -2080,8 +2056,7 @@ void Window::save_session() {
for(auto &debug_run_argument : Project::debug_run_arguments) { for(auto &debug_run_argument : Project::debug_run_arguments) {
if(debug_run_argument.second.arguments.empty() && !debug_run_argument.second.remote_enabled && debug_run_argument.second.remote_host_port.empty()) if(debug_run_argument.second.arguments.empty() && !debug_run_argument.second.remote_enabled && debug_run_argument.second.remote_host_port.empty())
continue; continue;
boost::system::error_code ec; if(boost::filesystem::exists(debug_run_argument.first) && boost::filesystem::is_directory(debug_run_argument.first)) {
if(boost::filesystem::exists(debug_run_argument.first, ec) && boost::filesystem::is_directory(debug_run_argument.first, ec)) {
boost::property_tree::ptree debug_run_argument_pt; boost::property_tree::ptree debug_run_argument_pt;
debug_run_argument_pt.put("path", debug_run_argument.first); debug_run_argument_pt.put("path", debug_run_argument.first);
debug_run_argument_pt.put("arguments", debug_run_argument.second.arguments); debug_run_argument_pt.put("arguments", debug_run_argument.second.arguments);

Loading…
Cancel
Save