From c2d31f20ffeb4db77cd450806709093faacb2396 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 15 Jun 2023 12:04:45 +0200 Subject: [PATCH] Fixed autocomplete on for instance '.' after newline --- src/source_base.cpp | 7 ++++++- src/source_base.hpp | 1 + src/source_clang.cpp | 26 +++++++++++++++++--------- src/source_language_protocol.cpp | 19 +++++-------------- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/source_base.cpp b/src/source_base.cpp index 4cef68d..e4a973f 100644 --- a/src/source_base.cpp +++ b/src/source_base.cpp @@ -884,9 +884,14 @@ Gtk::TextIter Source::BaseView::get_tabs_end_iter() { return get_tabs_end_iter(get_buffer()->get_insert()); } +bool Source::BaseView::is_whitespace_char(gunichar chr) { + return chr == ' ' || chr == '\t' || chr == '\n' || chr == '\r' || chr == '\f'; +} + bool Source::BaseView::is_token_char(gunichar chr) { return (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9') || chr == '_' || chr == '$' || chr >= 128 || - (language_id == "css" && chr == '-'); + (language_id == "css" && chr == '-') || + (language_id == "rust" && chr == '!'); } std::pair Source::BaseView::get_token_iters(Gtk::TextIter iter) { diff --git a/src/source_base.hpp b/src/source_base.hpp index caede83..f6a160a 100644 --- a/src/source_base.hpp +++ b/src/source_base.hpp @@ -173,6 +173,7 @@ namespace Source { Gtk::TextIter get_tabs_end_iter(); public: + bool is_whitespace_char(gunichar chr); bool is_token_char(gunichar chr); protected: diff --git a/src/source_clang.cpp b/src/source_clang.cpp index a96b4a4..66d6bae 100644 --- a/src/source_clang.cpp +++ b/src/source_clang.cpp @@ -916,15 +916,23 @@ Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::pa prev = prefix_start; prev.backward_char(); auto prevprev = prev; - if(*prev == '.') { - auto iter = prev; - bool starts_with_num = false; - size_t count = 0; + + auto valid_continuation_token = [this](Gtk::TextIter iter) { + while(iter.backward_char() && is_whitespace_char(*iter)) { + } + if(!is_whitespace_char(*iter)) + iter.forward_char(); + bool found_token = false; + bool starts_with_digit = false; while(iter.backward_char() && is_token_char(*iter)) { - ++count; - starts_with_num = Glib::Unicode::isdigit(*iter); + found_token = true; + starts_with_digit = Glib::Unicode::isdigit(*iter); } - if((count >= 1 || *iter == ')' || *iter == ']') && !starts_with_num) { + return (found_token || *iter == ')' || *iter == ']' || *iter == '}') && !starts_with_digit; + }; + + if(*prev == '.') { + if(valid_continuation_token(prev)) { { LockGuard lock(autocomplete.prefix_mutex); autocomplete.prefix = get_buffer()->get_text(prefix_start, prefix_end); @@ -932,8 +940,8 @@ Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::pa return true; } } - else if((prevprev.backward_char() && ((*prevprev == ':' && *prev == ':') || (*prevprev == '-' && *prev == '>')))) { - if(*prev == ':' || (prevprev.backward_char() && (is_token_char(*prevprev) || *prevprev == ')' || *prevprev == ']'))) { + else if(prevprev.backward_char() && ((*prevprev == ':' && *prev == ':') || (*prevprev == '-' && *prev == '>'))) { + if(*prev == ':' || valid_continuation_token(prevprev)) { { LockGuard lock(autocomplete.prefix_mutex); autocomplete.prefix = get_buffer()->get_text(prefix_start, prefix_end); diff --git a/src/source_language_protocol.cpp b/src/source_language_protocol.cpp index 146a3d8..617a603 100644 --- a/src/source_language_protocol.cpp +++ b/src/source_language_protocol.cpp @@ -1644,22 +1644,13 @@ void Source::LanguageProtocolView::setup_autocomplete() { prev.backward_char(); auto prevprev = prev; if(*prev == '.') { - auto iter = prev; - bool starts_with_num = false; - size_t count = 0; - while(iter.backward_char() && is_token_char(*iter)) { - ++count; - starts_with_num = Glib::Unicode::isdigit(*iter); - } - if((count >= 1 || *iter == ')' || *iter == ']' || *iter == '"' || *iter == '\'' || *iter == '?') && !starts_with_num) { - { - LockGuard lock(autocomplete->prefix_mutex); - autocomplete->prefix = get_buffer()->get_text(prefix_start, prefix_end); - } - return true; + { + LockGuard lock(autocomplete->prefix_mutex); + autocomplete->prefix = get_buffer()->get_text(prefix_start, prefix_end); } + return true; } - else if((prevprev.backward_char() && *prevprev == ':' && *prev == ':')) { + else if(prevprev.backward_char() && *prevprev == ':' && *prev == ':') { { LockGuard lock(autocomplete->prefix_mutex); autocomplete->prefix = get_buffer()->get_text(prefix_start, prefix_end);