From 328fbce8a1b524bc6c6d198fefd51f3f1ba2ab62 Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 27 Jul 2015 13:27:32 +0200 Subject: [PATCH] Rename refactoring implemented. --- juci/notebook.cc | 63 ++++++++++++++++++++++++++++++++++++++---------- juci/notebook.h | 1 - juci/source.cc | 53 +++++++++++++++++++++++++++++++++++++--- juci/source.h | 5 +++- juci/window.cc | 8 ++---- 5 files changed, 106 insertions(+), 24 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 9babea7..e624ee8 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -113,10 +113,43 @@ void Notebook::Controller::CreateKeybindings() { }); menu->action_group->add(Gtk::Action::create("SourceRename", "Rename function/variable"), Gtk::AccelKey(menu->key_map["source_rename"]), [this]() { + entry_box.clear(); if(CurrentPage()!=-1) { - if(CurrentSourceView()->get_token) { - auto token=CurrentSourceView()->get_token(); - CurrentSourceView()->tag_similar_tokens(token); + if(CurrentSourceView()->get_token && CurrentSourceView()->get_token_name) { + auto token=std::make_shared(CurrentSourceView()->get_token()); + if(token->size()>0 && CurrentSourceView()->get_token_name) { + auto token_name=std::make_shared(CurrentSourceView()->get_token_name()); + for(int c=0;cview->tag_similar_tokens) { + source_views.at(c)->view->tag_similar_tokens(*token); + } + } + entry_box.labels.emplace_back(); + auto label_it=entry_box.labels.begin(); + label_it->update=[label_it](int state, const std::string& message){ + label_it->set_text("Warning: only opened and parsed tabs will have its content renamed, and modified files will be saved."); + }; + label_it->update(0, ""); + entry_box.entries.emplace_back(*token_name, [this, token_name, token](const std::string& content){ + if(CurrentPage()!=-1 && content!=*token_name) { + for(int c=0;cview->rename_similar_tokens) { + auto number=source_views.at(c)->view->rename_similar_tokens(*token, content); + if(number>0) { + Singleton::terminal()->print("Replaced "+std::to_string(number)+" occurrences in file "+source_views.at(c)->view->file_path+"\n"); + source_views.at(c)->view->save(); + } + } + } + entry_box.hide(); + } + }); + auto entry_it=entry_box.entries.begin(); + entry_box.buttons.emplace_back("Rename", [this, entry_it](){ + entry_it->activate(); + }); + entry_box.show(); + } } } }); @@ -242,12 +275,21 @@ void Notebook::Controller::open_file(std::string path) { view.notebook.set_focus_child(*source_views.back()->view); CurrentSourceView()->get_buffer()->set_modified(false); //Add star on tab label when the page is not saved: - CurrentSourceView()->get_buffer()->signal_modified_changed().connect([this]() { - boost::filesystem::path file_path(CurrentSourceView()->file_path); + auto source_view=CurrentSourceView(); + CurrentSourceView()->get_buffer()->signal_modified_changed().connect([this, source_view]() { + boost::filesystem::path file_path(source_view->file_path); std::string title=file_path.filename().string(); - if(CurrentSourceView()->get_buffer()->get_modified()) + if(source_view->get_buffer()->get_modified()) title+="*"; - view.notebook.set_tab_label_text(*(view.notebook.get_nth_page(CurrentPage())), title); + int page=-1; + for(int c=0;cview.get()==source_view) { + page=c; + break; + } + } + if(page!=-1) + view.notebook.set_tab_label_text(*(view.notebook.get_nth_page(page)), title); }); } @@ -333,10 +375,6 @@ int Notebook::Controller::Pages() { return view.notebook.get_n_pages(); } -bool Notebook::Controller:: OnSaveFile() { - std::string path=CurrentSourceView()->file_path; - return OnSaveFile(path); -} bool Notebook::Controller:: OnSaveFile(std::string path) { INFO("Notebook save file with path"); if (path != "" && CurrentSourceView()->get_buffer()->get_modified()) { @@ -346,7 +384,6 @@ bool Notebook::Controller:: OnSaveFile(std::string path) { file.close(); boost::filesystem::path path(CurrentSourceView()->file_path); std::string title=path.filename().string(); - view.notebook.set_tab_label_text(*view.notebook.get_nth_page(CurrentPage()), title); CurrentSourceView()->get_buffer()->set_modified(false); return true; } @@ -401,7 +438,7 @@ void Notebook::Controller::AskToSaveDialog() { case(Gtk::RESPONSE_YES): { DEBUG("AskToSaveDialog: save file: yes, trying to save file"); - OnSaveFile(); + CurrentSourceView()->save(); DEBUG("AskToSaveDialog: save file: yes, saved sucess"); break; } diff --git a/juci/notebook.h b/juci/notebook.h index a840a86..b84d983 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -25,7 +25,6 @@ namespace Notebook { int CurrentPage(); void OnCloseCurrentPage(); void OnFileNewFile(); - bool OnSaveFile(); bool OnSaveFile(std::string path); void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); diff --git a/juci/source.cc b/juci/source.cc index e65c38c..d92b8a3 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -56,6 +56,20 @@ file_path(file_path), project_path(project_path) { g_signal_connect(search_context, "notify::occurrences-count", G_CALLBACK(search_occurrences_updated), this); } +bool Source::View::save() { + INFO("Source save file"); + if (file_path != "" && get_buffer()->get_modified()) { + std::ofstream file; + file.open (file_path); + file << get_buffer()->get_text(); + file.close(); + get_buffer()->set_modified(false); + Singleton::terminal()->print("File saved to: " +file_path+"\n"); + return true; + } + return false; +} + void Source::View::search_occurrences_updated(GtkWidget* widget, GParamSpec* property, gpointer data) { auto view=(Source::View*)data; if(view->update_search_occurrences) @@ -913,6 +927,20 @@ Source::ClangViewAutocomplete(file_path, project_path) { return ""; }; + get_token_name=[this]() -> std::string { + if(clang_readable) { + for(auto &token: *clang_tokens) { + if(token.get_kind()==clang::Token_Identifier && token.has_type()) { + auto insert_offset=(unsigned)get_buffer()->get_insert()->get_iter().get_offset(); + if(insert_offset>=token.offsets.first && insert_offset<=token.offsets.second) { + return token.get_spelling(); + } + } + } + } + return ""; + }; + tag_similar_tokens=[this](const std::string &usr){ if(clang_readable) { if(usr.size()>0 && last_similar_tokens_tagged!=usr) { @@ -923,11 +951,30 @@ Source::ClangViewAutocomplete(file_path, project_path) { } last_similar_tokens_tagged=usr; } - if(usr.size()==0 && last_similar_tokens_tagged!="") { - get_buffer()->remove_tag(similar_tokens_tag, get_buffer()->begin(), get_buffer()->end()); - last_similar_tokens_tagged=""; + } + if(usr.size()==0 && last_similar_tokens_tagged!="") { + get_buffer()->remove_tag(similar_tokens_tag, get_buffer()->begin(), get_buffer()->end()); + last_similar_tokens_tagged=""; + } + }; + + rename_similar_tokens=[this](const std::string &usr, const std::string &text) { + size_t number=0; + if(clang_readable) { + auto offsets=clang_tokens->get_similar_token_offsets(usr); + std::vector, Glib::RefPtr > > marks; + for(auto &offset: offsets) { + marks.emplace_back(get_buffer()->create_mark(get_buffer()->get_iter_at_offset(offset.first)), get_buffer()->create_mark(get_buffer()->get_iter_at_offset(offset.second))); + number++; + } + for(auto &mark: marks) { + get_buffer()->erase(mark.first->get_iter(), mark.second->get_iter()); + get_buffer()->insert_with_tag(mark.first->get_iter(), text, similar_tokens_tag); + get_buffer()->delete_mark(mark.first); + get_buffer()->delete_mark(mark.second); } } + return number; }; get_buffer()->signal_mark_set().connect([this](const Gtk::TextBuffer::iterator& iterator, const Glib::RefPtr& mark){ diff --git a/juci/source.h b/juci/source.h index b1c120d..1b1ea53 100644 --- a/juci/source.h +++ b/juci/source.h @@ -50,6 +50,8 @@ public: View(const std::string& file_path, const std::string& project_path); ~View(); + bool save(); + void search_highlight(const std::string &text, bool case_sensitive, bool regex); std::function update_search_occurrences; void search_forward(); @@ -67,8 +69,9 @@ public: std::function()> get_declaration_location; std::function goto_method; std::function get_token; + std::function get_token_name; std::function tag_similar_tokens; - std::function rename_similar_tokens; + std::function rename_similar_tokens; protected: bool on_key_press_event(GdkEventKey* key); private: diff --git a/juci/window.cc b/juci/window.cc index 473cf2e..8494385 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -179,13 +179,9 @@ void Window::OnOpenFile() { } } +//TODO: Move to notebook. Maybe also replace bool with void? bool Window::SaveFile() { - if(Singleton::notebook()->OnSaveFile()) { - Singleton::terminal()->print("File saved to: " + - Singleton::notebook()->CurrentSourceView()->file_path+"\n"); - return true; - } - return false; + return Singleton::notebook()->CurrentSourceView()->save(); } bool Window::SaveFileAs() { if(Singleton::notebook()->OnSaveFile(Singleton::notebook()->OnSaveFileAs())){