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.
This commit is contained in:
Andrew Vos 2020-11-16 16:02:13 +00:00
parent efd38881d3
commit f3f3f80624
No known key found for this signature in database
GPG Key ID: EAEE27C95790CBBE
4 changed files with 20 additions and 6 deletions

View File

@ -348,7 +348,8 @@ InsertCompletion complete_line(const SelectionList& sels,
const ColumnCount tabstop = options["tabstop"].get<int>(); const ColumnCount tabstop = options["tabstop"].get<int>();
const ColumnCount column = get_column(buffer, tabstop, cursor_pos); 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; InsertCompletion::CandidateList candidates;
auto add_candidates = [&](const Buffer& buf) { 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) if (buf.name() == buffer.name() && l == cursor_pos.line)
continue; 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())) if (prefix == line.substr(0_byte, prefix.length()))
{ {
StringView candidate = line.substr(0_byte, line.length()-1); StringView candidate = trim_indent(line.substr(0_byte, line.length()));
candidates.push_back({candidate.str(), "", candidates.push_back({candidate.str(), "", {expand_tabs(candidate, tabstop, column), {}} });
{expand_tabs(candidate, tabstop, column), {}}});
// perf: it's unlikely the user intends to search among >10 candidates anyway // perf: it's unlikely the user intends to search among >10 candidates anyway
if (candidates.size() == 100) if (candidates.size() == 100)
break; break;
@ -384,7 +389,7 @@ InsertCompletion complete_line(const SelectionList& sels,
return {}; return {};
std::sort(candidates.begin(), candidates.end()); std::sort(candidates.begin(), candidates.end());
candidates.erase(std::unique(candidates.begin(), candidates.end()), 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() };
} }
} }

View File

@ -0,0 +1 @@
gjA<c-x>l<c-n><c-n><esc>

View File

@ -0,0 +1,4 @@
w111111
w222222

View File

@ -0,0 +1,4 @@
w111111
w222222
w222222