From feea0064d8648f0d50eb773c708a3716af5f4458 Mon Sep 17 00:00:00 2001 From: Jason Felice Date: Sun, 9 Sep 2018 10:25:08 -0400 Subject: [PATCH 1/2] Simplify surround selection This has the same effect with fewer conditions, and I think it also specifies the intent more closely this way. --- src/selectors.cc | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/src/selectors.cc b/src/selectors.cc index cbbc3f03..8320c0b0 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -294,7 +294,9 @@ find_opening(Iterator pos, const Container& container, int level, bool nestable) { MatchResults 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 res[0].second == pos) pos = res[0].first; @@ -351,24 +353,10 @@ find_surrounding(const Container& container, Iterator pos, ObjectFlags flags, int level) { const bool nestable = opening != closing; - - // When onto the token of a non nestable block, consider it as an opening. - MatchResults 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 last = pos; 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)) { first = (flags & ObjectFlags::Inner) ? res->second : res->first; @@ -383,13 +371,6 @@ find_surrounding(const Container& container, Iterator pos, } 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)) last = (flags & ObjectFlags::Inner) ? utf8::previous(res->first, 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); - // When we already had the full object selected, select its parent - if (res and flags == (ObjectFlags::ToBegin | ObjectFlags::ToEnd) and - res->first.coord() == selection.min() and res->second.coord() == selection.max()) + // If the ends we're changing didn't move, find the parent + if (res and not (flags & ObjectFlags::Inner) and + (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); if (res) From c08ec08513f1bd87c696d38ba167df097d507cdc Mon Sep 17 00:00:00 2001 From: Delapouite Date: Sat, 27 Oct 2018 12:48:39 +0200 Subject: [PATCH 2/2] test: add end-extending-parent/braces for }B}B - Ref #2479 --- test/normal/object/end-extending-parent/braces/cmd | 1 + test/normal/object/end-extending-parent/braces/in | 3 +++ test/normal/object/end-extending-parent/braces/selections | 2 ++ 3 files changed, 6 insertions(+) create mode 100644 test/normal/object/end-extending-parent/braces/cmd create mode 100644 test/normal/object/end-extending-parent/braces/in create mode 100644 test/normal/object/end-extending-parent/braces/selections diff --git a/test/normal/object/end-extending-parent/braces/cmd b/test/normal/object/end-extending-parent/braces/cmd new file mode 100644 index 00000000..7eebadaa --- /dev/null +++ b/test/normal/object/end-extending-parent/braces/cmd @@ -0,0 +1 @@ +}B}B diff --git a/test/normal/object/end-extending-parent/braces/in b/test/normal/object/end-extending-parent/braces/in new file mode 100644 index 00000000..3c8a4900 --- /dev/null +++ b/test/normal/object/end-extending-parent/braces/in @@ -0,0 +1,3 @@ +{ + "foo": { %(bar) } +} diff --git a/test/normal/object/end-extending-parent/braces/selections b/test/normal/object/end-extending-parent/braces/selections new file mode 100644 index 00000000..93a412a9 --- /dev/null +++ b/test/normal/object/end-extending-parent/braces/selections @@ -0,0 +1,2 @@ +'bar } +}'