From 1728c67fef457566ab0a08dae5642edfc3374ef5 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 3 Mar 2017 22:03:20 +0000 Subject: [PATCH] Fix indent selection respect for original selection cursor position Fixes #1233 --- src/selectors.cc | 18 +++++++++++++----- .../cmd | 1 + .../in | 3 +++ .../selections | 3 +++ 4 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 test/regression/1233-corrent-cursor-handling-for-indent-objects/cmd create mode 100644 test/regression/1233-corrent-cursor-handling-for-indent-objects/in create mode 100644 test/regression/1233-corrent-cursor-handling-for-indent-objects/selections diff --git a/src/selectors.cc b/src/selectors.cc index 85af2958..6f23935b 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -64,7 +64,8 @@ template Optional select_to_next_word(const Buffer&, template Optional select_to_next_word(const Buffer&, const Selection&); template -Optional select_to_next_word_end(const Buffer& buffer, const Selection& selection) +Optional +select_to_next_word_end(const Buffer& buffer, const Selection& selection) { Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer}; if (begin+1 == buffer.end()) @@ -632,12 +633,16 @@ select_indent(const Buffer& buffer, const Selection& selection, return it == str.end(); }; + const bool to_begin = flags & ObjectFlags::ToBegin; + const bool to_end = flags & ObjectFlags::ToEnd; + int tabstop = buffer.options()["tabstop"].get(); - LineCount line = selection.cursor().line; + auto pos = selection.cursor(); + LineCount line = pos.line; auto indent = get_indent(buffer[line], tabstop); LineCount begin_line = line - 1; - if (flags & ObjectFlags::ToBegin) + if (to_begin) { while (begin_line >= 0 and (buffer[begin_line] == StringView{"\n"} or get_indent(buffer[begin_line], tabstop) >= indent)) @@ -645,7 +650,7 @@ select_indent(const Buffer& buffer, const Selection& selection, } ++begin_line; LineCount end_line = line + 1; - if (flags & ObjectFlags::ToEnd) + if (to_end) { const LineCount end = buffer.line_count(); while (end_line < end and (buffer[end_line] == StringView{"\n"} or @@ -663,7 +668,10 @@ select_indent(const Buffer& buffer, const Selection& selection, is_only_whitespaces(buffer[end_line])) --end_line; } - return Selection{begin_line, {end_line, buffer[end_line].length() - 1}}; + + auto first = to_begin ? begin_line : pos; + auto last = to_end ? BufferCoord{end_line, buffer[end_line].length() - 1} : pos; + return to_end ? Selection{first, last} : Selection{last, first}; } Optional diff --git a/test/regression/1233-corrent-cursor-handling-for-indent-objects/cmd b/test/regression/1233-corrent-cursor-handling-for-indent-objects/cmd new file mode 100644 index 00000000..9db4a74d --- /dev/null +++ b/test/regression/1233-corrent-cursor-handling-for-indent-objects/cmd @@ -0,0 +1 @@ +]i diff --git a/test/regression/1233-corrent-cursor-handling-for-indent-objects/in b/test/regression/1233-corrent-cursor-handling-for-indent-objects/in new file mode 100644 index 00000000..843e8571 --- /dev/null +++ b/test/regression/1233-corrent-cursor-handling-for-indent-objects/in @@ -0,0 +1,3 @@ + foo + %(b)ar + baz diff --git a/test/regression/1233-corrent-cursor-handling-for-indent-objects/selections b/test/regression/1233-corrent-cursor-handling-for-indent-objects/selections new file mode 100644 index 00000000..af9714bb --- /dev/null +++ b/test/regression/1233-corrent-cursor-handling-for-indent-objects/selections @@ -0,0 +1,3 @@ +bar + baz +