summaryrefslogtreecommitdiff
path: root/src/selectors.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2018-03-25 10:25:12 +1100
committerMaxime Coste <mawww@kakoune.org>2018-03-25 10:25:56 +1100
commit59e91088122728fa03a8031b20686e037bd9b76d (patch)
tree224ba4d63a9ddc6306ae155ce103ff705725b8ec /src/selectors.cc
parent81f605709c51fc2818caae202dd27212fb9bed71 (diff)
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
Diffstat (limited to 'src/selectors.cc')
-rw-r--r--src/selectors.cc15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/selectors.cc b/src/selectors.cc
index 378692cc..d8ca897b 100644
--- a/src/selectors.cc
+++ b/src/selectors.cc
@@ -654,6 +654,17 @@ select_indent(const Context& context, const Selection& selection,
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 it = str.begin();
skip_while(it, str.end(),
@@ -667,8 +678,8 @@ select_indent(const Context& context, const Selection& selection,
auto& buffer = context.buffer();
int tabstop = context.options()["tabstop"].get<int>();
auto pos = selection.cursor();
- LineCount line = pos.line;
- auto indent = get_indent(buffer[line], tabstop);
+ const LineCount line = pos.line;
+ const auto indent = get_current_indent(buffer, line, tabstop);
LineCount begin_line = line - 1;
if (to_begin)