From 82ea15ea9f3ff1de9875b0effb36b34c5e88a02e Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 5 Aug 2015 16:16:26 +0200 Subject: [PATCH] project_path cleanup. --- src/directories.cc | 18 +++++++++++++----- src/directories.h | 4 ++-- src/juci.cc | 1 - src/notebook.cc | 28 ++++++++++++---------------- src/notebook.h | 1 - src/source.cc | 7 +++---- src/source.h | 6 +++--- src/window.cc | 25 ++++++++++++------------- 8 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/directories.cc b/src/directories.cc index 587ce60..1d2d735 100644 --- a/src/directories.cc +++ b/src/directories.cc @@ -41,9 +41,16 @@ Directories::Directories() { } void Directories::open_folder(const boost::filesystem::path& dir_path) { + auto new_path=dir_path; INFO("Open folder"); + if(new_path=="") { + if(current_path=="") + return; + new_path=current_path; + } + std::vector expanded_paths; - if(last_dir_path==dir_path) { + if(current_path==new_path) { tree_view.map_expanded_rows([&expanded_paths](Gtk::TreeView* tree_view, const Gtk::TreeModel::Path& path){ expanded_paths.emplace_back(path); }); @@ -51,18 +58,19 @@ void Directories::open_folder(const boost::filesystem::path& dir_path) { tree_store->clear(); - if(last_dir_path!=dir_path) - cmake=std::unique_ptr(new CMake(dir_path)); + if(current_path!=new_path) + cmake=std::unique_ptr(new CMake(new_path)); auto project=cmake->get_functions_parameters("project"); if(project.size()>0 && project[0].second.size()>0) tree_view.get_column(0)->set_title(project[0].second[0]); else tree_view.get_column(0)->set_title(""); - add_paths(dir_path, Gtk::TreeModel::Row(), 0); + add_paths(new_path, Gtk::TreeModel::Row(), 0); for(auto &path: expanded_paths) tree_view.expand_row(path, false); - last_dir_path=dir_path; + + current_path=new_path; DEBUG("Folder opened"); } diff --git a/src/directories.h b/src/directories.h index 9f688fe..322aba6 100644 --- a/src/directories.h +++ b/src/directories.h @@ -28,11 +28,12 @@ public: }; Directories(); - void open_folder(const boost::filesystem::path& dir_path); + void open_folder(const boost::filesystem::path& dir_path=""); void select_path(const std::string &path); std::function on_row_activated; std::unique_ptr cmake; + boost::filesystem::path current_path; private: void add_paths(const boost::filesystem::path& dir_path, const Gtk::TreeModel::Row &row, unsigned depth); @@ -40,7 +41,6 @@ private: Gtk::TreeView tree_view; Glib::RefPtr tree_store; ColumnRecord column_record; - boost::filesystem::path last_dir_path; }; #endif // JUCI_DIRECTORIES_H_ diff --git a/src/juci.cc b/src/juci.cc index 629c22b..83d8f14 100644 --- a/src/juci.cc +++ b/src/juci.cc @@ -36,7 +36,6 @@ void Juci::on_activate() { add_window(*window); window->show(); if(directory!="") { - window->notebook.project_path=directory; window->directories.open_folder(directory); } for(auto &f: files) diff --git a/src/notebook.cc b/src/notebook.cc index c66630f..8ebbb8b 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -54,28 +54,24 @@ void Notebook::open(std::string path) { auto language=Source::guess_language(path); if(language && (language->get_id()=="chdr" || language->get_id()=="c" || language->get_id()=="cpp" || language->get_id()=="objc")) { - auto view_project_path=project_path; + std::string project_path; if(directories.cmake && directories.cmake->project_path!="") - view_project_path=directories.cmake->project_path.string(); - if(view_project_path=="") { + project_path=directories.cmake->project_path.string(); + else { auto parent_path=boost::filesystem::path(path).parent_path(); - view_project_path=parent_path.string(); + project_path=parent_path.string(); CMake cmake(parent_path); if(cmake.project_path!="") { - view_project_path=cmake.project_path.string(); - Singleton::terminal()->print("Project path for "+path+" set to "+view_project_path+"\n"); + project_path=cmake.project_path.string(); + Singleton::terminal()->print("Project path for "+path+" set to "+project_path+"\n"); } else Singleton::terminal()->print("Error: could not find project path for "+path+"\n"); } - source_views.emplace_back(new Source::ClangView(path, view_project_path)); - } - else { - auto view_project_path=project_path; - if(view_project_path=="") - view_project_path=boost::filesystem::path(path).parent_path().string(); - source_views.emplace_back(new Source::GenericView(path, view_project_path, language)); + source_views.emplace_back(new Source::ClangView(path, project_path)); } + else + source_views.emplace_back(new Source::GenericView(path, language)); scrolled_windows.emplace_back(new Gtk::ScrolledWindow()); hboxes.emplace_back(new Gtk::HBox()); @@ -125,11 +121,11 @@ bool Notebook::save(int page) { //If CMakeLists.txt have been modified: if(boost::filesystem::path(view->file_path).filename().string()=="CMakeLists.txt") { - if(project_path!="" && directories.cmake && directories.cmake->project_path!="" && CMake::create_compile_commands(directories.cmake->project_path.string())) { - directories.open_folder(project_path); + if(directories.cmake && directories.cmake->project_path!="" && CMake::create_compile_commands(directories.cmake->project_path.string())) { + directories.open_folder(); for(auto source_view: source_views) { if(auto source_clang_view=dynamic_cast(source_view)) { - if(project_path==source_view->project_path) { + if(directories.cmake->project_path.string()==source_clang_view->project_path) { if(source_clang_view->restart_parse()) Singleton::terminal()->print("Reparsing "+source_clang_view->file_path+"\n"); else diff --git a/src/notebook.h b/src/notebook.h index 848c50c..8ad8480 100644 --- a/src/notebook.h +++ b/src/notebook.h @@ -20,7 +20,6 @@ public: void open(std::string filename); bool save(int page); bool save_current(); - std::string project_path; //TODO: remove, and also remove Source::View::project_path (project_path only needed in Source::ClangView) private: bool make_compile_commands(const std::string &path); diff --git a/src/source.cc b/src/source.cc index abbd4c9..70df906 100644 --- a/src/source.cc +++ b/src/source.cc @@ -36,8 +36,7 @@ Glib::RefPtr Source::guess_language(const std::string &file_path) ////////////// //// View //// ////////////// -Source::View::View(const std::string& file_path, const std::string& project_path): -file_path(file_path), project_path(project_path) { +Source::View::View(const std::string& file_path): file_path(file_path) { set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); set_show_line_numbers(Singleton::Config::source()->show_line_numbers); set_highlight_current_line(Singleton::Config::source()->highlight_current_line); @@ -248,7 +247,7 @@ bool Source::View::on_key_press_event(GdkEventKey* key) { ///////////////////// //// GenericView //// ///////////////////// -Source::GenericView::GenericView(const std::string& file_path, const std::string& project_path, Glib::RefPtr language) : View(file_path, project_path) { +Source::GenericView::GenericView(const std::string& file_path, Glib::RefPtr language) : View(file_path) { auto style_scheme_manager=Gsv::StyleSchemeManager::get_default(); //TODO: add?: style_scheme_manager->prepend_search_path("~/.juci/"); auto scheme=style_scheme_manager->get_scheme("classic"); @@ -278,7 +277,7 @@ Source::GenericView::GenericView(const std::string& file_path, const std::string clang::Index Source::ClangViewParse::clang_index(0, 0); Source::ClangViewParse::ClangViewParse(const std::string& file_path, const std::string& project_path): -Source::View(file_path, project_path) { +Source::View(file_path), project_path(project_path) { override_font(Pango::FontDescription(Singleton::Config::source()->font)); override_background_color(Gdk::RGBA(Singleton::Config::source()->background)); override_background_color(Gdk::RGBA(Singleton::Config::source()->background_selected), Gtk::StateFlags::STATE_FLAG_SELECTED); diff --git a/src/source.h b/src/source.h index 7bec853..ed434cd 100644 --- a/src/source.h +++ b/src/source.h @@ -46,7 +46,7 @@ namespace Source { class View : public Gsv::View { public: - View(const std::string& file_path, const std::string& project_path); + View(const std::string& file_path); ~View(); void search_highlight(const std::string &text, bool case_sensitive, bool regex); @@ -58,7 +58,6 @@ namespace Source { void replace_all(const std::string &replacement); std::string file_path; - std::string project_path; std::function()> get_declaration_location; std::function goto_method; @@ -84,12 +83,13 @@ namespace Source { class GenericView : public View { public: - GenericView(const std::string& file_path, const std::string& project_path, Glib::RefPtr language); + GenericView(const std::string& file_path, Glib::RefPtr language); }; class ClangViewParse : public View { public: ClangViewParse(const std::string& file_path, const std::string& project_path); + std::string project_path; protected: void init_parse(); void start_reparse(); diff --git a/src/window.cc b/src/window.cc index ec369f9..04a2fe3 100644 --- a/src/window.cc +++ b/src/window.cc @@ -294,18 +294,18 @@ void Window::new_file_entry() { entry_box.entries.emplace_back("untitled", [this](const std::string& content){ std::string filename=content; if(filename!="") { - if(notebook.project_path!="" && !boost::filesystem::path(filename).is_absolute()) - filename=notebook.project_path+"/"+filename; + if(directories.current_path!="" && !boost::filesystem::path(filename).is_absolute()) + filename=directories.current_path.string()+"/"+filename; boost::filesystem::path p(filename); if(boost::filesystem::exists(p)) { Singleton::terminal()->print("Error: "+p.string()+" already exists.\n"); } else { if(juci::filesystem::write(p)) { - if(notebook.project_path!="") - directories.open_folder(notebook.project_path); - notebook.open(boost::filesystem::canonical(p).string()); - Singleton::terminal()->print("New file "+p.string()+" created.\n"); + if(directories.current_path!="") + directories.open_folder(); + notebook.open(boost::filesystem::canonical(p).string()); + Singleton::terminal()->print("New file "+p.string()+" created.\n"); } else Singleton::terminal()->print("Error: could not create new file "+p.string()+".\n"); @@ -322,8 +322,8 @@ void Window::new_file_entry() { void Window::open_folder_dialog() { Gtk::FileChooserDialog dialog("Please choose a folder", Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); - if(notebook.project_path.size()>0) - gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), notebook.project_path.c_str()); + if(directories.current_path!="") + gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), directories.current_path.string().c_str()); else gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), boost::filesystem::current_path().string().c_str()); dialog.set_transient_for(*this); @@ -335,7 +335,6 @@ void Window::open_folder_dialog() { if(result==Gtk::RESPONSE_OK) { std::string project_path=dialog.get_filename(); - notebook.project_path=project_path; directories.open_folder(project_path); if(notebook.get_current_page()!=-1) directories.select_path(notebook.get_current_view()->file_path); @@ -344,8 +343,8 @@ void Window::open_folder_dialog() { void Window::open_file_dialog() { Gtk::FileChooserDialog dialog("Please choose a file", Gtk::FILE_CHOOSER_ACTION_OPEN); - if(notebook.project_path.size()>0) - gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), notebook.project_path.c_str()); + if(directories.current_path!="") + gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), directories.current_path.string().c_str()); else gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), boost::filesystem::current_path().string().c_str()); dialog.set_transient_for(*this); @@ -399,8 +398,8 @@ void Window::save_file_dialog() { if(file) { file << notebook.get_current_view()->get_buffer()->get_text(); file.close(); - if(notebook.project_path!="") - directories.open_folder(notebook.project_path); + if(directories.current_path!="") + directories.open_folder(); notebook.open(path); Singleton::terminal()->print("File saved to: " + notebook.get_current_view()->file_path+"\n"); }