From a26ae56aaef72e1c15d60584c9fe69f5c2bf0ded Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 22 Jun 2020 14:39:05 +0200 Subject: [PATCH] Cleanup of SelectionDialog::on_change and on_select --- src/autocomplete.cpp | 17 ++++++++--------- src/autocomplete.hpp | 5 +++-- src/project.cpp | 12 +++--------- src/selection_dialog.cpp | 12 ++++++++---- src/selection_dialog.hpp | 2 +- src/source_clang.cpp | 4 ++-- src/source_language_protocol.cpp | 2 +- src/tooltips.cpp | 2 +- src/tooltips.hpp | 2 +- src/window.cpp | 10 ---------- 10 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/autocomplete.cpp b/src/autocomplete.cpp index 8e7cbfe..9eb0fdd 100644 --- a/src/autocomplete.cpp +++ b/src/autocomplete.cpp @@ -148,15 +148,16 @@ void Autocomplete::setup_dialog() { reparse(); }; - CompletionDialog::get()->on_changed = [this](unsigned int index, const std::string &text) { - if(index >= rows.size()) { + CompletionDialog::get()->on_change = [this](boost::optional index, const std::string &text) { + if(on_change) + on_change(index, text); + + if(!index) { tooltips.hide(); return; } - on_changed(index, text); - - auto set_buffer = set_tooltip_buffer(index); + auto set_buffer = set_tooltip_buffer(*index); if(!set_buffer) tooltips.hide(); else { @@ -170,9 +171,7 @@ void Autocomplete::setup_dialog() { }; CompletionDialog::get()->on_select = [this](unsigned int index, const std::string &text, bool hide_window) { - if(index >= rows.size()) - return; - - on_select(index, text, hide_window); + if(on_select) + on_select(index, text, hide_window); }; } diff --git a/src/autocomplete.hpp b/src/autocomplete.hpp index 08a8552..b55bde0 100644 --- a/src/autocomplete.hpp +++ b/src/autocomplete.hpp @@ -3,6 +3,7 @@ #include "mutex.hpp" #include "tooltips.hpp" #include +#include #include class Autocomplete { @@ -46,8 +47,8 @@ public: std::function on_show = [] {}; std::function on_hide = [] {}; - std::function on_changed = [](unsigned int index, const std::string &text) {}; - std::function on_select = [](unsigned int index, const std::string &text, bool hide_window) {}; + std::function, const std::string &)> on_change; + std::function on_select; std::function(unsigned int)> set_tooltip_buffer = [](unsigned int index) { return nullptr; }; diff --git a/src/project.cpp b/src/project.cpp index 0b8a3be..ca2ec5c 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -245,8 +245,6 @@ void Project::Base::show_symbols() { if(rows.size() == 0) return; SelectionDialog::get()->on_select = [rows = std::move(rows), project_path = std::move(ctags.project_path)](unsigned int index, const std::string &text, bool hide_window) { - if(index >= rows.size()) - return; auto offset = rows[index]; auto full_path = project_path / offset.file_path; boost::system::error_code ec; @@ -556,8 +554,6 @@ void Project::LLDB::debug_backtrace() { } SelectionDialog::get()->on_select = [rows = std::move(rows)](unsigned int index, const std::string &text, bool hide_window) { - if(index >= rows.size()) - return; auto frame = rows[index]; if(!frame.file_path.empty()) { if(Notebook::get().open(frame.file_path)) { @@ -597,8 +593,6 @@ void Project::LLDB::debug_show_variables() { } SelectionDialog::get()->on_select = [rows](unsigned int index, const std::string &text, bool hide_window) { - if(index >= rows->size()) - return; auto variable = (*rows)[index]; Debug::LLDB::get().select_frame(variable.frame_index, variable.thread_index_id); if(!variable.file_path.empty()) { @@ -617,15 +611,15 @@ void Project::LLDB::debug_show_variables() { self->debug_variable_tooltips.clear(); }; - SelectionDialog::get()->on_changed = [self = this->shared_from_this(), rows, view](unsigned int index, const std::string &text) { - if(index >= rows->size()) { + SelectionDialog::get()->on_change = [self = this->shared_from_this(), rows, view](boost::optional index, const std::string &text) { + if(!index) { self->debug_variable_tooltips.hide(); return; } self->debug_variable_tooltips.clear(); auto set_tooltip_buffer = [rows, index](Tooltip &tooltip) { - auto variable = (*rows)[index]; + auto variable = (*rows)[*index]; Glib::ustring value = variable.value; if(!value.empty()) { diff --git a/src/selection_dialog.cpp b/src/selection_dialog.cpp index ae6bd2c..2950115 100644 --- a/src/selection_dialog.cpp +++ b/src/selection_dialog.cpp @@ -155,11 +155,11 @@ void SelectionDialogBase::cursor_changed() { index = it->get_value(list_view_text.column_record.index); if(last_index == index) return; - if(on_changed) { + if(on_change) { std::string text; if(it) text = it->get_value(list_view_text.column_record.text); - on_changed(index.value_or(-1), text); + on_change(index, text); } last_index = index; } @@ -288,8 +288,12 @@ bool SelectionDialog::on_key_press(GdkEventKey *key) { } else if(key->keyval == GDK_KEY_Return || key->keyval == GDK_KEY_KP_Enter || key->keyval == GDK_KEY_ISO_Left_Tab || key->keyval == GDK_KEY_Tab) { auto it = list_view_text.get_selection()->get_selected(); - auto column = list_view_text.get_column(0); - list_view_text.row_activated(list_view_text.get_model()->get_path(it), *column); + if(it) { + auto column = list_view_text.get_column(0); + list_view_text.row_activated(list_view_text.get_model()->get_path(it), *column); + } + else + hide(); return true; } else if(key->keyval == GDK_KEY_Escape) { diff --git a/src/selection_dialog.hpp b/src/selection_dialog.hpp index c438bed..c12dad3 100644 --- a/src/selection_dialog.hpp +++ b/src/selection_dialog.hpp @@ -50,8 +50,8 @@ public: std::function on_show; std::function on_hide; + std::function index, const std::string &text)> on_change; std::function on_select; - std::function on_changed; std::function on_search_entry_changed; Glib::RefPtr start_mark; diff --git a/src/source_clang.cpp b/src/source_clang.cpp index f718401..0ce90ce 100644 --- a/src/source_clang.cpp +++ b/src/source_clang.cpp @@ -923,8 +923,8 @@ Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::pa code_complete_results = nullptr; }; - autocomplete.on_changed = [this](unsigned int index, const std::string &text) { - selected_completion_string = completion_strings[index]; + autocomplete.on_change = [this](boost::optional index, const std::string &text) { + selected_completion_string = index ? completion_strings[*index] : nullptr; }; autocomplete.on_select = [this](unsigned int index, const std::string &text, bool hide_window) { diff --git a/src/source_language_protocol.cpp b/src/source_language_protocol.cpp index 1af3b6e..6de28ac 100644 --- a/src/source_language_protocol.cpp +++ b/src/source_language_protocol.cpp @@ -211,8 +211,8 @@ void LanguageProtocol::Client::parse_server_message() { if(header_read) { server_message_stream.seekg(0, std::ios::end); size_t read_size = server_message_stream.tellg(); - std::stringstream tmp; if(read_size >= *server_message_size) { + std::stringstream tmp; if(read_size > *server_message_size) { server_message_stream.seekg(*server_message_size, std::ios::beg); server_message_stream.seekp(*server_message_size, std::ios::beg); diff --git a/src/tooltips.cpp b/src/tooltips.cpp index 21cdc75..4b7398b 100644 --- a/src/tooltips.cpp +++ b/src/tooltips.cpp @@ -803,7 +803,7 @@ void Tooltips::show(bool disregard_drawn) { } } -void Tooltips::hide(boost::optional> last_mouse_pos, boost::optional> mouse_pos) { +void Tooltips::hide(const boost::optional> &last_mouse_pos, const boost::optional> &mouse_pos) { for(auto &tooltip : tooltip_list) tooltip.hide(last_mouse_pos, mouse_pos); } diff --git a/src/tooltips.hpp b/src/tooltips.hpp index 8bce712..d4f62b5 100644 --- a/src/tooltips.hpp +++ b/src/tooltips.hpp @@ -62,7 +62,7 @@ public: void show(const Gdk::Rectangle &rectangle, bool disregard_drawn = false); void show(bool disregard_drawn = false); - void hide(boost::optional> last_mouse_pos = {}, boost::optional> mouse_pos = {}); + void hide(const boost::optional> &last_mouse_pos = {}, const boost::optional> &mouse_pos = {}); void clear() { tooltip_list.clear(); }; template diff --git a/src/window.cpp b/src/window.cpp index 888896c..9beafcf 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -963,8 +963,6 @@ void Window::set_menu_actions() { } SelectionDialog::get()->on_select = [paths = std::move(paths)](unsigned int index, const std::string &text, bool hide_window) { - if(index >= paths.size()) - return; if(Notebook::get().open(paths[index])) { auto view = Notebook::get().get_current_view(); view->hide_tooltips(); @@ -1117,8 +1115,6 @@ void Window::set_menu_actions() { return; } SelectionDialog::get()->on_select = [rows = std::move(rows)](unsigned int index, const std::string &text, bool hide_window) { - if(index >= rows.size()) - return; auto location = rows[index]; boost::system::error_code ec; if(!boost::filesystem::is_regular_file(location.file_path, ec)) @@ -1177,8 +1173,6 @@ void Window::set_menu_actions() { if(rows.size() == 0) return; SelectionDialog::get()->on_select = [rows = std::move(rows)](unsigned int index, const std::string &text, bool hide_window) { - if(index >= rows.size()) - return; auto offset = rows[index]; boost::system::error_code ec; if(!boost::filesystem::is_regular_file(offset.file_path, ec)) @@ -1210,8 +1204,6 @@ void Window::set_menu_actions() { SelectionDialog::get()->set_cursor_at_last_row(); } SelectionDialog::get()->on_select = [view, rows = std::move(rows)](unsigned int index, const std::string &text, bool hide_window) { - if(index >= rows.size()) - return; auto offset = rows[index]; view->get_buffer()->place_cursor(view->get_iter_at_line_pos(offset.line, offset.index)); view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5); @@ -1572,8 +1564,6 @@ void Window::set_menu_actions() { } SelectionDialog::get()->on_select = [rows = std::move(rows)](unsigned int index, const std::string &text, bool hide_window) { - if(index >= rows.size()) - return; if(Notebook::get().open(rows[index].file_path)) { auto view = Notebook::get().get_current_view(); view->place_cursor_at_line_pos(rows[index].line, rows[index].index);