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/juci.cc b/juci/juci.cc index cedf99d..1a6ec46 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..4ce2cb2 --- /dev/null +++ b/juci/juci.h @@ -0,0 +1,20 @@ +#ifndef JUCI_JUCI_H_ +#define JUCI_JUCI_H_ + +#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; +}; + +#endif // JUCI_JUCI_H_ \ No newline at end of file diff --git a/juci/notebook.cc b/juci/notebook.cc index 2d4166f..d0ba733 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -17,14 +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; - OnNewPage("untitled"); refClipboard_ = Gtk::Clipboard::get(); - ispopup = false; - view().pack1(directories_.widget(), true, true); + view().pack1(directories.widget(), true, true); CreateKeybindings(keybindings); INFO("Notebook Controller Success"); } // Constructor @@ -33,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)); @@ -147,7 +145,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 +154,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(). @@ -177,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())->parser.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())->parser. - 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; @@ -298,21 +188,14 @@ Gtk::Box& Notebook::Controller::entry_view() { return 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); - -} - void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); - OnCreatePage(); - text_vec_.back()->on_open_file(path); + 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) @@ -320,34 +203,25 @@ 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); -} - -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); - TextViewHandlers(text_vec_.back()->view); + Notebook().set_focus_child(*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; + 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+"*"); + Notebook().set_tab_label_text(*(Notebook().get_nth_page(CurrentPage())), filename+"*"); } - }; + text_vec_.at(CurrentPage())->is_saved=false; + }); } 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(); @@ -447,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); } @@ -467,7 +341,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() { @@ -476,7 +350,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() { @@ -494,96 +368,12 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr }); } -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, - 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())->parser.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; - } + return text_vec_.at(CurrentPage())->view->file_path; } 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) { @@ -593,7 +383,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) @@ -644,7 +434,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 8361dd8..98af702 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -58,45 +58,23 @@ 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_; } 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); - - protected: - void TextViewHandlers(Gtk::TextView& textview); - void PopupSelectHandler(Gtk::Dialog &popup, - Gtk::ListViewText &listview, - std::map - *items); + std::string project_path; + Directories::Controller directories; 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; - 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_; @@ -106,8 +84,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/selectiondialog.cc b/juci/selectiondialog.cc new file mode 100644 index 0000000..8eca51d --- /dev/null +++ b/juci/selectiondialog.cc @@ -0,0 +1,85 @@ +#include "selectiondialog.h" + +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); + 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&)(*text_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 = text_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; + 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; + text_view.buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_WIDGET, + temp1.get_x(), temp1.get_y(), x, y); + 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 > text_view.get_width()-current_x) { + x -= current_x; + if (x < view_edge_x) x = view_edge_x; + } + 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; + } + 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..f8479ce --- /dev/null +++ b/juci/selectiondialog.h @@ -0,0 +1,24 @@ +#ifndef JUCI_SELECTIONDIALOG_H_ +#define JUCI_SELECTIONDIALOG_H_ + +#include "gtkmm.h" +#include "logging.h" +#include "source.h" + +class SelectionDialog : public Gtk::Dialog { +public: + SelectionDialog(Gtk::TextView& text_view); + void show(const std::map& rows); + bool close(GdkEventFocus*); + + std::function on_select; + +private: + void adjust(int current_x, int current_y); + + Gtk::TextView& text_view; + Gtk::ScrolledWindow scrolled_window; + Gtk::ListViewText list_view_text; +}; + +#endif // JUCI_SELECTIONDIALOG_H_ \ No newline at end of file diff --git a/juci/source.cc b/juci/source.cc index cd9eab4..1993db2 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); @@ -20,8 +21,17 @@ bool Source::Config::legal_extension(std::string e) const { ////////////// //// View //// ////////////// -Source::View::View() { +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); + 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 +50,193 @@ string Source::View::get_line_before_insert() { return line; } -/////////////// -//// Parser /// -/////////////// -clang::Index Source::Parser::clang_index(0, 1); +//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. + 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(); + 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; + } + //"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; +} + +////////////////// +//// ClangView /// +////////////////// +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), +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); + signal_key_release_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_release), false); +} -Source::Parser::~Parser() { +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,9 +260,9 @@ 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::CompilationDatabase db(project_path); clang::CompileCommands commands(file_path, &db); std::vector cmds = commands.get_commands(); std::vector arguments; @@ -123,7 +275,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 +295,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 +340,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,264 +356,179 @@ 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(false), 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; - } - 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() { - 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::on_new_empty_file() { - string filename("/tmp/untitled"); - sourcefile s(filename); - parser.file_path=filename; - parser.project_path=filename; - s.save(""); -} - -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()); - is_saved=true; - buffer()->get_undo_manager()->end_not_undoable_action(); - 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"); +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; } - else { - parse_thread_go=true; + 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; } - }); - - 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(); - } + } 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; } -Glib::RefPtr Source::Controller::buffer() { - return view.get_source_buffer(); -} - -//TODO: move indentation to Parser, replace indentation methods with a better implementation or -//maybe use libclang -bool Source::Controller::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 +//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(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 depending on if/else/etc and brackets 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(); + 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;cget_insert()); - buffer()->place_cursor(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)) { - 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()); + return true; } 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()); + return true; } 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()); - } - 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); - 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); } - 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); - 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); - 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_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=view.get_line_before_insert(); + 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 = 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(); - 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); - 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); - } - } - } + + return Source::View::on_key_press(key); +} + +//////////////////// +//// Controller //// +//////////////////// + +// Source::Controller::Controller() +// Constructor for Controller +Source::Controller::Controller(const Source::Config &config, + const std::string& file_path, std::string project_path) { + if(project_path=="") { + project_path=boost::filesystem::path(file_path).parent_path().string(); } - return false; + 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 99ede6b..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: @@ -22,6 +18,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 @@ -43,13 +40,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 +54,37 @@ 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 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(); + std::string file_path; + std::string project_path; + protected: + const Source::Config& config; + bool on_key_press(GdkEventKey* key); + }; // class View + + class GenericView : public View { + public: + 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 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::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 +92,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,29 +105,9 @@ 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 on_new_empty_file(); - void on_open_file(const std::string &filename); - Glib::RefPtr buffer(); bool on_key_press(GdkEventKey* key); + bool on_key_release(GdkEventKey* key); - bool is_saved = true; - - Parser parser; - View view; - - std::function signal_buffer_changed; - - private: Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; std::thread parse_thread; @@ -128,8 +116,17 @@ 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; - const Config& config; + std::unique_ptr view; }; // class Controller } // namespace Source #endif // JUCI_SOURCE_H_ 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 8758903..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(); @@ -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()); @@ -142,7 +138,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): @@ -201,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(); };