diff --git a/juci/notebook.cc b/juci/notebook.cc index f4862da..c8fae34 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -196,7 +196,7 @@ bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) { bool Notebook::Controller::GeneratePopup(int key_id) { INFO("Notebook genereate popup, getting iters"); - std::string path = text_vec_.at(CurrentPage())->path(); + std::string path = text_vec_.at(CurrentPage())->parser.file_path; if (!LegalExtension(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(); @@ -312,14 +312,14 @@ void Notebook::Controller::OnNewPage(std::string name) { 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()); + Notebook().set_focus_child(text_vec_.at(Pages()-1)->view); } void Notebook::Controller:: -MapBuffers(std::map *buffers) { +MapBuffers(std::map *buffers) const { for (auto &buffer : text_vec_) { - buffers->operator[](buffer->model().file_path()) = + buffers->operator[](buffer->parser.file_path) = buffer->buffer()->get_text().raw(); } } @@ -328,29 +328,28 @@ void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); OnCreatePage(); text_vec_.back()->OnOpenFile(path); - text_vec_.back()->set_is_saved(true); + text_vec_.back()->is_saved=true; unsigned pos = path.find_last_of("/\\"); Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); - Notebook().set_focus_child(text_vec_.back()->view()); - text_vec_.back()->set_is_changed(false); + 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(), this)); + text_vec_.emplace_back(new Source::Controller(source_config(), *this)); scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); editor_vec_.push_back(new Gtk::HBox()); - scrolledtext_vec_.back()->add(text_vec_.back()->view()); + scrolledtext_vec_.back()->add(text_vec_.back()->view); editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); - TextViewHandlers(text_vec_.back()->view()); + TextViewHandlers(text_vec_.back()->view); } void Notebook::Controller::OnCloseCurrentPage() { INFO("Notebook close page"); if (Pages() != 0) { - if(text_vec_.back()->is_changed()){ + if(text_vec_.back()->is_changed){ AskToSaveDialog(); } int page = CurrentPage(); @@ -458,7 +457,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() { @@ -467,7 +466,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() { @@ -536,7 +535,7 @@ void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll, } std::string Notebook::Controller::CurrentPagePath(){ - return text_vec_.at(CurrentPage())->path(); + return text_vec_.at(CurrentPage())->parser.file_path; } void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, @@ -575,9 +574,9 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, bool Notebook::Controller:: OnSaveFile() { INFO("Notebook save file"); - if (text_vec_.at(CurrentPage())->is_saved()) { + if (text_vec_.at(CurrentPage())->is_saved) { std::ofstream file; - file.open (text_vec_.at(CurrentPage())->path()); + file.open (text_vec_.at(CurrentPage())->parser.file_path); file << CurrentTextView().get_buffer()->get_text(); file.close(); return true; @@ -593,8 +592,8 @@ bool Notebook::Controller:: OnSaveFile(std::string path) { file.open (path); file << CurrentTextView().get_buffer()->get_text(); file.close(); - text_vec_.at(CurrentPage())->set_file_path(path); - text_vec_.at(CurrentPage())->set_is_saved(true); + text_vec_.at(CurrentPage())->parser.file_path=path; + text_vec_.at(CurrentPage())->is_saved=true; return true; } return false; @@ -639,7 +638,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())->path()+" ?"); + text_vec_.at(CurrentPage())->parser.file_path+" ?"); DEBUG("AskToSaveDialog: run dialog"); int result = dialog.run(); diff --git a/juci/notebook.h b/juci/notebook.h index c8f0d9f..d923c59 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -62,7 +62,7 @@ namespace Notebook { void OnOpenFile(std::string filename); void OnCreatePage(); bool ScrollEventCallback(GdkEventScroll* scroll_event); - void MapBuffers(std::map *buffers); + void MapBuffers(std::map *buffers) const; clang::Index* index() { return &index_; } int Pages(); Directories::Controller& directories() { return directories_; } diff --git a/juci/source.cc b/juci/source.cc index 847b2cd..7a1ce2c 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -30,7 +30,6 @@ Range(const Source::Range &org) : ////////////// Source::View::View() { Gsv::init(); - set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); } string Source::View::GetLine(size_t line_number) { @@ -49,18 +48,6 @@ string Source::View::GetLineBeforeInsert() { return line; } -// Source::View::ApplyConfig -// Applies theme in textview -void Source::View::ApplyConfig(const Source::Config &config) { - override_font(Pango::FontDescription(config.font)); - set_show_line_numbers(config.show_line_numbers); - set_highlight_current_line(config.highlight_current_line); - this->override_background_color(Gdk::RGBA(config.background)); - for (auto &item : config.tagtable()) { - get_buffer()->create_tag(item.first)->property_foreground() = item.second; - } -} - // Source::View::Config::tagtable() // returns a const refrence to the tagtable const std::unordered_map& Source::Config::tagtable() const { @@ -105,11 +92,7 @@ SetTagTable(const std::unordered_map &tagtable) { /////////////// //// Model //// /////////////// -Source::Model::Model(const Source::Config &config) : - config_(config) { -} - -void Source::Model:: +void Source::Parser:: InitSyntaxHighlighting(const std::string &filepath, const std::string &project_path, const std::map @@ -117,7 +100,7 @@ InitSyntaxHighlighting(const std::string &filepath, int start_offset, int end_offset, clang::Index *index) { - set_project_path(project_path); + this->project_path=project_path; std::vector arguments = get_compilation_commands(); tu_ = std::unique_ptr(new clang::TranslationUnit(index, filepath, @@ -133,9 +116,9 @@ OnLineEdit(const std::vector &locations, } // Source::Model::UpdateLine -int Source::Model:: +int Source::Parser:: ReParse(const std::map &buffer) { - return tu_->ReparseTranslationUnit(file_path(), buffer); + return tu_->ReparseTranslationUnit(file_path, buffer); } @@ -151,8 +134,8 @@ GetAutoCompleteSuggestions(int line_number, INFO("Getting auto complete suggestions"); parsing.lock(); std::map buffers; - notebook_->MapBuffers(&buffers); - model().GetAutoCompleteSuggestions(buffers, + notebook.MapBuffers(&buffers); + parser.GetAutoCompleteSuggestions(buffers, line_number, column, suggestions); @@ -161,14 +144,14 @@ GetAutoCompleteSuggestions(int line_number, parsing.unlock(); } -void Source::Model:: +void Source::Parser:: GetAutoCompleteSuggestions(const std::map &buffers, int line_number, int column, std::vector *suggestions) { clang::CodeCompleteResults results(tu_.get(), - file_path(), + file_path, buffers, line_number, column); @@ -182,33 +165,10 @@ GetAutoCompleteSuggestions(const std::map &buffers, } } -// sets the filepath for this mvc -void Source::Model:: -set_file_path(const std::string &file_path) { - file_path_ = file_path; -} -// sets the project path for this mvc -void Source::Model:: -set_project_path(const std::string &project_path) { - project_path_ = project_path; -} -// gets the file_path member -const std::string& Source::Model::file_path() const { - return file_path_; -} -// gets the project_path member -const std::string& Source::Model::project_path() const { - return project_path_; -} -// gets the config member -const Source::Config& Source::Model::config() const { - return config_; -} - -std::vector Source::Model:: +std::vector Source::Parser:: get_compilation_commands() { - clang::CompilationDatabase db(project_path()+"/"); - clang::CompileCommands commands(file_path(), &db); + clang::CompilationDatabase db(project_path+"/"); + clang::CompileCommands commands(file_path, &db); std::vector cmds = commands.get_commands(); std::vector arguments; for (auto &i : cmds) { @@ -220,11 +180,11 @@ get_compilation_commands() { return arguments; } -std::vector Source::Model:: +std::vector Source::Parser:: ExtractTokens(int start_offset, int end_offset) { std::vector ranges; - clang::SourceLocation start(tu_.get(), file_path(), start_offset); - clang::SourceLocation end(tu_.get(), file_path(), end_offset); + clang::SourceLocation start(tu_.get(), file_path, start_offset); + clang::SourceLocation end(tu_.get(), file_path, end_offset); clang::SourceRange range(&start, &end); clang::Tokens tokens(tu_.get(), &range); std::vector tks = tokens.tokens(); @@ -240,7 +200,7 @@ ExtractTokens(int start_offset, int end_offset) { return ranges; } -void Source::Model:: +void Source::Parser:: HighlightCursor(clang::Token *token, std::vector *source_ranges) { clang::SourceLocation location = token->get_source_location(tu_.get()); @@ -256,7 +216,7 @@ HighlightCursor(clang::Token *token, Source::Location(end_line_num, end_offset), (int) cursor.kind()); } -void Source::Model:: +void Source::Parser:: HighlightToken(clang::Token *token, std::vector *source_ranges, int token_kind) { @@ -279,10 +239,18 @@ HighlightToken(clang::Token *token, // Source::Controller::Controller() // Constructor for Controller Source::Controller::Controller(const Source::Config &config, - Notebook::Controller *notebook) : - model_(config), notebook_(notebook) { + Notebook::Controller ¬ebook) : + config(config), notebook(notebook) { INFO("Source Controller with childs constructed"); - view().signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::OnKeyPress), false); + view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::OnKeyPress), 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.tagtable()) { + buffer()->create_tag(item.first)->property_foreground() = item.second; + } } Source::Controller::~Controller() { @@ -290,29 +258,14 @@ Source::Controller::~Controller() { parsing.unlock(); } -// Source::Controller::view() -// return shared_ptr to the view -Source::View& Source::Controller::view() { - return view_; -} -// Source::Controller::model() -// return shared_ptr to the model() -Source::Model& Source::Controller::model() { - return model_; -} - void Source::Controller::OnNewEmptyFile() { string filename("/tmp/juci_t"); sourcefile s(filename); - model().set_file_path(filename); - model().set_project_path(filename); + parser.file_path=filename; + parser.project_path=filename; s.save(""); } -string extract_file_path(const std::string &file_path) { - return file_path.substr(0, file_path.find_last_of('/')); -} - void Source::View::OnUpdateSyntax(const std::vector &ranges, const Source::Config &config) { if (ranges.empty() || ranges.size() == 0) { @@ -344,36 +297,33 @@ void Source::View::OnUpdateSyntax(const std::vector &ranges, } void Source::Controller::OnOpenFile(const string &filepath) { - set_file_path(filepath); + parser.file_path=filepath; sourcefile s(filepath); std::map buffers; - notebook_->MapBuffers(&buffers); + notebook.MapBuffers(&buffers); buffers[filepath] = s.get_content(); buffer()->get_undo_manager()->begin_not_undoable_action(); buffer()->set_text(s.get_content()); buffer()->get_undo_manager()->end_not_undoable_action(); int start_offset = buffer()->begin().get_offset(); int end_offset = buffer()->end().get_offset(); - view().ApplyConfig(model().config()); - if (notebook_->LegalExtension(filepath.substr(filepath.find_last_of(".") + 1))) { - model().InitSyntaxHighlighting(filepath, - extract_file_path(filepath), + if (notebook.LegalExtension(filepath.substr(filepath.find_last_of(".") + 1))) { + parser.InitSyntaxHighlighting(filepath, + parser.file_path.substr(0, parser.file_path.find_last_of('/')), buffers, start_offset, end_offset, - notebook_->index()); - view().OnUpdateSyntax(model().ExtractTokens(start_offset, end_offset), - model().config()); + notebook.index()); + view.OnUpdateSyntax(parser.ExtractTokens(start_offset, end_offset), config); //OnUpdateSyntax must happen in main thread, so the parse-thread //sends a signal to the main thread that it is to call the following function: parsing_done.connect([this](){ - INFO("Updating syntax"); - view(). - OnUpdateSyntax(model().ExtractTokens(0, buffer()->get_text().size()), - model().config()); - INFO("Syntax updated"); - }); + INFO("Updating syntax"); + view. + OnUpdateSyntax(parser.ExtractTokens(0, buffer()->get_text().size()), config); + INFO("Syntax updated"); + }); buffer()->signal_end_user_action().connect([this]() { std::thread parse([this]() { @@ -382,9 +332,9 @@ void Source::Controller::OnOpenFile(const string &filepath) { while (true) { const std::string raw = buffer()->get_text().raw(); std::map buffers; - notebook_->MapBuffers(&buffers); - buffers[model().file_path()] = raw; - if (model().ReParse(buffers) == 0 && + notebook.MapBuffers(&buffers); + buffers[parser.file_path] = raw; + if (parser.ReParse(buffers) == 0 && raw == buffer()->get_text().raw()) { break; } @@ -400,7 +350,7 @@ void Source::Controller::OnOpenFile(const string &filepath) { } Glib::RefPtr Source::Controller::buffer() { - return view().get_source_buffer(); + return view.get_source_buffer(); } bool Source::Controller::OnKeyPress(GdkEventKey* key) { @@ -411,44 +361,44 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) { //Indent as in previous line, and indent right after if/else/etc if(key->keyval==GDK_KEY_Return && key->state==0) { - string line(view().GetLineBeforeInsert()); + string line(view.GetLineBeforeInsert()); std::smatch sm; if(std::regex_match(line, sm, bracket_regex)) { - buffer()->insert_at_cursor("\n"+sm[1].str()+model().config().tab+"\n"+sm[1].str()+"}"); + buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}"); auto insert_it = buffer()->get_insert()->get_iter(); - for(size_t c=0;cget_insert()); + view.scroll_to(buffer()->get_insert()); buffer()->place_cursor(insert_it); } else if(std::regex_match(line, sm, no_bracket_statement_regex)) { - buffer()->insert_at_cursor("\n"+sm[1].str()+model().config().tab); - view().scroll_to(buffer()->get_insert()); + buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); + view.scroll_to(buffer()->get_insert()); } else if(std::regex_match(line, sm, no_bracket_no_para_statement_regex)) { - buffer()->insert_at_cursor("\n"+sm[1].str()+model().config().tab); - view().scroll_to(buffer()->get_insert()); + buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); + view.scroll_to(buffer()->get_insert()); } else if(std::regex_match(line, sm, spaces_regex)) { std::smatch sm2; size_t line_nr=buffer()->get_insert()->get_iter().get_line(); - if(line_nr>0 && sm[1].str().size()>=model().config().tab_size) { - string previous_line=view().GetLine(line_nr-1); + if(line_nr>0 && sm[1].str().size()>=config.tab_size) { + string previous_line=view.GetLine(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()); + view.scroll_to(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()); + view.scroll_to(buffer()->get_insert()); return true; } } } buffer()->insert_at_cursor("\n"+sm[1].str()); - view().scroll_to(buffer()->get_insert()); + view.scroll_to(buffer()->get_insert()); } return true; } @@ -460,7 +410,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) { 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, model().config().tab); + buffer()->insert(line_it, config.tab); } return true; } @@ -472,8 +422,8 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) { int line_end=selection_end.get_line(); for(int line_nr=line_start;line_nr<=line_end;line_nr++) { - string line=view().GetLine(line_nr); - if(!(line.size()>=model().config().tab_size && line.substr(0, model().config().tab_size)==model().config().tab)) + string line=view.GetLine(line_nr); + if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) return true; } @@ -481,7 +431,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) { 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); } @@ -489,8 +439,8 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) { } //Indent left when writing } on a new line else if(key->keyval==GDK_KEY_braceright) { - string line=view().GetLineBeforeInsert(); - if(line.size()>=model().config().tab_size) { + string line=view.GetLineBeforeInsert(); + if(line.size()>=config.tab_size) { for(auto c: line) { if(c!=' ') return false; @@ -498,7 +448,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) { Gtk::TextIter insert_it = buffer()->get_insert()->get_iter(); Gtk::TextIter line_it = 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); @@ -510,8 +460,8 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) { Gtk::TextIter insert_it=buffer()->get_insert()->get_iter(); int line_nr=insert_it.get_line(); if(line_nr>0) { - string line=view().GetLine(line_nr); - string previous_line=view().GetLine(line_nr-1); + string line=view.GetLine(line_nr); + string previous_line=view.GetLine(line_nr-1); smatch sm; if(std::regex_match(previous_line, sm, spaces_regex)) { if(line==sm[1]) { diff --git a/juci/source.h b/juci/source.h index b4b0000..72554ce 100644 --- a/juci/source.h +++ b/juci/source.h @@ -64,7 +64,6 @@ namespace Source { public: View(); virtual ~View() { } - void ApplyConfig(const Config &config); void OnLineEdit(const std::vector &locations, const Config &config); void OnUpdateSyntax(const std::vector &locations, @@ -91,10 +90,8 @@ namespace Source { std::vector chunks_; }; - class Model{ + class Parser{ public: - // constructor for Source::Model - explicit Model(const Source::Config &config); // inits the syntax highligthing on file open void InitSyntaxHighlighting(const std::string &filepath, const std::string &project_path, @@ -103,31 +100,18 @@ namespace Source { int start_offset, int end_offset, clang::Index *index); - // sets the filepath for this mvc - void set_file_path(const std::string &file_path); - // sets the project path for this mvc - void set_project_path(const std::string &project_path); - - // gets the file_path member - const std::string& file_path() const; - // gets the project_path member - const std::string& project_path() const; - // gets the config member - const Config& config() const; 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); + std::string file_path; + std::string project_path; private: - Config config_; - std::string file_path_; - std::string project_path_; std::unique_ptr tu_; //use unique_ptr since it is not initialized in constructor void HighlightToken(clang::Token *token, std::vector *source_ranges, @@ -140,11 +124,9 @@ namespace Source { class Controller { public: Controller(const Source::Config &config, - Notebook::Controller *notebook); + Notebook::Controller ¬ebook); Controller(); ~Controller(); - View& view(); - Model& model(); void OnNewEmptyFile(); void OnOpenFile(const std::string &filename); void GetAutoCompleteSuggestions(int line_number, @@ -152,26 +134,22 @@ namespace Source { std::vector *suggestions); Glib::RefPtr buffer(); - bool is_saved() { return is_saved_; } - bool is_changed() { return is_changed_; } - std::string path() { return model().file_path(); } - void set_is_saved(bool isSaved) { is_saved_ = isSaved; } - void set_is_changed(bool isChanged) { is_changed_ = isChanged; } - void set_file_path(std::string path) { model().set_file_path(path); } bool OnKeyPress(GdkEventKey* key); - + + bool is_saved = false; //TODO: Is never set to false in Notebook::Controller + bool is_changed = false; //TODO: Is never set to true + + Parser parser; + View view; + private: void OnLineEdit(); void OnSaveFile(); std::mutex parsing; Glib::Dispatcher parsing_done; - bool is_saved_ = false; - bool is_changed_ = false; - - protected: - View view_; - Model model_; - Notebook::Controller *notebook_; + + const Config& config; + Notebook::Controller& notebook; //TODO: should maybe be const, but that involves a small change in libclangmm }; // class Controller } // namespace Source #endif // JUCI_SOURCE_H_