From 98e89fccec73ef0682d79bb16b2cb179cd134654 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 16 Mar 2021 13:26:56 +0100 Subject: [PATCH] Improved extend selection on markdown code blocks --- src/source.cpp | 38 ++++++++++++++++++++++++++++++++------ src/source_spellcheck.cpp | 12 ++++++------ src/source_spellcheck.hpp | 2 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/source.cpp b/src/source.cpp index 4db08b8..e64e33b 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -1398,19 +1398,17 @@ void Source::View::extend_selection() { return tabs; }; - // Forward to code iter - forward_to_code(start_stored); - if(start_stored > end_stored) - end_stored = start_stored; - // Forward start to non-empty line start = start_stored; + forward_to_code(start); start = get_buffer()->get_iter_at_line(start.get_line()); - while(!start.is_end() && (*start == ' ' || *start == '\t') && start.forward_char()) { + while(!start.is_end() && (*start == ' ' || *start == '\t' || start.ends_line()) && start.forward_char()) { } // Forward end to end of line end = end_stored; + if(start > end) + end = start; if(!end.ends_line()) end.forward_to_line_end(); @@ -1482,6 +1480,34 @@ void Source::View::extend_selection() { end = get_buffer()->end(); } } + + // Select no_spellcheck_tag block if markdown, and not about to select line + if(no_spellcheck_tag && language->get_id() == "markdown" && start_stored.has_tag(no_spellcheck_tag) && end_stored.has_tag(no_spellcheck_tag) && + !(start.starts_line() && end.ends_line() && start.has_tag(no_spellcheck_tag) && end.has_tag(no_spellcheck_tag))) { + start = start_stored; + end = end_stored; + if(!start.starts_tag(no_spellcheck_tag)) + start.backward_to_tag_toggle(no_spellcheck_tag); + if(!end.ends_tag(no_spellcheck_tag)) + end.forward_to_tag_toggle(no_spellcheck_tag); + auto prev = start; + while(*start == '`' && start.forward_char()) { + } + if(start.get_offset() - prev.get_offset() > 1) { + start.forward_to_line_end(); + start.forward_char(); + } + while(end.backward_char() && *end == '`') { + } + if(!end.ends_line()) + end.forward_char(); + + if(start == start_stored && end == end_stored) { + start = get_buffer()->begin(); + end = get_buffer()->end(); + } + } + get_buffer()->select_range(start, end); return; } diff --git a/src/source_spellcheck.cpp b/src/source_spellcheck.cpp index cd0eb8e..a0a787f 100644 --- a/src/source_spellcheck.cpp +++ b/src/source_spellcheck.cpp @@ -206,7 +206,7 @@ Source::SpellCheckView::SpellCheckView(const boost::filesystem::path &file_path, else if(tag->property_name() == "gtksourceview:context-classes:string") string_tag = tag; else if(tag->property_name() == "gtksourceview:context-classes:no-spell-check") - no_spell_check_tag = tag; + no_spellcheck_tag = tag; }); signal_tag_removed_connection = get_buffer()->get_tag_table()->signal_tag_removed().connect([this](const Glib::RefPtr &tag) { if(tag->property_name() == "gtksourceview:context-classes:comment") @@ -214,7 +214,7 @@ Source::SpellCheckView::SpellCheckView(const boost::filesystem::path &file_path, else if(tag->property_name() == "gtksourceview:context-classes:string") string_tag.reset(); else if(tag->property_name() == "gtksourceview:context-classes:no-spell-check") - no_spell_check_tag.reset(); + no_spellcheck_tag.reset(); }); } @@ -342,8 +342,8 @@ bool Source::SpellCheckView::is_spellcheck_iter(const Gtk::TextIter &iter) { return true; } if(spellcheck_all) { - if(no_spell_check_tag) { - if(iter.has_tag(no_spell_check_tag) || iter.begins_tag(no_spell_check_tag) || iter.ends_tag(no_spell_check_tag)) + if(no_spellcheck_tag) { + if(iter.has_tag(no_spellcheck_tag) || iter.begins_tag(no_spellcheck_tag) || iter.ends_tag(no_spellcheck_tag)) return false; // workaround for gtksourceview bug if(iter.ends_line()) { @@ -352,7 +352,7 @@ bool Source::SpellCheckView::is_spellcheck_iter(const Gtk::TextIter &iter) { if(*previous_iter == '\'' || *previous_iter == '"') { auto next_iter = iter; next_iter.forward_char(); - if(next_iter.begins_tag(no_spell_check_tag) || next_iter.is_end()) + if(next_iter.begins_tag(no_spellcheck_tag) || next_iter.is_end()) return false; } } @@ -360,7 +360,7 @@ bool Source::SpellCheckView::is_spellcheck_iter(const Gtk::TextIter &iter) { // for example, mark first " as not spellcheck iter in this case: r"" if(*iter == '\'' || *iter == '"') { auto previous_iter = iter; - if(previous_iter.backward_char() && *previous_iter != '\'' && *previous_iter != '\"' && previous_iter.ends_tag(no_spell_check_tag)) + if(previous_iter.backward_char() && *previous_iter != '\'' && *previous_iter != '\"' && previous_iter.ends_tag(no_spellcheck_tag)) return false; } } diff --git a/src/source_spellcheck.hpp b/src/source_spellcheck.hpp index ab4d4ee..5d4d755 100644 --- a/src/source_spellcheck.hpp +++ b/src/source_spellcheck.hpp @@ -28,9 +28,9 @@ namespace Source { Glib::RefPtr comment_tag; Glib::RefPtr string_tag; + Glib::RefPtr no_spellcheck_tag; private: - Glib::RefPtr no_spell_check_tag; Glib::RefPtr spellcheck_error_tag; sigc::connection signal_tag_added_connection;