Completion: decentralise, move buffer name completion to BufferManager
This commit is contained in:
parent
82baa55eed
commit
f5ad32f137
|
@ -36,5 +36,17 @@ Buffer* BufferManager::get_buffer(const std::string& name)
|
||||||
return m_buffers[name].get();
|
return m_buffers[name].get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CandidateList BufferManager::complete_buffername(const std::string& prefix,
|
||||||
|
size_t cursor_pos)
|
||||||
|
{
|
||||||
|
std::string real_prefix = prefix.substr(0, cursor_pos);
|
||||||
|
CandidateList result;
|
||||||
|
for (auto& buffer : m_buffers)
|
||||||
|
{
|
||||||
|
if (buffer.first.substr(0, real_prefix.length()) == real_prefix)
|
||||||
|
result.push_back(buffer.first);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define buffer_manager_hh_INCLUDED
|
#define buffer_manager_hh_INCLUDED
|
||||||
|
|
||||||
#include "buffer.hh"
|
#include "buffer.hh"
|
||||||
|
#include "completion.hh"
|
||||||
#include "utils.hh"
|
#include "utils.hh"
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
@ -25,14 +26,17 @@ public:
|
||||||
Buffer* operator->() const { return parent_type::operator*().second.get(); }
|
Buffer* operator->() const { return parent_type::operator*().second.get(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
iterator begin() const { return iterator(m_buffers.begin()); }
|
|
||||||
iterator end() const { return iterator(m_buffers.end()); }
|
|
||||||
|
|
||||||
void register_buffer(Buffer* buffer);
|
void register_buffer(Buffer* buffer);
|
||||||
void delete_buffer(Buffer* buffer);
|
void delete_buffer(Buffer* buffer);
|
||||||
|
|
||||||
|
iterator begin() const { return iterator(m_buffers.begin()); }
|
||||||
|
iterator end() const { return iterator(m_buffers.end()); }
|
||||||
|
|
||||||
Buffer* get_buffer(const std::string& name);
|
Buffer* get_buffer(const std::string& name);
|
||||||
|
|
||||||
|
CandidateList complete_buffername(const std::string& prefix,
|
||||||
|
size_t cursor_pos = std::string::npos);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BufferMap m_buffers;
|
BufferMap m_buffers;
|
||||||
};
|
};
|
||||||
|
|
|
@ -34,18 +34,4 @@ CandidateList complete_filename(const std::string& prefix,
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
CandidateList complete_buffername(const std::string& prefix,
|
|
||||||
size_t cursor_pos)
|
|
||||||
{
|
|
||||||
std::string real_prefix = prefix.substr(0, cursor_pos);
|
|
||||||
CandidateList result;
|
|
||||||
for (auto& buffer : BufferManager::instance())
|
|
||||||
{
|
|
||||||
if (buffer.name().substr(0, real_prefix.length()) == real_prefix)
|
|
||||||
result.push_back(buffer.name());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,12 +26,8 @@ struct Completions
|
||||||
CandidateList complete_filename(const std::string& prefix,
|
CandidateList complete_filename(const std::string& prefix,
|
||||||
size_t cursor_pos = std::string::npos);
|
size_t cursor_pos = std::string::npos);
|
||||||
|
|
||||||
CandidateList complete_buffername(const std::string& prefix,
|
|
||||||
size_t cursor_pos = std::string::npos);
|
|
||||||
|
|
||||||
typedef std::function<Completions (const std::string&, size_t)> Completer;
|
typedef std::function<Completions (const std::string&, size_t)> Completer;
|
||||||
|
|
||||||
|
|
||||||
inline Completions complete_nothing(const std::string&, size_t cursor_pos)
|
inline Completions complete_nothing(const std::string&, size_t cursor_pos)
|
||||||
{
|
{
|
||||||
return Completions(cursor_pos, cursor_pos);
|
return Completions(cursor_pos, cursor_pos);
|
||||||
|
|
|
@ -615,7 +615,9 @@ int main(int argc, char* argv[])
|
||||||
command_manager.register_command(std::vector<std::string>{ "w", "write" }, write_buffer,
|
command_manager.register_command(std::vector<std::string>{ "w", "write" }, write_buffer,
|
||||||
PerArgumentCommandCompleter{ complete_filename });
|
PerArgumentCommandCompleter{ complete_filename });
|
||||||
command_manager.register_command(std::vector<std::string>{ "b", "buffer" }, show_buffer,
|
command_manager.register_command(std::vector<std::string>{ "b", "buffer" }, show_buffer,
|
||||||
PerArgumentCommandCompleter { complete_buffername });
|
PerArgumentCommandCompleter {
|
||||||
|
std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2)
|
||||||
|
});
|
||||||
command_manager.register_command(std::vector<std::string>{ "af", "addfilter" }, add_filter);
|
command_manager.register_command(std::vector<std::string>{ "af", "addfilter" }, add_filter);
|
||||||
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter);
|
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user