Merge remote-tracking branch 'eraserhd/refactor-surround'

This commit is contained in:
Maxime Coste 2018-10-29 19:27:18 +11:00
commit 6271d0d9ff
4 changed files with 13 additions and 25 deletions

View File

@ -294,7 +294,9 @@ find_opening(Iterator pos, const Container& container,
int level, bool nestable) int level, bool nestable)
{ {
MatchResults<Iterator> res; MatchResults<Iterator> res;
if (backward_regex_search(container.begin(), pos, // When on the token of a non-nestable block, we want to consider it opening
if (nestable and
backward_regex_search(container.begin(), pos,
container.begin(), container.end(), res, closing) and container.begin(), container.end(), res, closing) and
res[0].second == pos) res[0].second == pos)
pos = res[0].first; pos = res[0].first;
@ -351,24 +353,10 @@ find_surrounding(const Container& container, Iterator pos,
ObjectFlags flags, int level) ObjectFlags flags, int level)
{ {
const bool nestable = opening != closing; const bool nestable = opening != closing;
// When onto the token of a non nestable block, consider it as an opening.
MatchResults<Iterator> matches;
if (not nestable and regex_search(pos, container.end(), container.begin(),
container.end(), matches, opening) and
matches[0].first == pos)
pos = matches[0].second;
auto first = pos; auto first = pos;
auto last = pos; auto last = pos;
if (flags & ObjectFlags::ToBegin) if (flags & ObjectFlags::ToBegin)
{ {
// When positionned onto opening and searching to opening, search the parent one
if (nestable and first != container.begin() and not (flags & ObjectFlags::ToEnd) and
regex_search(first, container.end(), container.begin(), container.end(),
matches, opening) and matches[0].first == first)
first = utf8::previous(first, container.begin());
if (auto res = find_opening(first+1, container, opening, closing, level, nestable)) if (auto res = find_opening(first+1, container, opening, closing, level, nestable))
{ {
first = (flags & ObjectFlags::Inner) ? res->second : res->first; first = (flags & ObjectFlags::Inner) ? res->second : res->first;
@ -383,13 +371,6 @@ find_surrounding(const Container& container, Iterator pos,
} }
if (flags & ObjectFlags::ToEnd) if (flags & ObjectFlags::ToEnd)
{ {
// When positionned onto closing and searching to closing, search the parent one
auto next = utf8::next(last, container.end());
if (nestable and next != container.end() and not (flags & ObjectFlags::ToBegin) and
backward_regex_search(container.begin(), next, container.begin(), container.end(),
matches, closing) and matches[0].second == next)
last = next;
if (auto res = find_closing(last, container, opening, closing, level, nestable)) if (auto res = find_closing(last, container, opening, closing, level, nestable))
last = (flags & ObjectFlags::Inner) ? utf8::previous(res->first, container.begin()) last = (flags & ObjectFlags::Inner) ? utf8::previous(res->first, container.begin())
: utf8::previous(res->second, container.begin()); : utf8::previous(res->second, container.begin());
@ -412,9 +393,10 @@ select_surrounding(const Context& context, const Selection& selection,
auto res = find_surrounding(buffer, pos, opening, closing, flags, level); auto res = find_surrounding(buffer, pos, opening, closing, flags, level);
// When we already had the full object selected, select its parent // If the ends we're changing didn't move, find the parent
if (res and flags == (ObjectFlags::ToBegin | ObjectFlags::ToEnd) and if (res and not (flags & ObjectFlags::Inner) and
res->first.coord() == selection.min() and res->second.coord() == selection.max()) (res->first.coord() == selection.min() or not (flags & ObjectFlags::ToBegin)) and
(res->second.coord() == selection.max() or not (flags & ObjectFlags::ToEnd)))
res = find_surrounding(buffer, pos, opening, closing, flags, level+1); res = find_surrounding(buffer, pos, opening, closing, flags, level+1);
if (res) if (res)

View File

@ -0,0 +1 @@
}B}B

View File

@ -0,0 +1,3 @@
{
"foo": { %(bar) }
}

View File

@ -0,0 +1,2 @@
'bar }
}'