From 9d3cda41992ab23361d0093a8d98f8d127f15e45 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 18 Jun 2015 14:37:32 +0200 Subject: [PATCH] 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;