From f3f3f806243efcd15d8000b96119608dd79163ab Mon Sep 17 00:00:00 2001 From: Andrew Vos Date: Mon, 16 Nov 2020 16:02:13 +0000 Subject: [PATCH] Ignore indent when completing lines When doing line completion, we previously used to not complete the line if it had different indent to the potential completion. This commit changes the behaviour to ignore indentation when completing lines. --- src/insert_completer.cc | 17 +++++++++++------ test/compose/line-completion/cmd | 1 + test/compose/line-completion/in | 4 ++++ test/compose/line-completion/out | 4 ++++ 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 test/compose/line-completion/cmd create mode 100644 test/compose/line-completion/in create mode 100644 test/compose/line-completion/out diff --git a/src/insert_completer.cc b/src/insert_completer.cc index ca6b08a1..43a4fc61 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -348,7 +348,8 @@ InsertCompletion complete_line(const SelectionList& sels, const ColumnCount tabstop = options["tabstop"].get(); const ColumnCount column = get_column(buffer, tabstop, cursor_pos); - StringView prefix = buffer[cursor_pos.line].substr(0_byte, cursor_pos.column); + StringView prefix = trim_indent(buffer[cursor_pos.line].substr(0_byte, cursor_pos.column)); + BufferCoord replace_begin = buffer.advance(cursor_pos, -prefix.length()); InsertCompletion::CandidateList candidates; auto add_candidates = [&](const Buffer& buf) { @@ -356,12 +357,16 @@ InsertCompletion complete_line(const SelectionList& sels, { if (buf.name() == buffer.name() && l == cursor_pos.line) continue; - const StringView line = buf[l]; + + const StringView line = trim_indent(buf[l]); + + if (line.length() == 0) + continue; + if (prefix == line.substr(0_byte, prefix.length())) { - StringView candidate = line.substr(0_byte, line.length()-1); - candidates.push_back({candidate.str(), "", - {expand_tabs(candidate, tabstop, column), {}}}); + StringView candidate = trim_indent(line.substr(0_byte, line.length())); + candidates.push_back({candidate.str(), "", {expand_tabs(candidate, tabstop, column), {}} }); // perf: it's unlikely the user intends to search among >10 candidates anyway if (candidates.size() == 100) break; @@ -384,7 +389,7 @@ InsertCompletion complete_line(const SelectionList& sels, return {}; std::sort(candidates.begin(), candidates.end()); candidates.erase(std::unique(candidates.begin(), candidates.end()), candidates.end()); - return { std::move(candidates), cursor_pos.line, cursor_pos, buffer.timestamp() }; + return { std::move(candidates), replace_begin, cursor_pos, buffer.timestamp() }; } } diff --git a/test/compose/line-completion/cmd b/test/compose/line-completion/cmd new file mode 100644 index 00000000..46a2bfdd --- /dev/null +++ b/test/compose/line-completion/cmd @@ -0,0 +1 @@ +gjAl diff --git a/test/compose/line-completion/in b/test/compose/line-completion/in new file mode 100644 index 00000000..d7d4c490 --- /dev/null +++ b/test/compose/line-completion/in @@ -0,0 +1,4 @@ + w111111 + + w222222 + diff --git a/test/compose/line-completion/out b/test/compose/line-completion/out new file mode 100644 index 00000000..398d9af4 --- /dev/null +++ b/test/compose/line-completion/out @@ -0,0 +1,4 @@ + w111111 + + w222222 + w222222