Regex selectors more tolerant to matches not ending on char boundaries

This commit is contained in:
Maxime Coste 2015-02-02 22:48:54 +00:00
parent 340fc63f84
commit 18eec7e511
2 changed files with 12 additions and 5 deletions

View File

@ -510,8 +510,8 @@ void select_all_matches(SelectionList& selections, const Regex& regex)
for (; re_it != re_end; ++re_it) for (; re_it != re_end; ++re_it)
{ {
auto& begin = (*re_it)[0].first; auto begin = ensure_char_start(buffer, (*re_it)[0].first);
auto& end = (*re_it)[0].second; auto end = ensure_char_start(buffer, (*re_it)[0].second);
if (begin == sel_end) if (begin == sel_end)
continue; continue;
@ -549,8 +549,9 @@ void split_selections(SelectionList& selections, const Regex& regex)
if (end == buf_end) if (end == buf_end)
continue; continue;
end = ensure_char_start(buffer, end);
result.push_back(keep_direction({ begin.coord(), (begin == end) ? end.coord() : utf8::previous(end, begin).coord() }, sel)); result.push_back(keep_direction({ begin.coord(), (begin == end) ? end.coord() : utf8::previous(end, begin).coord() }, sel));
begin = (*re_it)[0].second; begin = ensure_char_start(buffer, (*re_it)[0].second);
} }
if (begin.coord() <= sel.max()) if (begin.coord() <= sel.max())
result.push_back(keep_direction({ begin.coord(), sel.max() }, sel)); result.push_back(keep_direction({ begin.coord(), sel.max() }, sel));

View File

@ -256,6 +256,12 @@ bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos,
find_last_match(buffer.begin(), buffer.end(), matches, ex)); find_last_match(buffer.begin(), buffer.end(), matches, ex));
} }
inline BufferIterator ensure_char_start(const Buffer& buffer, const BufferIterator& it)
{
return it != buffer.end() ?
utf8::character_start(it, buffer.iterator_at(it.coord().line)) : it;
}
template<Direction direction> template<Direction direction>
Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex) Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex)
{ {
@ -269,8 +275,8 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege
: utf8::previous(begin, buffer.begin()); : utf8::previous(begin, buffer.begin());
if ((found = find_match_in_buffer<direction>(buffer, pos, matches, regex))) if ((found = find_match_in_buffer<direction>(buffer, pos, matches, regex)))
{ {
begin = matches[0].first; begin = ensure_char_start(buffer, matches[0].first);
end = matches[0].second; end = ensure_char_start(buffer, matches[0].second);
for (auto& match : matches) for (auto& match : matches)
captures.emplace_back(match.first, match.second); captures.emplace_back(match.first, match.second);
} }