From dcc31583eac125d5ceb976550e81109e02e0876a Mon Sep 17 00:00:00 2001 From: oyvang Date: Mon, 13 Apr 2015 15:35:42 +0200 Subject: [PATCH 01/10] wrong branch, nothing worng tho --- juci/.#notebook.h | 1 - juci/terminal.cc | 5 ++++- juci/terminal.h | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) delete mode 120000 juci/.#notebook.h diff --git a/juci/.#notebook.h b/juci/.#notebook.h deleted file mode 120000 index 6989c31..0000000 --- a/juci/.#notebook.h +++ /dev/null @@ -1 +0,0 @@ -forgie@surface.6435:1428916838 \ No newline at end of file diff --git a/juci/terminal.cc b/juci/terminal.cc index e96a21a..001cc29 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -1,6 +1,9 @@ #include "terminal.h" - +Terminal::View::View(){ + textview_.add(buffer_); + view_.add(textview_); +} diff --git a/juci/terminal.h b/juci/terminal.h index 83d0738..c4bb4c9 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -10,15 +10,16 @@ namespace Terminal { class View { public: View(); - //Gtk::HBox view() {return view_;} + Gtk::HBox& view() {return view_;} private: Gtk::HBox view_; - Gtk::TextBuffer buffer_; + Glib::RefPtr buffer_; Gtk::TextView textview_; }; // class view class Controller { public: + }; // class controller From a27517293c0ff277cd93065f754ba3f086ebff68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Thu, 16 Apr 2015 10:00:55 +0200 Subject: [PATCH 02/10] add autocompletion demo --- juci/notebook.cc | 12 ++++++------ juci/source.cc | 33 +++++++++++++++++++++++++++++++++ juci/source.h | 9 ++++++++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 695591b..a6a613e 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -1,4 +1,5 @@ #include "notebook.h" +#include "clangmm.h" Notebook::Model::Model() { cc_extension_ = ".cc"; @@ -388,14 +389,13 @@ void Notebook::Controller::OnBufferChange() { end = Buffer(text_vec_.at(page))->get_insert()->get_iter(); start.backward_char(); word = Buffer(text_vec_.at(page))->get_text(start, end); + std::cout << start.get_line() << std::endl; + std::cout << start.get_line_offset() << std::endl; if (word == ".") { // TODO(Forgie) Zalox,Forgie) Remove TEST - std::vector TEST; - TEST.push_back("toString()"); - TEST.push_back("toLower()"); - TEST.push_back("toUpper()"); - TEST.push_back("fuckOFF()"); - TEST.push_back("fuckOFF()"); + std::vector TEST + = text_vec_[page]->GetAutoCompleteSuggestions(start.get_line()+1, + start.get_line_offset()+2); GeneratePopup(TEST); } } diff --git a/juci/source.cc b/juci/source.cc index 2117b79..5946bf1 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -132,6 +132,39 @@ ReParse(const std::string &buffer) { // fired when a line in the buffer is edited void Source::Controller::OnLineEdit() { } +std::vector Source::Controller:: +GetAutoCompleteSuggestions(int line_number, + int column) { + return model().GetAutoCompleteSuggestions(view().get_buffer() + ->get_text().raw(), + line_number, + column); +} + +std::vector Source::Model:: +GetAutoCompleteSuggestions(const std::string& buffer, + int line_number, + int column) { + + std::vector res; + parsing.lock(); + clang::CodeCompleteResults results(&tu_, + file_path(), + buffer, + line_number, + column); + for (int i = 0; i < results.size(); i++) { + std::stringstream ss; + const vector c = results.get(i).get_chunks(); + for (auto &stringchunk : c) { + ss << stringchunk.chunk(); + } + res.emplace_back(ss.str()); + } + parsing.unlock(); + return res; +} + // sets the filepath for this mvc void Source::Model:: set_file_path(const std::string &file_path) { diff --git a/juci/source.h b/juci/source.h index 67b3d9a..c674110 100644 --- a/juci/source.h +++ b/juci/source.h @@ -95,7 +95,11 @@ namespace Source { const string& project_path() const; // gets the config member const Config& config() const; - ~Model() { } + std::vector + GetAutoCompleteSuggestions(const std::string& buffer, + int line_number, + int column); + ~Model() { } int ReParse(const std::string &buffer); std::vector ExtractTokens(int, int); @@ -121,6 +125,9 @@ namespace Source { Model& model(); void OnNewEmptyFile(); void OnOpenFile(const string &filename); + std::vector + GetAutoCompleteSuggestions(int line_number, + int column); Glib::RefPtr buffer(); private: From bc0a24ec77698c6e5036b81bc24426dbe0439a2d Mon Sep 17 00:00:00 2001 From: oyvang Date: Thu, 16 Apr 2015 10:45:26 +0200 Subject: [PATCH 03/10] Fikxed Popup Bugs and started working on terminal --- juci/config.json | 3 +- juci/menu.xml | 3 +- juci/notebook.cc | 199 +++++++++++++++++++++++++++++++---------------- juci/notebook.h | 25 ++++-- juci/terminal.cc | 67 +++++++++++++++- juci/terminal.h | 23 ++++-- juci/window.cc | 53 +++++++++++++ juci/window.h | 7 +- 8 files changed, 290 insertions(+), 90 deletions(-) diff --git a/juci/config.json b/juci/config.json index e4ff1f5..e4ab5e7 100644 --- a/juci/config.json +++ b/juci/config.json @@ -23,7 +23,8 @@ "new_h_file": "h", "new_cc_file": "c", "close_tab": "w", - "open_folder": "o" + "open_folder": "o", + "save_as": "s" }, "directoryfilter": { "ignore": [ diff --git a/juci/menu.xml b/juci/menu.xml index 930c460..e5ad384 100644 --- a/juci/menu.xml +++ b/juci/menu.xml @@ -7,7 +7,8 @@ - + + diff --git a/juci/notebook.cc b/juci/notebook.cc index 16cbea2..c6cc3d9 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -1,4 +1,6 @@ #include "notebook.h" +#include + Notebook::Model::Model() { cc_extension_ = ".cc"; @@ -15,12 +17,12 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings, Source::Config& source_cfg, Directories::Config& dir_cfg) : source_config_(source_cfg), - directories_(dir_cfg) { + directories_(dir_cfg) { OnNewPage("juCi++"); refClipboard_ = Gtk::Clipboard::get(); + ispopup = false; view().pack1(directories_.widget(),true,true); - CreateKeybindings(keybindings); - + CreateKeybindings(keybindings); }// Constructor void Notebook::Controller::CreateKeybindings(Keybindings::Controller @@ -94,12 +96,6 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller [this]() { OnEditPaste(); }); - keybindings.action_group_hidden()-> - add(Gtk::Action::create("EditPaste", - Gtk::Stock::PASTE), - [this]() { - OnEditPaste(); - }); entry_.view_.entry().signal_activate(). connect( [this]() { @@ -128,58 +124,62 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller }); entry_.button_prev().signal_clicked(). connect( - [this]() { + [this]() { Search(false); }); } -void Notebook::Controller::GeneratePopup(std::vector items){ +bool Notebook::Controller:: OnMouseRelease(GdkEventButton* button){ + if(button->button == 1 && ispopup){ + popup_.response(Gtk::RESPONSE_DELETE_EVENT); + return true; + } + return false; +} + +bool Notebook::Controller::GeneratePopup(Gtk::Window* window){ + // Get function to fill popup with suggests item vector under is for testing + std::vector items; + items.push_back("toString(std::string& string) -> void"); + items.push_back("toLower(std::string& string) -> void"); + items.push_back("toUpper(std::string& string) -> void"); + items.push_back("fuckOFF(const std::string string) -> bool"); + items.push_back("fuckOFF(const std::string string) -> bool"); + items.push_back("toString(std::string& string) -> void"); + items.push_back("toLower(std::string& string) -> void"); + items.push_back("toUpper(std::string& string) -> void"); + items.push_back("fuckOFF(const std::string string) -> bool"); + items.push_back("fuckOFF(const std::string string) -> bool"); + + // Replace over with get suggestions from zalox! OVER IS JUST FOR TESTING + Gtk::ScrolledWindow popup_scroll_; Gtk::ListViewText listview_(1,false,Gtk::SelectionMode::SELECTION_SINGLE); - Gtk::Dialog popup_("",true); + + popup_scroll_.set_policy(Gtk::PolicyType::POLICY_NEVER, + Gtk::PolicyType::POLICY_NEVER); listview_.set_enable_search(false); + listview_.set_headers_visible(false); listview_.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); listview_.set_activate_on_single_click(true); - listview_.signal_row_activated(). - connect([this, &listview_, &popup_](const Gtk::TreeModel::Path& path, - Gtk::TreeViewColumn*) { - std::string t = listview_.get_text(listview_.get_selected()[0]); - CurrentTextView().get_buffer()->insert_at_cursor(t); - popup_.response(Gtk::RESPONSE_DELETE_EVENT); - }); for (auto &i : items) listview_.append(i); - listview_.set_headers_visible(false); popup_scroll_.add(listview_); popup_.get_vbox()->pack_start(popup_scroll_); - popup_.set_size_request(80,80); + popup_.set_transient_for(*window); popup_.show_all(); - Gdk::Rectangle temp1, temp2; - CurrentTextView(). - get_cursor_locations( - CurrentTextView(). - get_buffer()->get_insert()-> - get_iter(), temp1, temp2); - - int x = temp1.get_x(); - int y = temp1.get_y(); - text_vec_.at(CurrentPage())-> - view().buffer_to_window_coords( - Gtk::TextWindowType::TEXT_WINDOW_WIDGET, - temp2.get_x(), - temp2.get_y(), - x, y); - - - int widht = Notebook().get_width()-88; - int height = Notebook().get_height()-180; - if(x>widht){ - x = widht; } - if(y>height){ - y =height; - } - popup_.move(x, y+88); + 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); + PopupSelectHandler(popup_, listview_); + ispopup = true; popup_.run(); + popup_.hide(); + ispopup = false; + return true; } bool Notebook::Controller::ScrollEventCallback(GdkEventScroll* scroll_event) { @@ -235,6 +235,7 @@ void Notebook::Controller::OnOpenFile(std::string path) { OnBufferChange(); } + void Notebook::Controller::OnCreatePage(){ text_vec_.push_back(new Source::Controller(source_config())); linenumbers_vec_.push_back(new Source::Controller(source_config())); @@ -252,13 +253,13 @@ void Notebook::Controller::OnCreatePage(){ linenumbers_vec_.back()->view().set_sensitive(false); editor_vec_.back()->pack_start(*scrolledline_vec_.back(),false,false); editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); - BufferChangeHandler(text_vec_.back()->view().get_buffer()); + TextViewHandlers(text_vec_.back()->view()); } void Notebook::Controller::OnCloseCurrentPage() { //TODO (oyvang, zalox, forgi) Save a temp file, in case you close one you dont want to close? if(Pages()!=0){ - int page = CurrentPage(); + int page = CurrentPage(); //(Zalox)Have to call your destructors to kill / join threads Notebook().remove_page(page); delete text_vec_.at(page); delete linenumbers_vec_.at(page); @@ -365,7 +366,8 @@ void Notebook::Controller::OnBufferChange() { "\n"+std::to_string(line_nr)+" "); } while (line_nr > text_nr ){ - Gtk::TextIter iter = Buffer(linenumbers_vec_.at(page))->get_iter_at_line(line_nr); + Gtk::TextIter iter = + Buffer(linenumbers_vec_.at(page))->get_iter_at_line(line_nr); iter.backward_char(); line_nr--; Buffer(linenumbers_vec_.at(page))-> @@ -382,22 +384,6 @@ void Notebook::Controller::OnBufferChange() { ScrollEventCallback(scroll); delete scroll; } - Gtk::TextIter start,end; - std::string word; - start = Buffer(text_vec_.at(page))->get_insert()->get_iter(); - end = Buffer(text_vec_.at(page))->get_insert()->get_iter(); - start.backward_char(); - word = Buffer(text_vec_.at(page))->get_text(start,end); - if( word == "."){ - //TODO(Oyvang,Zalox,Forgie) Remove TEST - std::vector TEST; - TEST.push_back("toString()"); - TEST.push_back("toLower()"); - TEST.push_back("toUpper()"); - TEST.push_back("fuckOFF()"); - TEST.push_back("fuckOFF()"); - GeneratePopup(TEST); - } } void Notebook::Controller ::OnDirectoryNavigation(const Gtk::TreeModel::Path& path, @@ -439,11 +425,88 @@ Gtk::Notebook& Notebook::Controller::Notebook() { return view_.notebook(); } -void Notebook::Controller::BufferChangeHandler(Glib::RefPtr - buffer) { - buffer->signal_changed().connect( +void Notebook::Controller::TextViewHandlers(Gtk::TextView& textview) { + textview.get_buffer()->signal_changed().connect( [this]() { OnBufferChange(); }); + + textview.signal_button_release_event(). + connect(sigc::mem_fun(*this,&Notebook::Controller::OnMouseRelease),false); +} +void Notebook::Controller::PopupSelectHandler(Gtk::Dialog &popup, + Gtk::ListViewText &listview){ + listview.signal_row_activated(). + connect([this, &listview, &popup](const Gtk::TreeModel::Path& path, + Gtk::TreeViewColumn*) { + std::string selected = + 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) { + 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); + } +} + +void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, + int popup_x, + int popup_y, + int &x, + int &y){ + 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 ((textview_edge_y-y)*-1 > textview.get_height()-popup_y) { + y -= (popup_y+14) + 15; + } } +void Notebook::Controller:: OnSaveFile(std::string path){ + std::ofstream file; + file.open (path); + file << CurrentTextView().get_buffer()->get_text(); + file.close(); +} diff --git a/juci/notebook.h b/juci/notebook.h index b3fb6f9..d8f0e40 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -6,6 +6,7 @@ #include "entry.h" #include "source.h" + #include "directories.h" #include @@ -33,7 +34,6 @@ namespace Notebook { }; class Controller { public: - Controller(Keybindings::Controller& keybindings, Source::Config& config, Directories::Config& dir_cfg); @@ -54,6 +54,7 @@ namespace Notebook { void OnFileNewEmptyfile(); void OnFileNewHeaderFile(); void OnFileOpenFolder(); + void OnSaveFile(std::string path); void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); void OnNewPage(std::string name); @@ -61,19 +62,27 @@ namespace Notebook { void OnCreatePage(); bool ScrollEventCallback(GdkEventScroll* scroll_event); int Pages(); - Directories::Controller& directories() { return directories_; } Gtk::Paned& view(); - - void GeneratePopup(std::vector items); - // Gtk::HBox& view(); - + bool GeneratePopup(Gtk::Window* window); void Search(bool forward); const Source::Config& source_config() { return source_config_; } + bool OnMouseRelease(GdkEventButton* button); protected: - void BufferChangeHandler(Glib::RefPtr buffer); + void TextViewHandlers(Gtk::TextView& textview);//Glib::RefPtr buffer); + void PopupSelectHandler(Gtk::Dialog &popup, + Gtk::ListViewText &listview); + 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); Glib::RefPtr m_refBuilder; Glib::RefPtr refActionGroup; Source::Config source_config_; @@ -89,6 +98,8 @@ namespace Notebook { Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; Glib::RefPtr refClipboard_; + bool ispopup; + Gtk::Dialog popup_; }; // class controller } // namespace Notebook diff --git a/juci/terminal.cc b/juci/terminal.cc index 001cc29..d81ff11 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -1,9 +1,70 @@ #include "terminal.h" +#include Terminal::View::View(){ - textview_.add(buffer_); - view_.add(textview_); + scrolledwindow_.add(textview_); + scrolledwindow_.set_size_request(-1,150); + view_.add(scrolledwindow_); +} + + +Terminal::Controller::Controller() { + root = ""; + Terminal().signal_key_release_event(). + connect(sigc::mem_fun(*this,&Terminal::Controller::OnButtonRealeaseEvenet),false); +} + +bool Terminal::Controller::OnButtonRealeaseEvenet(GdkEventKey *key) { + if(key->keyval == 65421 || key->keyval == 65293){ + ExecuteCommand(); + } + return false; +} +void Terminal::Controller::ExecuteCommand() { + + std::cout << "EXECUTE COMMAND ALGORITHM "<< std::endl; + std::string temp = getCommand(); + if(temp != ""){ + std::cout << "EXECUTE COMMAND: "<insert(Terminal().get_buffer()->end(),"Command Failed\n"); + + } else{ + char buffer[1028]; + + while (fgets(buffer, 1028, p) != NULL) + { + Terminal().get_buffer()->insert(Terminal().get_buffer()->end(),buffer); + } + pclose(p); + + + } + Terminal().get_buffer()->insert(Terminal().get_buffer()->end(),"Command Executed\n"); + }else{ + std::cout << "NO COMMAND TO RUN"<< std::endl; + } + +} + +std::string Terminal::Controller::getCommand(){ + std::string command = ""; + Gtk::TextIter start,end; + int a = Terminal().get_buffer()->get_insert()->get_iter().get_line()-1; + if(a==-1)a=0; + start = Terminal().get_buffer()->get_iter_at_line(a); + end =Terminal().get_buffer()->get_iter_at_line(a); + while(!end.ends_line()) { + end++; + } + while(!start.starts_line()) { + start--; + } + + command = Terminal().get_buffer()->get_text(start,end); + return command; } - diff --git a/juci/terminal.h b/juci/terminal.h index c4bb4c9..d91876c 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -1,8 +1,6 @@ - -#ifndef JUCI_NOTEBOOK_H_ -#define JUCI_NOTEBOOK_H_ +#ifndef JUCI_TERMINAL_H_ +#define JUCI_TERMINAL_H_ -#include #include "gtkmm.h" namespace Terminal { @@ -11,18 +9,27 @@ namespace Terminal { public: View(); Gtk::HBox& view() {return view_;} + Gtk::TextView& textview() {return textview_;} private: Gtk::HBox view_; - Glib::RefPtr buffer_; Gtk::TextView textview_; + Gtk::ScrolledWindow scrolledwindow_; }; // class view - class Controller { + class Controller { public: - + Controller(); + Gtk::HBox& view() {return view_.view();} + Gtk::TextView& Terminal(){return view_.textview();} + private: + void ExecuteCommand(); + std::string getCommand(); + bool OnButtonRealeaseEvenet(GdkEventKey* key); + Terminal::View view_; + std::string root; }; // class controller } // namespace Terminal -#endif // JUCI_NOTEBOOK_H_ +#endif // JUCI_TERMINAL_H_ diff --git a/juci/window.cc b/juci/window.cc index 1097e52..cf6e5c6 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -26,6 +26,22 @@ Window::Window() : [this]() { OnFileOpenFolder(); }); + + keybindings_.action_group_menu()->add(Gtk::Action::create("FileSaveAs", + "Save as"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["save_as"]), + [this]() { + OnSaveFileAs(); + }); + + notebook_.CurrentTextView().signal_key_release_event(). + connect(sigc::mem_fun(*this,&Window::OnKeyRelease),false); + 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); + PluginApi::menu_ = &menu_; PluginApi::notebook_ = ¬ebook_; PluginApi::InitPlugins(); @@ -37,6 +53,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_.view()); + window_box_.pack_end(terminal_.view(),Gtk::PACK_SHRINK); show_all_children(); } // Window constructor @@ -112,6 +129,7 @@ void Window::OnOpenFile() { case(Gtk::RESPONSE_OK): { std::cout << "Open clicked." << std::endl; std::string path = dialog.get_filename(); + std::cout << "File selected: " << path << std::endl; notebook_.OnOpenFile(path); break; @@ -126,3 +144,38 @@ void Window::OnOpenFile() { } } } +void Window::OnSaveFileAs(){ + Gtk::FileChooserDialog dialog("Please choose a file", + Gtk::FILE_CHOOSER_ACTION_SAVE); + dialog.set_transient_for(*this); + dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS); + dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL); + dialog.add_button("_Save", Gtk::RESPONSE_OK); + int result = dialog.run(); + switch (result) { + case(Gtk::RESPONSE_OK): { + std::string path = dialog.get_filename(); + unsigned pos = path.find_last_of("/\\"); + std::cout << path<< std::endl; + notebook_.OnSaveFile(path); + break; + } + case(Gtk::RESPONSE_CANCEL): { + break; + } + default: { + std::cout << "Unexpected button clicked." << std::endl; + break; + } + } +} +bool Window::OnKeyRelease(GdkEventKey* key){ + if(key->keyval==46){ + return notebook_.GeneratePopup(this); + } + return false; +} +bool Window::OnMouseRelease(GdkEventButton *button){ + return notebook_.OnMouseRelease(button); +} + diff --git a/juci/window.h b/juci/window.h index a0b810f..88297da 100644 --- a/juci/window.h +++ b/juci/window.h @@ -3,6 +3,7 @@ #include "api.h" #include "config.h" +#include "terminal.h" #include @@ -19,8 +20,7 @@ public: Keybindings::Controller keybindings_; Menu::Controller menu_; Notebook::Controller notebook_; - - + Terminal::Controller terminal_; Keybindings::Controller& keybindings() { return keybindings_; } private: @@ -28,6 +28,9 @@ public: void OnWindowHide(); void OnOpenFile(); void OnFileOpenFolder(); + void OnSaveFileAs(); + bool OnKeyRelease(GdkEventKey* key); + bool OnMouseRelease(GdkEventButton* button); }; From 63d69c6ca3f007c440be4e62663f05ee409098df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Thu, 16 Apr 2015 10:58:04 +0200 Subject: [PATCH 04/10] add vector as parameter --- juci/notebook.cc | 7 ++++--- juci/source.cc | 28 ++++++++++++++-------------- juci/source.h | 14 +++++++------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index a6a613e..876e619 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -393,9 +393,10 @@ void Notebook::Controller::OnBufferChange() { std::cout << start.get_line_offset() << std::endl; if (word == ".") { // TODO(Forgie) Zalox,Forgie) Remove TEST - std::vector TEST - = text_vec_[page]->GetAutoCompleteSuggestions(start.get_line()+1, - start.get_line_offset()+2); + std::vector TEST; + text_vec_[page]->GetAutoCompleteSuggestions(start.get_line()+1, + start.get_line_offset()+2, + &TEST); GeneratePopup(TEST); } } diff --git a/juci/source.cc b/juci/source.cc index 5946bf1..baec800 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -132,22 +132,24 @@ ReParse(const std::string &buffer) { // fired when a line in the buffer is edited void Source::Controller::OnLineEdit() { } -std::vector Source::Controller:: +void Source::Controller:: GetAutoCompleteSuggestions(int line_number, - int column) { - return model().GetAutoCompleteSuggestions(view().get_buffer() - ->get_text().raw(), - line_number, - column); + int column, + std::vector *suggestions) { + parsing.lock(); + model().GetAutoCompleteSuggestions(view().get_buffer() + ->get_text().raw(), + line_number, + column, + suggestions); + parsing.unlock(); } -std::vector Source::Model:: +void Source::Model:: GetAutoCompleteSuggestions(const std::string& buffer, int line_number, - int column) { - - std::vector res; - parsing.lock(); + int column, + std::vector *suggestions) { clang::CodeCompleteResults results(&tu_, file_path(), buffer, @@ -159,10 +161,8 @@ GetAutoCompleteSuggestions(const std::string& buffer, for (auto &stringchunk : c) { ss << stringchunk.chunk(); } - res.emplace_back(ss.str()); + suggestions->emplace_back(ss.str()); } - parsing.unlock(); - return res; } // sets the filepath for this mvc diff --git a/juci/source.h b/juci/source.h index c674110..8450807 100644 --- a/juci/source.h +++ b/juci/source.h @@ -95,10 +95,10 @@ namespace Source { const string& project_path() const; // gets the config member const Config& config() const; - std::vector - GetAutoCompleteSuggestions(const std::string& buffer, - int line_number, - int column); + void GetAutoCompleteSuggestions(const std::string& buffer, + int line_number, + int column, + std::vector *suggestions); ~Model() { } int ReParse(const std::string &buffer); std::vector ExtractTokens(int, int); @@ -125,9 +125,9 @@ namespace Source { Model& model(); void OnNewEmptyFile(); void OnOpenFile(const string &filename); - std::vector - GetAutoCompleteSuggestions(int line_number, - int column); + void GetAutoCompleteSuggestions(int line_number, + int column, + std::vector *suggestions); Glib::RefPtr buffer(); private: From 1ccca62ebe377c68c775a0f1085be496e49fe0db Mon Sep 17 00:00:00 2001 From: oyvang Date: Thu, 16 Apr 2015 12:43:11 +0200 Subject: [PATCH 05/10] fixed popup bug and merged with master --- juci/notebook.cc | 23 ++++++++++++++++++++--- juci/notebook.h | 6 ++++-- juci/window.cc | 10 +--------- juci/window.h | 1 - 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index e2b04c9..b5f437b 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -13,11 +13,12 @@ Notebook::View::View() { view_.set_position(120); } -Notebook::Controller::Controller(Keybindings::Controller& keybindings, +Notebook::Controller::Controller(Gtk::Window& window, Keybindings::Controller& keybindings, Source::Config& source_cfg, Directories::Config& dir_cfg) : source_config_(source_cfg), directories_(dir_cfg) { + window_ = &window; OnNewPage("juCi++"); refClipboard_ = Gtk::Clipboard::get(); ispopup = false; @@ -138,13 +139,21 @@ bool Notebook::Controller:: OnMouseRelease(GdkEventButton* button){ return false; } -bool Notebook::Controller::GeneratePopup(Gtk::Window* window){ +bool Notebook::Controller::OnKeyRelease(GdkEventKey* key){ + if(key->keyval==46){ + return GeneratePopup(); + } + return false; +} + +bool Notebook::Controller::GeneratePopup(){ // Get function to fill popup with suggests item vector under is for testing std::vector items; Gtk::TextIter start = CurrentTextView().get_buffer()->get_insert()->get_iter(); text_vec_.at(CurrentPage())->GetAutoCompleteSuggestions(start.get_line()+1, start.get_line_offset()+2, &items); + std::cout << items.size()<< std::endl; // Replace over with get suggestions from zalox! OVER IS JUST FOR TESTING @@ -160,7 +169,7 @@ bool Notebook::Controller::GeneratePopup(Gtk::Window* window){ for (auto &i : items) listview_.append(i); popup_scroll_.add(listview_); popup_.get_vbox()->pack_start(popup_scroll_); - popup_.set_transient_for(*window); + popup_.set_transient_for(*window_); popup_.show_all(); int popup_x = popup_.get_width(); @@ -428,6 +437,11 @@ void Notebook::Controller::TextViewHandlers(Gtk::TextView& textview) { textview.signal_button_release_event(). connect(sigc::mem_fun(*this,&Notebook::Controller::OnMouseRelease),false); + + textview.signal_key_release_event(). + connect(sigc::mem_fun(*this,&Notebook::Controller::OnKeyRelease),false); + + } void Notebook::Controller::PopupSelectHandler(Gtk::Dialog &popup, Gtk::ListViewText &listview){ @@ -447,6 +461,7 @@ void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll, int textview_y = 150; bool is_never_scroll_x = true; bool is_never_scroll_y = true; + std::cout << textview_x << std::endl; if (current_x > textview_x) { current_x = textview_x; is_never_scroll_x = false; @@ -493,9 +508,11 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, y+=textview_edge_y; if ((textview_edge_x-x)*-1 > textview.get_width()-popup_x) { x -= popup_x; + if(x textview.get_height()-popup_y) { y -= (popup_y+14) + 15; + if(x refClipboard_; bool ispopup; Gtk::Dialog popup_; + Gtk::Window* window_; }; // class controller } // namespace Notebook #endif // JUCI_NOTEBOOK_H_ diff --git a/juci/window.cc b/juci/window.cc index cf6e5c6..a922c67 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -4,7 +4,7 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), main_config_(), keybindings_(main_config_.keybindings_cfg()), - notebook_(keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()), + notebook_(*this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()), menu_(keybindings()) { set_title("juCi++"); set_default_size(600, 400); @@ -35,8 +35,6 @@ Window::Window() : OnSaveFileAs(); }); - notebook_.CurrentTextView().signal_key_release_event(). - connect(sigc::mem_fun(*this,&Window::OnKeyRelease),false); this->signal_button_release_event(). connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); terminal_.Terminal().signal_button_release_event(). @@ -169,12 +167,6 @@ void Window::OnSaveFileAs(){ } } } -bool Window::OnKeyRelease(GdkEventKey* key){ - if(key->keyval==46){ - return notebook_.GeneratePopup(this); - } - return false; -} bool Window::OnMouseRelease(GdkEventButton *button){ return notebook_.OnMouseRelease(button); } diff --git a/juci/window.h b/juci/window.h index 88297da..ca28c3b 100644 --- a/juci/window.h +++ b/juci/window.h @@ -29,7 +29,6 @@ public: void OnOpenFile(); void OnFileOpenFolder(); void OnSaveFileAs(); - bool OnKeyRelease(GdkEventKey* key); bool OnMouseRelease(GdkEventButton* button); }; From 64ce7473ed20c2adc144d64a80b6300dfce38d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 17 Apr 2015 13:01:12 +0200 Subject: [PATCH 06/10] juci working auto complete --- juci/notebook.cc | 193 +++++++++++++++++++++++++++++------------------ juci/notebook.h | 25 +++--- juci/source.cc | 16 ++-- juci/source.h | 25 +++++- 4 files changed, 166 insertions(+), 93 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index b5f437b..bf3ef2b 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -1,6 +1,5 @@ #include "notebook.h" #include -#include "clangmm.h" Notebook::Model::Model() { cc_extension_ = ".cc"; @@ -13,7 +12,8 @@ Notebook::View::View() { view_.set_position(120); } -Notebook::Controller::Controller(Gtk::Window& window, Keybindings::Controller& keybindings, +Notebook::Controller::Controller(Gtk::Window& window, + Keybindings::Controller& keybindings, Source::Config& source_cfg, Directories::Config& dir_cfg) : source_config_(source_cfg), @@ -23,8 +23,8 @@ Notebook::Controller::Controller(Gtk::Window& window, Keybindings::Controller& k refClipboard_ = Gtk::Clipboard::get(); ispopup = false; view().pack1(directories_.widget(), true, true); - CreateKeybindings(keybindings); -} // Constructor + CreateKeybindings(keybindings); + } // Constructor void Notebook::Controller::CreateKeybindings(Keybindings::Controller @@ -98,7 +98,7 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller [this]() { OnEditPaste(); }); - entry_.view_.entry().signal_activate(). + entry_.view_.entry().signal_activate(). connect( [this]() { if (is_new_file_) { @@ -131,54 +131,103 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller }); } -bool Notebook::Controller:: OnMouseRelease(GdkEventButton* button){ - if(button->button == 1 && ispopup){ - popup_.response(Gtk::RESPONSE_DELETE_EVENT); - return true; +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){ - if(key->keyval==46){ - return GeneratePopup(); - } - return false; +bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) { + return GeneratePopup(key->keyval); } -bool Notebook::Controller::GeneratePopup(){ +bool Notebook::Controller::GeneratePopup(int key_id) { // Get function to fill popup with suggests item vector under is for testing - std::vector items; - Gtk::TextIter start = CurrentTextView().get_buffer()->get_insert()->get_iter(); - text_vec_.at(CurrentPage())->GetAutoCompleteSuggestions(start.get_line()+1, - start.get_line_offset()+2, - &items); - std::cout << items.size()<< std::endl; - + 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); + 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; + } + std::vector acdata; + text_vec_.at(CurrentPage())-> + GetAutoCompleteSuggestions(beg.get_line()+1, + beg.get_line_offset()+2, + &acdata); + 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; + } + } + items[ss.str() + " --> " + return_value] = ss.str(); + } // Replace over with get suggestions from zalox! OVER IS JUST FOR TESTING - Gtk::ScrolledWindow popup_scroll_; - Gtk::ListViewText listview_(1,false,Gtk::SelectionMode::SELECTION_SINGLE); - + 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(false); + 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); - for (auto &i : items) listview_.append(i); - popup_scroll_.add(listview_); + 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(); 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); + 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); - PopupSelectHandler(popup_, listview_); + PopupSelectHandler(popup_, listview_, &items); ispopup = true; popup_.run(); popup_.hide(); @@ -232,9 +281,7 @@ void Notebook::Controller::OnOpenFile(std::string path) { unsigned pos = path.find_last_of("/\\"); Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); Notebook().show_all_children(); - std::cout << "setting current page"<< std::endl; Notebook().set_current_page(Pages()-1); - std::cout << "current page set" << std::endl; Notebook().set_focus_child(text_vec_.back()->view()); OnBufferChange(); } @@ -431,37 +478,37 @@ Gtk::Notebook& Notebook::Controller::Notebook() { void Notebook::Controller::TextViewHandlers(Gtk::TextView& textview) { textview.get_buffer()->signal_changed().connect( - [this]() { - OnBufferChange(); - }); + [this]() { + OnBufferChange(); + }); textview.signal_button_release_event(). - connect(sigc::mem_fun(*this,&Notebook::Controller::OnMouseRelease),false); + connect(sigc::mem_fun(*this, &Notebook::Controller::OnMouseRelease), false); textview.signal_key_release_event(). - connect(sigc::mem_fun(*this,&Notebook::Controller::OnKeyRelease),false); - - + connect(sigc::mem_fun(*this, &Notebook::Controller::OnKeyRelease), false); } + void Notebook::Controller::PopupSelectHandler(Gtk::Dialog &popup, - Gtk::ListViewText &listview){ + Gtk::ListViewText &listview, + std::map + *items) { listview.signal_row_activated(). - connect([this, &listview, &popup](const Gtk::TreeModel::Path& path, - Gtk::TreeViewColumn*) { - std::string selected = - listview.get_text(listview.get_selected()[0]); - CurrentTextView().get_buffer()->insert_at_cursor(selected); - popup.response(Gtk::RESPONSE_DELETE_EVENT); - }); + 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) { + int ¤t_x, + int ¤t_y) { int textview_x = CurrentTextView().get_width(); int textview_y = 150; bool is_never_scroll_x = true; bool is_never_scroll_y = true; - std::cout << textview_x << std::endl; if (current_x > textview_x) { current_x = textview_x; is_never_scroll_x = false; @@ -470,36 +517,36 @@ void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll, current_y = textview_y; is_never_scroll_y = false; } - scroll.set_size_request(current_x,current_y); + 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); + 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); + 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); + Gtk::PolicyType::POLICY_AUTOMATIC); } } void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, - int popup_x, - int popup_y, - int &x, - int &y){ + int popup_x, + int popup_y, + int &x, + int &y) { Gdk::Rectangle temp1, temp2; textview.get_cursor_locations( - CurrentTextView(). - get_buffer()->get_insert()-> - get_iter(), temp1,temp2); + 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); + 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); @@ -508,17 +555,17 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, y+=textview_edge_y; if ((textview_edge_x-x)*-1 > textview.get_width()-popup_x) { x -= popup_x; - if(x textview.get_height()-popup_y) { y -= (popup_y+14) + 15; - if(xget_text(); file.close(); } diff --git a/juci/notebook.h b/juci/notebook.h index 5eac6b0..ccab51b 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -8,6 +8,7 @@ #include "directories.h" #include #include +#include #include namespace Notebook { @@ -57,27 +58,31 @@ namespace Notebook { void OnCreatePage(); bool ScrollEventCallback(GdkEventScroll* scroll_event); int Pages(); - Directories::Controller& directories() { return directories_; } + Directories::Controller& directories() { return directories_; } Gtk::Paned& view(); - bool GeneratePopup(); + bool GeneratePopup(int key); void Search(bool forward); const Source::Config& source_config() { return source_config_; } bool OnMouseRelease(GdkEventButton* button); bool OnKeyRelease(GdkEventKey* key); + protected: void TextViewHandlers(Gtk::TextView& textview); void PopupSelectHandler(Gtk::Dialog &popup, - Gtk::ListViewText &listview); - private: + 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); + int popup_x, + int popup_y, + int &x, + int &y); void PopupSetSize(Gtk::ScrolledWindow& scroll, - int ¤t_x, - int ¤t_y); + int ¤t_x, + int ¤t_y); Glib::RefPtr m_refBuilder; Glib::RefPtr refActionGroup; Source::Config source_config_; diff --git a/juci/source.cc b/juci/source.cc index baec800..5210541 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -135,7 +135,8 @@ void Source::Controller::OnLineEdit() { } void Source::Controller:: GetAutoCompleteSuggestions(int line_number, int column, - std::vector *suggestions) { + std::vector + *suggestions) { parsing.lock(); model().GetAutoCompleteSuggestions(view().get_buffer() ->get_text().raw(), @@ -149,19 +150,20 @@ void Source::Model:: GetAutoCompleteSuggestions(const std::string& buffer, int line_number, int column, - std::vector *suggestions) { + std::vector + *suggestions) { clang::CodeCompleteResults results(&tu_, file_path(), buffer, line_number, column); for (int i = 0; i < results.size(); i++) { - std::stringstream ss; - const vector c = results.get(i).get_chunks(); - for (auto &stringchunk : c) { - ss << stringchunk.chunk(); + const vector chunks_ = results.get(i).get_chunks(); + std::vector chunks; + for (auto &chunk : chunks_) { + chunks.emplace_back(chunk); } - suggestions->emplace_back(ss.str()); + suggestions->emplace_back(chunks); } } diff --git a/juci/source.h b/juci/source.h index 8450807..cf8789a 100644 --- a/juci/source.h +++ b/juci/source.h @@ -74,6 +74,24 @@ namespace Source { string GetLine(const Gtk::TextIter &begin); }; // class View + class AutoCompleteChunk { + public: + explicit AutoCompleteChunk(const clang::CompletionChunk &chunk) : + chunk_(chunk.chunk()), kind_(chunk.kind()) { } + const std::string& chunk() const { return chunk_; } + const clang::CompletionChunkKind& kind() const { return kind_; } + private: + std::string chunk_; + enum clang::CompletionChunkKind kind_; + }; + + class AutoCompleteData { + public: + explicit AutoCompleteData(const std::vector &chunks) : + chunks_(chunks) { } + std::vector chunks_; + }; + class Model{ public: // constructor for Source::Model @@ -98,7 +116,8 @@ namespace Source { void GetAutoCompleteSuggestions(const std::string& buffer, int line_number, int column, - std::vector *suggestions); + std::vector + *suggestions); ~Model() { } int ReParse(const std::string &buffer); std::vector ExtractTokens(int, int); @@ -113,7 +132,6 @@ namespace Source { int token_kind); void HighlightCursor(clang::Token *token, std::vector *source_ranges); - std::vector get_compilation_commands(); }; @@ -127,7 +145,8 @@ namespace Source { void OnOpenFile(const string &filename); void GetAutoCompleteSuggestions(int line_number, int column, - std::vector *suggestions); + std::vector + *suggestions); Glib::RefPtr buffer(); private: From 59925ea19c2b9b0f3f2b53d6650ac9df39f500a5 Mon Sep 17 00:00:00 2001 From: oyvang Date: Fri, 17 Apr 2015 15:17:09 +0200 Subject: [PATCH 07/10] Working console for linux, missing merge with forgie --- juci/config.json | 4 +- juci/menu.cc | 2 + juci/menu.xml | 6 ++- juci/notebook.cc | 63 +++++++++++++------------ juci/terminal.cc | 120 ++++++++++++++++++++++++++++------------------- juci/terminal.h | 19 +++++--- juci/window.cc | 25 +++++++++- juci/window.h | 3 +- 8 files changed, 153 insertions(+), 89 deletions(-) diff --git a/juci/config.json b/juci/config.json index e4ab5e7..03cbad8 100644 --- a/juci/config.json +++ b/juci/config.json @@ -24,7 +24,9 @@ "new_cc_file": "c", "close_tab": "w", "open_folder": "o", - "save_as": "s" + "save_as": "s", + "compile_and_run": "r>", + "compile": "r" }, "directoryfilter": { "ignore": [ diff --git a/juci/menu.cc b/juci/menu.cc index 880d7df..4fba192 100644 --- a/juci/menu.cc +++ b/juci/menu.cc @@ -27,6 +27,8 @@ Menu::Controller::Controller(Keybindings::Controller& keybindings) : [this]() { OnWindowSplitWindow(); }); + keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectMenu", + "P_roject")); keybindings_.action_group_menu()->add(Gtk::Action::create("PluginMenu", "_Plugins")); keybindings_.action_group_menu()->add(Gtk::Action::create("HelpMenu", diff --git a/juci/menu.xml b/juci/menu.xml index e5ad384..7007147 100644 --- a/juci/menu.xml +++ b/juci/menu.xml @@ -18,7 +18,11 @@ - + + + + + diff --git a/juci/notebook.cc b/juci/notebook.cc index b5f437b..a2d0d93 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -151,39 +151,42 @@ bool Notebook::Controller::GeneratePopup(){ std::vector items; Gtk::TextIter start = CurrentTextView().get_buffer()->get_insert()->get_iter(); text_vec_.at(CurrentPage())->GetAutoCompleteSuggestions(start.get_line()+1, - start.get_line_offset()+2, - &items); + start.get_line_offset()+2, + &items); std::cout << items.size()<< std::endl; + if(items.size()>0){ - // Replace over with get suggestions from zalox! OVER IS JUST FOR TESTING + // Replace over with get suggestions from zalox! OVER IS JUST FOR TESTING - 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(false); - listview_.set_headers_visible(false); - listview_.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); - listview_.set_activate_on_single_click(true); - for (auto &i : items) listview_.append(i); - popup_scroll_.add(listview_); - popup_.get_vbox()->pack_start(popup_scroll_); - popup_.set_transient_for(*window_); - popup_.show_all(); - - 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); - PopupSelectHandler(popup_, listview_); - ispopup = true; - popup_.run(); - popup_.hide(); - ispopup = false; - return true; + 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(false); + listview_.set_headers_visible(false); + listview_.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); + listview_.set_activate_on_single_click(true); + for (auto &i : items) listview_.append(i); + popup_scroll_.add(listview_); + popup_.get_vbox()->pack_start(popup_scroll_); + popup_.set_transient_for(*window_); + popup_.show_all(); + + 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); + PopupSelectHandler(popup_, listview_); + ispopup = true; + popup_.run(); + popup_.hide(); + ispopup = false; + return true; + } + return false; } bool Notebook::Controller::ScrollEventCallback(GdkEventScroll* scroll_event) { diff --git a/juci/terminal.cc b/juci/terminal.cc index d81ff11..e512af0 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -1,70 +1,94 @@ #include "terminal.h" #include +#include Terminal::View::View(){ scrolledwindow_.add(textview_); scrolledwindow_.set_size_request(-1,150); view_.add(scrolledwindow_); + textview_.set_editable(false); + //Pango::TabArray tabsize; + //tabsize.set_tab(200,Pango::TAB_LEFT, 200); + //textview_.set_tabs(tabsize); } Terminal::Controller::Controller() { - root = ""; - Terminal().signal_key_release_event(). - connect(sigc::mem_fun(*this,&Terminal::Controller::OnButtonRealeaseEvenet),false); + folder_command_ = ""; } -bool Terminal::Controller::OnButtonRealeaseEvenet(GdkEventKey *key) { - if(key->keyval == 65421 || key->keyval == 65293){ - ExecuteCommand(); - } - return false; +void Terminal::Controller::SetFolderCommand(std::string path) { + int pos = path.find_last_of("/\\"); + path.erase(path.begin()+pos,path.end()); + folder_command_ = "cd "+ path + "; "; } -void Terminal::Controller::ExecuteCommand() { - - std::cout << "EXECUTE COMMAND ALGORITHM "<< std::endl; - std::string temp = getCommand(); - if(temp != ""){ - std::cout << "EXECUTE COMMAND: "<insert(Terminal().get_buffer()->end(),"Command Failed\n"); - } else{ - char buffer[1028]; - - while (fgets(buffer, 1028, p) != NULL) - { - Terminal().get_buffer()->insert(Terminal().get_buffer()->end(),buffer); - } - pclose(p); +void Terminal::Controller::CompileAndRun(std::string project_name) { + if (folder_command_=="") { + PrintMessage("juCi++ ERROR: Can not find project's CMakeList.txt\n"); + } else { + if (running.try_lock()) { + std::thread execute([=]() { + Terminal().get_buffer()->set_text(""); + ExecuteCommand("cmake ."); + if (ExistInConsole(cmake_sucsess)){ + ExecuteCommand("make"); + if (ExistInConsole(make_built)){ + if (FindExecutable(project_name)) { + ExecuteCommand("./"+project_name); + } else { + PrintMessage("juCi++ ERROR: Can not find Executable\n"); + } + } + } + }); + execute.detach(); + running.unlock(); + } + } +} - - } - Terminal().get_buffer()->insert(Terminal().get_buffer()->end(),"Command Executed\n"); - }else{ - std::cout << "NO COMMAND TO RUN"<< std::endl; - } - +void Terminal::Controller::PrintMessage(std::string message){ + Terminal().get_buffer()-> + insert(Terminal().get_buffer()-> end(),"> "+message); } -std::string Terminal::Controller::getCommand(){ - std::string command = ""; - Gtk::TextIter start,end; - int a = Terminal().get_buffer()->get_insert()->get_iter().get_line()-1; - if(a==-1)a=0; - start = Terminal().get_buffer()->get_iter_at_line(a); - end =Terminal().get_buffer()->get_iter_at_line(a); - while(!end.ends_line()) { - end++; +bool Terminal::Controller::FindExecutable(std::string executable) { + std::string build = Terminal().get_buffer()->get_text(); + double pos = build.find(make_built); + Gtk::TextIter start = Terminal().get_buffer()->get_iter_at_offset(pos); + Gtk::TextIter end = Terminal().get_buffer()->get_iter_at_offset(pos); + while (!end.ends_line()) { + end.forward_char(); } - while(!start.starts_line()) { - start--; - } - - command = Terminal().get_buffer()->get_text(start,end); - return command; + build = Terminal().get_buffer()->get_text(start, end); + pos = build.find_last_of(" "); + std::cout << "FINNER NY POS" << std::endl; + build = build.substr(pos+1); + std::cout <<"BUILD TARGET = "<< build << std::endl; + std::cout << "EXECUTABLE FILE = "<< executable << std::endl; + if(build != executable) return false; + return true; } +bool Terminal::Controller::ExistInConsole(std::string string) { + double pos = Terminal().get_buffer()-> + get_text().find(string); + if (pos == std::string::npos) return false; + return true; +} +void Terminal::Controller::ExecuteCommand(std::string command) { + command = folder_command_+command; + std::cout << "EXECUTE COMMAND: "<< command << std::endl; + FILE* p = popen(command.c_str(), "r"); + if (p == NULL) { + PrintMessage("juCi++ ERROR: Failed to run command" + command + "\n"); + }else { + char buffer[1028]; + while (fgets(buffer, 1028, p) != NULL) { + PrintMessage(buffer); + } + pclose(p); + } +} diff --git a/juci/terminal.h b/juci/terminal.h index d91876c..fe890fc 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -1,6 +1,7 @@ #ifndef JUCI_TERMINAL_H_ #define JUCI_TERMINAL_H_ +#include #include "gtkmm.h" namespace Terminal { @@ -21,14 +22,20 @@ namespace Terminal { Controller(); Gtk::HBox& view() {return view_.view();} Gtk::TextView& Terminal(){return view_.textview();} + void SetFolderCommand(std::string path); + void CompileAndRun(std::string project_name); private: - void ExecuteCommand(); - std::string getCommand(); - bool OnButtonRealeaseEvenet(GdkEventKey* key); + void ExecuteCommand(std::string command); + bool OnButtonRealeaseEvent(GdkEventKey* key); + bool ExistInConsole(std::string string); + bool FindExecutable(std::string executable); + void PrintMessage(std::string message); Terminal::View view_; - std::string root; - - + std::string folder_command_; + std::mutex running; + const std::string cmake_sucsess = "Build files have been written to:"; + const std::string make_built = "Built target"; + const std::string make_executable = "Linking CXX executable"; }; // class controller } // namespace Terminal diff --git a/juci/window.cc b/juci/window.cc index a922c67..61033ae 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -5,7 +5,7 @@ Window::Window() : main_config_(), keybindings_(main_config_.keybindings_cfg()), notebook_(*this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()), - menu_(keybindings()) { + menu_(keybindings()) { set_title("juCi++"); set_default_size(600, 400); add(window_box_); @@ -34,6 +34,28 @@ Window::Window() : [this]() { OnSaveFileAs(); }); + + keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompileAndRun", + "Compile And Run"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["compile_and_run"]), + [this]() { + terminal_. + SetFolderCommand("/home/gm/ClionProjects/testi/CM.txt"); + std::string p = notebook_.directories().get_project_name("/home/gm/ClionProjects/testi"); + terminal_.CompileAndRun(p); + }); + + keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompile", + "Compile"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["compile"]), + [this]() { + terminal_. + SetFolderCommand("/home/gm/ClionProjects/testi/CM.txt"); + std::string p = notebook_.directories().get_project_name("/home/gm/ClionProjects/testi"); + terminal_.CompileAndRun(p); + }); this->signal_button_release_event(). connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); @@ -77,6 +99,7 @@ void Window::OnFileOpenFolder() { std::cout << "Folder selected: " << dialog.get_filename() << std::endl; notebook_.directories().open_folder(dialog.get_filename()); + std::cout << dialog.get_filename()<< std::endl; break; } case(Gtk::RESPONSE_CANCEL): diff --git a/juci/window.h b/juci/window.h index ca28c3b..954072e 100644 --- a/juci/window.h +++ b/juci/window.h @@ -29,8 +29,7 @@ public: void OnOpenFile(); void OnFileOpenFolder(); void OnSaveFileAs(); - bool OnMouseRelease(GdkEventButton* button); - + bool OnMouseRelease(GdkEventButton* button); }; #endif // JUCI_WINDOW_H From 5a41847518200383cf843b8cca1af827e9f20dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sun, 19 Apr 2015 19:28:35 +0200 Subject: [PATCH 08/10] Moves the clang index to notebook for better parsing between edited files --- juci/api.h | 4 +-- juci/config.cc | 4 +-- juci/notebook.cc | 22 +++++++++----- juci/notebook.h | 4 +++ juci/source.cc | 46 ++++++++++++++++------------ juci/source.h | 79 ++++++++++++++++++++++++------------------------ 6 files changed, 89 insertions(+), 70 deletions(-) diff --git a/juci/api.h b/juci/api.h index eb17fc5..46f3f98 100644 --- a/juci/api.h +++ b/juci/api.h @@ -55,9 +55,9 @@ namespace libjuci { const std::string plugin_path, const std::string menu_keybinding); void AddMenuXml(const std::string plugin_name, - const string parent_menu); + const std::string parent_menu); void AddSubMenuXml(const std::string plugin_name, - const string parent_menu); + const std::string parent_menu); ////////////////////////////// //// Boost.Python methods //// ////////////////////////////// diff --git a/juci/config.cc b/juci/config.cc index 3e8a264..689fd19 100644 --- a/juci/config.cc +++ b/juci/config.cc @@ -17,16 +17,14 @@ void MainConfig::GenerateSource() { boost::property_tree::ptree colors_json = source_json.get_child("colors"); for ( auto &i : colors_json ) { source_cfg_.InsertTag(i.first, i.second.get_value()); - std::cout << "inserting tag, key: " << i.first << " value: " << i.second.get_value() << std::endl; } for ( auto &i : syntax_json ) { source_cfg_.InsertType(i.first, i.second.get_value()); - std::cout << "inserting type, key: " << i.first << " value: " << i.second.get_value() << std::endl; } } void MainConfig::GenerateKeybindings() { - string line; + std::string line; std::ifstream menu_xml("menu.xml"); if (menu_xml.is_open()) { while (getline(menu_xml, line)) { diff --git a/juci/notebook.cc b/juci/notebook.cc index bf3ef2b..c918d65 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -7,7 +7,7 @@ Notebook::Model::Model() { scrollvalue_ = 50; } -Notebook::View::View() { +Notebook::View::View() : notebook_() { view_.pack2(notebook_); view_.set_position(120); } @@ -17,7 +17,8 @@ Notebook::Controller::Controller(Gtk::Window& window, Source::Config& source_cfg, Directories::Config& dir_cfg) : source_config_(source_cfg), - directories_(dir_cfg) { + directories_(dir_cfg), + index_(0, 1) { window_ = &window; OnNewPage("juCi++"); refClipboard_ = Gtk::Clipboard::get(); @@ -26,7 +27,6 @@ Notebook::Controller::Controller(Gtk::Window& window, CreateKeybindings(keybindings); } // Constructor - void Notebook::Controller::CreateKeybindings(Keybindings::Controller &keybindings) { directories().m_TreeView.signal_row_activated() @@ -275,6 +275,14 @@ void Notebook::Controller::OnNewPage(std::string name) { Notebook().set_focus_child(text_vec_.at(Pages()-1)->view()); } +void Notebook::Controller:: +MapBuffers(std::map *buffers) { + for (auto &buffer : text_vec_) { + buffers->operator[](buffer->model().file_path()) = + buffer->buffer()->get_text().raw(); + } +} + void Notebook::Controller::OnOpenFile(std::string path) { OnCreatePage(); text_vec_.back()->OnOpenFile(path); @@ -287,8 +295,8 @@ void Notebook::Controller::OnOpenFile(std::string path) { } void Notebook::Controller::OnCreatePage() { - text_vec_.push_back(new Source::Controller(source_config())); - linenumbers_vec_.push_back(new Source::Controller(source_config())); + text_vec_.push_back(new Source::Controller(source_config(), this)); + linenumbers_vec_.push_back(new Source::Controller(source_config(), this)); scrolledline_vec_.push_back(new Gtk::ScrolledWindow()); scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); editor_vec_.push_back(new Gtk::HBox()); @@ -551,8 +559,8 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, 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; + 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; diff --git a/juci/notebook.h b/juci/notebook.h index ccab51b..1d2f0aa 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -10,6 +10,7 @@ #include #include #include +#include "clangmm.h" namespace Notebook { class Model { @@ -57,6 +58,8 @@ namespace Notebook { void OnOpenFile(std::string filename); void OnCreatePage(); bool ScrollEventCallback(GdkEventScroll* scroll_event); + void MapBuffers(std::map *buffers); + clang::Index* index() { return &index_; } int Pages(); Directories::Controller& directories() { return directories_; } Gtk::Paned& view(); @@ -101,6 +104,7 @@ namespace Notebook { bool ispopup; Gtk::Dialog popup_; Gtk::Window* window_; + clang::Index index_; }; // class controller } // namespace Notebook #endif // JUCI_NOTEBOOK_H_ diff --git a/juci/source.cc b/juci/source.cc index 5210541..b8d8950 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -4,7 +4,7 @@ #include #include #include - +#include "notebook.h" #define log( var ) \ std::cout << "source.cc (" << __LINE__ << ") " << #var << std::endl @@ -102,16 +102,18 @@ Source::Model::Model(const Source::Config &config) : void Source::Model:: InitSyntaxHighlighting(const std::string &filepath, const std::string &project_path, - const std::string &text, + const std::map + &buffers, int start_offset, - int end_offset) { + int end_offset, + clang::Index *index) { set_file_path(filepath); set_project_path(project_path); std::vector arguments = get_compilation_commands(); - tu_ = clang::TranslationUnit(true, + tu_ = clang::TranslationUnit(index, filepath, arguments, - text); + buffers); } // Source::View::UpdateLine @@ -123,7 +125,7 @@ OnLineEdit(const std::vector &locations, // Source::Model::UpdateLine int Source::Model:: -ReParse(const std::string &buffer) { +ReParse(const std::map &buffer) { return tu_.ReparseTranslationUnit(file_path(), buffer); } @@ -138,8 +140,9 @@ GetAutoCompleteSuggestions(int line_number, std::vector *suggestions) { parsing.lock(); - model().GetAutoCompleteSuggestions(view().get_buffer() - ->get_text().raw(), + std::map buffers; + notebook_->MapBuffers(&buffers); + model().GetAutoCompleteSuggestions(buffers, line_number, column, suggestions); @@ -147,14 +150,14 @@ GetAutoCompleteSuggestions(int line_number, } void Source::Model:: -GetAutoCompleteSuggestions(const std::string& buffer, +GetAutoCompleteSuggestions(const std::map &buffers, int line_number, int column, std::vector *suggestions) { clang::CodeCompleteResults results(&tu_, file_path(), - buffer, + buffers, line_number, column); for (int i = 0; i < results.size(); i++) { @@ -263,9 +266,9 @@ HighlightToken(clang::Token *token, // Source::Controller::Controller() // Constructor for Controller -Source::Controller::Controller(const Source::Config &config) : - model_(config) { -} +Source::Controller::Controller(const Source::Config &config, + Notebook::Controller *notebook) : + model_(config), notebook_(notebook) { } // Source::Controller::view() // return shared_ptr to the view @@ -313,7 +316,7 @@ void Source::View::OnUpdateSyntax(const std::vector &ranges, Glib::RefPtr buffer = get_buffer(); buffer->remove_all_tags(buffer->begin(), buffer->end()); for (auto &range : ranges) { - string type = std::to_string(range.kind()); + std::string type = std::to_string(range.kind()); try { config.typetable().at(type); } catch (std::exception) { @@ -337,17 +340,20 @@ void Source::View::OnUpdateSyntax(const std::vector &ranges, void Source::Controller::OnOpenFile(const string &filepath) { sourcefile s(filepath); + std::map buffers; + notebook_->MapBuffers(&buffers); + buffers[filepath] = s.get_content(); buffer()->set_text(s.get_content()); int start_offset = buffer()->begin().get_offset(); int end_offset = buffer()->end().get_offset(); - if (check_extention(filepath)) { view().ApplyConfig(model().config()); model().InitSyntaxHighlighting(filepath, extract_file_path(filepath), - buffer()->get_text().raw(), + buffers, start_offset, - end_offset); + end_offset, + notebook_->index()); view().OnUpdateSyntax(model().ExtractTokens(start_offset, end_offset), model().config()); } @@ -358,7 +364,10 @@ void Source::Controller::OnOpenFile(const string &filepath) { if (parsing.try_lock()) { while (true) { const std::string raw = buffer()->get_text().raw(); - if (model().ReParse(raw) == 0 && + std::map buffers; + notebook_->MapBuffers(&buffers); + buffers[model().file_path()] = raw; + if (model().ReParse(buffers) == 0 && raw == buffer()->get_text().raw()) { syntax.lock(); go = true; @@ -373,7 +382,6 @@ void Source::Controller::OnOpenFile(const string &filepath) { } }); - buffer()->signal_begin_user_action().connect([this]() { if (go) { syntax.lock(); diff --git a/juci/source.h b/juci/source.h index cf8789a..3f03c58 100644 --- a/juci/source.h +++ b/juci/source.h @@ -3,31 +3,34 @@ #include #include #include -#include #include "gtkmm.h" #include "clangmm.h" #include #include +#include -using std::string; +namespace Notebook { + class Controller; +} namespace Source { - class Config { public: Config(const Config &original); Config(); - const std::unordered_map& tagtable() const; - const std::unordered_map& typetable() const; - void SetTagTable(const std::unordered_map &tagtable); - void InsertTag(const string &key, const string &value); - void SetTypeTable(const std::unordered_map &tagtable); - void InsertType(const string &key, const string &value); + const std::unordered_map& tagtable() const; + const std::unordered_map& typetable() const; + void SetTagTable(const std::unordered_map + &tagtable); + void InsertTag(const std::string &key, const std::string &value); + void SetTypeTable(const std::unordered_map + &tagtable); + void InsertType(const std::string &key, const std::string &value); private: - std::unordered_map tagtable_; - std::unordered_map typetable_; - string background_; + std::unordered_map tagtable_; + std::unordered_map typetable_; + std::string background_; }; // class Config class Location { @@ -48,13 +51,6 @@ namespace Source { const Location& start() const { return start_; } const Location& end() const { return end_; } int kind() const { return kind_; } - void to_stream() const { - std::cout << "range: [" << start_.line_number()-1; - std::cout << ", " << end_.line_number()-1 << "] "; - std::cout << "<" << start_.column_offset()-1; - std::cout << ", " << end_.column_offset()-1 << ">"; - std::cout << std::endl; - } private: Location start_; Location end_; @@ -71,7 +67,7 @@ namespace Source { const Config &config); private: - string GetLine(const Gtk::TextIter &begin); + std::string GetLine(const Gtk::TextIter &begin); }; // class View class AutoCompleteChunk { @@ -98,34 +94,37 @@ namespace Source { explicit Model(const Source::Config &config); // inits the syntax highligthing on file open void InitSyntaxHighlighting(const std::string &filepath, - const std::string &project_path, - const std::string &text, - int start_offset, - int end_offset); + const std::string &project_path, + const std::map + &buffers, + int start_offset, + int end_offset, + clang::Index *index); // sets the filepath for this mvc - void set_file_path(const string &file_path); + void set_file_path(const std::string &file_path); // sets the project path for this mvc - void set_project_path(const string &project_path); + void set_project_path(const std::string &project_path); // gets the file_path member - const string& file_path() const; + const std::string& file_path() const; // gets the project_path member - const string& project_path() const; + const std::string& project_path() const; // gets the config member const Config& config() const; - void GetAutoCompleteSuggestions(const std::string& buffer, - int line_number, - int column, - std::vector - *suggestions); - ~Model() { } - int ReParse(const std::string &buffer); + void GetAutoCompleteSuggestions(const std::map + &buffers, + int line_number, + int column, + std::vector + *suggestions); + ~Model() { } + int ReParse(const std::map &buffers); std::vector ExtractTokens(int, int); private: Config config_; - string file_path_; - string project_path_; + std::string file_path_; + std::string project_path_; clang::TranslationUnit tu_; void HighlightToken(clang::Token *token, std::vector *source_ranges, @@ -137,12 +136,13 @@ namespace Source { class Controller { public: - explicit Controller(const Source::Config &config); + Controller(const Source::Config &config, + Notebook::Controller *notebook); Controller(); View& view(); Model& model(); void OnNewEmptyFile(); - void OnOpenFile(const string &filename); + void OnOpenFile(const std::string &filename); void GetAutoCompleteSuggestions(int line_number, int column, std::vector @@ -159,6 +159,7 @@ namespace Source { protected: View view_; Model model_; + Notebook::Controller *notebook_; }; // class Controller } // namespace Source #endif // JUCI_SOURCE_H_ From aac51b73ee9f7a9a885dca363b734e5cfd279963 Mon Sep 17 00:00:00 2001 From: oyvang Date: Mon, 20 Apr 2015 08:45:39 +0200 Subject: [PATCH 09/10] Fixed save and save as --- juci/config.json | 1 + juci/menu.xml | 1 + juci/notebook.cc | 65 ++++++++++++++++++++++++++++++++++++++++++------ juci/notebook.h | 5 ++-- juci/source.cc | 2 ++ juci/source.h | 7 ++++++ juci/window.cc | 37 ++++++++------------------- juci/window.h | 3 ++- 8 files changed, 83 insertions(+), 38 deletions(-) diff --git a/juci/config.json b/juci/config.json index 03cbad8..3c68b32 100644 --- a/juci/config.json +++ b/juci/config.json @@ -24,6 +24,7 @@ "new_cc_file": "c", "close_tab": "w", "open_folder": "o", + "save": "s", "save_as": "s", "compile_and_run": "r>", "compile": "r" diff --git a/juci/menu.xml b/juci/menu.xml index 7007147..acb0ed2 100644 --- a/juci/menu.xml +++ b/juci/menu.xml @@ -8,6 +8,7 @@ + diff --git a/juci/notebook.cc b/juci/notebook.cc index a2d0d93..766c9d8 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -13,17 +13,24 @@ Notebook::View::View() { view_.set_position(120); } -Notebook::Controller::Controller(Gtk::Window& window, Keybindings::Controller& keybindings, +Notebook::Controller::Controller(Gtk::Window* window, Keybindings::Controller& keybindings, Source::Config& source_cfg, Directories::Config& dir_cfg) : source_config_(source_cfg), directories_(dir_cfg) { - window_ = &window; + std::cout << "NOTEBOOK CONTROLLER"<< std::endl; + window_ = window; + std::cout << "1"<< std::endl; OnNewPage("juCi++"); + std::cout << "2"<< std::endl; refClipboard_ = Gtk::Clipboard::get(); + std::cout << "3"<< std::endl; ispopup = false; + std::cout << "4"<< std::endl; view().pack1(directories_.widget(), true, true); - CreateKeybindings(keybindings); + std::cout << "5"<< std::endl; + CreateKeybindings(keybindings); + std::cout << "FERDIG"<< std::endl; } // Constructor @@ -232,6 +239,7 @@ void Notebook::Controller::OnNewPage(std::string name) { void Notebook::Controller::OnOpenFile(std::string path) { OnCreatePage(); text_vec_.back()->OnOpenFile(path); + text_vec_.back()->set_is_saved(true); unsigned pos = path.find_last_of("/\\"); Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); Notebook().show_all_children(); @@ -519,9 +527,50 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, } } -void Notebook::Controller:: OnSaveFile(std::string path){ - std::ofstream file; - file.open (path); - file << CurrentTextView().get_buffer()->get_text(); - file.close(); +void Notebook::Controller:: OnSaveFile() { + if (text_vec_.at(CurrentPage())->is_saved()) { + std::ofstream file; + file.open (text_vec_.at(CurrentPage())->path()); + file << CurrentTextView().get_buffer()->get_text(); + file.close(); + } else { + std::string path = OnSaveFileAs(); + if (path != "") { + std::ofstream file; + file.open (path); + file << CurrentTextView().get_buffer()->get_text(); + file.close(); + text_vec_.at(CurrentPage())->set_path(path); + text_vec_.at(CurrentPage())->set_is_saved(true); + } + } +} + + +std::string Notebook::Controller::OnSaveFileAs(){ + Gtk::FileChooserDialog dialog("Please choose a file", + Gtk::FILE_CHOOSER_ACTION_SAVE); + 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); + int result = dialog.run(); + switch (result) { + case(Gtk::RESPONSE_OK): { + std::string path = dialog.get_filename(); + unsigned pos = path.find_last_of("/\\"); + std::cout << path<< std::endl; + //notebook_.OnSaveFile(path); + return path; + break; + } + case(Gtk::RESPONSE_CANCEL): { + break; + } + default: { + std::cout << "Unexpected button clicked." << std::endl; + break; + } + } + return ""; } diff --git a/juci/notebook.h b/juci/notebook.h index 5eac6b0..3022aae 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -29,7 +29,7 @@ namespace Notebook { }; class Controller { public: - Controller(Gtk::Window& window, Keybindings::Controller& keybindings, + Controller(Gtk::Window* window, Keybindings::Controller& keybindings, Source::Config& config, Directories::Config& dir_cfg); ~Controller(); @@ -49,7 +49,7 @@ namespace Notebook { void OnFileNewEmptyfile(); void OnFileNewHeaderFile(); void OnFileOpenFolder(); - void OnSaveFile(std::string path); + void OnSaveFile(); void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); void OnNewPage(std::string name); @@ -64,6 +64,7 @@ namespace Notebook { const Source::Config& source_config() { return source_config_; } bool OnMouseRelease(GdkEventButton* button); bool OnKeyRelease(GdkEventKey* key); + std::string OnSaveFileAs(); protected: void TextViewHandlers(Gtk::TextView& textview); void PopupSelectHandler(Gtk::Dialog &popup, diff --git a/juci/source.cc b/juci/source.cc index baec800..32b84f1 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -282,6 +282,7 @@ void Source::Controller::OnNewEmptyFile() { model().set_file_path(filename); model().set_project_path(filename); s.save(""); + //OnOpenFile(filename); //OYVANG ADDED; REMOVE IF WRONG } string extract_file_path(const std::string &file_path) { @@ -334,6 +335,7 @@ void Source::View::OnUpdateSyntax(const std::vector &ranges, } void Source::Controller::OnOpenFile(const string &filepath) { + path_=filepath; sourcefile s(filepath); buffer()->set_text(s.get_content()); int start_offset = buffer()->begin().get_offset(); diff --git a/juci/source.h b/juci/source.h index 8450807..6975bbd 100644 --- a/juci/source.h +++ b/juci/source.h @@ -129,13 +129,20 @@ namespace Source { int column, std::vector *suggestions); Glib::RefPtr buffer(); + bool is_saved(){return is_saved_;}; + std::string path(){return path_;}; + void set_is_saved(bool isSaved){is_saved_ = isSaved;}; + void set_path(std::string path){path_ = path;}; + private: void OnLineEdit(); void OnSaveFile(); std::mutex syntax; std::mutex parsing; + std::string path_; bool go = false; + bool is_saved_ = false; protected: View view_; diff --git a/juci/window.cc b/juci/window.cc index 61033ae..5177995 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -4,7 +4,7 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), main_config_(), keybindings_(main_config_.keybindings_cfg()), - notebook_(*this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()), + notebook_(this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()), menu_(keybindings()) { set_title("juCi++"); set_default_size(600, 400); @@ -32,7 +32,15 @@ Window::Window() : Gtk::AccelKey(keybindings_.config_ .key_map()["save_as"]), [this]() { - OnSaveFileAs(); + notebook_.OnSaveFile(); + }); + + keybindings_.action_group_menu()->add(Gtk::Action::create("FileSave", + "Save"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["save"]), + [this]() { + notebook_.OnSaveFile(); }); keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompileAndRun", @@ -165,31 +173,6 @@ void Window::OnOpenFile() { } } } -void Window::OnSaveFileAs(){ - Gtk::FileChooserDialog dialog("Please choose a file", - Gtk::FILE_CHOOSER_ACTION_SAVE); - dialog.set_transient_for(*this); - dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS); - dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL); - dialog.add_button("_Save", Gtk::RESPONSE_OK); - int result = dialog.run(); - switch (result) { - case(Gtk::RESPONSE_OK): { - std::string path = dialog.get_filename(); - unsigned pos = path.find_last_of("/\\"); - std::cout << path<< std::endl; - notebook_.OnSaveFile(path); - break; - } - case(Gtk::RESPONSE_CANCEL): { - break; - } - default: { - std::cout << "Unexpected button clicked." << std::endl; - break; - } - } -} bool Window::OnMouseRelease(GdkEventButton *button){ return notebook_.OnMouseRelease(button); } diff --git a/juci/window.h b/juci/window.h index 954072e..3e968dc 100644 --- a/juci/window.h +++ b/juci/window.h @@ -11,6 +11,7 @@ class Window : public Gtk::Window { public: Window(); MainConfig& main_config() { return main_config_; } + // std::string OnSaveFileAs(); Gtk::Box window_box_; @@ -28,7 +29,7 @@ public: void OnWindowHide(); void OnOpenFile(); void OnFileOpenFolder(); - void OnSaveFileAs(); + bool OnMouseRelease(GdkEventButton* button); }; From 10c3ece12c26383dc44833d9493db6ba977b6f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 20 Apr 2015 08:47:06 +0200 Subject: [PATCH 10/10] add virtual destructor --- juci/source.h | 1 + juci/window.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/juci/source.h b/juci/source.h index 3f03c58..d4211ff 100644 --- a/juci/source.h +++ b/juci/source.h @@ -60,6 +60,7 @@ namespace Source { class View : public Gtk::TextView { public: View(); + virtual ~View() { } void ApplyConfig(const Config &config); void OnLineEdit(const std::vector &locations, const Config &config); diff --git a/juci/window.h b/juci/window.h index ca28c3b..9362a46 100644 --- a/juci/window.h +++ b/juci/window.h @@ -12,7 +12,7 @@ public: Window(); MainConfig& main_config() { return main_config_; } Gtk::Box window_box_; - + virtual ~Window() { } //private: