Browse Source

Rename refactoring implemented.

merge-requests/365/head
eidheim 11 years ago
parent
commit
328fbce8a1
  1. 63
      juci/notebook.cc
  2. 1
      juci/notebook.h
  3. 53
      juci/source.cc
  4. 5
      juci/source.h
  5. 8
      juci/window.cc

63
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<std::string>(CurrentSourceView()->get_token());
if(token->size()>0 && CurrentSourceView()->get_token_name) {
auto token_name=std::make_shared<std::string>(CurrentSourceView()->get_token_name());
for(int c=0;c<Pages();c++) {
if(source_views.at(c)->view->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;c<Pages();c++) {
if(source_views.at(c)->view->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;c<Pages();c++) {
if(source_views.at(c)->view.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;
}

1
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);

53
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<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > 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<Gtk::TextBuffer::Mark>& mark){

5
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<void(int number)> update_search_occurrences;
void search_forward();
@ -67,8 +69,9 @@ public:
std::function<std::pair<std::string, unsigned>()> get_declaration_location;
std::function<void()> goto_method;
std::function<std::string()> get_token;
std::function<std::string()> get_token_name;
std::function<void(const std::string &token)> tag_similar_tokens;
std::function<void(const std::string &token, const std::string &text)> rename_similar_tokens;
std::function<size_t(const std::string &token, const std::string &text)> rename_similar_tokens;
protected:
bool on_key_press_event(GdkEventKey* key);
private:

8
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())){

Loading…
Cancel
Save