Tweak inner indent object code

Inner indent is now the set of lines whose indent is >= current
line indent, triming lines containing only whitespaces at start
and end.

Fixes #140
This commit is contained in:
Maxime Coste 2014-05-26 21:33:31 +01:00
parent bf50a1c191
commit c1a7759e7f

View File

@ -352,6 +352,14 @@ static CharCount get_indent(const String& str, int tabstop)
return indent; return indent;
} }
static bool is_only_whitespaces(const String& str)
{
auto it = str.begin();
skip_while(it, str.end(),
[](char c){ return c == ' ' or c == '\t' or c == '\n'; });
return it == str.end();
}
Selection select_whole_indent(const Buffer& buffer, const Selection& selection, ObjectFlags flags) Selection select_whole_indent(const Buffer& buffer, const Selection& selection, ObjectFlags flags)
{ {
int tabstop = buffer.options()["tabstop"].get<int>(); int tabstop = buffer.options()["tabstop"].get<int>();
@ -368,26 +376,22 @@ Selection select_whole_indent(const Buffer& buffer, const Selection& selection,
LineCount end_line = line + 1; LineCount end_line = line + 1;
if (flags & ObjectFlags::ToEnd) if (flags & ObjectFlags::ToEnd)
{ {
LineCount end = buffer.line_count(); const LineCount end = buffer.line_count();
while (end_line < end and (buffer[end_line] == "\n" or get_indent(buffer[end_line], tabstop) >= indent)) while (end_line < end and (buffer[end_line] == "\n" or get_indent(buffer[end_line], tabstop) >= indent))
++end_line; ++end_line;
} }
--end_line; --end_line;
ByteCoord first = begin_line; // remove only whitespaces lines in inner mode
// keep the first line indent in inner mode
if (flags & ObjectFlags::Inner) if (flags & ObjectFlags::Inner)
{ {
CharCount i = 0; while (begin_line < end_line and
for (; i < indent; ++first.column) is_only_whitespaces(buffer[begin_line]))
{ ++begin_line;
auto c = buffer.byte_at(first); while (begin_line < end_line and
if (c == ' ') is_only_whitespaces(buffer[end_line]))
++i; --end_line;
if (c == '\t')
i = (i / tabstop + 1) * tabstop;
}
} }
return Selection{first, {end_line, buffer[end_line].length() - 1}}; return Selection{begin_line, {end_line, buffer[end_line].length() - 1}};
} }
Selection select_whole_lines(const Buffer& buffer, const Selection& selection) Selection select_whole_lines(const Buffer& buffer, const Selection& selection)