Browse Source

Improved extend selection on markdown code blocks

pipelines/280567345
eidheim 5 years ago
parent
commit
98e89fccec
  1. 38
      src/source.cpp
  2. 12
      src/source_spellcheck.cpp
  3. 2
      src/source_spellcheck.hpp

38
src/source.cpp

@ -1398,19 +1398,17 @@ void Source::View::extend_selection() {
return tabs; 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 // Forward start to non-empty line
start = start_stored; start = start_stored;
forward_to_code(start);
start = get_buffer()->get_iter_at_line(start.get_line()); 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 // Forward end to end of line
end = end_stored; end = end_stored;
if(start > end)
end = start;
if(!end.ends_line()) if(!end.ends_line())
end.forward_to_line_end(); end.forward_to_line_end();
@ -1482,6 +1480,34 @@ void Source::View::extend_selection() {
end = get_buffer()->end(); 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); get_buffer()->select_range(start, end);
return; return;
} }

12
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") else if(tag->property_name() == "gtksourceview:context-classes:string")
string_tag = tag; string_tag = tag;
else if(tag->property_name() == "gtksourceview:context-classes:no-spell-check") 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<Gtk::TextTag> &tag) { signal_tag_removed_connection = get_buffer()->get_tag_table()->signal_tag_removed().connect([this](const Glib::RefPtr<Gtk::TextTag> &tag) {
if(tag->property_name() == "gtksourceview:context-classes:comment") 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") else if(tag->property_name() == "gtksourceview:context-classes:string")
string_tag.reset(); string_tag.reset();
else if(tag->property_name() == "gtksourceview:context-classes:no-spell-check") 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; return true;
} }
if(spellcheck_all) { if(spellcheck_all) {
if(no_spell_check_tag) { if(no_spellcheck_tag) {
if(iter.has_tag(no_spell_check_tag) || iter.begins_tag(no_spell_check_tag) || iter.ends_tag(no_spell_check_tag)) if(iter.has_tag(no_spellcheck_tag) || iter.begins_tag(no_spellcheck_tag) || iter.ends_tag(no_spellcheck_tag))
return false; return false;
// workaround for gtksourceview bug // workaround for gtksourceview bug
if(iter.ends_line()) { if(iter.ends_line()) {
@ -352,7 +352,7 @@ bool Source::SpellCheckView::is_spellcheck_iter(const Gtk::TextIter &iter) {
if(*previous_iter == '\'' || *previous_iter == '"') { if(*previous_iter == '\'' || *previous_iter == '"') {
auto next_iter = iter; auto next_iter = iter;
next_iter.forward_char(); 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; 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"" // for example, mark first " as not spellcheck iter in this case: r""
if(*iter == '\'' || *iter == '"') { if(*iter == '\'' || *iter == '"') {
auto previous_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; return false;
} }
} }

2
src/source_spellcheck.hpp

@ -28,9 +28,9 @@ namespace Source {
Glib::RefPtr<Gtk::TextTag> comment_tag; Glib::RefPtr<Gtk::TextTag> comment_tag;
Glib::RefPtr<Gtk::TextTag> string_tag; Glib::RefPtr<Gtk::TextTag> string_tag;
Glib::RefPtr<Gtk::TextTag> no_spellcheck_tag;
private: private:
Glib::RefPtr<Gtk::TextTag> no_spell_check_tag;
Glib::RefPtr<Gtk::TextTag> spellcheck_error_tag; Glib::RefPtr<Gtk::TextTag> spellcheck_error_tag;
sigc::connection signal_tag_added_connection; sigc::connection signal_tag_added_connection;

Loading…
Cancel
Save