Browse Source

More code cleanup: get_autocomplete_suggestions (previously GetAutoCompleteSuggestions) now only in Source::Parser class.

merge-requests/365/head
eidheim 11 years ago
parent
commit
9d3cda4199
  1. 8
      juci/notebook.cc
  2. 53
      juci/source.cc
  3. 15
      juci/source.h

8
juci/notebook.cc

@ -229,11 +229,9 @@ bool Notebook::Controller::GeneratePopup(int key_id) {
return false; return false;
} }
INFO("Notebook genereate popup, getting autocompletions"); INFO("Notebook genereate popup, getting autocompletions");
std::vector<Source::AutoCompleteData> acdata; std::vector<Source::AutoCompleteData> acdata=text_vec_.at(CurrentPage())->parser.
text_vec_.at(CurrentPage())-> get_autocomplete_suggestions(beg.get_line()+1,
GetAutoCompleteSuggestions(beg.get_line()+1, beg.get_line_offset()+2);
beg.get_line_offset()+2,
&acdata);
std::map<std::string, std::string> items; std::map<std::string, std::string> items;
for (auto &data : acdata) { for (auto &data : acdata) {
std::stringstream ss; std::stringstream ss;

53
juci/source.cc

@ -88,7 +88,7 @@ SetTagTable(const std::unordered_map<string, string> &tagtable) {
tagtable_ = 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); std::transform(e.begin(), e.end(),e.begin(), ::tolower);
if (find(extensiontable_.begin(), extensiontable_.end(), e) != extensiontable_.end()) { if (find(extensiontable_.begin(), extensiontable_.end(), e) != extensiontable_.end()) {
DEBUG("Legal extension"); DEBUG("Legal extension");
@ -103,6 +103,11 @@ bool Source::Config::legal_extension(std::string e) const {
/////////////// ///////////////
clang::Index Source::Parser::clang_index(0, 1); 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:: void Source::Parser::
InitSyntaxHighlighting(const std::string &filepath, InitSyntaxHighlighting(const std::string &filepath,
const std::string &project_path, const std::string &project_path,
@ -142,36 +147,16 @@ ReParse(const std::map<std::string, std::string> &buffer) {
return tu_->ReparseTranslationUnit(file_path, buffer); return tu_->ReparseTranslationUnit(file_path, buffer);
} }
std::vector<Source::AutoCompleteData> Source::Parser::
// Source::Controller::OnLineEdit() get_autocomplete_suggestions(int line_number,
// fired when a line in the buffer is edited int column) {
void Source::Controller::OnLineEdit() { }
void Source::Controller::
GetAutoCompleteSuggestions(int line_number,
int column,
std::vector<Source::AutoCompleteData>
*suggestions) {
INFO("Getting auto complete suggestions"); INFO("Getting auto complete suggestions");
parsing.lock(); std::vector<Source::AutoCompleteData> suggestions;
parser.GetAutoCompleteSuggestions(parser.get_buffer_map(), auto buffer_map=get_buffer_map();
line_number, parsing_mutex.lock();
column,
suggestions);
DEBUG("Number of suggestions");
DEBUG_VAR(suggestions->size());
parsing.unlock();
}
void Source::Parser::
GetAutoCompleteSuggestions(const std::map<std::string, std::string> &buffers,
int line_number,
int column,
std::vector<Source::AutoCompleteData>
*suggestions) {
clang::CodeCompleteResults results(tu_.get(), clang::CodeCompleteResults results(tu_.get(),
file_path, file_path,
buffers, buffer_map,
line_number, line_number,
column); column);
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
@ -180,8 +165,12 @@ GetAutoCompleteSuggestions(const std::map<std::string, std::string> &buffers,
for (auto &chunk : chunks_) { for (auto &chunk : chunks_) {
chunks.emplace_back(chunk); 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<std::string> Source::Parser:: std::vector<std::string> Source::Parser::
@ -274,8 +263,6 @@ Source::Controller::Controller(const Source::Config &config,
Source::Controller::~Controller() { Source::Controller::~Controller() {
parse_thread_stop=true; parse_thread_stop=true;
parsing.lock(); //Be sure not to destroy while still parsing with libclang
parsing.unlock();
if(parse_thread.joinable()) if(parse_thread.joinable())
parse_thread.join(); parse_thread.join();
} }
@ -370,10 +357,10 @@ void Source::Controller::OnOpenFile(const string &filepath) {
parse_thread_go=false; parse_thread_go=false;
parse_start(); 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); parser.ReParse(this->parse_thread_buffer_map);
parse_thread_go=false; parse_thread_go=false;
parsing.unlock(); parser.parsing_mutex.unlock();
parse_thread_buffer_map_mutex.unlock(); parse_thread_buffer_map_mutex.unlock();
parse_done(); parse_done();
} }

15
juci/source.h

@ -98,6 +98,7 @@ namespace Source {
public: public:
Parser(const std::vector<std::unique_ptr<Source::Controller> > &controllers): Parser(const std::vector<std::unique_ptr<Source::Controller> > &controllers):
controllers(controllers) {} controllers(controllers) {}
~Parser();
// inits the syntax highligthing on file open // inits the syntax highligthing on file open
void InitSyntaxHighlighting(const std::string &filepath, void InitSyntaxHighlighting(const std::string &filepath,
const std::string &project_path, const std::string &project_path,
@ -106,12 +107,7 @@ namespace Source {
int start_offset, int start_offset,
int end_offset, int end_offset,
clang::Index *index); clang::Index *index);
void GetAutoCompleteSuggestions(const std::map<std::string, std::string> std::vector<Source::AutoCompleteData> get_autocomplete_suggestions(int line_number, int column);
&buffers,
int line_number,
int column,
std::vector<AutoCompleteData>
*suggestions);
int ReParse(const std::map<std::string, std::string> &buffers); int ReParse(const std::map<std::string, std::string> &buffers);
std::vector<Range> ExtractTokens(int, int); std::vector<Range> ExtractTokens(int, int);
@ -119,6 +115,7 @@ namespace Source {
std::string project_path; std::string project_path;
static clang::Index clang_index; static clang::Index clang_index;
std::map<std::string, std::string> get_buffer_map() const; std::map<std::string, std::string> get_buffer_map() const;
std::mutex parsing_mutex;
private: private:
std::unique_ptr<clang::TranslationUnit> tu_; //use unique_ptr since it is not initialized in constructor std::unique_ptr<clang::TranslationUnit> tu_; //use unique_ptr since it is not initialized in constructor
void HighlightToken(clang::Token *token, void HighlightToken(clang::Token *token,
@ -138,13 +135,8 @@ namespace Source {
~Controller(); ~Controller();
void OnNewEmptyFile(); void OnNewEmptyFile();
void OnOpenFile(const std::string &filename); void OnOpenFile(const std::string &filename);
void GetAutoCompleteSuggestions(int line_number,
int column,
std::vector<AutoCompleteData>
*suggestions);
Glib::RefPtr<Gsv::Buffer> buffer(); Glib::RefPtr<Gsv::Buffer> buffer();
bool OnKeyPress(GdkEventKey* key); bool OnKeyPress(GdkEventKey* key);
bool LegalExtension(std::string e);
bool is_saved = false; //TODO: Is never set to false in Notebook::Controller 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_changed = false; //TODO: Is never set to true
@ -155,7 +147,6 @@ namespace Source {
private: private:
void OnLineEdit(); void OnLineEdit();
void OnSaveFile(); void OnSaveFile();
std::mutex parsing;
Glib::Dispatcher parse_done; Glib::Dispatcher parse_done;
Glib::Dispatcher parse_start; Glib::Dispatcher parse_start;
std::thread parse_thread; std::thread parse_thread;

Loading…
Cancel
Save