From a10a0dc3c078417ad3f3062687af86ad29fe9b2c Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 21 Jun 2015 16:08:24 +0200 Subject: [PATCH 01/25] Fixed marking of changed tabs that are not saved. --- juci/notebook.cc | 7 +++---- juci/source.cc | 22 +++++++++++++--------- juci/source.h | 1 + 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 2d4166f..0f55682 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -301,18 +301,16 @@ Gtk::Box& Notebook::Controller::entry_view() { void Notebook::Controller::OnNewPage(std::string name) { INFO("Notebook Generate new page"); OnCreatePage(); - text_vec_.back()->on_new_empty_file(); Notebook().append_page(*editor_vec_.back(), name); Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); Notebook().set_focus_child(text_vec_.at(Pages()-1)->view); - + text_vec_.back()->on_new_empty_file(); } void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); OnCreatePage(); - text_vec_.back()->on_open_file(path); size_t pos = path.find_last_of("/\\"); std::string filename=path; if(pos!=std::string::npos) @@ -321,6 +319,7 @@ void Notebook::Controller::OnOpenFile(std::string path) { Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); Notebook().set_focus_child(text_vec_.back()->view); + text_vec_.back()->on_open_file(path); } void Notebook::Controller::OnCreatePage() { @@ -339,7 +338,7 @@ void Notebook::Controller::OnCreatePage() { std::string filename=path; if(pos!=std::string::npos) filename=path.substr(pos+1); - Notebook().set_tab_label_text(*Notebook().get_nth_page(CurrentPage()), filename+"*"); + Notebook().set_tab_label_text(*(Notebook().get_nth_page(CurrentPage())), filename+"*"); } }; } diff --git a/juci/source.cc b/juci/source.cc index cd9eab4..d8442e7 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -183,7 +183,7 @@ highlight_token(clang::Token *token, // Constructor for Controller Source::Controller::Controller(const Source::Config &config, const std::vector > &controllers) : - config(config), parser(controllers), parse_thread_go(false), parse_thread_mapped(false), parse_thread_stop(false) { + config(config), parser(controllers), parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { INFO("Source Controller with childs constructed"); view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::on_key_press), false); view.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); @@ -194,13 +194,6 @@ Source::Controller::Controller(const Source::Config &config, for (auto &item : config.tags) { buffer()->create_tag(item.first)->property_foreground() = item.second; } - buffer()->signal_changed().connect([this]() { - if(signal_buffer_changed) - signal_buffer_changed(is_saved); - is_saved=false; - parse_thread_mapped=false; - parse_thread_go=true; - }); } Source::Controller::~Controller() { @@ -238,12 +231,23 @@ void Source::Controller::update_syntax(const std::vector &ranges) } } +void Source::Controller::set_handlers() { + buffer()->signal_changed().connect([this]() { + if(signal_buffer_changed) + signal_buffer_changed(is_saved); + is_saved=false; + parse_thread_mapped=false; + parse_thread_go=true; + }); +} + void Source::Controller::on_new_empty_file() { string filename("/tmp/untitled"); sourcefile s(filename); parser.file_path=filename; parser.project_path=filename; s.save(""); + set_handlers(); } void Source::Controller::on_open_file(const string &filepath) { @@ -253,8 +257,8 @@ void Source::Controller::on_open_file(const string &filepath) { buffer_map[filepath] = s.get_content(); buffer()->get_undo_manager()->begin_not_undoable_action(); buffer()->set_text(s.get_content()); - is_saved=true; buffer()->get_undo_manager()->end_not_undoable_action(); + set_handlers(); int start_offset = buffer()->begin().get_offset(); int end_offset = buffer()->end().get_offset(); if (config.legal_extension(filepath.substr(filepath.find_last_of(".") + 1))) { diff --git a/juci/source.h b/juci/source.h index 99ede6b..6469915 100644 --- a/juci/source.h +++ b/juci/source.h @@ -107,6 +107,7 @@ namespace Source { const std::vector > &controllers); ~Controller(); void update_syntax(const std::vector &locations); + void set_handlers(); void on_new_empty_file(); void on_open_file(const std::string &filename); Glib::RefPtr buffer(); From 26bae99661e6d1fea9a0998a7b60a3e4cb0723bf Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 21 Jun 2015 16:52:24 +0200 Subject: [PATCH 02/25] Fixed save changed file dialog activation, and removed Source::Controller::signal_buffer_changed. --- juci/notebook.cc | 35 +++++++++++++++++++---------------- juci/notebook.h | 2 +- juci/source.cc | 3 --- juci/source.h | 2 -- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 0f55682..d00c298 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -306,6 +306,7 @@ void Notebook::Controller::OnNewPage(std::string name) { Notebook().set_current_page(Pages()-1); Notebook().set_focus_child(text_vec_.at(Pages()-1)->view); text_vec_.back()->on_new_empty_file(); + set_source_handlers(*text_vec_.back()); } void Notebook::Controller::OnOpenFile(std::string path) { @@ -320,6 +321,7 @@ void Notebook::Controller::OnOpenFile(std::string path) { Notebook().set_current_page(Pages()-1); Notebook().set_focus_child(text_vec_.back()->view); text_vec_.back()->on_open_file(path); + set_source_handlers(*text_vec_.back()); } void Notebook::Controller::OnCreatePage() { @@ -329,24 +331,12 @@ void Notebook::Controller::OnCreatePage() { editor_vec_.push_back(new Gtk::HBox()); scrolledtext_vec_.back()->add(text_vec_.back()->view); editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); - TextViewHandlers(text_vec_.back()->view); - //Add star on tab label when the page is not saved: - text_vec_.back()->signal_buffer_changed=[this](bool was_saved) { - if(was_saved) { - std::string path=text_vec_.at(CurrentPage())->parser.file_path; - size_t pos = path.find_last_of("/\\"); - std::string filename=path; - if(pos!=std::string::npos) - filename=path.substr(pos+1); - Notebook().set_tab_label_text(*(Notebook().get_nth_page(CurrentPage())), filename+"*"); - } - }; } void Notebook::Controller::OnCloseCurrentPage() { INFO("Notebook close page"); if (Pages() != 0) { - if(!text_vec_.back()->is_saved){ + if(!text_vec_.at(CurrentPage())->is_saved){ AskToSaveDialog(); } int page = CurrentPage(); @@ -493,12 +483,25 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr }); } -void Notebook::Controller::TextViewHandlers(Gtk::TextView& textview) { - textview.signal_button_release_event(). +void Notebook::Controller::set_source_handlers(Source::Controller& controller) { + controller.view.signal_button_release_event(). connect(sigc::mem_fun(*this, &Notebook::Controller::OnMouseRelease), false); - textview.signal_key_release_event(). + controller.view.signal_key_release_event(). connect(sigc::mem_fun(*this, &Notebook::Controller::OnKeyRelease), false); + + //Add star on tab label when the page is not saved: + controller.buffer()->signal_changed().connect([this]() { + if(text_vec_.at(CurrentPage())->is_saved) { + std::string path=text_vec_.at(CurrentPage())->parser.file_path; + size_t pos = path.find_last_of("/\\"); + std::string filename=path; + if(pos!=std::string::npos) + filename=path.substr(pos+1); + Notebook().set_tab_label_text(*(Notebook().get_nth_page(CurrentPage())), filename+"*"); + } + text_vec_.at(CurrentPage())->is_saved=false; + }); } void Notebook::Controller::PopupSelectHandler(Gtk::Dialog &popup, diff --git a/juci/notebook.h b/juci/notebook.h index 8361dd8..e9305b2 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -74,7 +74,7 @@ namespace Notebook { bool LegalExtension(std::string extension); protected: - void TextViewHandlers(Gtk::TextView& textview); + void set_source_handlers(Source::Controller& controller); void PopupSelectHandler(Gtk::Dialog &popup, Gtk::ListViewText &listview, std::map diff --git a/juci/source.cc b/juci/source.cc index d8442e7..44b0e7b 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -233,9 +233,6 @@ void Source::Controller::update_syntax(const std::vector &ranges) void Source::Controller::set_handlers() { buffer()->signal_changed().connect([this]() { - if(signal_buffer_changed) - signal_buffer_changed(is_saved); - is_saved=false; parse_thread_mapped=false; parse_thread_go=true; }); diff --git a/juci/source.h b/juci/source.h index 6469915..927af94 100644 --- a/juci/source.h +++ b/juci/source.h @@ -118,8 +118,6 @@ namespace Source { Parser parser; View view; - std::function signal_buffer_changed; - private: Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; From 714d87a089137848b7b2f9cc9c4017d99435b028 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 21 Jun 2015 21:13:15 +0200 Subject: [PATCH 03/25] Initial restructure of source.h/cc. If its decided to restructure further, this will form an ok starting point for this work. Source::ClangView is now used for cpp-files, and Source::GenericView is used for other files. Source::View is inherited in both cases. See TODO-comment regarding new files. --- juci/notebook.cc | 57 +++---- juci/notebook.h | 4 +- juci/source.cc | 401 +++++++++++++++++++++++------------------------ juci/source.h | 75 +++++---- juci/window.cc | 4 +- 5 files changed, 253 insertions(+), 288 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index d00c298..177f104 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -21,7 +21,6 @@ Notebook::Controller::Controller(Gtk::Window* window, source_config_(source_cfg) { INFO("Create notebook"); window_ = window; - OnNewPage("untitled"); refClipboard_ = Gtk::Clipboard::get(); ispopup = false; view().pack1(directories_.widget(), true, true); @@ -147,7 +146,7 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller connect( [this]() { if (is_new_file_) { - OnNewPage(entry_.text()); + //OnNewPage(entry_.text()); //TODO: rewrite new file (the file needs to be created before opened with Source::Controller in order to choose the correct view implementation) entry_.OnHideEntries(is_new_file_); } else { Search(true); @@ -156,7 +155,7 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller entry_.button_apply().signal_clicked(). connect( [this]() { - OnNewPage(entry_.text()); + //OnNewPage(entry_.text()); //TODO: rewrite new file (the file needs to be created before opened with Source::Controller in order to choose the correct view implementation) entry_.OnHideEntries(is_new_file_); }); entry_.button_close().signal_clicked(). @@ -191,7 +190,7 @@ bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) { bool Notebook::Controller::GeneratePopup(int key_id) { INFO("Notebook genereate popup, getting iters"); - std::string path = text_vec_.at(CurrentPage())->parser.file_path; + std::string path = text_vec_.at(CurrentPage())->view->file_path; if (!source_config().legal_extension(path.substr(path.find_last_of(".") + 1))) return false; // Get function to fill popup with suggests item vector under is for testing Gtk::TextIter beg = CurrentTextView().get_buffer()->get_insert()->get_iter(); @@ -229,7 +228,7 @@ bool Notebook::Controller::GeneratePopup(int key_id) { return false; } INFO("Notebook genereate popup, getting autocompletions"); - std::vector acdata=text_vec_.at(CurrentPage())->parser. + std::vector acdata=text_vec_.at(CurrentPage())->view-> get_autocomplete_suggestions(beg.get_line()+1, beg.get_line_offset()+2); std::map items; @@ -298,20 +297,14 @@ Gtk::Box& Notebook::Controller::entry_view() { return entry_.view(); } -void Notebook::Controller::OnNewPage(std::string name) { - INFO("Notebook Generate new page"); - OnCreatePage(); - Notebook().append_page(*editor_vec_.back(), name); - Notebook().show_all_children(); - Notebook().set_current_page(Pages()-1); - Notebook().set_focus_child(text_vec_.at(Pages()-1)->view); - text_vec_.back()->on_new_empty_file(); - set_source_handlers(*text_vec_.back()); -} - void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); - OnCreatePage(); + INFO("Notebook create page"); + text_vec_.emplace_back(new Source::Controller(source_config(), path, project_path)); + scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); + editor_vec_.push_back(new Gtk::HBox()); + scrolledtext_vec_.back()->add(*text_vec_.back()->view); + editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); size_t pos = path.find_last_of("/\\"); std::string filename=path; if(pos!=std::string::npos) @@ -319,20 +312,10 @@ void Notebook::Controller::OnOpenFile(std::string path) { Notebook().append_page(*editor_vec_.back(), filename); Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); - Notebook().set_focus_child(text_vec_.back()->view); - text_vec_.back()->on_open_file(path); + Notebook().set_focus_child(*text_vec_.back()->view); set_source_handlers(*text_vec_.back()); } -void Notebook::Controller::OnCreatePage() { - INFO("Notebook create page"); - text_vec_.emplace_back(new Source::Controller(source_config(), text_vec_)); - scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); - editor_vec_.push_back(new Gtk::HBox()); - scrolledtext_vec_.back()->add(text_vec_.back()->view); - editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); -} - void Notebook::Controller::OnCloseCurrentPage() { INFO("Notebook close page"); if (Pages() != 0) { @@ -456,7 +439,7 @@ void Notebook::Controller Source::View& Notebook::Controller::CurrentTextView() { INFO("Getting sourceview"); - return text_vec_.at(CurrentPage())->view; + return *text_vec_.at(CurrentPage())->view; } int Notebook::Controller::CurrentPage() { @@ -465,7 +448,7 @@ int Notebook::Controller::CurrentPage() { Glib::RefPtr Notebook::Controller::Buffer(Source::Controller &source) { - return source.view.get_buffer(); + return source.view->get_buffer(); } int Notebook::Controller::Pages() { @@ -484,16 +467,16 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr } void Notebook::Controller::set_source_handlers(Source::Controller& controller) { - controller.view.signal_button_release_event(). + controller.view->signal_button_release_event(). connect(sigc::mem_fun(*this, &Notebook::Controller::OnMouseRelease), false); - controller.view.signal_key_release_event(). + controller.view->signal_key_release_event(). connect(sigc::mem_fun(*this, &Notebook::Controller::OnKeyRelease), false); //Add star on tab label when the page is not saved: controller.buffer()->signal_changed().connect([this]() { if(text_vec_.at(CurrentPage())->is_saved) { - std::string path=text_vec_.at(CurrentPage())->parser.file_path; + std::string path=text_vec_.at(CurrentPage())->view->file_path; size_t pos = path.find_last_of("/\\"); std::string filename=path; if(pos!=std::string::npos) @@ -547,7 +530,7 @@ void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll, } std::string Notebook::Controller::CurrentPagePath(){ - return text_vec_.at(CurrentPage())->parser.file_path; + return text_vec_.at(CurrentPage())->view->file_path; } void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, @@ -585,7 +568,7 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, } bool Notebook::Controller:: OnSaveFile() { - std::string path=text_vec_.at(CurrentPage())->parser.file_path; + std::string path=text_vec_.at(CurrentPage())->view->file_path; return OnSaveFile(path); } bool Notebook::Controller:: OnSaveFile(std::string path) { @@ -595,7 +578,7 @@ bool Notebook::Controller:: OnSaveFile(std::string path) { file.open (path); file << CurrentTextView().get_buffer()->get_text(); file.close(); - text_vec_.at(CurrentPage())->parser.file_path=path; + text_vec_.at(CurrentPage())->view->file_path=path; size_t pos = path.find_last_of("/\\"); std::string filename=path; if(pos!=std::string::npos) @@ -646,7 +629,7 @@ void Notebook::Controller::AskToSaveDialog() { false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); dialog.set_secondary_text( "Do you want to save: " + - text_vec_.at(CurrentPage())->parser.file_path+" ?"); + text_vec_.at(CurrentPage())->view->file_path+" ?"); DEBUG("AskToSaveDialog: run dialog"); int result = dialog.run(); diff --git a/juci/notebook.h b/juci/notebook.h index e9305b2..0ffff31 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -58,9 +58,7 @@ namespace Notebook { bool OnSaveFile(std::string path); void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); - void OnNewPage(std::string name); void OnOpenFile(std::string filename); - void OnCreatePage(); bool ScrollEventCallback(GdkEventScroll* scroll_event); int Pages(); Directories::Controller& directories() { return directories_; } @@ -72,7 +70,7 @@ namespace Notebook { bool OnKeyRelease(GdkEventKey* key); std::string OnSaveFileAs(); bool LegalExtension(std::string extension); - + std::string project_path; protected: void set_source_handlers(Source::Controller& controller); void PopupSelectHandler(Gtk::Dialog &popup, diff --git a/juci/source.cc b/juci/source.cc index 44b0e7b..4b0b423 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -20,8 +20,17 @@ bool Source::Config::legal_extension(std::string e) const { ////////////// //// View //// ////////////// -Source::View::View() { +Source::View::View(const Config& config, const std::string& file_path, const std::string& project_path): +config(config), file_path(file_path), project_path(project_path) { Gsv::init(); + set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); + set_show_line_numbers(config.show_line_numbers); + set_highlight_current_line(config.highlight_current_line); + + sourcefile s(file_path); + get_source_buffer()->get_undo_manager()->begin_not_undoable_action(); + get_source_buffer()->set_text(s.get_content()); + get_source_buffer()->get_undo_manager()->end_not_undoable_action(); } string Source::View::get_line(size_t line_number) { @@ -40,51 +49,113 @@ string Source::View::get_line_before_insert() { return line; } -/////////////// -//// Parser /// -/////////////// -clang::Index Source::Parser::clang_index(0, 1); +////////////////// +//// ClangView /// +////////////////// +clang::Index Source::ClangView::clang_index(0, 1); -Source::Parser::~Parser() { +Source::ClangView::ClangView(const Config& config, const std::string& file_path, const std::string& project_path): +Source::View::View(config, file_path, project_path), +parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { + override_font(Pango::FontDescription(config.font)); + override_background_color(Gdk::RGBA(config.background)); + for (auto &item : config.tags) { + get_source_buffer()->create_tag(item.first)->property_foreground() = item.second; + } + + int start_offset = get_source_buffer()->begin().get_offset(); + int end_offset = get_source_buffer()->end().get_offset(); + init_syntax_highlighting(get_buffer_map(), + start_offset, + end_offset, + &ClangView::clang_index); + update_syntax(extract_tokens(start_offset, end_offset)); + + //GTK-calls must happen in main thread, so the parse_thread + //sends signals to the main thread that it is to call the following functions: + parse_start.connect([this]{ + if(parse_thread_buffer_map_mutex.try_lock()) { + parse_thread_buffer_map=get_buffer_map(); + parse_thread_mapped=true; + parse_thread_buffer_map_mutex.unlock(); + } + parse_thread_go=true; + }); + + parse_done.connect([this](){ + if(parse_thread_mapped) { + INFO("Updating syntax"); + update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); + INFO("Syntax updated"); + } + else { + parse_thread_go=true; + } + }); + + parse_thread=std::thread([this]() { + while(true) { + while(!parse_thread_go && !parse_thread_stop) + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + if(parse_thread_stop) + break; + if(!parse_thread_mapped) { + parse_thread_go=false; + parse_start(); + } + else if (parse_thread_mapped && parsing_mutex.try_lock() && parse_thread_buffer_map_mutex.try_lock()) { + reparse(parse_thread_buffer_map); + parse_thread_go=false; + parsing_mutex.unlock(); + parse_thread_buffer_map_mutex.unlock(); + parse_done(); + } + } + }); + + get_source_buffer()->signal_changed().connect([this]() { + parse_thread_mapped=false; + parse_thread_go=true; + }); + + signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false); +} + +Source::ClangView::~ClangView() { + parse_thread_stop=true; + if(parse_thread.joinable()) + parse_thread.join(); parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang parsing_mutex.unlock(); } -void Source::Parser:: -init_syntax_highlighting(const std::string &filepath, - const std::string &project_path, - const std::map - &buffers, - int start_offset, - int end_offset, - clang::Index *index) { - this->project_path=project_path; +void Source::ClangView:: +init_syntax_highlighting(const std::map + &buffers, + int start_offset, + int end_offset, + clang::Index *index) { std::vector arguments = get_compilation_commands(); tu_ = std::unique_ptr(new clang::TranslationUnit(index, - filepath, - arguments, - buffers)); + file_path, + arguments, + buffers)); } -std::map Source::Parser:: +std::map Source::ClangView:: get_buffer_map() const { std::map buffer_map; - for (auto &controller : controllers) { - buffer_map.operator[](controller->parser.file_path) = - controller->buffer()->get_text().raw(); - } + buffer_map[file_path]=get_source_buffer()->get_text().raw(); return buffer_map; } -// Source::Model::UpdateLine -int Source::Parser:: +int Source::ClangView:: reparse(const std::map &buffer) { return tu_->ReparseTranslationUnit(file_path, buffer); } -std::vector Source::Parser:: -get_autocomplete_suggestions(int line_number, - int column) { +std::vector Source::ClangView:: +get_autocomplete_suggestions(int line_number, int column) { INFO("Getting auto complete suggestions"); std::vector suggestions; auto buffer_map=get_buffer_map(); @@ -108,7 +179,7 @@ get_autocomplete_suggestions(int line_number, return suggestions; } -std::vector Source::Parser:: +std::vector Source::ClangView:: get_compilation_commands() { clang::CompilationDatabase db(project_path+"/"); clang::CompileCommands commands(file_path, &db); @@ -123,7 +194,7 @@ get_compilation_commands() { return arguments; } -std::vector Source::Parser:: +std::vector Source::ClangView:: extract_tokens(int start_offset, int end_offset) { std::vector ranges; clang::SourceLocation start(tu_.get(), file_path, start_offset); @@ -143,7 +214,36 @@ extract_tokens(int start_offset, int end_offset) { return ranges; } -void Source::Parser:: +void Source::ClangView::update_syntax(const std::vector &ranges) { + if (ranges.empty() || ranges.size() == 0) { + return; + } + auto buffer = get_source_buffer(); + buffer->remove_all_tags(buffer->begin(), buffer->end()); + for (auto &range : ranges) { + std::string type = std::to_string(range.kind); + try { + config.types.at(type); + } catch (std::exception) { + continue; + } + int linum_start = range.start.line_number-1; + int linum_end = range.end.line_number-1; + int begin = range.start.column_offset-1; + int end = range.end.column_offset-1; + + if (end < 0) end = 0; + if (begin < 0) begin = 0; + Gtk::TextIter begin_iter = + buffer->get_iter_at_line_offset(linum_start, begin); + Gtk::TextIter end_iter = + buffer->get_iter_at_line_offset(linum_end, end); + buffer->apply_tag_by_name(config.types.at(type), + begin_iter, end_iter); + } +} + +void Source::ClangView:: highlight_cursor(clang::Token *token, std::vector *source_ranges) { clang::SourceLocation location = token->get_source_location(tu_.get()); @@ -159,7 +259,7 @@ highlight_cursor(clang::Token *token, Source::Location(end_line_num, end_offset), (int) cursor.kind()); } -void Source::Parser:: +void Source::ClangView:: highlight_token(clang::Token *token, std::vector *source_ranges, int token_kind) { @@ -175,265 +275,150 @@ highlight_token(clang::Token *token, end_offset), token_kind); } -//////////////////// -//// Controller //// -//////////////////// - -// Source::Controller::Controller() -// Constructor for Controller -Source::Controller::Controller(const Source::Config &config, - const std::vector > &controllers) : - config(config), parser(controllers), parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { - INFO("Source Controller with childs constructed"); - view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::on_key_press), false); - view.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); - view.override_font(Pango::FontDescription(config.font)); - view.set_show_line_numbers(config.show_line_numbers); - view.set_highlight_current_line(config.highlight_current_line); - view.override_background_color(Gdk::RGBA(config.background)); - for (auto &item : config.tags) { - buffer()->create_tag(item.first)->property_foreground() = item.second; - } -} - -Source::Controller::~Controller() { - parse_thread_stop=true; - if(parse_thread.joinable()) - parse_thread.join(); -} - -void Source::Controller::update_syntax(const std::vector &ranges) { - if (ranges.empty() || ranges.size() == 0) { - return; - } - auto buffer = view.get_buffer(); - buffer->remove_all_tags(buffer->begin(), buffer->end()); - for (auto &range : ranges) { - std::string type = std::to_string(range.kind); - try { - config.types.at(type); - } catch (std::exception) { - continue; - } - int linum_start = range.start.line_number-1; - int linum_end = range.end.line_number-1; - int begin = range.start.column_offset-1; - int end = range.end.column_offset-1; - - if (end < 0) end = 0; - if (begin < 0) begin = 0; - Gtk::TextIter begin_iter = - buffer->get_iter_at_line_offset(linum_start, begin); - Gtk::TextIter end_iter = - buffer->get_iter_at_line_offset(linum_end, end); - buffer->apply_tag_by_name(config.types.at(type), - begin_iter, end_iter); - } -} - -void Source::Controller::set_handlers() { - buffer()->signal_changed().connect([this]() { - parse_thread_mapped=false; - parse_thread_go=true; - }); -} - -void Source::Controller::on_new_empty_file() { - string filename("/tmp/untitled"); - sourcefile s(filename); - parser.file_path=filename; - parser.project_path=filename; - s.save(""); - set_handlers(); -} - -void Source::Controller::on_open_file(const string &filepath) { - parser.file_path=filepath; - sourcefile s(filepath); - auto buffer_map=parser.get_buffer_map(); - buffer_map[filepath] = s.get_content(); - buffer()->get_undo_manager()->begin_not_undoable_action(); - buffer()->set_text(s.get_content()); - buffer()->get_undo_manager()->end_not_undoable_action(); - set_handlers(); - int start_offset = buffer()->begin().get_offset(); - int end_offset = buffer()->end().get_offset(); - if (config.legal_extension(filepath.substr(filepath.find_last_of(".") + 1))) { - parser.init_syntax_highlighting(filepath, - parser.file_path.substr(0, parser.file_path.find_last_of('/')), - buffer_map, - start_offset, - end_offset, - &Parser::clang_index); - update_syntax(parser.extract_tokens(start_offset, end_offset)); - - //GTK-calls must happen in main thread, so the parse_thread - //sends signals to the main thread that it is to call the following functions: - parse_start.connect([this]{ - if(parse_thread_buffer_map_mutex.try_lock()) { - this->parse_thread_buffer_map=parser.get_buffer_map(); - parse_thread_mapped=true; - parse_thread_buffer_map_mutex.unlock(); - } - parse_thread_go=true; - }); - - parse_done.connect([this](){ - if(parse_thread_mapped) { - INFO("Updating syntax"); - update_syntax(parser.extract_tokens(0, buffer()->get_text().size())); - INFO("Syntax updated"); - } - else { - parse_thread_go=true; - } - }); - - parse_thread=std::thread([this]() { - while(true) { - while(!parse_thread_go && !parse_thread_stop) - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - if(parse_thread_stop) - break; - if(!parse_thread_mapped) { - parse_thread_go=false; - parse_start(); - } - else if (parse_thread_mapped && parser.parsing_mutex.try_lock() && parse_thread_buffer_map_mutex.try_lock()) { - parser.reparse(this->parse_thread_buffer_map); - parse_thread_go=false; - parser.parsing_mutex.unlock(); - parse_thread_buffer_map_mutex.unlock(); - parse_done(); - } - } - }); - } -} - -Glib::RefPtr Source::Controller::buffer() { - return view.get_source_buffer(); -} - -//TODO: move indentation to Parser, replace indentation methods with a better implementation or +//TODO: replace indentation methods with a better implementation or //maybe use libclang -bool Source::Controller::on_key_press(GdkEventKey* key) { +bool Source::ClangView::on_key_press(GdkEventKey* key) { const std::regex bracket_regex("^( *).*\\{ *$"); const std::regex no_bracket_statement_regex("^( *)(if|for|else if|catch|while) *\\(.*[^;}] *$"); const std::regex no_bracket_no_para_statement_regex("^( *)(else|try|do) *$"); const std::regex spaces_regex("^( *).*$"); - + //Indent as in previous line, and indent right after if/else/etc if(key->keyval==GDK_KEY_Return && key->state==0) { - string line(view.get_line_before_insert()); + string line(get_line_before_insert()); std::smatch sm; if(std::regex_match(line, sm, bracket_regex)) { - buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}"); - auto insert_it = buffer()->get_insert()->get_iter(); + get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}"); + auto insert_it = get_source_buffer()->get_insert()->get_iter(); for(size_t c=0;cget_insert()); - buffer()->place_cursor(insert_it); + scroll_to(get_source_buffer()->get_insert()); + get_source_buffer()->place_cursor(insert_it); } else if(std::regex_match(line, sm, no_bracket_statement_regex)) { - buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); - view.scroll_to(buffer()->get_insert()); + get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); + scroll_to(get_source_buffer()->get_insert()); } else if(std::regex_match(line, sm, no_bracket_no_para_statement_regex)) { - buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); - view.scroll_to(buffer()->get_insert()); + get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); + scroll_to(get_source_buffer()->get_insert()); } else if(std::regex_match(line, sm, spaces_regex)) { std::smatch sm2; - size_t line_nr=buffer()->get_insert()->get_iter().get_line(); + size_t line_nr=get_source_buffer()->get_insert()->get_iter().get_line(); if(line_nr>0 && sm[1].str().size()>=config.tab_size) { - string previous_line=view.get_line(line_nr-1); + string previous_line=get_line(line_nr-1); if(!std::regex_match(previous_line, sm2, bracket_regex)) { if(std::regex_match(previous_line, sm2, no_bracket_statement_regex)) { - buffer()->insert_at_cursor("\n"+sm2[1].str()); - view.scroll_to(buffer()->get_insert()); + get_source_buffer()->insert_at_cursor("\n"+sm2[1].str()); + scroll_to(get_source_buffer()->get_insert()); return true; } else if(std::regex_match(previous_line, sm2, no_bracket_no_para_statement_regex)) { - buffer()->insert_at_cursor("\n"+sm2[1].str()); - view.scroll_to(buffer()->get_insert()); + get_source_buffer()->insert_at_cursor("\n"+sm2[1].str()); + scroll_to(get_source_buffer()->get_insert()); return true; } } } - buffer()->insert_at_cursor("\n"+sm[1].str()); - view.scroll_to(buffer()->get_insert()); + get_source_buffer()->insert_at_cursor("\n"+sm[1].str()); + scroll_to(get_source_buffer()->get_insert()); } return true; } //Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text. if(key->keyval==GDK_KEY_Tab && key->state==0) { Gtk::TextIter selection_start, selection_end; - buffer()->get_selection_bounds(selection_start, selection_end); + get_source_buffer()->get_selection_bounds(selection_start, selection_end); int line_start=selection_start.get_line(); int line_end=selection_end.get_line(); for(int line=line_start;line<=line_end;line++) { - Gtk::TextIter line_it = buffer()->get_iter_at_line(line); - buffer()->insert(line_it, config.tab); + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line); + get_source_buffer()->insert(line_it, config.tab); } return true; } //Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text. else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) { Gtk::TextIter selection_start, selection_end; - buffer()->get_selection_bounds(selection_start, selection_end); + get_source_buffer()->get_selection_bounds(selection_start, selection_end); int line_start=selection_start.get_line(); int line_end=selection_end.get_line(); for(int line_nr=line_start;line_nr<=line_end;line_nr++) { - string line=view.get_line(line_nr); + string line=get_line(line_nr); if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) return true; } for(int line_nr=line_start;line_nr<=line_end;line_nr++) { - Gtk::TextIter line_it = buffer()->get_iter_at_line(line_nr); + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); Gtk::TextIter line_plus_it=line_it; for(unsigned c=0;cerase(line_it, line_plus_it); + get_source_buffer()->erase(line_it, line_plus_it); } return true; } //Indent left when writing } on a new line else if(key->keyval==GDK_KEY_braceright) { - string line=view.get_line_before_insert(); + string line=get_line_before_insert(); if(line.size()>=config.tab_size) { for(auto c: line) { if(c!=' ') return false; } - Gtk::TextIter insert_it = buffer()->get_insert()->get_iter(); - Gtk::TextIter line_it = buffer()->get_iter_at_line(insert_it.get_line()); + Gtk::TextIter insert_it = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(insert_it.get_line()); Gtk::TextIter line_plus_it=line_it; for(unsigned c=0;cerase(line_it, line_plus_it); + get_source_buffer()->erase(line_it, line_plus_it); } return false; } //"Smart" backspace key else if(key->keyval==GDK_KEY_BackSpace) { - Gtk::TextIter insert_it=buffer()->get_insert()->get_iter(); + Gtk::TextIter insert_it=get_source_buffer()->get_insert()->get_iter(); int line_nr=insert_it.get_line(); if(line_nr>0) { - string line=view.get_line(line_nr); - string previous_line=view.get_line(line_nr-1); + string line=get_line(line_nr); + string previous_line=get_line(line_nr-1); smatch sm; if(std::regex_match(previous_line, sm, spaces_regex)) { if(line==sm[1]) { - Gtk::TextIter line_it = buffer()->get_iter_at_line(line_nr); - buffer()->erase(line_it, insert_it); + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); + get_source_buffer()->erase(line_it, insert_it); } } } } return false; } + +//////////////////// +//// Controller //// +//////////////////// + +// Source::Controller::Controller() +// Constructor for Controller +Source::Controller::Controller(const Source::Config &config, + const std::string& file_path, std::string project_path) : + config(config) { + if(project_path=="") { + size_t last_of=file_path.find_last_of("\\/"); + if(last_of!=std::string::npos) + project_path=file_path.substr(0, last_of); + } + if (config.legal_extension(file_path.substr(file_path.find_last_of(".") + 1))) + view=std::unique_ptr(new ClangView(config, file_path, project_path)); + else + view=std::unique_ptr(new GenericView(config, file_path, project_path)); + INFO("Source Controller with childs constructed"); +} + +Glib::RefPtr Source::Controller::buffer() { + return view->get_source_buffer(); +} \ No newline at end of file diff --git a/juci/source.h b/juci/source.h index 927af94..1deba5c 100644 --- a/juci/source.h +++ b/juci/source.h @@ -43,13 +43,6 @@ namespace Source { int kind; }; - class View : public Gsv::View { - public: - View(); - std::string get_line(size_t line_number); - std::string get_line_before_insert(); - }; // class View - class AutoCompleteChunk { public: explicit AutoCompleteChunk(const clang::CompletionChunk &clang_chunk) : @@ -64,18 +57,31 @@ namespace Source { chunks(chunks) { } std::vector chunks; }; - - class Controller; - class Parser{ + class View : public Gsv::View { public: - Parser(const std::vector > &controllers): - controllers(controllers) {} - ~Parser(); + View(const Config& config, const std::string& file_path, const std::string& project_path); + std::string get_line(size_t line_number); + std::string get_line_before_insert(); + virtual std::vector get_autocomplete_suggestions(int line_number, int column) {return std::vector();} + std::string file_path; + std::string project_path; + protected: + const Config& config; + }; // class View + + class GenericView : public View { + public: + GenericView(const Config& config, const std::string& file_path, const std::string& project_path): + View(config, file_path, project_path) {} + }; + + class ClangView : public View { + public: + ClangView(const Config& config, const std::string& file_path, const std::string& project_path); + ~ClangView(); // inits the syntax highligthing on file open - void init_syntax_highlighting(const std::string &filepath, - const std::string &project_path, - const std::map + void init_syntax_highlighting(const std::map &buffers, int start_offset, int end_offset, @@ -83,9 +89,8 @@ namespace Source { std::vector get_autocomplete_suggestions(int line_number, int column); int reparse(const std::map &buffers); std::vector extract_tokens(int, int); - - std::string file_path; - std::string project_path; + void update_syntax(const std::vector &locations); + static clang::Index clang_index; std::map get_buffer_map() const; std::mutex parsing_mutex; @@ -97,28 +102,8 @@ namespace Source { void highlight_cursor(clang::Token *token, std::vector *source_ranges); std::vector get_compilation_commands(); - //controllers is needed here, no way around that I think - const std::vector > &controllers; - }; - - class Controller { - public: - Controller(const Source::Config &config, - const std::vector > &controllers); - ~Controller(); - void update_syntax(const std::vector &locations); - void set_handlers(); - void on_new_empty_file(); - void on_open_file(const std::string &filename); - Glib::RefPtr buffer(); bool on_key_press(GdkEventKey* key); - bool is_saved = true; - - Parser parser; - View view; - - private: Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; std::thread parse_thread; @@ -127,7 +112,19 @@ namespace Source { std::atomic parse_thread_go; std::atomic parse_thread_mapped; std::atomic parse_thread_stop; + }; + + class Controller { + public: + Controller(const Source::Config &config, + const std::string& file_path, std::string project_path); + Glib::RefPtr buffer(); + + bool is_saved = true; + std::unique_ptr view; + + private: const Config& config; }; // class Controller } // namespace Source diff --git a/juci/window.cc b/juci/window.cc index 8758903..e139437 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -142,7 +142,9 @@ void Window::OnFileOpenFolder() { { case(Gtk::RESPONSE_OK): { - notebook_.directories().open_folder(dialog.get_filename()); + std::string project_path=dialog.get_filename(); + notebook_.project_path=project_path; + notebook_.directories().open_folder(project_path); break; } case(Gtk::RESPONSE_CANCEL): From efd1a6c2b82473e975e479a801c41b13fe0317f6 Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 22 Jun 2015 19:28:45 +0200 Subject: [PATCH 04/25] Can now open files and directories using the command line, for instance: juci . source.cc notebook.* --- juci/juci.cc | 46 ++++++++++++++++++++++++++++++++++++++-------- juci/juci.h | 15 +++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 juci/juci.h diff --git a/juci/juci.cc b/juci/juci.cc index cedf99d..5402b10 100644 --- a/juci/juci.cc +++ b/juci/juci.cc @@ -1,5 +1,4 @@ -#include "window.h" -#include "logging.h" +#include "juci.h" void init_logging() { add_common_attributes(); @@ -8,12 +7,43 @@ void init_logging() { INFO("Logging initalized"); } +int Juci::on_command_line(const Glib::RefPtr &cmd) { + Glib::set_prgname("juci"); + Glib::OptionContext ctx("[PATH ...]"); + Glib::OptionGroup gtk_group(gtk_get_option_group(true)); + ctx.add_group(gtk_group); + int argc; + char **argv = cmd->get_arguments(argc); + ctx.parse(argc, argv); + if(argc>=2) { + for(size_t c=1;c(new Window()); + add_window(*window); + window->show(); + if(directory!="") { + //TODO: use the following instead, window->notebook_.open_directory(directory); + window->notebook_.project_path=directory; + window->notebook_.directories().open_folder(directory); + } + for(auto &f: files) + window->notebook_.OnOpenFile(f); +} + int main(int argc, char *argv[]) { - Glib::RefPtr app = Gtk::Application::create( - argc, - argv, - "no.sout.juci"); init_logging(); - Window window; - return app->run(window); + return Juci().run(argc, argv); } diff --git a/juci/juci.h b/juci/juci.h new file mode 100644 index 0000000..9d55502 --- /dev/null +++ b/juci/juci.h @@ -0,0 +1,15 @@ +#include "window.h" +#include "logging.h" + +class Juci : public Gtk::Application { +public: + Juci(): Gtk::Application("no.sout.juci", Gio::APPLICATION_HANDLES_COMMAND_LINE) {} + + int on_command_line(const Glib::RefPtr &cmd); + void on_activate(); + +private: + std::unique_ptr window; + std::string directory; + std::vector files; +}; From e7b6e425a08c6ebee7486489b0479a85430f7c94 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 23 Jun 2015 14:00:13 +0200 Subject: [PATCH 05/25] Small change in setting and using Source::View::project_path. --- juci/source.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index 4b0b423..a623c40 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -181,7 +181,7 @@ get_autocomplete_suggestions(int line_number, int column) { std::vector Source::ClangView:: get_compilation_commands() { - clang::CompilationDatabase db(project_path+"/"); + clang::CompilationDatabase db(project_path); clang::CompileCommands commands(file_path, &db); std::vector cmds = commands.get_commands(); std::vector arguments; @@ -408,9 +408,7 @@ Source::Controller::Controller(const Source::Config &config, const std::string& file_path, std::string project_path) : config(config) { if(project_path=="") { - size_t last_of=file_path.find_last_of("\\/"); - if(last_of!=std::string::npos) - project_path=file_path.substr(0, last_of); + project_path=boost::filesystem::path(file_path).parent_path().string(); } if (config.legal_extension(file_path.substr(file_path.find_last_of(".") + 1))) view=std::unique_ptr(new ClangView(config, file_path, project_path)); From ecc56b66df656023ff8311a0ce4d2d7af2407e93 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 23 Jun 2015 14:15:53 +0200 Subject: [PATCH 06/25] Fixed annoying indentation issue when entering enter after a bracket. --- juci/source.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/juci/source.cc b/juci/source.cc index a623c40..8377728 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -288,6 +288,18 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { string line(get_line_before_insert()); std::smatch sm; if(std::regex_match(line, sm, bracket_regex)) { + size_t line_nr=get_source_buffer()->get_insert()->get_iter().get_line(); + if((line_nr+1)get_line_count()) { + string next_line=get_line(line_nr+1); + std::smatch sm2; + if(std::regex_match(next_line, sm2, spaces_regex)) { + if(sm2[1].str()==sm[1].str()+config.tab) { + get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); + scroll_to(get_source_buffer()->get_insert()); + return true; + } + } + } get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}"); auto insert_it = get_source_buffer()->get_insert()->get_iter(); for(size_t c=0;c Date: Tue, 23 Jun 2015 17:47:47 +0200 Subject: [PATCH 07/25] Moved completiondialog-code to seperate file: selectiondialog.h/cc. The selectiondialog is now started from Source::ClangView. Some cleanup. See source.cc lines 340-345 for example use of SelectionDialog. --- juci/selectiondialog.cc | 85 +++++++++++++++++++++++++++++++++++++++++ juci/selectiondialog.h | 19 +++++++++ 2 files changed, 104 insertions(+) create mode 100644 juci/selectiondialog.cc create mode 100644 juci/selectiondialog.h diff --git a/juci/selectiondialog.cc b/juci/selectiondialog.cc new file mode 100644 index 0000000..b618c12 --- /dev/null +++ b/juci/selectiondialog.cc @@ -0,0 +1,85 @@ +#include "selectiondialog.h" + +SelectionDialog::SelectionDialog(Source::View& view): Gtk::Dialog(), view(view), + list_view_text(1, false, Gtk::SelectionMode::SELECTION_SINGLE) { + scrolled_window.set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_NEVER); + list_view_text.set_enable_search(true); + list_view_text.set_headers_visible(false); + list_view_text.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); + list_view_text.set_activate_on_single_click(true); +} + +void SelectionDialog::show(const std::map& rows) { + for (auto &i : rows) { + list_view_text.append(i.first); + } + scrolled_window.add(list_view_text); + get_vbox()->pack_start(scrolled_window); + set_transient_for((Gtk::Window&)(*view.get_toplevel())); + show_all(); + int popup_x = get_width(); + int popup_y = rows.size() * 20; + adjust(popup_x, popup_y); + + list_view_text.signal_row_activated().connect([this](const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn*) { + if(on_select) + on_select(list_view_text); + response(Gtk::RESPONSE_DELETE_EVENT); + }); + + signal_focus_out_event().connect(sigc::mem_fun(*this, &SelectionDialog::close), false); + + run(); +} + +bool SelectionDialog::close(GdkEventFocus*) { + response(Gtk::RESPONSE_DELETE_EVENT); + return true; +} + +void SelectionDialog::adjust(int current_x, int current_y) { + INFO("SelectionDialog set size"); + int view_x = view.get_width(); + int view_y = 150; + bool is_never_scroll_x = true; + bool is_never_scroll_y = true; + if (current_x > view_x) { + current_x = view_x; + is_never_scroll_x = false; + } + if (current_y > view_y) { + current_y = view_y; + is_never_scroll_y = false; + } + scrolled_window.set_size_request(current_x, current_y); + if (!is_never_scroll_x && !is_never_scroll_y) { + scrolled_window.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, Gtk::PolicyType::POLICY_AUTOMATIC); + } else if (!is_never_scroll_x && is_never_scroll_y) { + scrolled_window.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, Gtk::PolicyType::POLICY_NEVER); + } else if (is_never_scroll_x && !is_never_scroll_y) { + scrolled_window.set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_AUTOMATIC); + } + + INFO("SelectionDialog set position"); + Gdk::Rectangle temp1, temp2; + view.get_cursor_locations(view.get_source_buffer()->get_insert()->get_iter(), temp1, temp2); + int view_edge_x = 0; + int view_edge_y = 0; + int x, y; + view.buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_WIDGET, + temp1.get_x(), temp1.get_y(), x, y); + Glib::RefPtr gdkw = view.get_window(Gtk::TextWindowType::TEXT_WINDOW_WIDGET); + gdkw->get_origin(view_edge_x, view_edge_y); + + x += view_edge_x; + y += view_edge_y; + if ((view_edge_x-x)*-1 > view.get_width()-current_x) { + x -= current_x; + if (x < view_edge_x) x = view_edge_x; + } + if ((view_edge_y-y)*-1 > view.get_height()-current_y) { + y -= (current_y+14) + 15; + if (x < view_edge_y) y = view_edge_y +15; + } + move(x, y+15); +} \ No newline at end of file diff --git a/juci/selectiondialog.h b/juci/selectiondialog.h new file mode 100644 index 0000000..6ea11c4 --- /dev/null +++ b/juci/selectiondialog.h @@ -0,0 +1,19 @@ +#include "gtkmm.h" +#include "logging.h" +#include "source.h" + +class SelectionDialog : public Gtk::Dialog { +public: + SelectionDialog(Source::View& view); + void show(const std::map& rows); + bool close(GdkEventFocus*); + + std::function on_select; + +private: + void adjust(int current_x, int current_y); + + Source::View& view; + Gtk::ScrolledWindow scrolled_window; + Gtk::ListViewText list_view_text; +}; \ No newline at end of file From d33df03dd7fa97360d79c93b4887739c31e1b711 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 23 Jun 2015 17:49:16 +0200 Subject: [PATCH 08/25] Moved completiondialog-code to seperate file: selectiondialog.h/cc. The selectiondialog is now started from Source::ClangView. Some cleanup. See source.cc lines 340-345 for example use of SelectionDialog. --- juci/CMakeLists.txt | 3 + juci/notebook.cc | 195 -------------------------------------------- juci/notebook.h | 17 ---- juci/source.cc | 72 ++++++++++++++++ juci/source.h | 1 + juci/window.cc | 7 -- juci/window.h | 1 - 7 files changed, 76 insertions(+), 220 deletions(-) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index b7f4dfa..4dcf244 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -99,6 +99,7 @@ endif() # name of the executable on Windows will be example.exe add_executable(${project_name} #list of every needed file to create the executable + juci.h juci.cc keybindings.h keybindings.cc @@ -106,6 +107,8 @@ add_executable(${project_name} menu.cc source.h source.cc + selectiondialog.h + selectiondialog.cc config.h config.cc sourcefile.h diff --git a/juci/notebook.cc b/juci/notebook.cc index 177f104..b2fb0b8 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -22,7 +22,6 @@ Notebook::Controller::Controller(Gtk::Window* window, INFO("Create notebook"); window_ = window; refClipboard_ = Gtk::Clipboard::get(); - ispopup = false; view().pack1(directories_.widget(), true, true); CreateKeybindings(keybindings); INFO("Notebook Controller Success"); @@ -176,114 +175,6 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller INFO("Notebook signal handlers sucsess"); } -bool Notebook::Controller:: OnMouseRelease(GdkEventButton* button) { - if (button->button == 1 && ispopup) { - popup_.response(Gtk::RESPONSE_DELETE_EVENT); - return true; - } - return false; -} - -bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) { - return GeneratePopup(key->keyval); -} - -bool Notebook::Controller::GeneratePopup(int key_id) { - INFO("Notebook genereate popup, getting iters"); - std::string path = text_vec_.at(CurrentPage())->view->file_path; - if (!source_config().legal_extension(path.substr(path.find_last_of(".") + 1))) return false; - // Get function to fill popup with suggests item vector under is for testing - Gtk::TextIter beg = CurrentTextView().get_buffer()->get_insert()->get_iter(); - Gtk::TextIter end = CurrentTextView().get_buffer()->get_insert()->get_iter(); - Gtk::TextIter tmp = CurrentTextView().get_buffer()->get_insert()->get_iter(); - Gtk::TextIter tmp1 = CurrentTextView().get_buffer()->get_insert()->get_iter(); - Gtk::TextIter line = - CurrentTextView().get_buffer()->get_iter_at_line(tmp.get_line()); - if (end.backward_char() && end.backward_char()) { - bool illegal_chars = - end.backward_search("\"", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line) - || - end.backward_search("//", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line); - INFO("Notebook genereate popup, checking key_id"); - if (illegal_chars) { - return false; - } - std::string c = text_vec_[CurrentPage()]->buffer()->get_text(end, beg); - switch (key_id) { - case 46: - break; - case 58: - if (c != "::") return false; - break; - case 60: - if (c != "->") return false; - break; - case 62: - if (c != "->") return false; - break; - default: - return false; - } - } else { - return false; - } - INFO("Notebook genereate popup, getting autocompletions"); - std::vector acdata=text_vec_.at(CurrentPage())->view-> - get_autocomplete_suggestions(beg.get_line()+1, - beg.get_line_offset()+2); - std::map items; - for (auto &data : acdata) { - std::stringstream ss; - std::string return_value; - for (auto &chunk : data.chunks) { - switch (chunk.kind) { - case clang::CompletionChunk_ResultType: - return_value = chunk.chunk; - break; - case clang::CompletionChunk_Informative: break; - default: ss << chunk.chunk; break; - } - } - if (ss.str().length() > 0) { // if length is 0 the result is empty - items[ss.str() + " --> " + return_value] = ss.str(); - } - } - Gtk::ScrolledWindow popup_scroll_; - Gtk::ListViewText listview_(1, false, Gtk::SelectionMode::SELECTION_SINGLE); - popup_scroll_.set_policy(Gtk::PolicyType::POLICY_NEVER, - Gtk::PolicyType::POLICY_NEVER); - listview_.set_enable_search(true); - listview_.set_headers_visible(false); - listview_.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); - listview_.set_activate_on_single_click(true); - if (items.empty()) { - items["No suggestions found..."] = ""; - } - for (auto &i : items) { - listview_.append(i.first); - } - popup_scroll_.add(listview_); - popup_.get_vbox()->pack_start(popup_scroll_); - popup_.set_transient_for(*window_); - popup_.show_all(); - INFO("Notebook genereate popup, moving popup"); - int popup_x = popup_.get_width(); - int popup_y = items.size() * 20; - PopupSetSize(popup_scroll_, popup_x, popup_y); - int x, y; - FindPopupPosition(CurrentTextView(), popup_x, popup_y, x, y); - popup_.move(x, y+15); - INFO("Notebook genereate popup, create handler"); - PopupSelectHandler(popup_, listview_, &items); - ispopup = true; - INFO("Notebook genereate popup, run popup"); - popup_.run(); - INFO("Notebook genereate popup, hide popup"); - popup_.hide(); - ispopup = false; - return true; -} - Notebook::Controller::~Controller() { INFO("Notebook destructor"); for (auto &i : editor_vec_) delete i; @@ -467,12 +358,6 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr } void Notebook::Controller::set_source_handlers(Source::Controller& controller) { - controller.view->signal_button_release_event(). - connect(sigc::mem_fun(*this, &Notebook::Controller::OnMouseRelease), false); - - controller.view->signal_key_release_event(). - connect(sigc::mem_fun(*this, &Notebook::Controller::OnKeyRelease), false); - //Add star on tab label when the page is not saved: controller.buffer()->signal_changed().connect([this]() { if(text_vec_.at(CurrentPage())->is_saved) { @@ -487,86 +372,6 @@ void Notebook::Controller::set_source_handlers(Source::Controller& controller) { }); } -void Notebook::Controller::PopupSelectHandler(Gtk::Dialog &popup, - Gtk::ListViewText &listview, - std::map - *items) { - listview.signal_row_activated(). - connect([this, &listview, &popup, items](const Gtk::TreeModel::Path& path, - Gtk::TreeViewColumn*) { - std::string selected = items-> - at(listview.get_text(listview.get_selected()[0])); - CurrentTextView().get_buffer()->insert_at_cursor(selected); - popup.response(Gtk::RESPONSE_DELETE_EVENT); - }); -} -void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll, - int ¤t_x, - int ¤t_y) { - INFO("Notebook popup set size"); - int textview_x = CurrentTextView().get_width(); - int textview_y = 150; - bool is_never_scroll_x = true; - bool is_never_scroll_y = true; - if (current_x > textview_x) { - current_x = textview_x; - is_never_scroll_x = false; - } - if (current_y > textview_y) { - current_y = textview_y; - is_never_scroll_y = false; - } - scroll.set_size_request(current_x, current_y); - if (!is_never_scroll_x && !is_never_scroll_y) { - scroll.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, - Gtk::PolicyType::POLICY_AUTOMATIC); - } else if (!is_never_scroll_x && is_never_scroll_y) { - scroll.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, - Gtk::PolicyType::POLICY_NEVER); - } else if (is_never_scroll_x && !is_never_scroll_y) { - scroll.set_policy(Gtk::PolicyType::POLICY_NEVER, - Gtk::PolicyType::POLICY_AUTOMATIC); - } -} - -std::string Notebook::Controller::CurrentPagePath(){ - return text_vec_.at(CurrentPage())->view->file_path; -} - -void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, - int popup_x, - int popup_y, - int &x, - int &y) { - INFO("Notebook popup find position"); - Gdk::Rectangle temp1, temp2; - textview.get_cursor_locations( - CurrentTextView(). - get_buffer()->get_insert()-> - get_iter(), temp1, temp2); - int textview_edge_x = 0; - int textview_edge_y = 0; - textview.buffer_to_window_coords( - Gtk::TextWindowType::TEXT_WINDOW_WIDGET, - temp1.get_x(), - temp1.get_y(), - x, y); - Glib::RefPtr gdkw = - CurrentTextView().get_window(Gtk::TextWindowType::TEXT_WINDOW_WIDGET); - gdkw->get_origin(textview_edge_x, textview_edge_y); - - x += textview_edge_x; - y += textview_edge_y; - if ((textview_edge_x-x)*-1 > textview.get_width()-popup_x) { - x -= popup_x; - if (x < textview_edge_x) x = textview_edge_x; - } - if ((textview_edge_y-y)*-1 > textview.get_height()-popup_y) { - y -= (popup_y+14) + 15; - if (x < textview_edge_y) y = textview_edge_y +15; - } -} - bool Notebook::Controller:: OnSaveFile() { std::string path=text_vec_.at(CurrentPage())->view->file_path; return OnSaveFile(path); diff --git a/juci/notebook.h b/juci/notebook.h index 0ffff31..21791ba 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -63,30 +63,15 @@ namespace Notebook { int Pages(); Directories::Controller& directories() { return directories_; } Gtk::Paned& view(); - bool GeneratePopup(int key); void Search(bool forward); Source::Config& source_config() { return source_config_; } - bool OnMouseRelease(GdkEventButton* button); - bool OnKeyRelease(GdkEventKey* key); std::string OnSaveFileAs(); bool LegalExtension(std::string extension); std::string project_path; protected: void set_source_handlers(Source::Controller& controller); - void PopupSelectHandler(Gtk::Dialog &popup, - Gtk::ListViewText &listview, - std::map - *items); private: void CreateKeybindings(Keybindings::Controller& keybindings); - void FindPopupPosition(Gtk::TextView& textview, - int popup_x, - int popup_y, - int &x, - int &y); - void PopupSetSize(Gtk::ScrolledWindow& scroll, - int ¤t_x, - int ¤t_y); void AskToSaveDialog(); Glib::RefPtr m_refBuilder; Glib::RefPtr refActionGroup; @@ -104,8 +89,6 @@ namespace Notebook { Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; Glib::RefPtr refClipboard_; - bool ispopup; - Gtk::Dialog popup_; Gtk::Window* window_; }; // class controller } // namespace Notebook diff --git a/juci/source.cc b/juci/source.cc index 8377728..0a5ad6c 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -6,6 +6,7 @@ #include "logging.h" #include #include +#include "selectiondialog.h" bool Source::Config::legal_extension(std::string e) const { std::transform(e.begin(), e.end(),e.begin(), ::tolower); @@ -119,6 +120,7 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { }); signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false); + signal_key_release_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_release), false); } Source::ClangView::~ClangView() { @@ -275,6 +277,76 @@ highlight_token(clang::Token *token, end_offset), token_kind); } +bool Source::ClangView::on_key_release(GdkEventKey* key) { + INFO("Source::ClangView::on_key_release getting iters"); + // Get function to fill popup with suggests item vector under is for testing + Gtk::TextIter beg = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter end = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter tmp = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter tmp1 = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter line = get_source_buffer()->get_iter_at_line(tmp.get_line()); + if (end.backward_char() && end.backward_char()) { + bool illegal_chars = + end.backward_search("\"", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line) + || + end.backward_search("//", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line); + INFO("Source::ClangView::on_key_release checking key->keyval"); + if (illegal_chars) { + return false; + } + std::string c = get_source_buffer()->get_text(end, beg); + switch (key->keyval) { + case 46: + break; + case 58: + if (c != "::") return false; + break; + case 60: + if (c != "->") return false; + break; + case 62: + if (c != "->") return false; + break; + default: + return false; + } + } else { + return false; + } + INFO("Source::ClangView::on_key_release getting autocompletions"); + std::vector acdata=get_autocomplete_suggestions(beg.get_line()+1, + beg.get_line_offset()+2); + std::map rows; + for (auto &data : acdata) { + std::stringstream ss; + std::string return_value; + for (auto &chunk : data.chunks) { + switch (chunk.kind) { + case clang::CompletionChunk_ResultType: + return_value = chunk.chunk; + break; + case clang::CompletionChunk_Informative: break; + default: ss << chunk.chunk; break; + } + } + if (ss.str().length() > 0) { // if length is 0 the result is empty + rows[ss.str() + " --> " + return_value] = ss.str(); + } + } + if (rows.empty()) { + rows["No suggestions found..."] = ""; + } + + SelectionDialog selection_dialog(*this); + selection_dialog.on_select=[this, &rows](Gtk::ListViewText& list_view_text){ + std::string selected = rows.at(list_view_text.get_text(list_view_text.get_selected()[0])); + get_source_buffer()->insert_at_cursor(selected); + }; + selection_dialog.show(rows); + + return true; +} + //TODO: replace indentation methods with a better implementation or //maybe use libclang bool Source::ClangView::on_key_press(GdkEventKey* key) { diff --git a/juci/source.h b/juci/source.h index 1deba5c..7c7b2a2 100644 --- a/juci/source.h +++ b/juci/source.h @@ -103,6 +103,7 @@ namespace Source { std::vector *source_ranges); std::vector get_compilation_commands(); bool on_key_press(GdkEventKey* key); + bool on_key_release(GdkEventKey* key); Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; diff --git a/juci/window.cc b/juci/window.cc index e139437..e3c116a 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -104,10 +104,6 @@ Window::Window() : execute.detach(); } }); - this->signal_button_release_event(). - connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); - terminal_.Terminal().signal_button_release_event(). - connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); add_accel_group(keybindings_.ui_manager_menu()->get_accel_group()); add_accel_group(keybindings_.ui_manager_hidden()->get_accel_group()); @@ -203,9 +199,6 @@ void Window::OnOpenFile() { } } } -bool Window::OnMouseRelease(GdkEventButton *button){ - return notebook_.OnMouseRelease(button); -} bool Window::SaveFile() { if(notebook_.OnSaveFile()) { diff --git a/juci/window.h b/juci/window.h index d60dfdb..344a51e 100644 --- a/juci/window.h +++ b/juci/window.h @@ -32,7 +32,6 @@ public: void OnWindowHide(); void OnOpenFile(); void OnFileOpenFolder(); - bool OnMouseRelease(GdkEventButton* button); bool SaveFile(); bool SaveFileAs(); }; From 27cff8be26cff7da7381a1bcb07250f98e35c326 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 23 Jun 2015 18:00:11 +0200 Subject: [PATCH 09/25] Did not intend to delete this function in last commit. --- juci/notebook.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/juci/notebook.cc b/juci/notebook.cc index b2fb0b8..1042816 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -372,6 +372,10 @@ void Notebook::Controller::set_source_handlers(Source::Controller& controller) { }); } +std::string Notebook::Controller::CurrentPagePath(){ + return text_vec_.at(CurrentPage())->parser.file_path; +} + bool Notebook::Controller:: OnSaveFile() { std::string path=text_vec_.at(CurrentPage())->view->file_path; return OnSaveFile(path); From 45706a3905cc634709e67f8b0721d6a7f8d49331 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 23 Jun 2015 18:02:01 +0200 Subject: [PATCH 10/25] Small fix, copied old code. --- juci/notebook.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 1042816..a813162 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -373,7 +373,7 @@ void Notebook::Controller::set_source_handlers(Source::Controller& controller) { } std::string Notebook::Controller::CurrentPagePath(){ - return text_vec_.at(CurrentPage())->parser.file_path; + return text_vec_.at(CurrentPage())->view->file_path; } bool Notebook::Controller:: OnSaveFile() { From 832ef8881cf14ff4af28394bba0cbadd9c81a4de Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 23 Jun 2015 23:07:33 +0200 Subject: [PATCH 11/25] Made SelectionDialog more general, now receiving Gtk::TextView& instead. Minor cleanup in source.*. --- juci/selectiondialog.cc | 16 ++++---- juci/selectiondialog.h | 4 +- juci/source.cc | 86 +++++++++++++++++++++-------------------- juci/source.h | 23 ++++++----- 4 files changed, 68 insertions(+), 61 deletions(-) diff --git a/juci/selectiondialog.cc b/juci/selectiondialog.cc index b618c12..8eca51d 100644 --- a/juci/selectiondialog.cc +++ b/juci/selectiondialog.cc @@ -1,6 +1,6 @@ #include "selectiondialog.h" -SelectionDialog::SelectionDialog(Source::View& view): Gtk::Dialog(), view(view), +SelectionDialog::SelectionDialog(Gtk::TextView& text_view): Gtk::Dialog(), text_view(text_view), list_view_text(1, false, Gtk::SelectionMode::SELECTION_SINGLE) { scrolled_window.set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_NEVER); list_view_text.set_enable_search(true); @@ -15,7 +15,7 @@ void SelectionDialog::show(const std::map& rows) { } scrolled_window.add(list_view_text); get_vbox()->pack_start(scrolled_window); - set_transient_for((Gtk::Window&)(*view.get_toplevel())); + set_transient_for((Gtk::Window&)(*text_view.get_toplevel())); show_all(); int popup_x = get_width(); int popup_y = rows.size() * 20; @@ -39,7 +39,7 @@ bool SelectionDialog::close(GdkEventFocus*) { void SelectionDialog::adjust(int current_x, int current_y) { INFO("SelectionDialog set size"); - int view_x = view.get_width(); + int view_x = text_view.get_width(); int view_y = 150; bool is_never_scroll_x = true; bool is_never_scroll_y = true; @@ -62,22 +62,22 @@ void SelectionDialog::adjust(int current_x, int current_y) { INFO("SelectionDialog set position"); Gdk::Rectangle temp1, temp2; - view.get_cursor_locations(view.get_source_buffer()->get_insert()->get_iter(), temp1, temp2); + text_view.get_cursor_locations(text_view.get_buffer()->get_insert()->get_iter(), temp1, temp2); int view_edge_x = 0; int view_edge_y = 0; int x, y; - view.buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_WIDGET, + text_view.buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_WIDGET, temp1.get_x(), temp1.get_y(), x, y); - Glib::RefPtr gdkw = view.get_window(Gtk::TextWindowType::TEXT_WINDOW_WIDGET); + Glib::RefPtr gdkw = text_view.get_window(Gtk::TextWindowType::TEXT_WINDOW_WIDGET); gdkw->get_origin(view_edge_x, view_edge_y); x += view_edge_x; y += view_edge_y; - if ((view_edge_x-x)*-1 > view.get_width()-current_x) { + if ((view_edge_x-x)*-1 > text_view.get_width()-current_x) { x -= current_x; if (x < view_edge_x) x = view_edge_x; } - if ((view_edge_y-y)*-1 > view.get_height()-current_y) { + if ((view_edge_y-y)*-1 > text_view.get_height()-current_y) { y -= (current_y+14) + 15; if (x < view_edge_y) y = view_edge_y +15; } diff --git a/juci/selectiondialog.h b/juci/selectiondialog.h index 6ea11c4..70e16de 100644 --- a/juci/selectiondialog.h +++ b/juci/selectiondialog.h @@ -4,7 +4,7 @@ class SelectionDialog : public Gtk::Dialog { public: - SelectionDialog(Source::View& view); + SelectionDialog(Gtk::TextView& text_view); void show(const std::map& rows); bool close(GdkEventFocus*); @@ -13,7 +13,7 @@ public: private: void adjust(int current_x, int current_y); - Source::View& view; + Gtk::TextView& text_view; Gtk::ScrolledWindow scrolled_window; Gtk::ListViewText list_view_text; }; \ No newline at end of file diff --git a/juci/source.cc b/juci/source.cc index 0a5ad6c..b7d794d 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -21,7 +21,7 @@ bool Source::Config::legal_extension(std::string e) const { ////////////// //// View //// ////////////// -Source::View::View(const Config& config, const std::string& file_path, const std::string& project_path): +Source::View::View(const Source::Config& config, const std::string& file_path, const std::string& project_path): config(config), file_path(file_path), project_path(project_path) { Gsv::init(); set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); @@ -50,13 +50,52 @@ string Source::View::get_line_before_insert() { return line; } +bool Source::View::on_key_press(GdkEventKey* key) { + //Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text. + if(key->keyval==GDK_KEY_Tab && key->state==0) { + Gtk::TextIter selection_start, selection_end; + get_source_buffer()->get_selection_bounds(selection_start, selection_end); + int line_start=selection_start.get_line(); + int line_end=selection_end.get_line(); + for(int line=line_start;line<=line_end;line++) { + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line); + get_source_buffer()->insert(line_it, config.tab); + } + return true; + } + //Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text. + else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) { + Gtk::TextIter selection_start, selection_end; + get_source_buffer()->get_selection_bounds(selection_start, selection_end); + int line_start=selection_start.get_line(); + int line_end=selection_end.get_line(); + + for(int line_nr=line_start;line_nr<=line_end;line_nr++) { + string line=get_line(line_nr); + if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) + return true; + } + + for(int line_nr=line_start;line_nr<=line_end;line_nr++) { + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); + Gtk::TextIter line_plus_it=line_it; + + for(unsigned c=0;cerase(line_it, line_plus_it); + } + return true; + } + return false; +} + ////////////////// //// ClangView /// ////////////////// clang::Index Source::ClangView::clang_index(0, 1); -Source::ClangView::ClangView(const Config& config, const std::string& file_path, const std::string& project_path): -Source::View::View(config, file_path, project_path), +Source::ClangView::ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path): +Source::View(config, file_path, project_path), parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { override_font(Pango::FontDescription(config.font)); override_background_color(Gdk::RGBA(config.background)); @@ -410,41 +449,6 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { } return true; } - //Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text. - if(key->keyval==GDK_KEY_Tab && key->state==0) { - Gtk::TextIter selection_start, selection_end; - get_source_buffer()->get_selection_bounds(selection_start, selection_end); - int line_start=selection_start.get_line(); - int line_end=selection_end.get_line(); - for(int line=line_start;line<=line_end;line++) { - Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line); - get_source_buffer()->insert(line_it, config.tab); - } - return true; - } - //Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text. - else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) { - Gtk::TextIter selection_start, selection_end; - get_source_buffer()->get_selection_bounds(selection_start, selection_end); - int line_start=selection_start.get_line(); - int line_end=selection_end.get_line(); - - for(int line_nr=line_start;line_nr<=line_end;line_nr++) { - string line=get_line(line_nr); - if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) - return true; - } - - for(int line_nr=line_start;line_nr<=line_end;line_nr++) { - Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); - Gtk::TextIter line_plus_it=line_it; - - for(unsigned c=0;cerase(line_it, line_plus_it); - } - return true; - } //Indent left when writing } on a new line else if(key->keyval==GDK_KEY_braceright) { string line=get_line_before_insert(); @@ -479,7 +483,8 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { } } } - return false; + + return Source::View::on_key_press(key); } //////////////////// @@ -489,8 +494,7 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { // Source::Controller::Controller() // Constructor for Controller Source::Controller::Controller(const Source::Config &config, - const std::string& file_path, std::string project_path) : - config(config) { + const std::string& file_path, std::string project_path) { if(project_path=="") { project_path=boost::filesystem::path(file_path).parent_path().string(); } diff --git a/juci/source.h b/juci/source.h index 7c7b2a2..e779c35 100644 --- a/juci/source.h +++ b/juci/source.h @@ -60,25 +60,31 @@ namespace Source { class View : public Gsv::View { public: - View(const Config& config, const std::string& file_path, const std::string& project_path); + View(const Source::Config& config, const std::string& file_path, const std::string& project_path); std::string get_line(size_t line_number); std::string get_line_before_insert(); - virtual std::vector get_autocomplete_suggestions(int line_number, int column) {return std::vector();} std::string file_path; std::string project_path; protected: - const Config& config; + const Source::Config& config; + bool on_key_press(GdkEventKey* key); }; // class View class GenericView : public View { public: - GenericView(const Config& config, const std::string& file_path, const std::string& project_path): - View(config, file_path, project_path) {} + GenericView(const Source::Config& config, const std::string& file_path, const std::string& project_path): + View(config, file_path, project_path) { + signal_key_press_event().connect(sigc::mem_fun(*this, &Source::GenericView::on_key_press), false); + } + private: + bool on_key_press(GdkEventKey* key) { + return Source::View::on_key_press(key); + } }; class ClangView : public View { public: - ClangView(const Config& config, const std::string& file_path, const std::string& project_path); + ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path); ~ClangView(); // inits the syntax highligthing on file open void init_syntax_highlighting(const std::map @@ -123,10 +129,7 @@ namespace Source { bool is_saved = true; - std::unique_ptr view; - - private: - const Config& config; + std::unique_ptr view; }; // class Controller } // namespace Source #endif // JUCI_SOURCE_H_ From 92e2417787e5f02d0513bb93e52d60aef7ab1c73 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 24 Jun 2015 11:14:15 +0200 Subject: [PATCH 12/25] Moved basic indentation to Source::View, Clang specific indentation still in Source::ClangView. Some fixes, improved indentation for non-clang files, also added tab_char to Source::Config. --- juci/source.cc | 80 ++++++++++++++++++++++++++++++++------------------ juci/source.h | 1 + 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index b7d794d..1993db2 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -50,9 +50,33 @@ string Source::View::get_line_before_insert() { return line; } +//Basic indentation bool Source::View::on_key_press(GdkEventKey* key) { + const std::regex spaces_regex(std::string("^(")+config.tab_char+"*).*$"); + //Indent as in next or previous line + if(key->keyval==GDK_KEY_Return && key->state==0) { + int line_nr=get_source_buffer()->get_insert()->get_iter().get_line(); + string line(get_line_before_insert()); + std::smatch sm; + if(std::regex_match(line, sm, spaces_regex)) { + if((line_nr+1)get_line_count()) { + string next_line=get_line(line_nr+1); + std::smatch sm2; + if(std::regex_match(next_line, sm2, spaces_regex)) { + if(sm2[1].str().size()>sm[1].str().size()) { + get_source_buffer()->insert_at_cursor("\n"+sm2[1].str()); + scroll_to(get_source_buffer()->get_insert()); + return true; + } + } + } + get_source_buffer()->insert_at_cursor("\n"+sm[1].str()); + scroll_to(get_source_buffer()->get_insert()); + return true; + } + } //Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text. - if(key->keyval==GDK_KEY_Tab && key->state==0) { + else if(key->keyval==GDK_KEY_Tab && key->state==0) { Gtk::TextIter selection_start, selection_end; get_source_buffer()->get_selection_bounds(selection_start, selection_end); int line_start=selection_start.get_line(); @@ -86,6 +110,22 @@ bool Source::View::on_key_press(GdkEventKey* key) { } return true; } + //"Smart" backspace key + else if(key->keyval==GDK_KEY_BackSpace) { + Gtk::TextIter insert_it=get_source_buffer()->get_insert()->get_iter(); + int line_nr=insert_it.get_line(); + if(line_nr>0) { + string line=get_line(line_nr); + string previous_line=get_line(line_nr-1); + smatch sm; + if(std::regex_match(previous_line, sm, spaces_regex)) { + if(line==sm[1] || line==(std::string(sm[1])+config.tab) || (line+config.tab==sm[1])) { + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); + get_source_buffer()->erase(line_it, insert_it); + } + } + } + } return false; } @@ -386,15 +426,15 @@ bool Source::ClangView::on_key_release(GdkEventKey* key) { return true; } -//TODO: replace indentation methods with a better implementation or -//maybe use libclang +//Clang indentation +//TODO: replace indentation methods with a better implementation or maybe use libclang bool Source::ClangView::on_key_press(GdkEventKey* key) { - const std::regex bracket_regex("^( *).*\\{ *$"); - const std::regex no_bracket_statement_regex("^( *)(if|for|else if|catch|while) *\\(.*[^;}] *$"); - const std::regex no_bracket_no_para_statement_regex("^( *)(else|try|do) *$"); - const std::regex spaces_regex("^( *).*$"); + const std::regex bracket_regex(std::string("^(")+config.tab_char+"*).*\\{ *$"); + const std::regex no_bracket_statement_regex(std::string("^(")+config.tab_char+"*)(if|for|else if|catch|while) *\\(.*[^;}] *$"); + const std::regex no_bracket_no_para_statement_regex(std::string("^(")+config.tab_char+"*)(else|try|do) *$"); + const std::regex spaces_regex(std::string("^(")+config.tab_char+"*).*$"); - //Indent as in previous line, and indent right after if/else/etc + //Indent depending on if/else/etc and brackets if(key->keyval==GDK_KEY_Return && key->state==0) { string line(get_line_before_insert()); std::smatch sm; @@ -417,14 +457,17 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { insert_it--; scroll_to(get_source_buffer()->get_insert()); get_source_buffer()->place_cursor(insert_it); + return true; } else if(std::regex_match(line, sm, no_bracket_statement_regex)) { get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); scroll_to(get_source_buffer()->get_insert()); + return true; } else if(std::regex_match(line, sm, no_bracket_no_para_statement_regex)) { get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); scroll_to(get_source_buffer()->get_insert()); + return true; } else if(std::regex_match(line, sm, spaces_regex)) { std::smatch sm2; @@ -444,17 +487,14 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { } } } - get_source_buffer()->insert_at_cursor("\n"+sm[1].str()); - scroll_to(get_source_buffer()->get_insert()); } - return true; } //Indent left when writing } on a new line else if(key->keyval==GDK_KEY_braceright) { string line=get_line_before_insert(); if(line.size()>=config.tab_size) { for(auto c: line) { - if(c!=' ') + if(c!=config.tab_char) return false; } Gtk::TextIter insert_it = get_source_buffer()->get_insert()->get_iter(); @@ -467,22 +507,6 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { } return false; } - //"Smart" backspace key - else if(key->keyval==GDK_KEY_BackSpace) { - Gtk::TextIter insert_it=get_source_buffer()->get_insert()->get_iter(); - int line_nr=insert_it.get_line(); - if(line_nr>0) { - string line=get_line(line_nr); - string previous_line=get_line(line_nr-1); - smatch sm; - if(std::regex_match(previous_line, sm, spaces_regex)) { - if(line==sm[1]) { - Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); - get_source_buffer()->erase(line_it, insert_it); - } - } - } - } return Source::View::on_key_press(key); } diff --git a/juci/source.h b/juci/source.h index e779c35..52d754f 100644 --- a/juci/source.h +++ b/juci/source.h @@ -22,6 +22,7 @@ namespace Source { unsigned tab_size; bool show_line_numbers, highlight_current_line; std::string tab, background, font; + char tab_char=' '; std::vector extensions; std::unordered_map tags, types; }; // class Config From 749e28a25942421620ac32734f024a9687897dc1 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 24 Jun 2015 13:01:06 +0200 Subject: [PATCH 13/25] Started cleaning up notebook.*. Fixed copy of Notebook::Controller::source_config. --- juci/juci.cc | 2 +- juci/juci.h | 5 +++++ juci/notebook.cc | 50 +++++++++++++++++++----------------------- juci/notebook.h | 11 +++------- juci/selectiondialog.h | 7 +++++- juci/source.h | 4 ---- juci/terminal.cc | 2 -- juci/window.cc | 4 ++-- 8 files changed, 40 insertions(+), 45 deletions(-) diff --git a/juci/juci.cc b/juci/juci.cc index 5402b10..1a6ec46 100644 --- a/juci/juci.cc +++ b/juci/juci.cc @@ -37,7 +37,7 @@ void Juci::on_activate() { if(directory!="") { //TODO: use the following instead, window->notebook_.open_directory(directory); window->notebook_.project_path=directory; - window->notebook_.directories().open_folder(directory); + window->notebook_.directories.open_folder(directory); } for(auto &f: files) window->notebook_.OnOpenFile(f); diff --git a/juci/juci.h b/juci/juci.h index 9d55502..4ce2cb2 100644 --- a/juci/juci.h +++ b/juci/juci.h @@ -1,3 +1,6 @@ +#ifndef JUCI_JUCI_H_ +#define JUCI_JUCI_H_ + #include "window.h" #include "logging.h" @@ -13,3 +16,5 @@ private: std::string directory; std::vector files; }; + +#endif // JUCI_JUCI_H_ \ No newline at end of file diff --git a/juci/notebook.cc b/juci/notebook.cc index a813162..d0ba733 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -17,12 +17,12 @@ Notebook::Controller::Controller(Gtk::Window* window, Keybindings::Controller& keybindings, Source::Config& source_cfg, Directories::Config& dir_cfg) : - directories_(dir_cfg), - source_config_(source_cfg) { + directories(dir_cfg), + source_config(source_cfg) { INFO("Create notebook"); window_ = window; refClipboard_ = Gtk::Clipboard::get(); - view().pack1(directories_.widget(), true, true); + view().pack1(directories.widget(), true, true); CreateKeybindings(keybindings); INFO("Notebook Controller Success"); } // Constructor @@ -31,7 +31,7 @@ Notebook::Controller::Controller(Gtk::Window* window, void Notebook::Controller::CreateKeybindings(Keybindings::Controller &keybindings) { INFO("Notebook create signal handlers"); - directories().m_TreeView.signal_row_activated() + directories.m_TreeView.signal_row_activated() .connect(sigc::mem_fun(*this, &Notebook::Controller::OnDirectoryNavigation)); @@ -191,7 +191,7 @@ Gtk::Box& Notebook::Controller::entry_view() { void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); INFO("Notebook create page"); - text_vec_.emplace_back(new Source::Controller(source_config(), path, project_path)); + text_vec_.emplace_back(new Source::Controller(source_config, path, project_path)); scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); editor_vec_.push_back(new Gtk::HBox()); scrolledtext_vec_.back()->add(*text_vec_.back()->view); @@ -204,7 +204,18 @@ void Notebook::Controller::OnOpenFile(std::string path) { Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); Notebook().set_focus_child(*text_vec_.back()->view); - set_source_handlers(*text_vec_.back()); + //Add star on tab label when the page is not saved: + text_vec_.back()->buffer()->signal_changed().connect([this]() { + if(text_vec_.at(CurrentPage())->is_saved) { + std::string path=text_vec_.at(CurrentPage())->view->file_path; + size_t pos = path.find_last_of("/\\"); + std::string filename=path; + if(pos!=std::string::npos) + filename=path.substr(pos+1); + Notebook().set_tab_label_text(*(Notebook().get_nth_page(CurrentPage())), filename+"*"); + } + text_vec_.at(CurrentPage())->is_saved=false; + }); } void Notebook::Controller::OnCloseCurrentPage() { @@ -310,18 +321,18 @@ void Notebook::Controller ::OnDirectoryNavigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column) { INFO("Notebook directory navigation"); - Gtk::TreeModel::iterator iter = directories().m_refTreeModel->get_iter(path); + Gtk::TreeModel::iterator iter = directories.m_refTreeModel->get_iter(path); if (iter) { Gtk::TreeModel::Row row = *iter; - std::string upath = Glib::ustring(row[directories().view().m_col_path]); + std::string upath = Glib::ustring(row[directories.view().m_col_path]); boost::filesystem::path fs_path(upath); if (boost::filesystem::is_directory(fs_path)) { - directories().m_TreeView.row_expanded(path) ? - directories().m_TreeView.collapse_row(path) : - directories().m_TreeView.expand_row(path, false); + directories.m_TreeView.row_expanded(path) ? + directories.m_TreeView.collapse_row(path) : + directories.m_TreeView.expand_row(path, false); } else { std::stringstream sstm; - sstm << row[directories().view().m_col_path]; + sstm << row[directories.view().m_col_path]; std::string file = sstm.str(); OnOpenFile(file); } @@ -357,21 +368,6 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr }); } -void Notebook::Controller::set_source_handlers(Source::Controller& controller) { - //Add star on tab label when the page is not saved: - controller.buffer()->signal_changed().connect([this]() { - if(text_vec_.at(CurrentPage())->is_saved) { - std::string path=text_vec_.at(CurrentPage())->view->file_path; - size_t pos = path.find_last_of("/\\"); - std::string filename=path; - if(pos!=std::string::npos) - filename=path.substr(pos+1); - Notebook().set_tab_label_text(*(Notebook().get_nth_page(CurrentPage())), filename+"*"); - } - text_vec_.at(CurrentPage())->is_saved=false; - }); -} - std::string Notebook::Controller::CurrentPagePath(){ return text_vec_.at(CurrentPage())->view->file_path; } diff --git a/juci/notebook.h b/juci/notebook.h index 21791ba..98af702 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -61,25 +61,20 @@ namespace Notebook { void OnOpenFile(std::string filename); bool ScrollEventCallback(GdkEventScroll* scroll_event); int Pages(); - Directories::Controller& directories() { return directories_; } Gtk::Paned& view(); void Search(bool forward); - Source::Config& source_config() { return source_config_; } std::string OnSaveFileAs(); - bool LegalExtension(std::string extension); std::string project_path; - protected: - void set_source_handlers(Source::Controller& controller); + Directories::Controller directories; private: void CreateKeybindings(Keybindings::Controller& keybindings); void AskToSaveDialog(); Glib::RefPtr m_refBuilder; Glib::RefPtr refActionGroup; - Source::Config source_config_; - Directories::Controller directories_; + Source::Config& source_config; View view_; Model model_; - bool is_new_file_; + bool is_new_file_; //TODO: Remove this Entry::Controller entry_; std::vector > text_vec_; diff --git a/juci/selectiondialog.h b/juci/selectiondialog.h index 70e16de..f8479ce 100644 --- a/juci/selectiondialog.h +++ b/juci/selectiondialog.h @@ -1,3 +1,6 @@ +#ifndef JUCI_SELECTIONDIALOG_H_ +#define JUCI_SELECTIONDIALOG_H_ + #include "gtkmm.h" #include "logging.h" #include "source.h" @@ -16,4 +19,6 @@ private: Gtk::TextView& text_view; Gtk::ScrolledWindow scrolled_window; Gtk::ListViewText list_view_text; -}; \ No newline at end of file +}; + +#endif // JUCI_SELECTIONDIALOG_H_ \ No newline at end of file diff --git a/juci/source.h b/juci/source.h index 52d754f..dce111d 100644 --- a/juci/source.h +++ b/juci/source.h @@ -11,10 +11,6 @@ #include #include "gtksourceviewmm.h" -namespace Notebook { - class Controller; -} - namespace Source { class Config { public: diff --git a/juci/terminal.cc b/juci/terminal.cc index 83f8205..d6b653d 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -87,11 +87,9 @@ void Terminal::Controller::ExecuteCommand(std::string command, std::string mode) FILE* p = NULL; std::cout << command << std::endl; p = popen(command.c_str(), mode.c_str()); - std::cout << "KJØRTE FINT!" << std::endl; if (p == NULL) { PrintMessage("juCi++ ERROR: Failed to run command" + command + "\n"); }else { - std::cout << "SKRIVER UT KOMMANDO RESULAT" << std::endl; char buffer[1028]; while (fgets(buffer, 1028, p) != NULL) { PrintMessage(buffer); diff --git a/juci/window.cc b/juci/window.cc index e3c116a..47ef193 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -73,7 +73,7 @@ Window::Window() : terminal_.SetFolderCommand(path); } terminal_.Compile(); - std::string executable = notebook_.directories(). + std::string executable = notebook_.directories. GetCmakeVarValue(path,"add_executable"); terminal_.Run(executable); running.unlock(); @@ -140,7 +140,7 @@ void Window::OnFileOpenFolder() { { std::string project_path=dialog.get_filename(); notebook_.project_path=project_path; - notebook_.directories().open_folder(project_path); + notebook_.directories.open_folder(project_path); break; } case(Gtk::RESPONSE_CANCEL): From bfb979530d8efe491cc6e76a9fd14f6aefb25395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Wed, 24 Jun 2015 17:20:19 +0200 Subject: [PATCH 14/25] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e1cc114..cf441e9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# juCi++ a lightweight C++-IDE with support for C++11 and C++14. +# juCi++ +###### a lightweight C++-IDE with support for C++11 and C++14. +## About juCi++ is a lightweight C++-IDE written in C++. You can write plugins in Python and configure the IDE from the config.json file. From 88c83660c174911b6415efee54774af349a2e881 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 25 Jun 2015 13:09:43 +0200 Subject: [PATCH 15/25] Cleaned up entry.*. New file now works, but did simplify it abit, that is removed new header and new c++ file, hope that was ok. More cleanup of source.* too. --- juci/config.json | 2 - juci/entry.cc | 121 ++++++++++++++++++++------------------------ juci/entry.h | 50 ++++++------------ juci/menu.xml | 6 +-- juci/notebook.cc | 128 ++++++++++++++++------------------------------- juci/notebook.h | 25 ++------- juci/window.cc | 2 +- 7 files changed, 118 insertions(+), 216 deletions(-) diff --git a/juci/config.json b/juci/config.json index 7c0b9d1..90723a7 100644 --- a/juci/config.json +++ b/juci/config.json @@ -40,8 +40,6 @@ }, "keybindings": { "new_file": "n", - "new_h_file": "h", - "new_cc_file": "c", "open_folder": "o", "open_file": "o", "save": "s", diff --git a/juci/entry.cc b/juci/entry.cc index 011ce6c..429016c 100644 --- a/juci/entry.cc +++ b/juci/entry.cc @@ -1,74 +1,61 @@ #include "entry.h" -Entry::View::View() : - view_(Gtk::ORIENTATION_HORIZONTAL), - button_apply_(Gtk::Stock::APPLY), - button_close_(Gtk::Stock::CLOSE), - button_next_("Next"), - button_prev_("Prev"){ -} -Gtk::Box& Entry::View::view() { - return view_; -} -void Entry::View::OnShowSetFilenName(std::string exstension) { - entry_.set_max_length(50); - entry_.set_text(exstension); - view_.pack_start(entry_); - view_.pack_end(button_close_, Gtk::PACK_SHRINK); - view_.pack_end(button_apply_, Gtk::PACK_SHRINK); -} -void Entry::View::OnShowSearch(std::string current){ - entry_.set_max_length(50); - entry_.set_text(current); - view_.pack_start(entry_); - view_.pack_start(button_next_, Gtk::PACK_SHRINK); - view_.pack_start(button_prev_, Gtk::PACK_SHRINK); - view_.pack_end(button_close_, Gtk::PACK_SHRINK); -} -void Entry::View::OnHideEntry(bool is_new_file) -{ - view_.remove(entry_); - view_.remove(button_close_); - if(!is_new_file){ - view_.remove(button_next_); - view_.remove(button_prev_); - }else{ - view_.remove(button_apply_); - } -} -Entry::Controller::Controller() { -} -Gtk::Box& Entry::Controller::view() { - return view_.view(); -} -void Entry::Controller::OnShowSetFilenName(std::string exstension) { - view_.OnShowSetFilenName(exstension); - view_.view().show_all(); - view_.entry().grab_focus(); - view_.entry().set_position(0); -} -void Entry::Controller::OnShowSearch(std::string current){ - view_.OnShowSearch(current); - view_.view().show_all(); - view_.entry().grab_focus(); - view_.entry().set_position(0); +Entry::Entry() : + Gtk::Box(Gtk::ORIENTATION_HORIZONTAL), + button_apply_set_filename(Gtk::Stock::APPLY), + button_close(Gtk::Stock::CLOSE), + button_next("Next"), + button_prev("Prev"){ + entry.signal_activate().connect([this](){ + if(activate) { + activate(); + } + }); + entry.signal_key_press_event().connect(sigc::mem_fun(*this, &Entry::on_key_press), false); } -void Entry::Controller::OnHideEntries(bool is_new_file){ - view_.OnHideEntry(is_new_file); -} -std::string Entry::Controller::text(){ - return view_.entry().get_text(); -} -Gtk::Button& Entry::Controller::button_apply(){ - return view_.button_apply(); -} -Gtk::Button& Entry::Controller::button_close(){ - return view_.button_close(); + +bool Entry::on_key_press(GdkEventKey* key) { + if(key->keyval==GDK_KEY_Escape) + hide(); + return false; } -Gtk::Button& Entry::Controller::button_next(){ - return view_.button_next(); + +void Entry::show_set_filename() { + hide(); + entry.set_max_length(50); + entry.set_text(""); + pack_start(entry); + pack_end(button_close, Gtk::PACK_SHRINK); + pack_end(button_apply_set_filename, Gtk::PACK_SHRINK); + show_all(); + entry.grab_focus(); + entry.set_position(0); + activate=[this](){ + button_apply_set_filename.clicked(); + }; } -Gtk::Button& Entry::Controller::button_prev(){ - return view_.button_prev(); + +void Entry::show_search(const std::string& current){ + hide(); + entry.set_max_length(50); + entry.set_text(current); + pack_start(entry); + pack_start(button_next, Gtk::PACK_SHRINK); + pack_start(button_prev, Gtk::PACK_SHRINK); + pack_end(button_close, Gtk::PACK_SHRINK); + show_all(); + entry.grab_focus(); + entry.set_position(0); + activate=[this](){ + button_next.clicked(); + }; +} +void Entry::hide() { + auto widgets=get_children(); + for(auto &w: widgets) + remove(*w); } +std::string Entry::operator()() { + return entry.get_text(); +} diff --git a/juci/entry.h b/juci/entry.h index ec8b888..36a45e2 100644 --- a/juci/entry.h +++ b/juci/entry.h @@ -2,43 +2,21 @@ #define JUCI_ENTRY_H_ #include +#include #include "gtkmm.h" -#include "keybindings.h" - -namespace Entry { - class View { - public: - View(); - Gtk::Box& view(); - Gtk::Entry& entry(){return entry_;} - Gtk::Button& button_apply(){return button_apply_;}; - Gtk::Button& button_close(){return button_close_;}; - Gtk::Button& button_next(){return button_next_;}; - Gtk::Button& button_prev(){return button_prev_;}; - void OnShowSetFilenName(std::string exstension); - void OnShowSearch(std::string current); - void OnHideEntry(bool is_new_file); - protected: - Gtk::Box view_; - Gtk::Entry entry_; - Gtk::Button button_apply_, button_close_, button_next_, button_prev_; - }; - class Controller { - public: - Controller(); - Gtk::Box& view(); - Gtk::Button& button_apply(); - Gtk::Button& button_close(); - Gtk::Button& button_next(); - Gtk::Button& button_prev(); - - std::string text(); - void OnShowSetFilenName(std::string exstension); - void OnShowSearch(std::string current); - void OnHideEntries(bool is_new_file); - View view_; - };// class controller -} // namespace notebook +class Entry: public Gtk::Box { +public: + Entry(); + void show_set_filename(); + void show_search(const std::string& current); + void hide(); + std::string operator()(); + Gtk::Entry entry; + Gtk::Button button_apply_set_filename, button_close, button_next, button_prev; +private: + bool on_key_press(GdkEventKey* key); + std::function activate; +}; #endif // JUCI_ENTRY_H_ diff --git a/juci/menu.xml b/juci/menu.xml index 2eab08a..18f82e3 100644 --- a/juci/menu.xml +++ b/juci/menu.xml @@ -1,11 +1,7 @@ - - - - - + diff --git a/juci/notebook.cc b/juci/notebook.cc index d0ba733..c1889d7 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -2,12 +2,6 @@ #include "notebook.h" #include "logging.h" -Notebook::Model::Model() { - cc_extension_ = ".cpp"; - h_extension_ = ".hpp"; - scrollvalue_ = 50; -} - Notebook::View::View() : notebook_() { view_.pack2(notebook_); view_.set_position(120); @@ -40,31 +34,12 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller Gtk::Stock::FILE)); keybindings.action_group_menu()-> - add(Gtk::Action::create("FileNewStandard", - "New empty file"), + add(Gtk::Action::create("FileNewFile", + "New file"), Gtk::AccelKey(keybindings.config_ .key_map()["new_file"]), [this]() { - is_new_file_ = true; - OnFileNewEmptyfile(); - }); - keybindings.action_group_menu()-> - add(Gtk::Action::create("FileNewCC", - "New source file"), - Gtk::AccelKey(keybindings.config_ - .key_map()["new_cc_file"]), - [this]() { - is_new_file_ = true; - OnFileNewCCFile(); - }); - keybindings.action_group_menu()-> - add(Gtk::Action::create("FileNewH", - "New header file"), - Gtk::AccelKey(keybindings.config_ - .key_map()["new_h_file"]), - [this]() { - is_new_file_ = true; - OnFileNewHeaderFile(); + OnFileNewFile(); }); keybindings.action_group_menu()-> add(Gtk::Action::create("WindowCloseTab", @@ -80,7 +55,6 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller Gtk::AccelKey(keybindings.config_ .key_map()["edit_find"]), [this]() { - is_new_file_ = false; OnEditSearch(); // TODO(Oyvang) Zalox, Forgi)Create function OnEditFind(); }); @@ -141,33 +115,41 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller INFO("Done Redo"); }); - entry_.view_.entry().signal_activate(). - connect( - [this]() { - if (is_new_file_) { - //OnNewPage(entry_.text()); //TODO: rewrite new file (the file needs to be created before opened with Source::Controller in order to choose the correct view implementation) - entry_.OnHideEntries(is_new_file_); - } else { - Search(true); - } - }); - entry_.button_apply().signal_clicked(). - connect( - [this]() { - //OnNewPage(entry_.text()); //TODO: rewrite new file (the file needs to be created before opened with Source::Controller in order to choose the correct view implementation) - entry_.OnHideEntries(is_new_file_); - }); - entry_.button_close().signal_clicked(). + entry.button_apply_set_filename.signal_clicked().connect([this]() { + std::string filename=entry(); + if(filename!="") { + if(project_path!="" && !boost::filesystem::path(filename).is_absolute()) + filename=project_path+"/"+filename; + boost::filesystem::path p(filename); + if(boost::filesystem::exists(p)) { + //TODO: alert user that file already exists + } + else { + std::ofstream f(p.string().c_str()); + if(f) { + OnOpenFile(boost::filesystem::canonical(p).string()); + if(project_path!="") + directories.open_folder(project_path); //TODO: Do refresh instead + } + else { + //TODO: alert user of error creating file + } + f.close(); + } + } + entry.hide(); + }); + entry.button_close.signal_clicked(). connect( [this]() { - entry_.OnHideEntries(is_new_file_); + entry.hide(); }); - entry_.button_next().signal_clicked(). + entry.button_next.signal_clicked(). connect( [this]() { Search(true); }); - entry_.button_prev().signal_clicked(). + entry.button_prev.signal_clicked(). connect( [this]() { Search(false); @@ -184,9 +166,6 @@ Notebook::Controller::~Controller() { Gtk::Paned& Notebook::Controller::view() { return view_.view(); } -Gtk::Box& Notebook::Controller::entry_view() { - return entry_.view(); -} void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); @@ -233,28 +212,22 @@ void Notebook::Controller::OnCloseCurrentPage() { editor_vec_.erase(editor_vec_.begin()+page); } } -void Notebook::Controller::OnFileNewEmptyfile() { - entry_.OnShowSetFilenName(""); -} -void Notebook::Controller::OnFileNewCCFile() { - entry_.OnShowSetFilenName(model_.cc_extension_); -} -void Notebook::Controller::OnFileNewHeaderFile() { - entry_.OnShowSetFilenName(model_.h_extension_); +void Notebook::Controller::OnFileNewFile() { + entry.show_set_filename(); } void Notebook::Controller::OnEditCopy() { if (Pages() != 0) { - Buffer(*text_vec_.at(CurrentPage()))->copy_clipboard(refClipboard_); + CurrentTextView().get_buffer()->copy_clipboard(refClipboard_); } } void Notebook::Controller::OnEditPaste() { if (Pages() != 0) { - Buffer(*text_vec_.at(CurrentPage()))->paste_clipboard(refClipboard_); + CurrentTextView().get_buffer()->paste_clipboard(refClipboard_); } } void Notebook::Controller::OnEditCut() { if (Pages() != 0) { - Buffer(*text_vec_.at(CurrentPage()))->cut_clipboard(refClipboard_); + CurrentTextView().get_buffer()->cut_clipboard(refClipboard_); } } @@ -263,8 +236,8 @@ std::string Notebook::Controller::GetCursorWord() { int page = CurrentPage(); std::string word; Gtk::TextIter start, end; - start = Buffer(*text_vec_.at(page))->get_insert()->get_iter(); - end = Buffer(*text_vec_.at(page))->get_insert()->get_iter(); + start = CurrentTextView().get_buffer()->get_insert()->get_iter(); + end = CurrentTextView().get_buffer()->get_insert()->get_iter(); if (!end.ends_line()) { while (!end.ends_word()) { end.forward_char(); @@ -275,28 +248,28 @@ std::string Notebook::Controller::GetCursorWord() { start.backward_char(); } } - word = Buffer(*text_vec_.at(page))->get_text(start, end); + word = CurrentTextView().get_buffer()->get_text(start, end); // TODO(Oyvang) fix selected text return word; } void Notebook::Controller::OnEditSearch() { search_match_end_ = - Buffer(*text_vec_.at(CurrentPage()))->get_iter_at_offset(0); - entry_.OnShowSearch(GetCursorWord()); + CurrentTextView().get_buffer()->get_iter_at_offset(0); + entry.show_search(GetCursorWord()); } void Notebook::Controller::Search(bool forward) { INFO("Notebook search"); int page = CurrentPage(); std::string search_word; - search_word = entry_.text(); + search_word = entry(); Gtk::TextIter test; if ( !forward ) { if ( search_match_start_ == 0 || search_match_start_.get_line_offset() == 0) { - search_match_start_ = Buffer(*text_vec_.at(CurrentPage()))->end(); + search_match_start_ = CurrentTextView().get_buffer()->end(); } search_match_start_. backward_search(search_word, @@ -306,7 +279,7 @@ void Notebook::Controller::Search(bool forward) { search_match_end_); } else { if ( search_match_end_ == 0 ) { - search_match_end_ = Buffer(*text_vec_.at(CurrentPage()))->begin(); + search_match_end_ = CurrentTextView().get_buffer()->begin(); } search_match_end_. forward_search(search_word, @@ -348,11 +321,6 @@ int Notebook::Controller::CurrentPage() { return Notebook().get_current_page(); } -Glib::RefPtr -Notebook::Controller::Buffer(Source::Controller &source) { - return source.view->get_buffer(); -} - int Notebook::Controller::Pages() { return Notebook().get_n_pages(); } @@ -360,14 +328,6 @@ Gtk::Notebook& Notebook::Controller::Notebook() { return view_.notebook(); } -void Notebook::Controller::BufferChangeHandler(Glib::RefPtr - buffer) { - buffer->signal_end_user_action().connect( - [this]() { - //UpdateHistory(); - }); -} - std::string Notebook::Controller::CurrentPagePath(){ return text_vec_.at(CurrentPage())->view->file_path; } diff --git a/juci/notebook.h b/juci/notebook.h index 98af702..597ec6f 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -11,15 +11,9 @@ #include #include #include "clangmm.h" +#include "keybindings.h" namespace Notebook { - class Model { - public: - Model(); - std::string cc_extension_; - std::string h_extension_; - int scrollvalue_; - }; class View { public: View(); @@ -35,37 +29,29 @@ namespace Notebook { Source::Config& config, Directories::Config& dir_cfg); ~Controller(); - Glib::RefPtr Buffer(Source::Controller &source); Source::View& CurrentTextView(); int CurrentPage(); - Gtk::Box& entry_view(); Gtk::Notebook& Notebook(); std::string CurrentPagePath(); - void OnBufferChange(); - void BufferChangeHandler(Glib::RefPtr - buffer); void OnCloseCurrentPage(); std::string GetCursorWord(); void OnEditCopy(); void OnEditCut(); void OnEditPaste(); void OnEditSearch(); - void OnFileNewCCFile(); - void OnFileNewEmptyfile(); - void OnFileNewHeaderFile(); - void OnFileOpenFolder(); + void OnFileNewFile(); bool OnSaveFile(); bool OnSaveFile(std::string path); void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); void OnOpenFile(std::string filename); - bool ScrollEventCallback(GdkEventScroll* scroll_event); int Pages(); Gtk::Paned& view(); void Search(bool forward); std::string OnSaveFileAs(); std::string project_path; - Directories::Controller directories; + Directories::Controller directories; //Todo: make private after creating open_directory() + Entry entry; private: void CreateKeybindings(Keybindings::Controller& keybindings); void AskToSaveDialog(); @@ -73,9 +59,6 @@ namespace Notebook { Glib::RefPtr refActionGroup; Source::Config& source_config; View view_; - Model model_; - bool is_new_file_; //TODO: Remove this - Entry::Controller entry_; std::vector > text_vec_; std::vector scrolledtext_vec_; diff --git a/juci/window.cc b/juci/window.cc index 47ef193..4c61bc0 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -111,7 +111,7 @@ Window::Window() : window_box_.pack_start(menu_.view(), Gtk::PACK_SHRINK); - window_box_.pack_start(notebook_.entry_view(), Gtk::PACK_SHRINK); + window_box_.pack_start(notebook_.entry, Gtk::PACK_SHRINK); paned_.set_position(300); paned_.pack1(notebook_.view(), true, false); paned_.pack2(terminal_.view(), true, true); From 68ac889879eaf234ab91649ad9fa10818d62abc0 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 25 Jun 2015 13:56:00 +0200 Subject: [PATCH 16/25] A little cleanup, mostly getting rid of warnings (Added -Wall and -Wno-reorder to compilation). --- juci/CMakeLists.txt | 2 +- juci/directories.cc | 4 +--- juci/juci.cc | 2 +- juci/notebook.cc | 15 +++++---------- juci/notebook.h | 1 - juci/source.cc | 4 ++-- juci/terminal.cc | 2 +- juci/window.cc | 12 ++++++------ 8 files changed, 17 insertions(+), 25 deletions(-) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index 4dcf244..b7a518e 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -3,7 +3,7 @@ set(project_name juci) set(module juci_to_python_api) project (${project_name}) add_definitions(-DBOOST_LOG_DYN_LINK) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -std=c++11 -pthread") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -std=c++11 -pthread -Wall -Wno-reorder") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/") #You are of course using Homebrew: diff --git a/juci/directories.cc b/juci/directories.cc index 199e4dd..6e8ca9e 100644 --- a/juci/directories.cc +++ b/juci/directories.cc @@ -43,8 +43,6 @@ list_dirs(const boost::filesystem::path& dir_path, unsigned row_id) { boost::filesystem::directory_iterator end_itr; - unsigned dir_counter = row_id; - unsigned file_counter = 0; Gtk::TreeModel::Row child; Gtk::TreeModel::Row row; DEBUG(""); @@ -82,7 +80,7 @@ list_dirs(const boost::filesystem::path& dir_path, int Directories::Controller::count(const std::string path) { int count = 0; - for (int i = 0; i < path.size(); i++) + for (size_t i = 0; i < path.size(); i++) if (path[i] == '/') count++; return count; diff --git a/juci/juci.cc b/juci/juci.cc index 1a6ec46..1ac98c6 100644 --- a/juci/juci.cc +++ b/juci/juci.cc @@ -16,7 +16,7 @@ int Juci::on_command_line(const Glib::RefPtr &cmd) char **argv = cmd->get_arguments(argc); ctx.parse(argc, argv); if(argc>=2) { - for(size_t c=1;cbuffer()->signal_changed().connect([this]() { if(text_vec_.at(CurrentPage())->is_saved) { - std::string path=text_vec_.at(CurrentPage())->view->file_path; + std::string path=CurrentTextView().file_path; size_t pos = path.find_last_of("/\\"); std::string filename=path; if(pos!=std::string::npos) @@ -328,12 +328,8 @@ Gtk::Notebook& Notebook::Controller::Notebook() { return view_.notebook(); } -std::string Notebook::Controller::CurrentPagePath(){ - return text_vec_.at(CurrentPage())->view->file_path; -} - bool Notebook::Controller:: OnSaveFile() { - std::string path=text_vec_.at(CurrentPage())->view->file_path; + std::string path=CurrentTextView().file_path; return OnSaveFile(path); } bool Notebook::Controller:: OnSaveFile(std::string path) { @@ -343,7 +339,7 @@ bool Notebook::Controller:: OnSaveFile(std::string path) { file.open (path); file << CurrentTextView().get_buffer()->get_text(); file.close(); - text_vec_.at(CurrentPage())->view->file_path=path; + CurrentTextView().file_path=path; size_t pos = path.find_last_of("/\\"); std::string filename=path; if(pos!=std::string::npos) @@ -373,8 +369,7 @@ std::string Notebook::Controller::OnSaveFileAs(){ case(Gtk::RESPONSE_OK): { DEBUG("get_filename()"); std::string path = dialog.get_filename(); - unsigned pos = path.find_last_of("/\\"); - return path; + return path; } case(Gtk::RESPONSE_CANCEL): { break; @@ -394,7 +389,7 @@ void Notebook::Controller::AskToSaveDialog() { false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); dialog.set_secondary_text( "Do you want to save: " + - text_vec_.at(CurrentPage())->view->file_path+" ?"); + CurrentTextView().file_path+" ?"); DEBUG("AskToSaveDialog: run dialog"); int result = dialog.run(); diff --git a/juci/notebook.h b/juci/notebook.h index 597ec6f..8816b86 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -32,7 +32,6 @@ namespace Notebook { Source::View& CurrentTextView(); int CurrentPage(); Gtk::Notebook& Notebook(); - std::string CurrentPagePath(); void OnCloseCurrentPage(); std::string GetCursorWord(); void OnEditCopy(); diff --git a/juci/source.cc b/juci/source.cc index 1993db2..fee103f 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -268,7 +268,7 @@ get_compilation_commands() { std::vector arguments; for (auto &i : cmds) { std::vector lol = i.get_command_as_args(); - for (int a = 1; a < lol.size()-4; a++) { + for (size_t a = 1; a < lol.size()-4; a++) { arguments.emplace_back(lol[a]); } } @@ -439,7 +439,7 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { string line(get_line_before_insert()); std::smatch sm; if(std::regex_match(line, sm, bracket_regex)) { - size_t line_nr=get_source_buffer()->get_insert()->get_iter().get_line(); + int line_nr=get_source_buffer()->get_insert()->get_iter().get_line(); if((line_nr+1)get_line_count()) { string next_line=get_line(line_nr+1); std::smatch sm2; diff --git a/juci/terminal.cc b/juci/terminal.cc index d6b653d..c58c97b 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -8,7 +8,7 @@ Terminal::Config::Config() { } Terminal::Config::Config(Terminal::Config& original) : run_command_(original.run_command_){ - for (auto it = 0; it Date: Thu, 25 Jun 2015 17:30:54 +0200 Subject: [PATCH 17/25] Instant file opening (can now see and write code while the file is being parsed for the first time), twice as fast initial parse+reparse, .h-files parsed as c++-files (guess this is ok). --- juci/source.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index fee103f..cef7072 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -145,11 +145,12 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { int start_offset = get_source_buffer()->begin().get_offset(); int end_offset = get_source_buffer()->end().get_offset(); - init_syntax_highlighting(get_buffer_map(), + std::map empty_buffer_map; + empty_buffer_map[file_path]=""; //for faster file opening + init_syntax_highlighting(empty_buffer_map, start_offset, end_offset, &ClangView::clang_index); - update_syntax(extract_tokens(start_offset, end_offset)); //GTK-calls must happen in main thread, so the parse_thread //sends signals to the main thread that it is to call the following functions: @@ -272,6 +273,8 @@ get_compilation_commands() { arguments.emplace_back(lol[a]); } } + if(boost::filesystem::path(file_path).extension()==".h") //TODO: temporary fix for .h-files (parse as c++) + arguments.emplace_back("-xc++"); return arguments; } From 6d7c4ffa09fdab4e292ca91e0bca0efc75c6887c Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 26 Jun 2015 16:40:04 +0200 Subject: [PATCH 18/25] Mainly cleanup of terminal.*. Added first time clang-parsing message in terminal. --- juci/config.cc | 16 +++++----- juci/config.h | 9 ++---- juci/juci.cc | 8 ++--- juci/notebook.cc | 29 +++++++----------- juci/notebook.h | 17 +++++------ juci/source.cc | 17 +++++++---- juci/source.h | 7 +++-- juci/terminal.cc | 79 ++++++++++++++++++++++++++++++------------------ juci/terminal.h | 35 ++++++++------------- juci/window.cc | 54 ++++++++++++++++----------------- juci/window.h | 6 ++-- 11 files changed, 139 insertions(+), 138 deletions(-) diff --git a/juci/config.cc b/juci/config.cc index 6ba32b7..22418eb 100644 --- a/juci/config.cc +++ b/juci/config.cc @@ -2,7 +2,7 @@ #include "logging.h" MainConfig::MainConfig() : - keybindings_cfg_(), source_cfg() { + keybindings_cfg(), source_cfg() { INFO("Reading config file"); boost::property_tree::json_parser::read_json("config.json", cfg_); INFO("Config file read"); @@ -22,7 +22,7 @@ void MainConfig::GenerateSource() { auto visual_json = source_json.get_child("visual"); for (auto &i : visual_json) { if (i.first == "background") { - source_cfg.background = i.second.get_value(); + source_cfg.background = i.second.get_value(); } if (i.first == "show_line_numbers") { source_cfg.show_line_numbers = i.second.get_value() == "1" ? true : false; @@ -55,10 +55,10 @@ void MainConfig::GenerateTerminalCommands() { boost::property_tree::ptree compile_commands_json = source_json.get_child("compile_commands"); boost::property_tree::ptree run_commands_json = source_json.get_child("run_commands"); for (auto &i : compile_commands_json) { - terminal_cfg_.InsertCompileCommand(i.second.get_value()); + terminal_cfg.compile_commands.emplace_back(i.second.get_value()); } for (auto &i : run_commands_json) { - terminal_cfg_.SetRunCommand(i.second.get_value()); + terminal_cfg.run_command=(i.second.get_value()); //TODO: run_commands array->one run_command? } } @@ -68,11 +68,11 @@ void MainConfig::GenerateKeybindings() { std::ifstream menu_xml("menu.xml"); if (menu_xml.is_open()) { while (getline(menu_xml, line)) - keybindings_cfg_.AppendXml(line); + keybindings_cfg.AppendXml(line); } boost::property_tree::ptree keys_json = cfg_.get_child("keybindings"); for (auto &i : keys_json) - keybindings_cfg_.key_map()[i.first] = i.second.get_value(); + keybindings_cfg.key_map()[i.first] = i.second.get_value(); DEBUG("Keybindings fetched"); } @@ -82,8 +82,8 @@ void MainConfig::GenerateDirectoryFilter() { boost::property_tree::ptree ignore_json = dir_json.get_child("ignore"); boost::property_tree::ptree except_json = dir_json.get_child("exceptions"); for ( auto &i : except_json ) - dir_cfg_.AddException(i.second.get_value()); + dir_cfg.AddException(i.second.get_value()); for ( auto &i : ignore_json ) - dir_cfg_.AddIgnore(i.second.get_value()); + dir_cfg.AddIgnore(i.second.get_value()); DEBUG("Directory filter fetched"); } diff --git a/juci/config.h b/juci/config.h index 309e297..bbe03cf 100644 --- a/juci/config.h +++ b/juci/config.h @@ -12,10 +12,10 @@ class MainConfig { public: Source::Config source_cfg; + Terminal::Config terminal_cfg; + Keybindings::Config keybindings_cfg; + Directories::Config dir_cfg; MainConfig(); - Keybindings::Config& keybindings_cfg() { return keybindings_cfg_; } - Directories::Config& dir_cfg() { return dir_cfg_; } - Terminal::Config& terminal_cfg() { return terminal_cfg_; } void PrintMenu(); void GenerateSource(); void GenerateKeybindings(); @@ -24,8 +24,5 @@ public: private: boost::property_tree::ptree cfg_; boost::property_tree::ptree key_tree_; - Keybindings::Config keybindings_cfg_; - Directories::Config dir_cfg_; - Terminal::Config terminal_cfg_; }; #endif diff --git a/juci/juci.cc b/juci/juci.cc index 1ac98c6..f19e7da 100644 --- a/juci/juci.cc +++ b/juci/juci.cc @@ -35,12 +35,12 @@ void Juci::on_activate() { add_window(*window); window->show(); if(directory!="") { - //TODO: use the following instead, window->notebook_.open_directory(directory); - window->notebook_.project_path=directory; - window->notebook_.directories.open_folder(directory); + //TODO: use the following instead, window->notebook.open_directory(directory); + window->notebook.project_path=directory; + window->notebook.directories.open_folder(directory); } for(auto &f: files) - window->notebook_.OnOpenFile(f); + window->notebook.OnOpenFile(f); } int main(int argc, char *argv[]) { diff --git a/juci/notebook.cc b/juci/notebook.cc index f916539..bc89429 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -2,21 +2,21 @@ #include "notebook.h" #include "logging.h" -Notebook::View::View() : notebook_() { - view_.pack2(notebook_); - view_.set_position(120); +Notebook::View::View() { + pack2(notebook); + set_position(120); } -Notebook::Controller::Controller(Gtk::Window* window, - Keybindings::Controller& keybindings, +Notebook::Controller::Controller(Keybindings::Controller& keybindings, + Terminal::Controller& terminal, Source::Config& source_cfg, Directories::Config& dir_cfg) : + terminal(terminal), directories(dir_cfg), source_config(source_cfg) { INFO("Create notebook"); - window_ = window; refClipboard_ = Gtk::Clipboard::get(); - view().pack1(directories.widget(), true, true); + view.pack1(directories.widget(), true, true); CreateKeybindings(keybindings); INFO("Notebook Controller Success"); } // Constructor @@ -163,14 +163,10 @@ Notebook::Controller::~Controller() { for (auto &i : scrolledtext_vec_) delete i; } -Gtk::Paned& Notebook::Controller::view() { - return view_.view(); -} - void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); INFO("Notebook create page"); - text_vec_.emplace_back(new Source::Controller(source_config, path, project_path)); + text_vec_.emplace_back(new Source::Controller(source_config, path, project_path, terminal)); scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); editor_vec_.push_back(new Gtk::HBox()); scrolledtext_vec_.back()->add(*text_vec_.back()->view); @@ -233,7 +229,6 @@ void Notebook::Controller::OnEditCut() { std::string Notebook::Controller::GetCursorWord() { INFO("Notebook get cursor word"); - int page = CurrentPage(); std::string word; Gtk::TextIter start, end; start = CurrentTextView().get_buffer()->get_insert()->get_iter(); @@ -261,7 +256,6 @@ void Notebook::Controller::OnEditSearch() { void Notebook::Controller::Search(bool forward) { INFO("Notebook search"); - int page = CurrentPage(); std::string search_word; search_word = entry(); Gtk::TextIter test; @@ -325,7 +319,7 @@ int Notebook::Controller::Pages() { return Notebook().get_n_pages(); } Gtk::Notebook& Notebook::Controller::Notebook() { - return view_.notebook(); + return view.notebook; } bool Notebook::Controller:: OnSaveFile() { @@ -354,10 +348,9 @@ bool Notebook::Controller:: OnSaveFile(std::string path) { std::string Notebook::Controller::OnSaveFileAs(){ INFO("Notebook save as"); - Gtk::FileChooserDialog dialog("Please choose a file", + Gtk::FileChooserDialog dialog((Gtk::Window&)(*view.get_toplevel()), "Please choose a file", Gtk::FILE_CHOOSER_ACTION_SAVE); DEBUG("SET TRANSISTEN FPR"); - dialog.set_transient_for(*window_); dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS); dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL); dialog.add_button("_Save", Gtk::RESPONSE_OK); @@ -385,7 +378,7 @@ std::string Notebook::Controller::OnSaveFileAs(){ void Notebook::Controller::AskToSaveDialog() { INFO("AskToSaveDialog"); DEBUG("AskToSaveDialog: Finding file path"); - Gtk::MessageDialog dialog(*window_, "Save file!", + Gtk::MessageDialog dialog((Gtk::Window&)(*view.get_toplevel()), "Save file!", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); dialog.set_secondary_text( "Do you want to save: " + diff --git a/juci/notebook.h b/juci/notebook.h index 8816b86..4a7cc2e 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -12,20 +12,18 @@ #include #include "clangmm.h" #include "keybindings.h" +#include "terminal.h" namespace Notebook { - class View { + class View : public Gtk::Paned { public: View(); - Gtk::Paned& view() {return view_;} - Gtk::Notebook& notebook() {return notebook_; } - protected: - Gtk::Paned view_; - Gtk::Notebook notebook_; + Gtk::Notebook notebook; }; class Controller { public: - Controller(Gtk::Window* window, Keybindings::Controller& keybindings, + Controller(Keybindings::Controller& keybindings, + Terminal::Controller& terminal, Source::Config& config, Directories::Config& dir_cfg); ~Controller(); @@ -45,8 +43,8 @@ namespace Notebook { Gtk::TreeViewColumn* column); void OnOpenFile(std::string filename); int Pages(); - Gtk::Paned& view(); void Search(bool forward); + View view; std::string OnSaveFileAs(); std::string project_path; Directories::Controller directories; //Todo: make private after creating open_directory() @@ -56,8 +54,8 @@ namespace Notebook { void AskToSaveDialog(); Glib::RefPtr m_refBuilder; Glib::RefPtr refActionGroup; + Terminal::Controller& terminal; Source::Config& source_config; - View view_; std::vector > text_vec_; std::vector scrolledtext_vec_; @@ -66,7 +64,6 @@ namespace Notebook { Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; Glib::RefPtr refClipboard_; - Gtk::Window* window_; }; // class controller } // namespace Notebook #endif // JUCI_NOTEBOOK_H_ diff --git a/juci/source.cc b/juci/source.cc index cef7072..8848c72 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -134,8 +134,8 @@ bool Source::View::on_key_press(GdkEventKey* key) { ////////////////// clang::Index Source::ClangView::clang_index(0, 1); -Source::ClangView::ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path): -Source::View(config, file_path, project_path), +Source::ClangView::ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path, Terminal::Controller& terminal): +Source::View(config, file_path, project_path), terminal(terminal), parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { override_font(Pango::FontDescription(config.font)); override_background_color(Gdk::RGBA(config.background)); @@ -163,10 +163,15 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { parse_thread_go=true; }); - parse_done.connect([this](){ + auto first_time_parse_done=this->terminal.PrintMessage("Parsing "+file_path, "finished"); + parse_done.connect([this, first_time_parse_done](){ if(parse_thread_mapped) { INFO("Updating syntax"); update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); + if(first_time_parse) { + first_time_parse_done(); + first_time_parse=false; + } INFO("Syntax updated"); } else { @@ -521,12 +526,12 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { // Source::Controller::Controller() // Constructor for Controller Source::Controller::Controller(const Source::Config &config, - const std::string& file_path, std::string project_path) { + const std::string& file_path, std::string project_path, Terminal::Controller& terminal) { if(project_path=="") { project_path=boost::filesystem::path(file_path).parent_path().string(); } if (config.legal_extension(file_path.substr(file_path.find_last_of(".") + 1))) - view=std::unique_ptr(new ClangView(config, file_path, project_path)); + view=std::unique_ptr(new ClangView(config, file_path, project_path, terminal)); else view=std::unique_ptr(new GenericView(config, file_path, project_path)); INFO("Source Controller with childs constructed"); @@ -534,4 +539,4 @@ Source::Controller::Controller(const Source::Config &config, Glib::RefPtr Source::Controller::buffer() { return view->get_source_buffer(); -} \ No newline at end of file +} diff --git a/juci/source.h b/juci/source.h index dce111d..a09dad6 100644 --- a/juci/source.h +++ b/juci/source.h @@ -10,6 +10,7 @@ #include #include #include "gtksourceviewmm.h" +#include "terminal.h" namespace Source { class Config { @@ -81,7 +82,7 @@ namespace Source { class ClangView : public View { public: - ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path); + ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path, Terminal::Controller& terminal); ~ClangView(); // inits the syntax highligthing on file open void init_syntax_highlighting(const std::map @@ -107,6 +108,8 @@ namespace Source { std::vector get_compilation_commands(); bool on_key_press(GdkEventKey* key); bool on_key_release(GdkEventKey* key); + Terminal::Controller& terminal; + bool first_time_parse=true; Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; @@ -121,7 +124,7 @@ namespace Source { class Controller { public: Controller(const Source::Config &config, - const std::string& file_path, std::string project_path); + const std::string& file_path, std::string project_path, Terminal::Controller& terminal); Glib::RefPtr buffer(); bool is_saved = true; diff --git a/juci/terminal.cc b/juci/terminal.cc index c58c97b..3c322a8 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -1,36 +1,18 @@ #include "terminal.h" #include #include +#include #include "logging.h" - -Terminal::Config::Config() { -} -Terminal::Config::Config(Terminal::Config& original) : - run_command_(original.run_command_){ - for (size_t it = 0; itset_text(""); + view.text_view.get_buffer()->set_text(""); DEBUG("Terminal: Compile: running cmake command"); - std::vector commands = config().compile_commands(); + std::vector commands = config.compile_commands; for (size_t it = 0; it < commands.size(); ++it) { ExecuteCommand(commands.at(it), "r"); @@ -60,21 +42,58 @@ void Terminal::Controller::Run(std::string executable) { PrintMessage("juCi++ execute: " + executable + "\n"); DEBUG("Terminal: Compile: running run command: "); DEBUG_VAR(executable); - ExecuteCommand("cd "+config().run_command() + "; ./"+executable, "r"); + ExecuteCommand("cd "+config.run_command + "; ./"+executable, "r"); PrintMessage("\n"); } -void Terminal::Controller::PrintMessage(std::string message){ +int Terminal::Controller::PrintMessage(std::string message){ INFO("Terminal: PrintMessage"); - Terminal().get_buffer()-> - insert(Terminal().get_buffer()-> end(),"> "+message); + view.text_view.get_buffer()->insert(view.text_view.get_buffer()->end(), "> "+message); + auto mark_end=view.text_view.get_buffer()->create_mark(view.text_view.get_buffer()->end()); + view.text_view.scroll_to(mark_end); + return mark_end->get_iter().get_line(); +} + +void Terminal::Controller::PrintMessage(int line_nr, std::string message){ + INFO("Terminal: PrintMessage at line " << line_nr); + auto iter=view.text_view.get_buffer()->get_iter_at_line(line_nr); + while(!iter.ends_line()) + iter++; + view.text_view.get_buffer()->insert(iter, message); } +std::function Terminal::Controller::PrintMessage(std::string start_msg, std::string done_msg) { + int line_nr=PrintMessage(start_msg+"...\n"); + + std::shared_ptr > stop(new std::atomic(false)); + std::shared_ptr waiting_print(new Glib::Dispatcher()); + waiting_print->connect([this, line_nr](){ + PrintMessage(line_nr-1, "."); + }); + + std::shared_ptr wait_thread(new std::thread([this, stop, waiting_print](){ + size_t c=0; + while(!*stop) { + if(c%100==0) + (*waiting_print)(); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + c++; + } + })); + + std::function done=[this, line_nr, stop, done_msg, waiting_print, wait_thread](){ + *stop=true; + PrintMessage(line_nr-1, done_msg); + if(wait_thread->joinable()) + wait_thread->join(); //waiting_print has to be destroyed in main thread + }; + return done; +} bool Terminal::Controller::ExistInConsole(std::string string) { INFO("Terminal: ExistInConsole"); DEBUG("Terminal: PrintMessage: finding string in buffer"); - double pos = Terminal().get_buffer()-> + double pos = view.text_view.get_buffer()-> get_text().find(string); if (pos == std::string::npos) return false; return true; diff --git a/juci/terminal.h b/juci/terminal.h index a07d5a5..12fecc1 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -2,6 +2,7 @@ #define JUCI_TERMINAL_H_ #include +#include #include "gtkmm.h" #include @@ -9,44 +10,32 @@ namespace Terminal { class Config { public: - Config (); - Config(Terminal::Config& original); - std::vector& compile_commands() { return compile_commands_; } - void InsertCompileCommand(std::string command); - std::string& run_command() { return run_command_; } - void SetRunCommand(std::string command); - private: - std::vector compile_commands_; - std::string run_command_; + std::vector compile_commands; + std::string run_command; }; - class View { + class View : public Gtk::HBox { public: - View(); - Gtk::HBox& view() {return view_;} - Gtk::TextView& textview() {return textview_;} - private: - Gtk::HBox view_; - Gtk::TextView textview_; - Gtk::ScrolledWindow scrolledwindow_; + View(); + Gtk::TextView text_view; + Gtk::ScrolledWindow scrolled_window; }; // class view class Controller { public: Controller(Terminal::Config& cfg); - Gtk::HBox& view() {return view_.view();} - Gtk::TextView& Terminal(){return view_.textview();} void SetFolderCommand(boost::filesystem::path CMake_path); void Run(std::string executable); void Compile(); - Terminal::Config& config() { return config_; } - void PrintMessage(std::string message); + int PrintMessage(std::string message); + void PrintMessage(int line_nr, std::string message); + std::function PrintMessage(std::string start_msg, std::string stop_msg); + Terminal::View view; private: - Terminal::Config config_; + Terminal::Config& config; void ExecuteCommand(std::string command, std::string mode); bool OnButtonRealeaseEvent(GdkEventKey* key); bool ExistInConsole(std::string string); - Terminal::View view_; std::string folder_command_; std::string path_; const std::string cmake_sucsess = "Build files have been written to:"; diff --git a/juci/window.cc b/juci/window.cc index fcbf1d1..c7c1340 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -4,13 +4,13 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), main_config_(), - keybindings_(main_config_.keybindings_cfg()), - terminal_(main_config_.terminal_cfg()), - notebook_(this,keybindings(), + keybindings_(main_config_.keybindings_cfg), + terminal(main_config_.terminal_cfg), + notebook(keybindings(), terminal, main_config_.source_cfg, - main_config_.dir_cfg()), + main_config_.dir_cfg), menu_(keybindings()), - api_(menu_, notebook_) { + api_(menu_, notebook) { INFO("Create Window"); set_title("juCi++"); set_default_size(600, 400); @@ -66,16 +66,16 @@ Window::Window() : SaveFile(); if (running.try_lock()) { std::thread execute([=]() { - std::string path = notebook_.CurrentTextView().file_path; + std::string path = notebook.CurrentTextView().file_path; size_t pos = path.find_last_of("/\\"); if(pos != std::string::npos) { path.erase(path.begin()+pos,path.end()); - terminal_.SetFolderCommand(path); + terminal.SetFolderCommand(path); } - terminal_.Compile(); - std::string executable = notebook_.directories. + terminal.Compile(); + std::string executable = notebook.directories. GetCmakeVarValue(path,"add_executable"); - terminal_.Run(executable); + terminal.Run(executable); running.unlock(); }); execute.detach(); @@ -92,13 +92,13 @@ Window::Window() : SaveFile(); if (running.try_lock()) { std::thread execute([=]() { - std::string path = notebook_.CurrentTextView().file_path; + std::string path = notebook.CurrentTextView().file_path; size_t pos = path.find_last_of("/\\"); if(pos != std::string::npos){ path.erase(path.begin()+pos,path.end()); - terminal_.SetFolderCommand(path); + terminal.SetFolderCommand(path); } - terminal_.Compile(); + terminal.Compile(); running.unlock(); }); execute.detach(); @@ -111,10 +111,10 @@ Window::Window() : window_box_.pack_start(menu_.view(), Gtk::PACK_SHRINK); - window_box_.pack_start(notebook_.entry, Gtk::PACK_SHRINK); + window_box_.pack_start(notebook.entry, Gtk::PACK_SHRINK); paned_.set_position(300); - paned_.pack1(notebook_.view(), true, false); - paned_.pack2(terminal_.view(), true, true); + paned_.pack1(notebook.view, true, false); + paned_.pack2(terminal.view, true, true); window_box_.pack_end(paned_); show_all_children(); INFO("Window created"); @@ -139,8 +139,8 @@ void Window::OnFileOpenFolder() { case(Gtk::RESPONSE_OK): { std::string project_path=dialog.get_filename(); - notebook_.project_path=project_path; - notebook_.directories.open_folder(project_path); + notebook.project_path=project_path; + notebook.directories.open_folder(project_path); break; } case(Gtk::RESPONSE_CANCEL): @@ -188,7 +188,7 @@ void Window::OnOpenFile() { switch (result) { case(Gtk::RESPONSE_OK): { std::string path = dialog.get_filename(); - notebook_.OnOpenFile(path); + notebook.OnOpenFile(path); break; } case(Gtk::RESPONSE_CANCEL): { @@ -201,20 +201,20 @@ void Window::OnOpenFile() { } bool Window::SaveFile() { - if(notebook_.OnSaveFile()) { - terminal_.PrintMessage("File saved to: " + - notebook_.CurrentTextView().file_path+"\n"); + if(notebook.OnSaveFile()) { + terminal.PrintMessage("File saved to: " + + notebook.CurrentTextView().file_path+"\n"); return true; } - terminal_.PrintMessage("File not saved"); + terminal.PrintMessage("File not saved"); return false; } bool Window::SaveFileAs() { - if(notebook_.OnSaveFile(notebook_.OnSaveFileAs())){ - terminal_.PrintMessage("File saved to: " + - notebook_.CurrentTextView().file_path+"\n"); + if(notebook.OnSaveFile(notebook.OnSaveFileAs())){ + terminal.PrintMessage("File saved to: " + + notebook.CurrentTextView().file_path+"\n"); return true; } - terminal_.PrintMessage("File not saved"); + terminal.PrintMessage("File not saved"); return false; } diff --git a/juci/window.h b/juci/window.h index 344a51e..dd63e6a 100644 --- a/juci/window.h +++ b/juci/window.h @@ -15,13 +15,11 @@ public: Gtk::Box window_box_; virtual ~Window() { } - -//private: MainConfig main_config_; Keybindings::Controller keybindings_; Menu::Controller menu_; - Notebook::Controller notebook_; - Terminal::Controller terminal_; + Notebook::Controller notebook; + Terminal::Controller terminal; PluginApi api_; Keybindings::Controller& keybindings() { return keybindings_; } From 5c21533435fc6e7b89a1c0df2873ed23723a6438 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 26 Jun 2015 17:08:26 +0200 Subject: [PATCH 19/25] Fixed a crash when tab was closed before the parsing was done. Juci sometimes crashes when quitting, maybe while parsing for the first time. Will look into it. --- juci/source.cc | 7 +++++-- juci/source.h | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index 8848c72..d053cac 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -163,13 +163,14 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { parse_thread_go=true; }); - auto first_time_parse_done=this->terminal.PrintMessage("Parsing "+file_path, "finished"); - parse_done.connect([this, first_time_parse_done](){ + first_time_parse_done=this->terminal.PrintMessage("Parsing "+file_path, "finished"); + parse_done.connect([this](){ if(parse_thread_mapped) { INFO("Updating syntax"); update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); if(first_time_parse) { first_time_parse_done(); + first_time_parse_done=[](){}; first_time_parse=false; } INFO("Syntax updated"); @@ -214,6 +215,8 @@ Source::ClangView::~ClangView() { parse_thread.join(); parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang parsing_mutex.unlock(); + if(first_time_parse) + first_time_parse_done(); //This function must be run if it is not run already } void Source::ClangView:: diff --git a/juci/source.h b/juci/source.h index a09dad6..3c0a71d 100644 --- a/juci/source.h +++ b/juci/source.h @@ -110,6 +110,7 @@ namespace Source { bool on_key_release(GdkEventKey* key); Terminal::Controller& terminal; bool first_time_parse=true; + std::function first_time_parse_done; Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; From dd862698375faf0c0824f90de21c99a53ec0fb58 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 26 Jun 2015 17:16:18 +0200 Subject: [PATCH 20/25] See TODO. Should fix this before merge. --- juci/terminal.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/juci/terminal.cc b/juci/terminal.cc index 3c322a8..c1ba7d3 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -62,6 +62,7 @@ void Terminal::Controller::PrintMessage(int line_nr, std::string message){ view.text_view.get_buffer()->insert(iter, message); } +//TODO: this was way too dirty, return a class instead. std::function Terminal::Controller::PrintMessage(std::string start_msg, std::string done_msg) { int line_nr=PrintMessage(start_msg+"...\n"); From c1714c5b35e58f9ee19422f80850f424dbd6104b Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 26 Jun 2015 21:32:11 +0200 Subject: [PATCH 21/25] Fixed crash from last commit. Moved the progress messages to a seperate class, and added cancel message. --- juci/notebook.h | 2 +- juci/source.cc | 12 +++---- juci/source.h | 3 +- juci/terminal.cc | 88 ++++++++++++++++++++++++++++-------------------- juci/terminal.h | 28 ++++++++++++--- juci/window.cc | 10 +++--- 6 files changed, 87 insertions(+), 56 deletions(-) diff --git a/juci/notebook.h b/juci/notebook.h index 4a7cc2e..6c96f5e 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -49,6 +49,7 @@ namespace Notebook { std::string project_path; Directories::Controller directories; //Todo: make private after creating open_directory() Entry entry; + std::vector > text_vec_; private: void CreateKeybindings(Keybindings::Controller& keybindings); void AskToSaveDialog(); @@ -57,7 +58,6 @@ namespace Notebook { Terminal::Controller& terminal; Source::Config& source_config; - std::vector > text_vec_; std::vector scrolledtext_vec_; std::vector editor_vec_; std::list listTargets_; diff --git a/juci/source.cc b/juci/source.cc index d053cac..cc6dbc6 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -163,16 +163,12 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { parse_thread_go=true; }); - first_time_parse_done=this->terminal.PrintMessage("Parsing "+file_path, "finished"); + parsing_in_progress=this->terminal.print_in_progress("Parsing "+file_path); parse_done.connect([this](){ if(parse_thread_mapped) { INFO("Updating syntax"); update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); - if(first_time_parse) { - first_time_parse_done(); - first_time_parse_done=[](){}; - first_time_parse=false; - } + parsing_in_progress->done("done"); INFO("Syntax updated"); } else { @@ -210,13 +206,13 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { } Source::ClangView::~ClangView() { + //TODO: Is it possible to stop the clang-process in progress? + parsing_in_progress->cancel("canceled"); parse_thread_stop=true; if(parse_thread.joinable()) parse_thread.join(); parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang parsing_mutex.unlock(); - if(first_time_parse) - first_time_parse_done(); //This function must be run if it is not run already } void Source::ClangView:: diff --git a/juci/source.h b/juci/source.h index 3c0a71d..7653ba1 100644 --- a/juci/source.h +++ b/juci/source.h @@ -109,8 +109,7 @@ namespace Source { bool on_key_press(GdkEventKey* key); bool on_key_release(GdkEventKey* key); Terminal::Controller& terminal; - bool first_time_parse=true; - std::function first_time_parse_done; + std::shared_ptr parsing_in_progress; Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; diff --git a/juci/terminal.cc b/juci/terminal.cc index c1ba7d3..02b9933 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -1,16 +1,54 @@ #include "terminal.h" #include -#include -#include #include "logging.h" +Terminal::InProgress::InProgress(Controller& terminal, const std::string& start_msg): + start_msg(start_msg), terminal(terminal), stop(false) { + waiting_print.connect([this](){ + this->terminal.print(line_nr-1, "."); + }); + start(); +} + +Terminal::InProgress::~InProgress() { + stop=true; + if(wait_thread.joinable()) + wait_thread.join(); +} + +void Terminal::InProgress::start() { + line_nr=this->terminal.print(start_msg+"...\n"); + wait_thread=std::thread([this](){ + size_t c=0; + while(!stop) { + if(c%100==0) + waiting_print(); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + c++; + } + }); +} + +void Terminal::InProgress::done(const std::string& msg) { + if(!stop) { + stop=true; + this->terminal.print(line_nr-1, msg); + } +} + +void Terminal::InProgress::cancel(const std::string& msg) { + if(!stop) { + stop=true; + this->terminal.print(line_nr-1, msg); + } +} + Terminal::View::View(){ text_view.set_editable(false); scrolled_window.add(text_view); add(scrolled_window); } - Terminal::Controller::Controller(Terminal::Config& cfg) : config(cfg) { folder_command_ = ""; @@ -33,20 +71,20 @@ void Terminal::Controller::Compile(){ ExecuteCommand(commands.at(it), "r"); } - PrintMessage("\n"); + print("\n"); DEBUG("Terminal: Compile: compile done"); } void Terminal::Controller::Run(std::string executable) { INFO("Terminal: Run"); - PrintMessage("juCi++ execute: " + executable + "\n"); + print("juCi++ execute: " + executable + "\n"); DEBUG("Terminal: Compile: running run command: "); DEBUG_VAR(executable); ExecuteCommand("cd "+config.run_command + "; ./"+executable, "r"); - PrintMessage("\n"); + print("\n"); } -int Terminal::Controller::PrintMessage(std::string message){ +int Terminal::Controller::print(std::string message){ INFO("Terminal: PrintMessage"); view.text_view.get_buffer()->insert(view.text_view.get_buffer()->end(), "> "+message); auto mark_end=view.text_view.get_buffer()->create_mark(view.text_view.get_buffer()->end()); @@ -54,7 +92,7 @@ int Terminal::Controller::PrintMessage(std::string message){ return mark_end->get_iter().get_line(); } -void Terminal::Controller::PrintMessage(int line_nr, std::string message){ +void Terminal::Controller::print(int line_nr, std::string message){ INFO("Terminal: PrintMessage at line " << line_nr); auto iter=view.text_view.get_buffer()->get_iter_at_line(line_nr); while(!iter.ends_line()) @@ -62,33 +100,9 @@ void Terminal::Controller::PrintMessage(int line_nr, std::string message){ view.text_view.get_buffer()->insert(iter, message); } -//TODO: this was way too dirty, return a class instead. -std::function Terminal::Controller::PrintMessage(std::string start_msg, std::string done_msg) { - int line_nr=PrintMessage(start_msg+"...\n"); - - std::shared_ptr > stop(new std::atomic(false)); - std::shared_ptr waiting_print(new Glib::Dispatcher()); - waiting_print->connect([this, line_nr](){ - PrintMessage(line_nr-1, "."); - }); - - std::shared_ptr wait_thread(new std::thread([this, stop, waiting_print](){ - size_t c=0; - while(!*stop) { - if(c%100==0) - (*waiting_print)(); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - c++; - } - })); - - std::function done=[this, line_nr, stop, done_msg, waiting_print, wait_thread](){ - *stop=true; - PrintMessage(line_nr-1, done_msg); - if(wait_thread->joinable()) - wait_thread->join(); //waiting_print has to be destroyed in main thread - }; - return done; +std::shared_ptr Terminal::Controller::print_in_progress(std::string start_msg) { + std::shared_ptr in_progress=std::shared_ptr(new Terminal::InProgress(*this, start_msg)); + return in_progress; } bool Terminal::Controller::ExistInConsole(std::string string) { @@ -108,11 +122,11 @@ void Terminal::Controller::ExecuteCommand(std::string command, std::string mode) std::cout << command << std::endl; p = popen(command.c_str(), mode.c_str()); if (p == NULL) { - PrintMessage("juCi++ ERROR: Failed to run command" + command + "\n"); + print("juCi++ ERROR: Failed to run command" + command + "\n"); }else { char buffer[1028]; while (fgets(buffer, 1028, p) != NULL) { - PrintMessage(buffer); + print(buffer); } pclose(p); } diff --git a/juci/terminal.h b/juci/terminal.h index 12fecc1..00e2fba 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -5,9 +5,10 @@ #include #include "gtkmm.h" #include +#include +#include namespace Terminal { - class Config { public: std::vector compile_commands; @@ -21,15 +22,34 @@ namespace Terminal { Gtk::ScrolledWindow scrolled_window; }; // class view + class Controller; + + //Temporary solution for displaying functions in progress, and when they are done. + class InProgress { + public: + InProgress(Controller& terminal, const std::string& start_msg); + ~InProgress(); + void done(const std::string& msg); + void cancel(const std::string& msg); + private: + void start(); + Controller& terminal; + std::string start_msg; + int line_nr; + std::atomic stop; + Glib::Dispatcher waiting_print; + std::thread wait_thread; + }; + class Controller { public: Controller(Terminal::Config& cfg); void SetFolderCommand(boost::filesystem::path CMake_path); void Run(std::string executable); void Compile(); - int PrintMessage(std::string message); - void PrintMessage(int line_nr, std::string message); - std::function PrintMessage(std::string start_msg, std::string stop_msg); + int print(std::string message); + void print(int line_nr, std::string message); + std::shared_ptr print_in_progress(std::string start_msg); Terminal::View view; private: Terminal::Config& config; diff --git a/juci/window.cc b/juci/window.cc index c7c1340..6c54878 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -121,6 +121,8 @@ Window::Window() : } // Window constructor void Window::OnWindowHide() { + for(size_t c=0;c Date: Fri, 26 Jun 2015 21:37:52 +0200 Subject: [PATCH 22/25] Small cleanup. --- juci/terminal.cc | 9 ++++----- juci/terminal.h | 3 +-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/juci/terminal.cc b/juci/terminal.cc index 02b9933..cce7496 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -2,12 +2,11 @@ #include #include "logging.h" -Terminal::InProgress::InProgress(Controller& terminal, const std::string& start_msg): - start_msg(start_msg), terminal(terminal), stop(false) { +Terminal::InProgress::InProgress(Controller& terminal, const std::string& start_msg): terminal(terminal), stop(false) { waiting_print.connect([this](){ this->terminal.print(line_nr-1, "."); }); - start(); + start(start_msg); } Terminal::InProgress::~InProgress() { @@ -16,8 +15,8 @@ Terminal::InProgress::~InProgress() { wait_thread.join(); } -void Terminal::InProgress::start() { - line_nr=this->terminal.print(start_msg+"...\n"); +void Terminal::InProgress::start(const std::string& msg) { + line_nr=this->terminal.print(msg+"...\n"); wait_thread=std::thread([this](){ size_t c=0; while(!stop) { diff --git a/juci/terminal.h b/juci/terminal.h index 00e2fba..317e43e 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -32,9 +32,8 @@ namespace Terminal { void done(const std::string& msg); void cancel(const std::string& msg); private: - void start(); + void start(const std::string& msg); Controller& terminal; - std::string start_msg; int line_nr; std::atomic stop; Glib::Dispatcher waiting_print; From 7d9403f1a234ab59981fe78ca259f732a16c3d4f Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 27 Jun 2015 00:09:04 +0200 Subject: [PATCH 23/25] Better autocompletion, working now even inside expressions. --- juci/source.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index cc6dbc6..4bf3e99 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -244,13 +244,15 @@ std::vector Source::ClangView:: get_autocomplete_suggestions(int line_number, int column) { INFO("Getting auto complete suggestions"); std::vector suggestions; - auto buffer_map=get_buffer_map(); + std::map buffer_map; + buffer_map[file_path]=get_source_buffer()->get_text(get_source_buffer()->begin(), get_source_buffer()->get_insert()->get_iter()); + buffer_map[file_path]+="\n"; parsing_mutex.lock(); clang::CodeCompleteResults results(tu_.get(), file_path, buffer_map, line_number, - column); + column-1); for (int i = 0; i < results.size(); i++) { const vector chunks_ = results.get(i).get_chunks(); std::vector chunks; From 18c1f262203eec9726848dc6e1f6ee1980a9e46b Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 28 Jun 2015 13:06:36 +0200 Subject: [PATCH 24/25] Added initial syntax highlighting when opening clang-files without #include. --- juci/source.cc | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index 4bf3e99..7fc9cf1 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -132,7 +132,7 @@ bool Source::View::on_key_press(GdkEventKey* key) { ////////////////// //// ClangView /// ////////////////// -clang::Index Source::ClangView::clang_index(0, 1); +clang::Index Source::ClangView::clang_index(0, 0); Source::ClangView::ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path, Terminal::Controller& terminal): Source::View(config, file_path, project_path), terminal(terminal), @@ -145,12 +145,25 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { int start_offset = get_source_buffer()->begin().get_offset(); int end_offset = get_source_buffer()->end().get_offset(); - std::map empty_buffer_map; - empty_buffer_map[file_path]=""; //for faster file opening - init_syntax_highlighting(empty_buffer_map, + auto buffer_map=get_buffer_map(); + //Remove includes for first parse for initial syntax highlighting + auto& str=buffer_map[file_path]; + std::size_t pos=0; + while((pos=str.find("#include", pos))!=std::string::npos) { + auto start_pos=pos; + pos=str.find('\n', pos+8); + if(pos==std::string::npos) + break; + if(start_pos==0 || str[start_pos-1]=='\n') { + str.replace(start_pos, pos-start_pos, pos-start_pos, ' '); + } + pos++; + } + init_syntax_highlighting(buffer_map, start_offset, end_offset, &ClangView::clang_index); + update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); //TODO: replace get_source_buffer()->get_text().size() //GTK-calls must happen in main thread, so the parse_thread //sends signals to the main thread that it is to call the following functions: From 7ad10db1fe685a7b4ce9667d392c3b56bf39a06e Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 28 Jun 2015 14:06:03 +0200 Subject: [PATCH 25/25] Added llvm version 3.6.1. --- juci/cmake/Modules/FindLibClang.cmake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/juci/cmake/Modules/FindLibClang.cmake b/juci/cmake/Modules/FindLibClang.cmake index 8a4ed46..3d15636 100644 --- a/juci/cmake/Modules/FindLibClang.cmake +++ b/juci/cmake/Modules/FindLibClang.cmake @@ -14,7 +14,8 @@ # Known LLVM release numbers. # most recent versions come first -set(LIBCLANG_KNOWN_LLVM_VERSIONS 3.6 +set(LIBCLANG_KNOWN_LLVM_VERSIONS 3.6.1 + 3.6 3.5.1 3.5.0 #Arch Linux 3.5 #LLVM Debian/Ubuntu packages from http://llvm.org/apt/