From 91c1d2716d4d1e1cdcfd4f4803e338387d3bfd85 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 14 Jul 2021 16:40:41 +0200 Subject: [PATCH] Cleanup of Source::BaseView::is_token_char --- src/autocomplete.cpp | 4 +-- src/autocomplete.hpp | 6 ++-- src/ctags.cpp | 4 +-- src/selection_dialog.cpp | 48 ++++++++++++++++---------------- src/selection_dialog.hpp | 16 +++++------ src/source.cpp | 4 +-- src/source_base.hpp | 2 +- src/source_generic.cpp | 4 +++ src/source_generic.hpp | 2 ++ src/tooltips.hpp | 3 +- tests/stubs/selection_dialog.cpp | 12 ++++---- 11 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/autocomplete.cpp b/src/autocomplete.cpp index 3307826..009c4b8 100644 --- a/src/autocomplete.cpp +++ b/src/autocomplete.cpp @@ -1,7 +1,7 @@ #include "autocomplete.hpp" #include "selection_dialog.hpp" -Autocomplete::Autocomplete(Gsv::View *view, bool &interactive_completion, guint &last_keyval, bool pass_buffer_and_strip_word) +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) { view->get_buffer()->signal_changed().connect([this, &last_keyval] { if(CompletionDialog::get() && CompletionDialog::get()->is_visible()) { @@ -65,7 +65,7 @@ void Autocomplete::run() { if(pass_buffer_and_strip_word) { auto pos = iter.get_offset() - 1; buffer = view->get_buffer()->get_text(); - while(pos >= 0 && Source::BaseView::is_token_char(buffer[pos])) { + while(pos >= 0 && view->is_token_char(buffer[pos])) { buffer.replace(pos, 1, " "); line_index--; pos--; diff --git a/src/autocomplete.hpp b/src/autocomplete.hpp index 76586ed..8e25ed0 100644 --- a/src/autocomplete.hpp +++ b/src/autocomplete.hpp @@ -7,7 +7,7 @@ #include class Autocomplete { - Gsv::View *view; + Source::BaseView *view; bool &interactive_completion; /// If view buffer should be passed to add_rows. Empty buffer is passed if not. /// Also, some utilities, like libclang, require that autocomplete is started at the beginning of a word. @@ -39,7 +39,7 @@ public: std::function()> get_parse_lock = [] { return nullptr; }; std::function stop_parse = [] {}; - std::function is_continue_key = [](guint keyval) { return Source::BaseView::is_token_char(gdk_keyval_to_unicode(keyval)); }; + std::function is_continue_key = [this](guint keyval) { return view->is_token_char(gdk_keyval_to_unicode(keyval)); }; std::function is_restart_key = [](guint) { return false; }; std::function run_check = [] { return false; }; @@ -58,7 +58,7 @@ public: std::function(unsigned int)> set_tooltip_buffer = [](unsigned int index) { return nullptr; }; - Autocomplete(Gsv::View *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); void run(); void stop(); diff --git a/src/ctags.cpp b/src/ctags.cpp index 7150adc..ba5367c 100644 --- a/src/ctags.cpp +++ b/src/ctags.cpp @@ -99,7 +99,7 @@ Ctags::Location Ctags::get_location(const std::string &line_, bool add_markup, b location.symbol = line.substr(0, symbol_end); if(9 < location.symbol.size() && location.symbol[8] == ' ' && starts_with(location.symbol, "operator")) { auto &chr = location.symbol[9]; - if(!Source::BaseView::is_token_char(chr)) + if(!((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9') || chr == '_' || static_cast(chr) >= 128)) location.symbol.erase(8, 1); } @@ -215,7 +215,7 @@ std::vector Ctags::get_type_parts(const std::string &type) { size_t text_start = std::string::npos; for(size_t c = 0; c < type.size(); ++c) { auto &chr = type[c]; - if(Source::BaseView::is_token_char(chr) || chr == '~') { + if((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9') || chr == '_' || static_cast(chr) >= 128 || chr == '~') { if(text_start == std::string::npos) text_start = c; } diff --git a/src/selection_dialog.cpp b/src/selection_dialog.cpp index 505d91b..5306820 100644 --- a/src/selection_dialog.cpp +++ b/src/selection_dialog.cpp @@ -35,8 +35,8 @@ void SelectionDialogBase::ListViewText::clear() { size = 0; } -SelectionDialogBase::SelectionDialogBase(Gtk::TextView *text_view, const boost::optional &start_iter, bool show_search_entry, bool use_markup) - : start_mark(start_iter ? Source::Mark(*start_iter) : Source::Mark()), text_view(text_view), window(Gtk::WindowType::WINDOW_POPUP), vbox(Gtk::Orientation::ORIENTATION_VERTICAL), list_view_text(use_markup), show_search_entry(show_search_entry) { +SelectionDialogBase::SelectionDialogBase(Source::BaseView *view_, const boost::optional &start_iter, bool show_search_entry_, bool use_markup) + : start_mark(start_iter ? Source::Mark(*start_iter) : Source::Mark()), view(view_), window(Gtk::WindowType::WINDOW_POPUP), vbox(Gtk::Orientation::ORIENTATION_VERTICAL), list_view_text(use_markup), show_search_entry(show_search_entry_) { auto g_application = g_application_get_default(); auto gio_application = Glib::wrap(g_application, true); auto application = Glib::RefPtr::cast_static(gio_application); @@ -88,12 +88,12 @@ SelectionDialogBase::SelectionDialogBase(Gtk::TextView *text_view, const boost:: ++c; } - if(this->text_view && row_width > this->text_view->get_width() * 2 / 3) - row_width = this->text_view->get_width() * 2 / 3; + if(view && row_width > view->get_width() * 2 / 3) + row_width = view->get_width() * 2 / 3; else if(row_width > application_window->get_width() / 2) row_width = application_window->get_width() / 2; - if(this->show_search_entry) + if(show_search_entry) window_height += search_entry.get_height(); int window_width = row_width + 1; window.resize(window_width, window_height); @@ -106,26 +106,26 @@ SelectionDialogBase::SelectionDialogBase(Gtk::TextView *text_view, const boost:: window.move(root_x, root_y); }; - if(this->text_view) { + if(view) { Gdk::Rectangle visible_rect; - this->text_view->get_visible_rect(visible_rect); + view->get_visible_rect(visible_rect); int visible_window_x, visible_window_max_y; - this->text_view->buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_TEXT, visible_rect.get_x(), visible_rect.get_y() + visible_rect.get_height(), visible_window_x, visible_window_max_y); + view->buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_TEXT, visible_rect.get_x(), visible_rect.get_y() + visible_rect.get_height(), visible_window_x, visible_window_max_y); Gdk::Rectangle iter_rect; - this->text_view->get_iter_location(this->start_mark->get_iter(), iter_rect); + view->get_iter_location(start_mark->get_iter(), iter_rect); int buffer_x = iter_rect.get_x(); int buffer_y = iter_rect.get_y() + iter_rect.get_height(); int window_x, window_y; - this->text_view->buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_TEXT, buffer_x, buffer_y, window_x, window_y); + view->buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_TEXT, buffer_x, buffer_y, window_x, window_y); - if(window_y < 0 || window_y > visible_window_max_y) // Move dialog to center if it is above or below visible parts of text_view + if(window_y < 0 || window_y > visible_window_max_y) // Move dialog to center if it is above or below visible parts of view move_window_to_center(); else { - window_x = std::max(window_x, visible_window_x); // Adjust right if dialog is left of text_view + window_x = std::max(window_x, visible_window_x); // Adjust right if dialog is left of view int root_x, root_y; - this->text_view->get_window(Gtk::TextWindowType::TEXT_WINDOW_TEXT)->get_root_coords(window_x, window_y, root_x, root_y); + view->get_window(Gtk::TextWindowType::TEXT_WINDOW_TEXT)->get_root_coords(window_x, window_y, root_x, root_y); // Adjust left if dialog is right of screen auto screen_width = Gdk::Screen::get_default()->get_width(); @@ -170,8 +170,8 @@ void SelectionDialogBase::erase_rows() { void SelectionDialogBase::show() { window.show_all(); - if(text_view) - text_view->grab_focus(); + if(view) + view->grab_focus(); if(list_view_text.get_model()->children().size() > 0) { if(!list_view_text.get_selection()->get_selected()) { @@ -210,8 +210,8 @@ void SelectionDialogBase::hide() { std::unique_ptr SelectionDialog::instance; -SelectionDialog::SelectionDialog(Gtk::TextView *text_view, const boost::optional &start_iter, bool show_search_entry, bool use_markup) - : SelectionDialogBase(text_view, start_iter, show_search_entry, use_markup) { +SelectionDialog::SelectionDialog(Source::BaseView *view, const boost::optional &start_iter, bool show_search_entry, bool use_markup) + : SelectionDialogBase(view, start_iter, show_search_entry, use_markup) { auto search_text = std::make_shared(); auto filter_model = Gtk::TreeModelFilter::create(list_view_text.get_model()); @@ -355,8 +355,8 @@ bool SelectionDialog::on_key_press(GdkEventKey *event) { std::unique_ptr CompletionDialog::instance; -CompletionDialog::CompletionDialog(Gtk::TextView *text_view, const Gtk::TextIter &start_iter) : SelectionDialogBase(text_view, start_iter, false, false) { - show_offset = text_view->get_buffer()->get_insert()->get_iter().get_offset(); +CompletionDialog::CompletionDialog(Source::BaseView *view, const Gtk::TextIter &start_iter) : SelectionDialogBase(view, start_iter, false, false) { + show_offset = view->get_buffer()->get_insert()->get_iter().get_offset(); auto search_text = std::make_shared(); auto filter_model = Gtk::TreeModelFilter::create(list_view_text.get_model()); @@ -392,7 +392,7 @@ CompletionDialog::CompletionDialog(Gtk::TextView *text_view, const Gtk::TextIter select(); }); - auto text = text_view->get_buffer()->get_text(start_mark->get_iter(), text_view->get_buffer()->get_insert()->get_iter()); + auto text = view->get_buffer()->get_text(start_mark->get_iter(), view->get_buffer()->get_insert()->get_iter()); if(text.size() > 0) { search_entry.set_text(text); list_view_text.set_search_entry(search_entry); @@ -414,10 +414,10 @@ bool CompletionDialog::on_key_release(GdkEventKey *event) { (event->keyval >= GDK_KEY_Shift_L && event->keyval <= GDK_KEY_Hyper_R)) return false; - if(show_offset > text_view->get_buffer()->get_insert()->get_iter().get_offset()) + if(show_offset > view->get_buffer()->get_insert()->get_iter().get_offset()) hide(); else { - auto text = text_view->get_buffer()->get_text(start_mark->get_iter(), text_view->get_buffer()->get_insert()->get_iter()); + auto text = view->get_buffer()->get_text(start_mark->get_iter(), view->get_buffer()->get_insert()->get_iter()); search_entry.set_text(text); list_view_text.set_search_entry(search_entry); if(text == "") { @@ -430,9 +430,9 @@ bool CompletionDialog::on_key_release(GdkEventKey *event) { } bool CompletionDialog::on_key_press(GdkEventKey *event) { - if(Source::BaseView::is_token_char(gdk_keyval_to_unicode(event->keyval)) || event->keyval == GDK_KEY_BackSpace) { + if(view->is_token_char(gdk_keyval_to_unicode(event->keyval)) || event->keyval == GDK_KEY_BackSpace) { if(row_in_entry) { - text_view->get_buffer()->erase(start_mark->get_iter(), text_view->get_buffer()->get_insert()->get_iter()); + view->get_buffer()->erase(start_mark->get_iter(), view->get_buffer()->get_insert()->get_iter()); row_in_entry = false; if(event->keyval == GDK_KEY_BackSpace) return true; diff --git a/src/selection_dialog.hpp b/src/selection_dialog.hpp index fba3b44..39f0926 100644 --- a/src/selection_dialog.hpp +++ b/src/selection_dialog.hpp @@ -38,7 +38,7 @@ class SelectionDialogBase { }; public: - SelectionDialogBase(Gtk::TextView *text_view, const boost::optional &start_iter, bool show_search_entry, bool use_markup); + SelectionDialogBase(Source::BaseView *view, const boost::optional &start_iter, bool show_search_entry, bool use_markup); virtual ~SelectionDialogBase() {} void add_row(const std::string &row); void erase_rows(); @@ -59,7 +59,7 @@ public: protected: void cursor_changed(); - Gtk::TextView *text_view; + Source::BaseView *view; Gtk::Window window; Gtk::Box vbox; Gtk::ScrolledWindow scrolled_window; @@ -71,14 +71,14 @@ protected: }; class SelectionDialog : public SelectionDialogBase { - SelectionDialog(Gtk::TextView *text_view, const boost::optional &start_iter, bool show_search_entry, bool use_markup); + SelectionDialog(Source::BaseView *view, const boost::optional &start_iter, bool show_search_entry, bool use_markup); static std::unique_ptr instance; public: bool on_key_press(GdkEventKey *event); - static void create(Gtk::TextView *text_view, bool show_search_entry = true, bool use_markup = false) { - instance = std::unique_ptr(new SelectionDialog(text_view, text_view->get_buffer()->get_insert()->get_iter(), show_search_entry, use_markup)); + static void create(Source::BaseView *view, bool show_search_entry = true, bool use_markup = false) { + instance = std::unique_ptr(new SelectionDialog(view, view->get_buffer()->get_insert()->get_iter(), show_search_entry, use_markup)); } static void create(bool show_search_entry = true, bool use_markup = false) { instance = std::unique_ptr(new SelectionDialog(nullptr, {}, show_search_entry, use_markup)); @@ -87,15 +87,15 @@ public: }; class CompletionDialog : public SelectionDialogBase { - CompletionDialog(Gtk::TextView *text_view, const Gtk::TextIter &start_iter); + CompletionDialog(Source::BaseView *view, const Gtk::TextIter &start_iter); static std::unique_ptr instance; public: bool on_key_release(GdkEventKey *event); bool on_key_press(GdkEventKey *event); - static void create(Gtk::TextView *text_view, const Gtk::TextIter &start_iter) { - instance = std::unique_ptr(new CompletionDialog(text_view, start_iter)); + static void create(Source::BaseView *view, const Gtk::TextIter &start_iter) { + instance = std::unique_ptr(new CompletionDialog(view, start_iter)); } static std::unique_ptr &get() { return instance; } diff --git a/src/source.cpp b/src/source.cpp index 57c45df..c5b07db 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -1218,7 +1218,7 @@ void Source::View::extend_selection() { // It is impossible to identify <> used for templates by syntax alone, but // this function works in most cases. - auto is_template_arguments = [](Gtk::TextIter start, Gtk::TextIter end) { + auto is_template_arguments = [this](Gtk::TextIter start, Gtk::TextIter end) { if(*start != '<' || *end != '>' || start.get_line() != end.get_line()) return false; auto prev = start; @@ -2080,7 +2080,7 @@ void Source::View::show_or_hide() { else if(std::none_of(starts_with_symbols.begin(), starts_with_symbols.end(), [&text](const std::string &symbol) { return starts_with(text, symbol); }) && - std::none_of(exact_tokens.begin(), exact_tokens.end(), [&text](const std::string &token) { + std::none_of(exact_tokens.begin(), exact_tokens.end(), [this, &text](const std::string &token) { return starts_with(text, token) && (text.size() <= token.size() || !is_token_char(text[token.size()])); })) { end = get_buffer()->get_iter_at_line(end.get_line()); diff --git a/src/source_base.hpp b/src/source_base.hpp index 43741f5..6fb0961 100644 --- a/src/source_base.hpp +++ b/src/source_base.hpp @@ -171,7 +171,7 @@ namespace Source { Gtk::TextIter get_tabs_end_iter(); public: - static bool is_token_char(gunichar chr); + virtual bool is_token_char(gunichar chr); protected: std::pair get_token_iters(Gtk::TextIter iter); diff --git a/src/source_generic.cpp b/src/source_generic.cpp index 97b836f..246cbc7 100644 --- a/src/source_generic.cpp +++ b/src/source_generic.cpp @@ -52,6 +52,10 @@ Source::GenericView::~GenericView() { autocomplete.thread.join(); } +bool Source::GenericView::is_token_char(gunichar chr) { + return Source::BaseView::is_token_char(chr) || (language_id == "css" && chr == '-'); +} + void Source::GenericView::parse_language_file(bool &has_context_class, const boost::property_tree::ptree &pt) { bool case_insensitive = false; for(auto &node : pt) { diff --git a/src/source_generic.hpp b/src/source_generic.hpp index 3e462fa..698e3d2 100644 --- a/src/source_generic.hpp +++ b/src/source_generic.hpp @@ -11,6 +11,8 @@ namespace Source { GenericView(const boost::filesystem::path &file_path, const Glib::RefPtr &language); ~GenericView(); + bool is_token_char(gunichar chr) override; + private: void parse_language_file(bool &has_context_class, const boost::property_tree::ptree &pt); diff --git a/src/tooltips.hpp b/src/tooltips.hpp index 8b3023d..d748c3b 100644 --- a/src/tooltips.hpp +++ b/src/tooltips.hpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -76,7 +75,7 @@ public: void clear() { tooltip_list.clear(); }; template - void emplace_back(Ts &&... params) { + void emplace_back(Ts &&...params) { tooltip_list.emplace_back(std::forward(params)...); } diff --git a/tests/stubs/selection_dialog.cpp b/tests/stubs/selection_dialog.cpp index edffbd7..f561b0e 100644 --- a/tests/stubs/selection_dialog.cpp +++ b/tests/stubs/selection_dialog.cpp @@ -2,8 +2,8 @@ SelectionDialogBase::ListViewText::ListViewText(bool use_markup) {} -SelectionDialogBase::SelectionDialogBase(Gtk::TextView *text_view, const boost::optional &start_iter, bool show_search_entry, bool use_markup) - : text_view(text_view), list_view_text(use_markup) {} +SelectionDialogBase::SelectionDialogBase(Source::BaseView *view, const boost::optional &start_iter, bool show_search_entry, bool use_markup) + : view(view), list_view_text(use_markup) {} void SelectionDialogBase::show() {} @@ -13,15 +13,15 @@ void SelectionDialogBase::add_row(const std::string &row) {} std::unique_ptr SelectionDialog::instance; -SelectionDialog::SelectionDialog(Gtk::TextView *text_view, const boost::optional &start_iter, bool show_search_entry, bool use_markup) - : SelectionDialogBase(text_view, start_iter, show_search_entry, use_markup) {} +SelectionDialog::SelectionDialog(Source::BaseView *view, const boost::optional &start_iter, bool show_search_entry, bool use_markup) + : SelectionDialogBase(view, start_iter, show_search_entry, use_markup) {} bool SelectionDialog::on_key_press(GdkEventKey *event) { return true; } std::unique_ptr CompletionDialog::instance; -CompletionDialog::CompletionDialog(Gtk::TextView *text_view, const Gtk::TextIter &start_iter) - : SelectionDialogBase(text_view, start_iter, false, false) {} +CompletionDialog::CompletionDialog(Source::BaseView *view, const Gtk::TextIter &start_iter) + : SelectionDialogBase(view, start_iter, false, false) {} bool CompletionDialog::on_key_press(GdkEventKey *event) { return true; }