Browse Source

Cleanup of Source::BaseView::is_token_char

pipelines/353213535
eidheim 4 years ago
parent
commit
91c1d2716d
  1. 4
      src/autocomplete.cpp
  2. 6
      src/autocomplete.hpp
  3. 4
      src/ctags.cpp
  4. 48
      src/selection_dialog.cpp
  5. 16
      src/selection_dialog.hpp
  6. 4
      src/source.cpp
  7. 2
      src/source_base.hpp
  8. 4
      src/source_generic.cpp
  9. 2
      src/source_generic.hpp
  10. 1
      src/tooltips.hpp
  11. 12
      tests/stubs/selection_dialog.cpp

4
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--;

6
src/autocomplete.hpp

@ -7,7 +7,7 @@
#include <thread>
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<std::unique_ptr<LockGuard>()> get_parse_lock = [] { return nullptr; };
std::function<void()> stop_parse = [] {};
std::function<bool(guint last_keyval)> is_continue_key = [](guint keyval) { return Source::BaseView::is_token_char(gdk_keyval_to_unicode(keyval)); };
std::function<bool(guint last_keyval)> is_continue_key = [this](guint keyval) { return view->is_token_char(gdk_keyval_to_unicode(keyval)); };
std::function<bool(guint last_keyval)> is_restart_key = [](guint) { return false; };
std::function<bool()> run_check = [] { return false; };
@ -58,7 +58,7 @@ public:
std::function<std::function<void(Tooltip &tooltip)>(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();

4
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<unsigned char>(chr) >= 128))
location.symbol.erase(8, 1);
}
@ -215,7 +215,7 @@ std::vector<std::string> 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<unsigned char>(chr) >= 128 || chr == '~') {
if(text_start == std::string::npos)
text_start = c;
}

48
src/selection_dialog.cpp

@ -35,8 +35,8 @@ void SelectionDialogBase::ListViewText::clear() {
size = 0;
}
SelectionDialogBase::SelectionDialogBase(Gtk::TextView *text_view, const boost::optional<Gtk::TextIter> &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<Gtk::TextIter> &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<Gtk::Application>::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> SelectionDialog::instance;
SelectionDialog::SelectionDialog(Gtk::TextView *text_view, const boost::optional<Gtk::TextIter> &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<Gtk::TextIter> &start_iter, bool show_search_entry, bool use_markup)
: SelectionDialogBase(view, start_iter, show_search_entry, use_markup) {
auto search_text = std::make_shared<std::string>();
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> 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<std::string>();
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;

16
src/selection_dialog.hpp

@ -38,7 +38,7 @@ class SelectionDialogBase {
};
public:
SelectionDialogBase(Gtk::TextView *text_view, const boost::optional<Gtk::TextIter> &start_iter, bool show_search_entry, bool use_markup);
SelectionDialogBase(Source::BaseView *view, const boost::optional<Gtk::TextIter> &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<Gtk::TextIter> &start_iter, bool show_search_entry, bool use_markup);
SelectionDialog(Source::BaseView *view, const boost::optional<Gtk::TextIter> &start_iter, bool show_search_entry, bool use_markup);
static std::unique_ptr<SelectionDialog> 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<SelectionDialog>(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<SelectionDialog>(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<SelectionDialog>(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<CompletionDialog> 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<CompletionDialog>(new CompletionDialog(text_view, start_iter));
static void create(Source::BaseView *view, const Gtk::TextIter &start_iter) {
instance = std::unique_ptr<CompletionDialog>(new CompletionDialog(view, start_iter));
}
static std::unique_ptr<CompletionDialog> &get() { return instance; }

4
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());

2
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<Gtk::TextIter, Gtk::TextIter> get_token_iters(Gtk::TextIter iter);

4
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) {

2
src/source_generic.hpp

@ -11,6 +11,8 @@ namespace Source {
GenericView(const boost::filesystem::path &file_path, const Glib::RefPtr<Gsv::Language> &language);
~GenericView();
bool is_token_char(gunichar chr) override;
private:
void parse_language_file(bool &has_context_class, const boost::property_tree::ptree &pt);

1
src/tooltips.hpp

@ -3,7 +3,6 @@
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <functional>
#include <gtksourceviewmm.h>
#include <list>
#include <set>
#include <string>

12
tests/stubs/selection_dialog.cpp

@ -2,8 +2,8 @@
SelectionDialogBase::ListViewText::ListViewText(bool use_markup) {}
SelectionDialogBase::SelectionDialogBase(Gtk::TextView *text_view, const boost::optional<Gtk::TextIter> &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<Gtk::TextIter> &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> SelectionDialog::instance;
SelectionDialog::SelectionDialog(Gtk::TextView *text_view, const boost::optional<Gtk::TextIter> &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<Gtk::TextIter> &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> 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; }

Loading…
Cancel
Save