From 0bd7470d28de099eaff15525575d446fda1589b1 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 17 Jun 2015 15:45:45 +0200 Subject: [PATCH 01/13] Last crash when writing code hopefully fixed. Taking care now not to read from view-buffer in a thread (only do this in GTK-main-thread). --- juci/source.cc | 73 ++++++++++++++++++++++++++++++-------------------- juci/source.h | 7 ++++- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index 7a1ce2c..14e31c6 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -240,7 +240,7 @@ HighlightToken(clang::Token *token, // Constructor for Controller Source::Controller::Controller(const Source::Config &config, Notebook::Controller ¬ebook) : - config(config), notebook(notebook) { + config(config), notebook(notebook), parse_thread_go(false), parse_thread_mapped(false) { INFO("Source Controller with childs constructed"); view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::OnKeyPress), false); view.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); @@ -316,36 +316,51 @@ void Source::Controller::OnOpenFile(const string &filepath) { 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(parser.ExtractTokens(0, buffer()->get_text().size()), config); - INFO("Syntax updated"); + //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_buffers_mutex.try_lock()) { + notebook.MapBuffers(&this->parse_thread_buffers); + parse_thread_mapped=true; + parse_thread_buffers_mutex.unlock(); + } + parse_thread_go=true; + }); + + parse_done.connect([this](){ + if(parse_thread_mapped) { + INFO("Updating syntax"); + view. + OnUpdateSyntax(parser.ExtractTokens(0, buffer()->get_text().size()), config); + INFO("Syntax updated"); + } + else { + parse_thread_go=true; + } }); - buffer()->signal_end_user_action().connect([this]() { - std::thread parse([this]() { - if (parsing.try_lock()) { - INFO("Starting parsing"); - while (true) { - const std::string raw = buffer()->get_text().raw(); - std::map buffers; - notebook.MapBuffers(&buffers); - buffers[parser.file_path] = raw; - if (parser.ReParse(buffers) == 0 && - raw == buffer()->get_text().raw()) { - break; - } - } - parsing.unlock(); - parsing_done(); - INFO("Parsing completed"); - } - }); - parse.detach(); - }); + std::thread parse_thread([this]() { + while(true) { + while(!parse_thread_go) std::this_thread::yield(); + if(!parse_thread_mapped) { + parse_thread_go=false; + parse_start(); + } + else if (parse_thread_mapped && parsing.try_lock() && parse_thread_buffers_mutex.try_lock()) { + parser.ReParse(this->parse_thread_buffers); + parse_thread_go=false; + parsing.unlock(); + parse_thread_buffers_mutex.unlock(); + parse_done(); + } + } + }); + parse_thread.detach(); + + buffer()->signal_changed().connect([this]() { + parse_thread_mapped=false; + parse_thread_go=true; + }); } } diff --git a/juci/source.h b/juci/source.h index 72554ce..99eb436 100644 --- a/juci/source.h +++ b/juci/source.h @@ -146,7 +146,12 @@ namespace Source { void OnLineEdit(); void OnSaveFile(); std::mutex parsing; - Glib::Dispatcher parsing_done; + Glib::Dispatcher parse_done; + Glib::Dispatcher parse_start; + std::map parse_thread_buffers; + std::mutex parse_thread_buffers_mutex; + std::atomic parse_thread_go; + std::atomic parse_thread_mapped; const Config& config; Notebook::Controller& notebook; //TODO: should maybe be const, but that involves a small change in libclangmm From 59449bfcb9cb01bf2bd41862041d692e837cad85 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 17 Jun 2015 16:20:28 +0200 Subject: [PATCH 02/13] Added missing #include. --- juci/source.h | 1 + 1 file changed, 1 insertion(+) diff --git a/juci/source.h b/juci/source.h index 99eb436..63c7b80 100644 --- a/juci/source.h +++ b/juci/source.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "gtksourceviewmm.h" namespace Notebook { From 81229a287ce4e99953eed3606bbdb6ac3f55f362 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 17 Jun 2015 21:20:57 +0200 Subject: [PATCH 03/13] More code cleanup. Removed dependency notebook.h from the Source-classes. Support for other languages than C++ can soon be added by implementing an interface of Source::Parser, but this is not something I will prioritize. --- juci/notebook.cc | 27 +++-------------------- juci/notebook.h | 3 --- juci/source.cc | 56 ++++++++++++++++++++++++++++++++---------------- juci/source.h | 18 +++++++++++----- 4 files changed, 53 insertions(+), 51 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 9368b11..c80ea0e 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -18,8 +18,7 @@ Notebook::Controller::Controller(Gtk::Window* window, Source::Config& source_cfg, Directories::Config& dir_cfg) : directories_(dir_cfg), - source_config_(source_cfg), - index_(0, 1) { + source_config_(source_cfg) { INFO("Create notebook"); window_ = window; OnNewPage("untitled"); @@ -193,7 +192,7 @@ bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) { bool Notebook::Controller::GeneratePopup(int key_id) { INFO("Notebook genereate popup, getting iters"); std::string path = text_vec_.at(CurrentPage())->parser.file_path; - if (!LegalExtension(path.substr(path.find_last_of(".") + 1))) return false; + 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(); @@ -312,14 +311,6 @@ void Notebook::Controller::OnNewPage(std::string name) { } -void Notebook::Controller:: -MapBuffers(std::map *buffers) const { - for (auto &buffer : text_vec_) { - buffers->operator[](buffer->parser.file_path) = - buffer->buffer()->get_text().raw(); - } -} - void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); OnCreatePage(); @@ -334,7 +325,7 @@ void Notebook::Controller::OnOpenFile(std::string path) { 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(), text_vec_)); scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); editor_vec_.push_back(new Gtk::HBox()); scrolledtext_vec_.back()->add(text_vec_.back()->view); @@ -674,15 +665,3 @@ void Notebook::Controller::AskToSaveDialog() { } } -bool Notebook::Controller::LegalExtension(std::string e) { - std::transform(e.begin(), e.end(),e.begin(), ::tolower); - std::vector extensions = - source_config().extensiontable(); - if (find(extensions.begin(), extensions.end(), e) != extensions.end()) { - DEBUG("Legal extension"); - return true; - } - DEBUG("Ilegal extension"); - return false; -} - diff --git a/juci/notebook.h b/juci/notebook.h index 510be38..8361dd8 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -62,8 +62,6 @@ namespace Notebook { void OnOpenFile(std::string filename); void OnCreatePage(); bool ScrollEventCallback(GdkEventScroll* scroll_event); - void MapBuffers(std::map *buffers) const; - clang::Index* index() { return &index_; } int Pages(); Directories::Controller& directories() { return directories_; } Gtk::Paned& view(); @@ -111,7 +109,6 @@ 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 14e31c6..2cd7bf5 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -3,7 +3,6 @@ #include #include #include -#include "notebook.h" #include "logging.h" #include #include @@ -89,9 +88,21 @@ SetTagTable(const std::unordered_map &tagtable) { tagtable_ = tagtable; } +bool Source::Config::legal_extension(std::string e) const { + std::transform(e.begin(), e.end(),e.begin(), ::tolower); + if (find(extensiontable_.begin(), extensiontable_.end(), e) != extensiontable_.end()) { + DEBUG("Legal extension"); + return true; + } + DEBUG("Ilegal extension"); + return false; +} + /////////////// -//// Model //// +//// Parser /// /////////////// +clang::Index Source::Parser::clang_index(0, 1); + void Source::Parser:: InitSyntaxHighlighting(const std::string &filepath, const std::string &project_path, @@ -108,6 +119,16 @@ InitSyntaxHighlighting(const std::string &filepath, buffers)); } +std::map Source::Parser:: +get_buffer_map() const { + std::map buffer_map; + for (auto &controller : controllers) { + buffer_map.operator[](controller->parser.file_path) = + controller->buffer()->get_text().raw(); + } + return buffer_map; +} + // Source::View::UpdateLine void Source::View:: OnLineEdit(const std::vector &locations, @@ -133,9 +154,7 @@ GetAutoCompleteSuggestions(int line_number, *suggestions) { INFO("Getting auto complete suggestions"); parsing.lock(); - std::map buffers; - notebook.MapBuffers(&buffers); - parser.GetAutoCompleteSuggestions(buffers, + parser.GetAutoCompleteSuggestions(parser.get_buffer_map(), line_number, column, suggestions); @@ -239,8 +258,8 @@ HighlightToken(clang::Token *token, // Source::Controller::Controller() // Constructor for Controller Source::Controller::Controller(const Source::Config &config, - Notebook::Controller ¬ebook) : - config(config), notebook(notebook), parse_thread_go(false), parse_thread_mapped(false) { + std::vector > &controllers) : + config(config), parser(controllers), parse_thread_go(false), parse_thread_mapped(false) { INFO("Source Controller with childs constructed"); view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::OnKeyPress), false); view.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); @@ -299,30 +318,29 @@ void Source::View::OnUpdateSyntax(const std::vector &ranges, void Source::Controller::OnOpenFile(const string &filepath) { parser.file_path=filepath; sourcefile s(filepath); - std::map buffers; - notebook.MapBuffers(&buffers); - buffers[filepath] = s.get_content(); + auto buffer_map=parser.get_buffer_map(); + buffer_map[filepath] = s.get_content(); buffer()->get_undo_manager()->begin_not_undoable_action(); buffer()->set_text(s.get_content()); buffer()->get_undo_manager()->end_not_undoable_action(); int start_offset = buffer()->begin().get_offset(); int end_offset = buffer()->end().get_offset(); - if (notebook.LegalExtension(filepath.substr(filepath.find_last_of(".") + 1))) { + if (config.legal_extension(filepath.substr(filepath.find_last_of(".") + 1))) { parser.InitSyntaxHighlighting(filepath, parser.file_path.substr(0, parser.file_path.find_last_of('/')), - buffers, + buffer_map, start_offset, end_offset, - notebook.index()); + &Parser::clang_index); view.OnUpdateSyntax(parser.ExtractTokens(start_offset, end_offset), config); //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_buffers_mutex.try_lock()) { - notebook.MapBuffers(&this->parse_thread_buffers); + if(parse_thread_buffer_map_mutex.try_lock()) { + this->parse_thread_buffer_map=parser.get_buffer_map(); parse_thread_mapped=true; - parse_thread_buffers_mutex.unlock(); + parse_thread_buffer_map_mutex.unlock(); } parse_thread_go=true; }); @@ -346,11 +364,11 @@ void Source::Controller::OnOpenFile(const string &filepath) { parse_thread_go=false; parse_start(); } - else if (parse_thread_mapped && parsing.try_lock() && parse_thread_buffers_mutex.try_lock()) { - parser.ReParse(this->parse_thread_buffers); + else if (parse_thread_mapped && parsing.try_lock() && parse_thread_buffer_map_mutex.try_lock()) { + parser.ReParse(this->parse_thread_buffer_map); parse_thread_go=false; parsing.unlock(); - parse_thread_buffers_mutex.unlock(); + parse_thread_buffer_map_mutex.unlock(); parse_done(); } } diff --git a/juci/source.h b/juci/source.h index 63c7b80..b64b509 100644 --- a/juci/source.h +++ b/juci/source.h @@ -29,6 +29,7 @@ namespace Source { void InsertType(const std::string &key, const std::string &value); void InsertExtension(const std::string &ext); std::vector extensiontable_; + bool legal_extension(std::string e) const ; // TODO: Have to clean away all the simple setter and getter methods at some point. It creates too much unnecessary code unsigned tab_size; bool show_line_numbers, highlight_current_line; @@ -90,9 +91,13 @@ namespace Source { chunks_(chunks) { } std::vector chunks_; }; + + class Controller; class Parser{ public: + Parser(std::vector > &controllers): + controllers(controllers) {} // inits the syntax highligthing on file open void InitSyntaxHighlighting(const std::string &filepath, const std::string &project_path, @@ -112,6 +117,8 @@ namespace Source { std::string file_path; std::string project_path; + static clang::Index clang_index; + std::map get_buffer_map() const; private: std::unique_ptr tu_; //use unique_ptr since it is not initialized in constructor void HighlightToken(clang::Token *token, @@ -120,13 +127,14 @@ namespace Source { void HighlightCursor(clang::Token *token, std::vector *source_ranges); std::vector get_compilation_commands(); + //controllers is needed here, no way around that I think + std::vector > &controllers; }; class Controller { public: Controller(const Source::Config &config, - Notebook::Controller ¬ebook); - Controller(); + std::vector > &controllers); ~Controller(); void OnNewEmptyFile(); void OnOpenFile(const std::string &filename); @@ -136,6 +144,7 @@ namespace Source { *suggestions); Glib::RefPtr buffer(); bool OnKeyPress(GdkEventKey* key); + bool LegalExtension(std::string e); bool is_saved = false; //TODO: Is never set to false in Notebook::Controller bool is_changed = false; //TODO: Is never set to true @@ -149,13 +158,12 @@ namespace Source { std::mutex parsing; Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; - std::map parse_thread_buffers; - std::mutex parse_thread_buffers_mutex; + std::map parse_thread_buffer_map; + std::mutex parse_thread_buffer_map_mutex; std::atomic parse_thread_go; std::atomic parse_thread_mapped; 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_ From b7af345e84c2272d25dd9b9e36bb30c67076f79d Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 17 Jun 2015 21:26:31 +0200 Subject: [PATCH 04/13] Added const to controllers reference. --- juci/source.cc | 2 +- juci/source.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index 2cd7bf5..647483a 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -258,7 +258,7 @@ HighlightToken(clang::Token *token, // Source::Controller::Controller() // Constructor for Controller Source::Controller::Controller(const Source::Config &config, - std::vector > &controllers) : + const std::vector > &controllers) : config(config), parser(controllers), parse_thread_go(false), parse_thread_mapped(false) { INFO("Source Controller with childs constructed"); view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::OnKeyPress), false); diff --git a/juci/source.h b/juci/source.h index b64b509..83816d6 100644 --- a/juci/source.h +++ b/juci/source.h @@ -96,7 +96,7 @@ namespace Source { class Parser{ public: - Parser(std::vector > &controllers): + Parser(const std::vector > &controllers): controllers(controllers) {} // inits the syntax highligthing on file open void InitSyntaxHighlighting(const std::string &filepath, @@ -128,13 +128,13 @@ namespace Source { std::vector *source_ranges); std::vector get_compilation_commands(); //controllers is needed here, no way around that I think - std::vector > &controllers; + const std::vector > &controllers; }; class Controller { public: Controller(const Source::Config &config, - std::vector > &controllers); + const std::vector > &controllers); ~Controller(); void OnNewEmptyFile(); void OnOpenFile(const std::string &filename); From 63dac5e737d4d9c2aadd08cf6bf0ab7e08deb580 Mon Sep 17 00:00:00 2001 From: Ole Christian Eidheim Date: Thu, 18 Jun 2015 12:35:02 +0200 Subject: [PATCH 05/13] Forgot to destroy thread in Source::Controller-destructor. --- juci/source.cc | 11 +++++++---- juci/source.h | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index 647483a..1c3d0df 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -259,7 +259,7 @@ HighlightToken(clang::Token *token, // Constructor for Controller Source::Controller::Controller(const Source::Config &config, const std::vector > &controllers) : - config(config), parser(controllers), parse_thread_go(false), parse_thread_mapped(false) { + 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::OnKeyPress), false); view.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); @@ -273,8 +273,10 @@ Source::Controller::Controller(const Source::Config &config, } Source::Controller::~Controller() { + parse_thread_stop=true; parsing.lock(); //Be sure not to destroy while still parsing with libclang parsing.unlock(); + parse_thread.join(); } void Source::Controller::OnNewEmptyFile() { @@ -357,9 +359,11 @@ void Source::Controller::OnOpenFile(const string &filepath) { } }); - std::thread parse_thread([this]() { + parse_thread=std::thread([this]() { while(true) { - while(!parse_thread_go) std::this_thread::yield(); + while(!parse_thread_go && !parse_thread_stop) std::this_thread::yield(); + if(parse_thread_stop) + break; if(!parse_thread_mapped) { parse_thread_go=false; parse_start(); @@ -373,7 +377,6 @@ void Source::Controller::OnOpenFile(const string &filepath) { } } }); - parse_thread.detach(); buffer()->signal_changed().connect([this]() { parse_thread_mapped=false; diff --git a/juci/source.h b/juci/source.h index 83816d6..a64499a 100644 --- a/juci/source.h +++ b/juci/source.h @@ -158,10 +158,12 @@ namespace Source { std::mutex parsing; Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; + std::thread parse_thread; std::map parse_thread_buffer_map; std::mutex parse_thread_buffer_map_mutex; std::atomic parse_thread_go; std::atomic parse_thread_mapped; + std::atomic parse_thread_stop; const Config& config; }; // class Controller From 394d6284eb16381ca9f0731a3c156acc6b1a2735 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 18 Jun 2015 13:48:10 +0200 Subject: [PATCH 06/13] join the parse_thread only if its joinable (created in this case). --- juci/source.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/juci/source.cc b/juci/source.cc index 1c3d0df..a57da0f 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -276,7 +276,8 @@ Source::Controller::~Controller() { parse_thread_stop=true; parsing.lock(); //Be sure not to destroy while still parsing with libclang parsing.unlock(); - parse_thread.join(); + if(parse_thread.joinable()) + parse_thread.join(); } void Source::Controller::OnNewEmptyFile() { From ef4f12469a33acc204bddf76dac4468fce6d349d Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 18 Jun 2015 13:56:35 +0200 Subject: [PATCH 07/13] sleep_for instead of yield reduces the cpu load. --- juci/source.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/juci/source.cc b/juci/source.cc index a57da0f..d981e32 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -362,7 +362,8 @@ void Source::Controller::OnOpenFile(const string &filepath) { parse_thread=std::thread([this]() { while(true) { - while(!parse_thread_go && !parse_thread_stop) std::this_thread::yield(); + 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) { From 9d3cda41992ab23361d0093a8d98f8d127f15e45 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 18 Jun 2015 14:37:32 +0200 Subject: [PATCH 08/13] More code cleanup: get_autocomplete_suggestions (previously GetAutoCompleteSuggestions) now only in Source::Parser class. --- juci/notebook.cc | 8 +++----- juci/source.cc | 53 ++++++++++++++++++------------------------------ juci/source.h | 15 +++----------- 3 files changed, 26 insertions(+), 50 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index c80ea0e..36c3c79 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -229,11 +229,9 @@ bool Notebook::Controller::GeneratePopup(int key_id) { return false; } INFO("Notebook genereate popup, getting autocompletions"); - std::vector acdata; - text_vec_.at(CurrentPage())-> - GetAutoCompleteSuggestions(beg.get_line()+1, - beg.get_line_offset()+2, - &acdata); + 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; diff --git a/juci/source.cc b/juci/source.cc index d981e32..3e18221 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -88,7 +88,7 @@ SetTagTable(const std::unordered_map &tagtable) { tagtable_ = tagtable; } -bool Source::Config::legal_extension(std::string e) const { +bool Source::Config::legal_extension(std::string e) const {/Users/eidheim/test/jucipp/juci std::transform(e.begin(), e.end(),e.begin(), ::tolower); if (find(extensiontable_.begin(), extensiontable_.end(), e) != extensiontable_.end()) { DEBUG("Legal extension"); @@ -103,6 +103,11 @@ bool Source::Config::legal_extension(std::string e) const { /////////////// clang::Index Source::Parser::clang_index(0, 1); +Source::Parser::~Parser() { + parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang + parsing_mutex.unlock(); +} + void Source::Parser:: InitSyntaxHighlighting(const std::string &filepath, const std::string &project_path, @@ -142,36 +147,16 @@ ReParse(const std::map &buffer) { return tu_->ReparseTranslationUnit(file_path, buffer); } - -// Source::Controller::OnLineEdit() -// fired when a line in the buffer is edited -void Source::Controller::OnLineEdit() { } - -void Source::Controller:: -GetAutoCompleteSuggestions(int line_number, - int column, - std::vector - *suggestions) { +std::vector Source::Parser:: +get_autocomplete_suggestions(int line_number, + int column) { INFO("Getting auto complete suggestions"); - parsing.lock(); - parser.GetAutoCompleteSuggestions(parser.get_buffer_map(), - line_number, - column, - suggestions); - DEBUG("Number of suggestions"); - DEBUG_VAR(suggestions->size()); - parsing.unlock(); -} - -void Source::Parser:: -GetAutoCompleteSuggestions(const std::map &buffers, - int line_number, - int column, - std::vector - *suggestions) { + std::vector suggestions; + auto buffer_map=get_buffer_map(); + parsing_mutex.lock(); clang::CodeCompleteResults results(tu_.get(), file_path, - buffers, + buffer_map, line_number, column); for (int i = 0; i < results.size(); i++) { @@ -180,8 +165,12 @@ GetAutoCompleteSuggestions(const std::map &buffers, for (auto &chunk : chunks_) { chunks.emplace_back(chunk); } - suggestions->emplace_back(chunks); + suggestions.emplace_back(chunks); } + parsing_mutex.unlock(); + DEBUG("Number of suggestions"); + DEBUG_VAR(suggestions.size()); + return suggestions; } std::vector Source::Parser:: @@ -274,8 +263,6 @@ Source::Controller::Controller(const Source::Config &config, Source::Controller::~Controller() { parse_thread_stop=true; - parsing.lock(); //Be sure not to destroy while still parsing with libclang - parsing.unlock(); if(parse_thread.joinable()) parse_thread.join(); } @@ -370,10 +357,10 @@ void Source::Controller::OnOpenFile(const string &filepath) { parse_thread_go=false; parse_start(); } - else if (parse_thread_mapped && parsing.try_lock() && parse_thread_buffer_map_mutex.try_lock()) { + 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; - parsing.unlock(); + parser.parsing_mutex.unlock(); parse_thread_buffer_map_mutex.unlock(); parse_done(); } diff --git a/juci/source.h b/juci/source.h index a64499a..55a137c 100644 --- a/juci/source.h +++ b/juci/source.h @@ -98,6 +98,7 @@ namespace Source { public: Parser(const std::vector > &controllers): controllers(controllers) {} + ~Parser(); // inits the syntax highligthing on file open void InitSyntaxHighlighting(const std::string &filepath, const std::string &project_path, @@ -106,12 +107,7 @@ namespace Source { int start_offset, int end_offset, clang::Index *index); - void GetAutoCompleteSuggestions(const std::map - &buffers, - int line_number, - int column, - std::vector - *suggestions); + std::vector get_autocomplete_suggestions(int line_number, int column); int ReParse(const std::map &buffers); std::vector ExtractTokens(int, int); @@ -119,6 +115,7 @@ namespace Source { std::string project_path; static clang::Index clang_index; std::map get_buffer_map() const; + std::mutex parsing_mutex; private: std::unique_ptr tu_; //use unique_ptr since it is not initialized in constructor void HighlightToken(clang::Token *token, @@ -138,13 +135,8 @@ namespace Source { ~Controller(); void OnNewEmptyFile(); void OnOpenFile(const std::string &filename); - void GetAutoCompleteSuggestions(int line_number, - int column, - std::vector - *suggestions); Glib::RefPtr buffer(); bool OnKeyPress(GdkEventKey* key); - bool LegalExtension(std::string e); bool is_saved = false; //TODO: Is never set to false in Notebook::Controller bool is_changed = false; //TODO: Is never set to true @@ -155,7 +147,6 @@ namespace Source { private: void OnLineEdit(); void OnSaveFile(); - std::mutex parsing; Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; std::thread parse_thread; From 4355fcdfaba3c79d601dc6317b7947ec073f2993 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 18 Jun 2015 14:48:15 +0200 Subject: [PATCH 09/13] Abit more cleanup, and removal of an accidental path-paste in source.cc. --- juci/source.cc | 9 +-------- juci/source.h | 4 ---- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index 3e18221..97f04dc 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -88,7 +88,7 @@ SetTagTable(const std::unordered_map &tagtable) { tagtable_ = tagtable; } -bool Source::Config::legal_extension(std::string e) const {/Users/eidheim/test/jucipp/juci +bool Source::Config::legal_extension(std::string e) const { std::transform(e.begin(), e.end(),e.begin(), ::tolower); if (find(extensiontable_.begin(), extensiontable_.end(), e) != extensiontable_.end()) { DEBUG("Legal extension"); @@ -134,13 +134,6 @@ get_buffer_map() const { return buffer_map; } -// Source::View::UpdateLine -void Source::View:: -OnLineEdit(const std::vector &locations, - const Source::Config &config) { - OnUpdateSyntax(locations, config); -} - // Source::Model::UpdateLine int Source::Parser:: ReParse(const std::map &buffer) { diff --git a/juci/source.h b/juci/source.h index 55a137c..be372d6 100644 --- a/juci/source.h +++ b/juci/source.h @@ -66,8 +66,6 @@ namespace Source { public: View(); virtual ~View() { } - void OnLineEdit(const std::vector &locations, - const Config &config); void OnUpdateSyntax(const std::vector &locations, const Config &config); std::string GetLine(size_t line_number); @@ -145,8 +143,6 @@ namespace Source { View view; private: - void OnLineEdit(); - void OnSaveFile(); Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; std::thread parse_thread; From 6647377ab524a4a5a2040792602b78cf63ac32d9 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 18 Jun 2015 15:39:26 +0200 Subject: [PATCH 10/13] More cleanup: mainly, OnUpdateSyntax->update_syntax moved to Source::Controller. --- juci/notebook.cc | 8 ++++---- juci/source.cc | 26 ++++++++++++-------------- juci/source.h | 18 +++++++----------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 36c3c79..e0fd548 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -236,13 +236,13 @@ bool Notebook::Controller::GeneratePopup(int key_id) { for (auto &data : acdata) { std::stringstream ss; std::string return_value; - for (auto &chunk : data.chunks_) { - switch (chunk.kind()) { + for (auto &chunk : data.chunks) { + switch (chunk.kind) { case clang::CompletionChunk_ResultType: - return_value = chunk.chunk(); + return_value = chunk.chunk; break; case clang::CompletionChunk_Informative: break; - default: ss << chunk.chunk(); break; + default: ss << chunk.chunk; break; } } if (ss.str().length() > 0) { // if length is 0 the result is empty diff --git a/juci/source.cc b/juci/source.cc index 97f04dc..68d8e6d 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -260,20 +260,11 @@ Source::Controller::~Controller() { parse_thread.join(); } -void Source::Controller::OnNewEmptyFile() { - string filename("/tmp/juci_t"); - sourcefile s(filename); - parser.file_path=filename; - parser.project_path=filename; - s.save(""); -} - -void Source::View::OnUpdateSyntax(const std::vector &ranges, - const Source::Config &config) { +void Source::Controller::update_syntax(const std::vector &ranges) { if (ranges.empty() || ranges.size() == 0) { return; } - Glib::RefPtr buffer = get_buffer(); + 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()); @@ -298,6 +289,14 @@ void Source::View::OnUpdateSyntax(const std::vector &ranges, } } +void Source::Controller::OnNewEmptyFile() { + string filename("/tmp/juci_t"); + sourcefile s(filename); + parser.file_path=filename; + parser.project_path=filename; + s.save(""); +} + void Source::Controller::OnOpenFile(const string &filepath) { parser.file_path=filepath; sourcefile s(filepath); @@ -315,7 +314,7 @@ void Source::Controller::OnOpenFile(const string &filepath) { start_offset, end_offset, &Parser::clang_index); - view.OnUpdateSyntax(parser.ExtractTokens(start_offset, end_offset), config); + update_syntax(parser.ExtractTokens(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: @@ -331,8 +330,7 @@ void Source::Controller::OnOpenFile(const string &filepath) { parse_done.connect([this](){ if(parse_thread_mapped) { INFO("Updating syntax"); - view. - OnUpdateSyntax(parser.ExtractTokens(0, buffer()->get_text().size()), config); + update_syntax(parser.ExtractTokens(0, buffer()->get_text().size())); INFO("Syntax updated"); } else { diff --git a/juci/source.h b/juci/source.h index be372d6..6c5e614 100644 --- a/juci/source.h +++ b/juci/source.h @@ -66,28 +66,23 @@ namespace Source { public: View(); virtual ~View() { } - void OnUpdateSyntax(const std::vector &locations, - const Config &config); std::string GetLine(size_t line_number); std::string GetLineBeforeInsert(); }; // 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_; + explicit AutoCompleteChunk(const clang::CompletionChunk &clang_chunk) : + chunk(clang_chunk.chunk()), kind(clang_chunk.kind()) { } + std::string chunk; + enum clang::CompletionChunkKind kind; }; class AutoCompleteData { public: explicit AutoCompleteData(const std::vector &chunks) : - chunks_(chunks) { } - std::vector chunks_; + chunks(chunks) { } + std::vector chunks; }; class Controller; @@ -131,6 +126,7 @@ namespace Source { Controller(const Source::Config &config, const std::vector > &controllers); ~Controller(); + void update_syntax(const std::vector &locations); void OnNewEmptyFile(); void OnOpenFile(const std::string &filename); Glib::RefPtr buffer(); From 541135cc2d4128007b01cf3da0cccbd425a613a3 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 19 Jun 2015 10:42:25 +0200 Subject: [PATCH 11/13] Finished cleaning up source.h/cc for now I think. --- juci/config.cc | 6 +- juci/notebook.cc | 4 +- juci/source.cc | 150 +++++++++++++++-------------------------------- juci/source.h | 62 +++++++------------- 4 files changed, 73 insertions(+), 149 deletions(-) diff --git a/juci/config.cc b/juci/config.cc index eaf4d62..6ba32b7 100644 --- a/juci/config.cc +++ b/juci/config.cc @@ -39,13 +39,13 @@ void MainConfig::GenerateSource() { source_cfg.tab+=" "; } for (auto &i : colors_json) { - source_cfg.InsertTag(i.first, i.second.get_value()); + source_cfg.tags[i.first]=i.second.get_value(); } for (auto &i : syntax_json) { - source_cfg.InsertType(i.first, i.second.get_value()); + source_cfg.types[i.first]=i.second.get_value(); } for (auto &i : extensions_json) { - source_cfg.InsertExtension(i.second.get_value()); + source_cfg.extensions.emplace_back(i.second.get_value()); } DEBUG("Source cfg fetched"); } diff --git a/juci/notebook.cc b/juci/notebook.cc index e0fd548..c4217e8 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -301,7 +301,7 @@ Gtk::Box& Notebook::Controller::entry_view() { void Notebook::Controller::OnNewPage(std::string name) { INFO("Notebook Generate new page"); OnCreatePage(); - text_vec_.back()->OnNewEmptyFile(); + text_vec_.back()->on_new_empty_file(); Notebook().append_page(*editor_vec_.back(), name); Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); @@ -312,7 +312,7 @@ void Notebook::Controller::OnNewPage(std::string name) { void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); OnCreatePage(); - text_vec_.back()->OnOpenFile(path); + text_vec_.back()->on_open_file(path); text_vec_.back()->is_saved=true; unsigned pos = path.find_last_of("/\\"); Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); diff --git a/juci/source.cc b/juci/source.cc index 68d8e6d..9183da4 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -7,22 +7,15 @@ #include #include -Source::Location:: -Location(int line_number, int column_offset) : - line_number_(line_number), column_offset_(column_offset) { } - -Source::Location:: -Location(const Source::Location &org) : - line_number_(org.line_number_), column_offset_(org.column_offset_) { } - -Source::Range:: -Range(const Location &start, const Location &end, int kind) : - start_(start), end_(end), kind_(kind) { } - -Source::Range:: -Range(const Source::Range &org) : - start_(org.start_), end_(org.end_), kind_(org.kind_) { } - +bool Source::Config::legal_extension(std::string e) const { + std::transform(e.begin(), e.end(),e.begin(), ::tolower); + if (find(extensions.begin(), extensions.end(), e) != extensions.end()) { + DEBUG("Legal extension"); + return true; + } + DEBUG("Ilegal extension"); + return false; +} ////////////// //// View //// @@ -31,7 +24,7 @@ Source::View::View() { Gsv::init(); } -string Source::View::GetLine(size_t line_number) { +string Source::View::get_line(size_t line_number) { Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_number); Gtk::TextIter line_end_it = line_it; while(!line_end_it.ends_line()) @@ -40,64 +33,13 @@ string Source::View::GetLine(size_t line_number) { return line; } -string Source::View::GetLineBeforeInsert() { +string Source::View::get_line_before_insert() { 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()); std::string line(get_source_buffer()->get_text(line_it, insert_it)); return line; } -// Source::View::Config::tagtable() -// returns a const refrence to the tagtable -const std::unordered_map& Source::Config::tagtable() const { - return tagtable_; -} - -// Source::View::Config::tagtable() -// returns a const refrence to the tagtable -const std::unordered_map& Source::Config::typetable() const { - return typetable_; -} - -std::vector& Source::Config::extensiontable(){ - return extensiontable_; -} - -void Source::Config::InsertTag(const string &key, const string &value) { - tagtable_[key] = value; -} - -void Source::Config::InsertExtension(const string &ext) { - extensiontable_.push_back(ext); -} - -// Source::View::Config::SetTagTable() -// sets the tagtable for the view -void Source::Config:: -SetTypeTable(const std::unordered_map &typetable) { - typetable_ = typetable; -} - -void Source::Config::InsertType(const string &key, const string &value) { - typetable_[key] = value; -} -// Source::View::Config::SetTagTable() -// sets the tagtable for the view -void Source::Config:: -SetTagTable(const std::unordered_map &tagtable) { - tagtable_ = tagtable; -} - -bool Source::Config::legal_extension(std::string e) const { - std::transform(e.begin(), e.end(),e.begin(), ::tolower); - if (find(extensiontable_.begin(), extensiontable_.end(), e) != extensiontable_.end()) { - DEBUG("Legal extension"); - return true; - } - DEBUG("Ilegal extension"); - return false; -} - /////////////// //// Parser /// /////////////// @@ -109,7 +51,7 @@ Source::Parser::~Parser() { } void Source::Parser:: -InitSyntaxHighlighting(const std::string &filepath, +init_syntax_highlighting(const std::string &filepath, const std::string &project_path, const std::map &buffers, @@ -136,7 +78,7 @@ get_buffer_map() const { // Source::Model::UpdateLine int Source::Parser:: -ReParse(const std::map &buffer) { +reparse(const std::map &buffer) { return tu_->ReparseTranslationUnit(file_path, buffer); } @@ -182,7 +124,7 @@ get_compilation_commands() { } std::vector Source::Parser:: -ExtractTokens(int start_offset, int end_offset) { +extract_tokens(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); @@ -191,18 +133,18 @@ ExtractTokens(int start_offset, int end_offset) { std::vector tks = tokens.tokens(); for (auto &token : tks) { switch (token.kind()) { - case 0: HighlightCursor(&token, &ranges); break; // PunctuationToken - case 1: HighlightToken(&token, &ranges, 702); break; // KeywordToken - case 2: HighlightCursor(&token, &ranges); break; // IdentifierToken - case 3: HighlightToken(&token, &ranges, 109); break; // LiteralToken - case 4: HighlightToken(&token, &ranges, 705); break; // CommentToken + case 0: highlight_cursor(&token, &ranges); break; // PunctuationToken + case 1: highlight_token(&token, &ranges, 702); break; // KeywordToken + case 2: highlight_cursor(&token, &ranges); break; // IdentifierToken + case 3: highlight_token(&token, &ranges, 109); break; // LiteralToken + case 4: highlight_token(&token, &ranges, 705); break; // CommentToken } } return ranges; } void Source::Parser:: -HighlightCursor(clang::Token *token, +highlight_cursor(clang::Token *token, std::vector *source_ranges) { clang::SourceLocation location = token->get_source_location(tu_.get()); clang::Cursor cursor(tu_.get(), &location); @@ -218,7 +160,7 @@ HighlightCursor(clang::Token *token, end_offset), (int) cursor.kind()); } void Source::Parser:: -HighlightToken(clang::Token *token, +highlight_token(clang::Token *token, std::vector *source_ranges, int token_kind) { clang::SourceRange range = token->get_source_range(tu_.get()); @@ -243,13 +185,13 @@ 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::OnKeyPress), false); + 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.tagtable()) { + for (auto &item : config.tags) { buffer()->create_tag(item.first)->property_foreground() = item.second; } } @@ -267,16 +209,16 @@ void Source::Controller::update_syntax(const std::vector &ranges) 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()); + std::string type = std::to_string(range.kind); try { - config.typetable().at(type); + 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; + 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; @@ -284,12 +226,12 @@ void Source::Controller::update_syntax(const std::vector &ranges) 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.typetable().at(type), + buffer->apply_tag_by_name(config.types.at(type), begin_iter, end_iter); } } -void Source::Controller::OnNewEmptyFile() { +void Source::Controller::on_new_empty_file() { string filename("/tmp/juci_t"); sourcefile s(filename); parser.file_path=filename; @@ -297,7 +239,7 @@ void Source::Controller::OnNewEmptyFile() { s.save(""); } -void Source::Controller::OnOpenFile(const string &filepath) { +void Source::Controller::on_open_file(const string &filepath) { parser.file_path=filepath; sourcefile s(filepath); auto buffer_map=parser.get_buffer_map(); @@ -308,13 +250,13 @@ void Source::Controller::OnOpenFile(const string &filepath) { 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.InitSyntaxHighlighting(filepath, + 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.ExtractTokens(start_offset, end_offset)); + 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: @@ -330,7 +272,7 @@ void Source::Controller::OnOpenFile(const string &filepath) { parse_done.connect([this](){ if(parse_thread_mapped) { INFO("Updating syntax"); - update_syntax(parser.ExtractTokens(0, buffer()->get_text().size())); + update_syntax(parser.extract_tokens(0, buffer()->get_text().size())); INFO("Syntax updated"); } else { @@ -342,14 +284,14 @@ void Source::Controller::OnOpenFile(const string &filepath) { 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_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); + parser.reparse(this->parse_thread_buffer_map); parse_thread_go=false; parser.parsing_mutex.unlock(); parse_thread_buffer_map_mutex.unlock(); @@ -369,7 +311,9 @@ Glib::RefPtr Source::Controller::buffer() { return view.get_source_buffer(); } -bool Source::Controller::OnKeyPress(GdkEventKey* key) { +//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) *$"); @@ -377,7 +321,7 @@ 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.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()+"}"); @@ -399,7 +343,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) { std::smatch sm2; size_t line_nr=buffer()->get_insert()->get_iter().get_line(); if(line_nr>0 && sm[1].str().size()>=config.tab_size) { - string previous_line=view.GetLine(line_nr-1); + string previous_line=view.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()); @@ -438,7 +382,7 @@ 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); + string line=view.get_line(line_nr); if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) return true; } @@ -455,7 +399,7 @@ 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(); + string line=view.get_line_before_insert(); if(line.size()>=config.tab_size) { for(auto c: line) { if(c!=' ') @@ -476,8 +420,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.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]) { diff --git a/juci/source.h b/juci/source.h index 6c5e614..86e38d5 100644 --- a/juci/source.h +++ b/juci/source.h @@ -18,56 +18,36 @@ namespace Notebook { namespace Source { class Config { public: - const std::unordered_map& tagtable() const; - const std::unordered_map& typetable() const; - std::vector& extensiontable(); - 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); - void InsertExtension(const std::string &ext); - std::vector extensiontable_; bool legal_extension(std::string e) const ; - // TODO: Have to clean away all the simple setter and getter methods at some point. It creates too much unnecessary code unsigned tab_size; bool show_line_numbers, highlight_current_line; std::string tab, background, font; - private: - std::unordered_map tagtable_, typetable_; + std::vector extensions; + std::unordered_map tags, types; }; // class Config class Location { public: - Location(const Location &location); - Location(int line_number, int column_offset); - int line_number() const { return line_number_; } - int column_offset() const { return column_offset_; } - private: - int line_number_; - int column_offset_; + Location(int line_number, int column_offset): + line_number(line_number), column_offset(column_offset) {} + int line_number; + int column_offset; }; class Range { public: - Range(const Location &start, const Location &end, int kind); - Range(const Range &org); - const Location& start() const { return start_; } - const Location& end() const { return end_; } - int kind() const { return kind_; } - private: - Location start_; - Location end_; - int kind_; + Range(const Location &start, const Location &end, int kind): + start(start), end(end), kind(kind) {} + Location start; + Location end; + int kind; }; class View : public Gsv::View { public: View(); - virtual ~View() { } - std::string GetLine(size_t line_number); - std::string GetLineBeforeInsert(); + std::string get_line(size_t line_number); + std::string get_line_before_insert(); }; // class View class AutoCompleteChunk { @@ -93,7 +73,7 @@ namespace Source { controllers(controllers) {} ~Parser(); // inits the syntax highligthing on file open - void InitSyntaxHighlighting(const std::string &filepath, + void init_syntax_highlighting(const std::string &filepath, const std::string &project_path, const std::map &buffers, @@ -101,8 +81,8 @@ namespace Source { int end_offset, clang::Index *index); std::vector get_autocomplete_suggestions(int line_number, int column); - int ReParse(const std::map &buffers); - std::vector ExtractTokens(int, int); + int reparse(const std::map &buffers); + std::vector extract_tokens(int, int); std::string file_path; std::string project_path; @@ -111,10 +91,10 @@ namespace Source { std::mutex parsing_mutex; private: std::unique_ptr tu_; //use unique_ptr since it is not initialized in constructor - void HighlightToken(clang::Token *token, + void highlight_token(clang::Token *token, std::vector *source_ranges, int token_kind); - void HighlightCursor(clang::Token *token, + 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 @@ -127,10 +107,10 @@ namespace Source { const std::vector > &controllers); ~Controller(); void update_syntax(const std::vector &locations); - void OnNewEmptyFile(); - void OnOpenFile(const std::string &filename); + void on_new_empty_file(); + void on_open_file(const std::string &filename); Glib::RefPtr buffer(); - bool OnKeyPress(GdkEventKey* key); + bool on_key_press(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 From 122fb853906c0ce9cc336608f37b4f3085ad8777 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 19 Jun 2015 12:01:50 +0200 Subject: [PATCH 12/13] Added mark to tab-label when content is changed and not saved. --- juci/notebook.cc | 39 ++++++++++++++++++++++++--------------- juci/source.cc | 12 +++++++----- juci/source.h | 5 +++-- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index c4217e8..2d4166f 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -313,9 +313,11 @@ void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); OnCreatePage(); text_vec_.back()->on_open_file(path); - text_vec_.back()->is_saved=true; - unsigned pos = path.find_last_of("/\\"); - Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); + size_t pos = path.find_last_of("/\\"); + std::string filename=path; + if(pos!=std::string::npos) + filename=path.substr(pos+1); + 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); @@ -329,12 +331,23 @@ void Notebook::Controller::OnCreatePage() { scrolledtext_vec_.back()->add(text_vec_.back()->view); editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); TextViewHandlers(text_vec_.back()->view); + //Add star on tab label when the page is not saved: + text_vec_.back()->signal_buffer_changed=[this](bool was_saved) { + if(was_saved) { + std::string path=text_vec_.at(CurrentPage())->parser.file_path; + size_t pos = path.find_last_of("/\\"); + std::string filename=path; + if(pos!=std::string::npos) + filename=path.substr(pos+1); + Notebook().set_tab_label_text(*Notebook().get_nth_page(CurrentPage()), filename+"*"); + } + }; } void Notebook::Controller::OnCloseCurrentPage() { INFO("Notebook close page"); if (Pages() != 0) { - if(text_vec_.back()->is_changed){ + if(!text_vec_.back()->is_saved){ AskToSaveDialog(); } int page = CurrentPage(); @@ -570,17 +583,8 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, } bool Notebook::Controller:: OnSaveFile() { - INFO("Notebook save file"); - if (text_vec_.at(CurrentPage())->is_saved) { - std::ofstream file; - file.open (text_vec_.at(CurrentPage())->parser.file_path); - file << CurrentTextView().get_buffer()->get_text(); - file.close(); - return true; - } else { - return OnSaveFile(OnSaveFileAs()); - } - return false; + std::string path=text_vec_.at(CurrentPage())->parser.file_path; + return OnSaveFile(path); } bool Notebook::Controller:: OnSaveFile(std::string path) { INFO("Notebook save file with path"); @@ -590,6 +594,11 @@ bool Notebook::Controller:: OnSaveFile(std::string path) { file << CurrentTextView().get_buffer()->get_text(); file.close(); text_vec_.at(CurrentPage())->parser.file_path=path; + size_t pos = path.find_last_of("/\\"); + std::string filename=path; + if(pos!=std::string::npos) + filename=path.substr(pos+1); + Notebook().set_tab_label_text(*Notebook().get_nth_page(CurrentPage()), filename); text_vec_.at(CurrentPage())->is_saved=true; return true; } diff --git a/juci/source.cc b/juci/source.cc index 9183da4..6fa2660 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -299,12 +299,14 @@ void Source::Controller::on_open_file(const string &filepath) { } } }); - - buffer()->signal_changed().connect([this]() { - parse_thread_mapped=false; - parse_thread_go=true; - }); } + 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; + }); } Glib::RefPtr Source::Controller::buffer() { diff --git a/juci/source.h b/juci/source.h index 86e38d5..99ede6b 100644 --- a/juci/source.h +++ b/juci/source.h @@ -112,12 +112,13 @@ namespace Source { Glib::RefPtr buffer(); bool on_key_press(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 + bool is_saved = true; Parser parser; View view; + std::function signal_buffer_changed; + private: Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; From a2f68575c98e23fa8bf08d3bc98b61416bb9321d Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 19 Jun 2015 12:33:43 +0200 Subject: [PATCH 13/13] Small fix, parsing of source now also starts right after the file is opened (in the background). --- juci/source.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index 6fa2660..cd9eab4 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -194,6 +194,13 @@ Source::Controller::Controller(const Source::Config &config, for (auto &item : config.tags) { buffer()->create_tag(item.first)->property_foreground() = item.second; } + buffer()->signal_changed().connect([this]() { + if(signal_buffer_changed) + signal_buffer_changed(is_saved); + is_saved=false; + parse_thread_mapped=false; + parse_thread_go=true; + }); } Source::Controller::~Controller() { @@ -232,7 +239,7 @@ void Source::Controller::update_syntax(const std::vector &ranges) } void Source::Controller::on_new_empty_file() { - string filename("/tmp/juci_t"); + string filename("/tmp/untitled"); sourcefile s(filename); parser.file_path=filename; parser.project_path=filename; @@ -246,6 +253,7 @@ void Source::Controller::on_open_file(const string &filepath) { buffer_map[filepath] = s.get_content(); buffer()->get_undo_manager()->begin_not_undoable_action(); buffer()->set_text(s.get_content()); + is_saved=true; buffer()->get_undo_manager()->end_not_undoable_action(); int start_offset = buffer()->begin().get_offset(); int end_offset = buffer()->end().get_offset(); @@ -300,13 +308,6 @@ void Source::Controller::on_open_file(const string &filepath) { } }); } - 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; - }); } Glib::RefPtr Source::Controller::buffer() {