From 2cdf86d6745d3dcd06fbd6687b68fb76894a16a5 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Sat, 23 Nov 2019 08:47:14 +0100 Subject: [PATCH] src: Complete register names in `%reg{}` expansions Builtin registers have name aliases that can be completed upon when using a `%reg{}` expansion from the prompt. --- src/command_manager.cc | 5 +++++ src/register_manager.cc | 30 ++++++++++++++++++------------ src/register_manager.hh | 2 ++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 099ee244..65b370ac 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -675,6 +675,11 @@ Completions CommandManager::complete(const Context& context, switch (token.type) { + case Token::Type::RegisterExpand: + return {start , cursor_pos, + RegisterManager::instance().complete_register_name( + token.content, cursor_pos_in_token) }; + case Token::Type::OptionExpand: return {start , cursor_pos, GlobalScope::instance().option_registry().complete_option_name( diff --git a/src/register_manager.cc b/src/register_manager.cc index 3abbd61b..37c93628 100644 --- a/src/register_manager.cc +++ b/src/register_manager.cc @@ -7,23 +7,24 @@ namespace Kakoune { +static const HashMap reg_names = { + { "slash", '/' }, + { "dquote", '"' }, + { "pipe", '|' }, + { "caret", '^' }, + { "arobase", '@' }, + { "percent", '%' }, + { "dot", '.' }, + { "hash", '#' }, + { "underscore", '_' }, + { "colon", ':' } +}; + Register& RegisterManager::operator[](StringView reg) const { if (reg.length() == 1) return (*this)[reg[0_byte]]; - static const HashMap reg_names = { - { "slash", '/' }, - { "dquote", '"' }, - { "pipe", '|' }, - { "caret", '^' }, - { "arobase", '@' }, - { "percent", '%' }, - { "dot", '.' }, - { "hash", '#' }, - { "underscore", '_' }, - { "colon", ':' } - }; auto it = reg_names.find(reg); if (it == reg_names.end()) throw runtime_error(format("no such register: '{}'", reg)); @@ -47,4 +48,9 @@ void RegisterManager::add_register(Codepoint c, std::unique_ptr reg) reg_ptr = std::move(reg); } +CandidateList RegisterManager::complete_register_name(StringView prefix, ByteCount cursor_pos) const +{ + return complete(prefix, cursor_pos, reg_names | transform([](auto& i) { return i.key; }) | gather>()); +} + } diff --git a/src/register_manager.hh b/src/register_manager.hh index 9d3ea3d9..f18bc989 100644 --- a/src/register_manager.hh +++ b/src/register_manager.hh @@ -2,6 +2,7 @@ #define register_manager_hh_INCLUDED #include "array_view.hh" +#include "completion.hh" #include "exception.hh" #include "utils.hh" #include "hash_map.hh" @@ -146,6 +147,7 @@ public: Register& operator[](StringView reg) const; Register& operator[](Codepoint c) const; void add_register(Codepoint c, std::unique_ptr reg); + CandidateList complete_register_name(StringView prefix, ByteCount cursor_pos) const; protected: HashMap, MemoryDomain::Registers> m_registers;