From 2df7b1f6da56eb4424233e87a7b15ca4b59d7514 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 2 Mar 2016 13:51:50 +0000 Subject: [PATCH] In buffer name completion, give priority to filename (not dirname) matches 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 --- src/commands.cc | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 40b0b976..3a8d6132 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -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& b) -> const String& - { return b->display_name(); }); + struct RankedMatchAndBuffer : RankedMatch + { + RankedMatchAndBuffer(const RankedMatch& m, const Buffer* b = nullptr) + : RankedMatch{m}, buffer{b} {} - return complete(prefix, cursor_pos, c); + using RankedMatch::operator==; + using RankedMatch::operator<; + + const Buffer* buffer; + }; + + query = query.substr(0, cursor_pos); + Vector filename_matches; + Vector 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 res; } const PerArgumentCommandCompleter buffer_completer({