diff --git a/src/source_clang.cc b/src/source_clang.cc index ad5e596..b2f763b 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -36,10 +36,10 @@ Source::View(file_path, project_path, language), parse_error(false) { //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_connection=parse_start.connect([this]{ - if(parse_thread_buffer_map_mutex.try_lock()) { - parse_thread_buffer_map=get_buffer_map(); + if(parse_thread_buffer_mutex.try_lock()) { + parse_thread_buffer=get_buffer()->get_text(); parse_thread_mapped=true; - parse_thread_buffer_map_mutex.unlock(); + parse_thread_buffer_mutex.unlock(); } parse_thread_go=true; }); @@ -113,22 +113,21 @@ void Source::ClangViewParse::init_parse() { parse_thread_mapped=false; parse_thread_stop=false; - auto buffer_map=get_buffer_map(); + auto buffer=get_buffer()->get_text(); //Remove includes for first parse for initial syntax highlighting - auto& str=buffer_map[file_path.string()]; std::size_t pos=0; - while((pos=str.find("#include", pos))!=std::string::npos) { + while((pos=buffer.find("#include", pos))!=std::string::npos) { auto start_pos=pos; - pos=str.find('\n', pos+8); + pos=buffer.find('\n', pos+8); if(pos==std::string::npos) break; - if(start_pos==0 || str[start_pos-1]=='\n') { - str.replace(start_pos, pos-start_pos, pos-start_pos, ' '); + if(start_pos==0 || buffer[start_pos-1]=='\n') { + buffer.replace(start_pos, pos-start_pos, pos-start_pos, ' '); } pos++; } - clang_tu = std::unique_ptr(new clang::TranslationUnit(clang_index, file_path.string(), get_compilation_commands(), buffer_map)); - clang_tokens=clang_tu->get_tokens(0, buffer_map.find(file_path.string())->second.size()-1); + clang_tu = std::unique_ptr(new clang::TranslationUnit(clang_index, file_path.string(), get_compilation_commands(), buffer.raw())); + clang_tokens=clang_tu->get_tokens(0, buffer.bytes()-1); update_syntax(); set_status("parsing..."); @@ -144,15 +143,15 @@ void Source::ClangViewParse::init_parse() { parse_thread_go=false; parse_start(); } - else if (parse_thread_mapped && parsing_mutex.try_lock() && parse_thread_buffer_map_mutex.try_lock()) { - auto status=clang_tu->ReparseTranslationUnit(parse_thread_buffer_map); + else if (parse_thread_mapped && parsing_mutex.try_lock() && parse_thread_buffer_mutex.try_lock()) { + auto status=clang_tu->ReparseTranslationUnit(parse_thread_buffer.raw()); if(status==0) - clang_tokens=clang_tu->get_tokens(0, parse_thread_buffer_map.find(file_path.string())->second.size()-1); + clang_tokens=clang_tu->get_tokens(0, parse_thread_buffer.bytes()-1); else parse_error=true; parse_thread_go=false; parsing_mutex.unlock(); - parse_thread_buffer_map_mutex.unlock(); + parse_thread_buffer_mutex.unlock(); if(status!=0) { parse_fail(); parse_thread_stop=true; @@ -164,12 +163,6 @@ void Source::ClangViewParse::init_parse() { }); } -std::map Source::ClangViewParse::get_buffer_map() const { - std::map buffer_map; - buffer_map[file_path.string()]=get_source_buffer()->get_text(); - return buffer_map; -} - void Source::ClangViewParse::soft_reparse() { parse_thread_mapped=false; parsed=false; @@ -807,25 +800,24 @@ void Source::ClangViewAutocomplete::autocomplete() { } }); - std::shared_ptr > buffer_map=std::make_shared >(); - auto ustr=get_buffer()->get_text(); + set_status("autocomplete..."); + if(autocomplete_thread.joinable()) + autocomplete_thread.join(); + auto buffer=std::make_shared(get_buffer()->get_text()); auto iter=get_buffer()->get_insert()->get_iter(); auto line_nr=iter.get_line()+1; auto column_nr=iter.get_line_offset()+1; auto pos=iter.get_offset()-1; - while(pos>=0 && ((ustr[pos]>='a' && ustr[pos]<='z') || (ustr[pos]>='A' && ustr[pos]<='Z') || (ustr[pos]>='0' && ustr[pos]<='9') || ustr[pos]=='_')) { - ustr.replace(pos, 1, " "); + while(pos>=0 && (((*buffer)[pos]>='a' && (*buffer)[pos]<='z') || ((*buffer)[pos]>='A' && (*buffer)[pos]<='Z') || + ((*buffer)[pos]>='0' && (*buffer)[pos]<='9') || (*buffer)[pos]=='_')) { + buffer->replace(pos, 1, " "); column_nr--; pos--; } - (*buffer_map)[this->file_path.string()]=std::move(ustr); //TODO: does this work? - set_status("autocomplete..."); - if(autocomplete_thread.joinable()) - autocomplete_thread.join(); - autocomplete_thread=std::thread([this, ac_data, line_nr, column_nr, buffer_map](){ + autocomplete_thread=std::thread([this, ac_data, line_nr, column_nr, buffer](){ parsing_mutex.lock(); if(!parse_thread_stop) - *ac_data=get_autocomplete_suggestions(line_nr, column_nr, *buffer_map); + *ac_data=get_autocomplete_suggestions(buffer->raw(), line_nr, column_nr); if(!parse_thread_stop) autocomplete_done(); else @@ -835,9 +827,9 @@ void Source::ClangViewAutocomplete::autocomplete() { } } -std::vector Source::ClangViewAutocomplete::get_autocomplete_suggestions(int line_number, int column, std::map& buffer_map) { +std::vector Source::ClangViewAutocomplete::get_autocomplete_suggestions(const std::string &buffer, int line_number, int column) { std::vector suggestions; - auto results=clang_tu->get_code_completions(buffer_map, line_number, column); + auto results=clang_tu->get_code_completions(buffer, line_number, column); if(results.cx_results==NULL) { parse_thread_stop=true; return suggestions; diff --git a/src/source_clang.h b/src/source_clang.h index 8ed8157..2e6a4d0 100644 --- a/src/source_clang.h +++ b/src/source_clang.h @@ -53,7 +53,6 @@ namespace Source { sigc::connection parse_start_connection; sigc::connection parse_fail_connection; private: - std::map get_buffer_map() const; void update_syntax(); std::set last_syntax_tags; void update_diagnostics(); @@ -64,8 +63,8 @@ namespace Source { Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; Glib::Dispatcher parse_fail; - std::map parse_thread_buffer_map; - std::mutex parse_thread_buffer_map_mutex; + Glib::ustring parse_thread_buffer; + std::mutex parse_thread_buffer_mutex; std::atomic parse_thread_go; std::atomic parse_thread_mapped; }; @@ -96,7 +95,7 @@ namespace Source { void autocomplete(); std::unique_ptr completion_dialog; bool completion_dialog_shown=false; - std::vector get_autocomplete_suggestions(int line_number, int column, std::map& buffer_map); + std::vector get_autocomplete_suggestions(const std::string &buffer, int line_number, int column); Glib::Dispatcher autocomplete_done; Glib::Dispatcher autocomplete_fail; bool autocomplete_starting=false;