Browse Source

Autocomplete is now run without threads in GenericView

pipelines/353213535
eidheim 4 years ago
parent
commit
a4db3cc621
  1. 31
      src/autocomplete.cpp
  2. 6
      src/autocomplete.hpp
  3. 2
      src/source_clang.cpp
  4. 27
      src/source_generic.cpp
  5. 1
      src/source_generic.hpp
  6. 2
      src/source_language_protocol.cpp

31
src/autocomplete.cpp

@ -1,8 +1,8 @@
#include "autocomplete.hpp"
#include "selection_dialog.hpp"
Autocomplete::Autocomplete(Source::BaseView *view, bool &interactive_completion, guint &last_keyval, bool pass_buffer_and_strip_word)
: view(view), interactive_completion(interactive_completion), pass_buffer_and_strip_word(pass_buffer_and_strip_word) {
Autocomplete::Autocomplete(Source::BaseView *view, bool &interactive_completion, guint &last_keyval, bool pass_buffer_and_strip_word, bool use_thread)
: view(view), interactive_completion(interactive_completion), pass_buffer_and_strip_word(pass_buffer_and_strip_word), use_thread(use_thread) {
view->get_buffer()->signal_changed().connect([this, &last_keyval] {
if(CompletionDialog::get() && CompletionDialog::get()->is_visible()) {
cancel_reparse();
@ -56,7 +56,7 @@ void Autocomplete::run() {
before_add_rows();
if(thread.joinable())
if(use_thread && thread.joinable())
thread.join();
auto iter = view->get_buffer()->get_insert()->get_iter();
auto line = iter.get_line();
@ -71,7 +71,8 @@ void Autocomplete::run() {
pos--;
}
}
thread = std::thread([this, line, line_index, buffer = std::move(buffer)] {
auto func = [this, line, line_index, buffer = std::move(buffer)] {
auto lock = get_parse_lock();
if(!is_processing())
return;
@ -84,7 +85,7 @@ void Autocomplete::run() {
return;
if(success) {
dispatcher.post([this]() {
auto func = [this]() {
after_add_rows();
if(state == State::restarting) {
state = State::idle;
@ -118,15 +119,27 @@ void Autocomplete::run() {
view->get_buffer()->begin_user_action();
CompletionDialog::get()->show();
}
});
};
if(use_thread)
dispatcher.post(std::move(func));
else
func();
}
else {
dispatcher.post([this] {
auto func = [this] {
state = State::canceled;
on_add_rows_error();
});
};
if(use_thread)
dispatcher.post(std::move(func));
else
func();
}
});
};
if(use_thread)
thread = std::thread(std::move(func));
else
func();
}
if(state != State::idle)

6
src/autocomplete.hpp

@ -47,8 +47,7 @@ public:
std::function<void()> after_add_rows = [] {};
std::function<void()> on_add_rows_error = [] {};
/// The handler is not run in the main loop. Should return false on error.
/// Column is line byte index.
/// The handler is not run in the main loop if use_thread is true. Should return false on error.
std::function<bool(std::string &buffer, int line, int line_index)> add_rows = [](std::string &, int, int) { return true; };
std::function<void()> on_show = [] {};
@ -58,11 +57,12 @@ public:
std::function<std::function<void(Tooltip &tooltip)>(unsigned int)> set_tooltip_buffer = [](unsigned int index) { return nullptr; };
Autocomplete(Source::BaseView *view, bool &interactive_completion, guint &last_keyval, bool pass_buffer_and_strip_word);
Autocomplete(Source::BaseView *view, bool &interactive_completion, guint &last_keyval, bool pass_buffer_and_strip_word, bool use_thread);
void run();
void stop();
private:
void setup_dialog();
bool use_thread;
};

2
src/source_clang.cpp

@ -808,7 +808,7 @@ void Source::ClangViewParse::remove_internal_namespaces(std::string &type) {
Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::path &file_path, const Glib::RefPtr<Gsv::Language> &language)
: BaseView(file_path, language), Source::ClangViewParse(file_path, language), autocomplete(this, interactive_completion, last_keyval, true) {
: BaseView(file_path, language), Source::ClangViewParse(file_path, language), autocomplete(this, interactive_completion, last_keyval, true, true) {
non_interactive_completion = [this] {
if(CompletionDialog::get() && CompletionDialog::get()->is_visible())
return;

27
src/source_generic.cpp

@ -8,7 +8,7 @@
#include <algorithm>
#include <boost/algorithm/string.hpp>
Source::GenericView::GenericView(const boost::filesystem::path &file_path, const Glib::RefPtr<Gsv::Language> &language) : BaseView(file_path, language), View(file_path, language, true), autocomplete(this, interactive_completion, last_keyval, false) {
Source::GenericView::GenericView(const boost::filesystem::path &file_path, const Glib::RefPtr<Gsv::Language> &language) : BaseView(file_path, language), View(file_path, language, true), autocomplete(this, interactive_completion, last_keyval, false, false) {
if(language) {
auto language_manager = LanguageManager::get_default();
auto search_paths = language_manager->get_search_path();
@ -41,12 +41,6 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const
setup_autocomplete();
}
Source::GenericView::~GenericView() {
autocomplete.state = Autocomplete::State::idle;
if(autocomplete.thread.joinable())
autocomplete.thread.join();
}
void Source::GenericView::parse_language_file(const boost::property_tree::ptree &pt) {
bool case_insensitive = false;
for(auto &node : pt) {
@ -188,11 +182,6 @@ void Source::GenericView::setup_autocomplete() {
autocomplete.run();
};
autocomplete.reparse = [this] {
autocomplete_comment.clear();
autocomplete_insert.clear();
};
autocomplete.run_check = [this]() {
auto prefix_start = get_buffer()->get_insert()->get_iter();
auto prefix_end = prefix_start;
@ -221,19 +210,7 @@ void Source::GenericView::setup_autocomplete() {
return false;
};
autocomplete.before_add_rows = [this] {
status_state = "autocomplete...";
if(update_status_state)
update_status_state(this);
};
autocomplete.after_add_rows = [this] {
status_state = "";
if(update_status_state)
update_status_state(this);
};
autocomplete.add_rows = [this](std::string &buffer, int /*line*/, int /*line_index*/) {
autocomplete.add_rows = [this](std::string & /*buffer*/, int /*line*/, int /*line_index*/) {
if(autocomplete.state == Autocomplete::State::starting) {
autocomplete_comment.clear();
autocomplete_insert.clear();

1
src/source_generic.hpp

@ -9,7 +9,6 @@ namespace Source {
class GenericView : public View {
public:
GenericView(const boost::filesystem::path &file_path, const Glib::RefPtr<Gsv::Language> &language);
~GenericView();
private:
void parse_language_file(const boost::property_tree::ptree &pt);

2
src/source_language_protocol.cpp

@ -1402,7 +1402,7 @@ void Source::LanguageProtocolView::setup_signals() {
}
void Source::LanguageProtocolView::setup_autocomplete() {
autocomplete = std::make_unique<Autocomplete>(this, interactive_completion, last_keyval, false);
autocomplete = std::make_unique<Autocomplete>(this, interactive_completion, last_keyval, false, true);
if(!capabilities.completion)
return;

Loading…
Cancel
Save