indent selector: When line is empty, find indent from surrounding lines

Look for the first non empty line preceeding the current line, or
if not found, the first non empty line following it.

Fixes #1904
This commit is contained in:
Maxime Coste 2018-03-25 10:25:12 +11:00
parent 81f605709c
commit 59e9108812
4 changed files with 22 additions and 2 deletions

View File

@ -654,6 +654,17 @@ select_indent(const Context& context, const Selection& selection,
return indent; return indent;
}; };
auto get_current_indent = [&](const Buffer& buffer, LineCount line, int tabstop)
{
for (auto l = line; l >= 0; --l)
if (buffer[l] != "\n"_sv)
return get_indent(buffer[l], tabstop);
for (auto l = line+1; l < buffer.line_count(); ++l)
if (buffer[l] != "\n"_sv)
return get_indent(buffer[l], tabstop);
return 0_char;
};
auto is_only_whitespaces = [](StringView str) { auto is_only_whitespaces = [](StringView str) {
auto it = str.begin(); auto it = str.begin();
skip_while(it, str.end(), skip_while(it, str.end(),
@ -667,8 +678,8 @@ select_indent(const Context& context, const Selection& selection,
auto& buffer = context.buffer(); auto& buffer = context.buffer();
int tabstop = context.options()["tabstop"].get<int>(); int tabstop = context.options()["tabstop"].get<int>();
auto pos = selection.cursor(); auto pos = selection.cursor();
LineCount line = pos.line; const LineCount line = pos.line;
auto indent = get_indent(buffer[line], tabstop); const auto indent = get_current_indent(buffer, line, tabstop);
LineCount begin_line = line - 1; LineCount begin_line = line - 1;
if (to_begin) if (to_begin)

View File

@ -0,0 +1 @@
2j<a-i>i

View File

@ -0,0 +1,4 @@
foo():
foo
bar

View File

@ -0,0 +1,4 @@
foo
bar