summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2016-03-02 13:51:50 +0000
committerMaxime Coste <frrrwww@gmail.com>2016-03-02 13:51:50 +0000
commit2df7b1f6da56eb4424233e87a7b15ca4b59d7514 (patch)
tree03cc34f3e9119eef2bbffc14fea974a8c8288747 /src
parent6d5900af16da325b1bd48ff2a18f8bd0bdc256cd (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/commands.cc43
1 files 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<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({