Skip to content

Commit

Permalink
In buffer name completion, give priority to filename (not dirname) ma…
Browse files Browse the repository at this point in the history
…tches

First list filename matches, then full path matches to allow fast selection
of buffers in deep hierarchies where queries match the path of every buffers
  • Loading branch information
mawww committed Mar 2, 2016
1 parent 6d5900a commit 2df7b1f
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,46 @@ const PerArgumentCommandCompleter filename_completer({
cursor_pos) }; }
});

static CandidateList complete_buffer_name(StringView prefix, ByteCount cursor_pos)
static CandidateList complete_buffer_name(StringView query, ByteCount cursor_pos)
{
auto c = transformed(BufferManager::instance(),
[](const SafePtr<Buffer>& b) -> const String&
{ return b->display_name(); });
struct RankedMatchAndBuffer : RankedMatch
{
RankedMatchAndBuffer(const RankedMatch& m, const Buffer* b = nullptr)
: RankedMatch{m}, buffer{b} {}

using RankedMatch::operator==;
using RankedMatch::operator<;

const Buffer* buffer;
};

query = query.substr(0, cursor_pos);
Vector<RankedMatchAndBuffer> filename_matches;
Vector<RankedMatchAndBuffer> matches;
for (const auto& buffer : BufferManager::instance())
{
StringView bufname = buffer->display_name();
if (buffer->flags() & Buffer::Flags::File)
{
if (RankedMatch match{split_path(bufname).second, query})
{
filename_matches.emplace_back(match, buffer.get());
continue;
}
}
if (RankedMatch match{bufname, query})
matches.emplace_back(match, buffer.get());
}
std::sort(filename_matches.begin(), filename_matches.end());
std::sort(matches.begin(), matches.end());

CandidateList res;
for (auto& match : filename_matches)
res.push_back(match.buffer->display_name());
for (auto& match : matches)
res.push_back(match.buffer->display_name());

return complete(prefix, cursor_pos, c);
return res;
}

const PerArgumentCommandCompleter buffer_completer({
Expand Down

0 comments on commit 2df7b1f

Please sign in to comment.