From efc069b531446c75e884dbaf06f0d490f617f128 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 26 Sep 2012 14:22:24 +0200 Subject: [PATCH] Extract IncrementalInserter::Mode as InsertMode move ClientMode classes in a namespace due to InsertMode name collisions --- src/client.cc | 63 ++++++++++++++++++++++++++--------------------- src/client.hh | 6 ++--- src/editor.cc | 26 +++++++++---------- src/editor.hh | 28 ++++++++++----------- src/main.cc | 16 ++++++------ src/unit_tests.cc | 2 +- 6 files changed, 74 insertions(+), 67 deletions(-) diff --git a/src/client.cc b/src/client.cc index f0ec7bb9..97d72412 100644 --- a/src/client.cc +++ b/src/client.cc @@ -1,6 +1,7 @@ #include "client.hh" #include "context.hh" +#include "editor.hh" #include "register_manager.hh" #include @@ -21,15 +22,18 @@ public: virtual void on_key(const Key& key, Context& context) = 0; protected: void reset_normal_mode(); - std::pair>& last_insert() { return m_client.m_last_insert; } + std::pair>& last_insert() { return m_client.m_last_insert; } private: Client& m_client; }; -class NormalMode : public ClientMode +namespace ClientModes +{ + +class Normal : public ClientMode { public: - NormalMode(Client& client) + Normal(Client& client) : ClientMode(client) { } @@ -58,16 +62,11 @@ private: int m_count = 0; }; -void ClientMode::reset_normal_mode() -{ - m_client.m_mode.reset(new NormalMode(m_client)); -} - -class MenuMode : public ClientMode +class Menu : public ClientMode { public: - MenuMode(Context& context, const memoryview& choices, - MenuCallback callback) + Menu(Context& context, const memoryview& choices, + MenuCallback callback) : ClientMode(context.client()), m_callback(callback), m_choice_count(choices.size()), m_selected(0) { @@ -125,11 +124,11 @@ private: int m_choice_count; }; -class PromptMode : public ClientMode +class Prompt : public ClientMode { public: - PromptMode(Context& context, const String& prompt, - Completer completer, PromptCallback callback) + Prompt(Context& context, const String& prompt, + Completer completer, PromptCallback callback) : ClientMode(context.client()), m_prompt(prompt), m_completer(completer), m_callback(callback) { @@ -296,12 +295,12 @@ private: static std::unordered_map> ms_history; std::vector::iterator m_history_it; }; -std::unordered_map> PromptMode::ms_history; +std::unordered_map> Prompt::ms_history; -class NextKeyMode : public ClientMode +class NextKey : public ClientMode { public: - NextKeyMode(Client& client, KeyCallback callback) + NextKey(Client& client, KeyCallback callback) : ClientMode(client), m_callback(callback) {} void on_key(const Key& key, Context& context) override @@ -316,10 +315,10 @@ private: KeyCallback m_callback; }; -class InsertMode : public ClientMode +class Insert : public ClientMode { public: - InsertMode(Client& client, Editor& editor, IncrementalInserter::Mode mode) + Insert(Client& client, Editor& editor, InsertMode mode) : ClientMode(client), m_inserter(editor, mode) { last_insert().first = mode; @@ -384,9 +383,17 @@ private: IncrementalInserter m_inserter; }; +} + +void ClientMode::reset_normal_mode() +{ + m_client.m_mode.reset(new ClientModes::Normal(m_client)); +} + + Client::Client() - : m_mode(new NormalMode(*this)), - m_last_insert(IncrementalInserter::Mode::Insert, {}) + : m_mode(new ClientModes::Normal(*this)), + m_last_insert(InsertMode::Insert, {}) { } @@ -394,9 +401,9 @@ Client::~Client() { } -void Client::insert(Editor& editor, IncrementalInserter::Mode mode) +void Client::insert(Editor& editor, InsertMode mode) { - m_mode.reset(new InsertMode(*this, editor, mode)); + m_mode.reset(new ClientModes::Insert(*this, editor, mode)); } void Client::repeat_last_insert(Context& context) @@ -408,29 +415,29 @@ void Client::repeat_last_insert(Context& context) swap(keys, m_last_insert.second); // m_last_insert will be refilled by the new InsertMode // this is very inefficient. - m_mode.reset(new InsertMode(*this, context.editor(), m_last_insert.first)); + m_mode.reset(new ClientModes::Insert(*this, context.editor(), m_last_insert.first)); for (auto& key : keys) m_mode->on_key(key, context); - assert(dynamic_cast(m_mode.get()) != nullptr); + assert(dynamic_cast(m_mode.get()) != nullptr); } void Client::prompt(const String& prompt, Completer completer, PromptCallback callback, Context& context) { assert(&context.client() == this); - m_mode.reset(new PromptMode(context, prompt, completer, callback)); + m_mode.reset(new ClientModes::Prompt(context, prompt, completer, callback)); } void Client::menu(const memoryview& choices, MenuCallback callback, Context& context) { assert(&context.client() == this); - m_mode.reset(new MenuMode(context, choices, callback)); + m_mode.reset(new ClientModes::Menu(context, choices, callback)); } void Client::on_next_key(KeyCallback callback) { - m_mode.reset(new NextKeyMode(*this, callback)); + m_mode.reset(new ClientModes::NextKey(*this, callback)); } void Client::handle_next_input(Context& context) diff --git a/src/client.hh b/src/client.hh index 27000ab4..d0a64bb3 100644 --- a/src/client.hh +++ b/src/client.hh @@ -5,7 +5,6 @@ #include "completion.hh" #include "utils.hh" #include "string.hh" -#include "window.hh" namespace Kakoune { @@ -18,6 +17,7 @@ using PromptCallback = std::function; using KeyCallback = std::function; class ClientMode; +enum class InsertMode : unsigned; class Client : public SafeCountable { @@ -25,7 +25,7 @@ public: Client(); ~Client(); - void insert(Editor& editor, IncrementalInserter::Mode mode); + void insert(Editor& editor, InsertMode mode); void repeat_last_insert(Context& context); void prompt(const String& prompt, Completer completer, @@ -41,7 +41,7 @@ public: private: friend class ClientMode; std::unique_ptr m_mode; - std::pair> m_last_insert; + std::pair> m_last_insert; }; struct prompt_aborted {}; diff --git a/src/editor.cc b/src/editor.cc index f98d8a8d..064ce365 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -303,12 +303,12 @@ void Editor::end_edition() --m_edition_level; } -IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode) +IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode) : m_editor(editor), m_edition(editor), m_mode(mode) { m_editor.on_incremental_insertion_begin(); - if (mode == Mode::Change) + if (mode == InsertMode::Change) { for (auto& sel : editor.m_selections) editor.m_buffer.erase(sel.begin(), sel.end()); @@ -319,20 +319,20 @@ IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode) BufferIterator first, last; switch (mode) { - case Mode::Insert: first = sel.end()-1; last = sel.begin(); break; - case Mode::Change: first = sel.end()-1; last = sel.begin(); break; - case Mode::Append: first = sel.begin(); last = sel.end(); break; + case InsertMode::Insert: first = sel.end()-1; last = sel.begin(); break; + case InsertMode::Change: first = sel.end()-1; last = sel.begin(); break; + case InsertMode::Append: first = sel.begin(); last = sel.end(); break; - case Mode::OpenLineBelow: - case Mode::AppendAtLineEnd: + case InsertMode::OpenLineBelow: + case InsertMode::AppendAtLineEnd: first = m_editor.m_buffer.iterator_at_line_end(sel.end() - 1) - 1; last = first; break; - case Mode::OpenLineAbove: - case Mode::InsertAtLineBegin: + case InsertMode::OpenLineAbove: + case InsertMode::InsertAtLineBegin: first = m_editor.m_buffer.iterator_at_line_begin(sel.begin()); - if (mode == Mode::OpenLineAbove) + if (mode == InsertMode::OpenLineAbove) --first; else { @@ -351,10 +351,10 @@ IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode) --last; sel = Selection(first, last); } - if (mode == Mode::OpenLineBelow or mode == Mode::OpenLineAbove) + if (mode == InsertMode::OpenLineBelow or mode == InsertMode::OpenLineAbove) { insert("\n"); - if (mode == Mode::OpenLineAbove) + if (mode == InsertMode::OpenLineAbove) { for (auto& sel : m_editor.m_selections) { @@ -370,7 +370,7 @@ IncrementalInserter::~IncrementalInserter() { for (auto& sel : m_editor.m_selections) { - if (m_mode == Mode::Append) + if (m_mode == InsertMode::Append) sel = Selection(sel.first(), sel.last()-1); sel.avoid_eol(); } diff --git a/src/editor.hh b/src/editor.hh index 322eea35..edddb4a4 100644 --- a/src/editor.hh +++ b/src/editor.hh @@ -101,22 +101,22 @@ private: Editor& m_editor; }; +enum class InsertMode : unsigned +{ + Insert, + Append, + Change, + InsertAtLineBegin, + AppendAtLineEnd, + OpenLineBelow, + OpenLineAbove +}; + // An IncrementalInserter manage insert mode class IncrementalInserter { public: - enum class Mode - { - Insert, - Append, - Change, - InsertAtLineBegin, - AppendAtLineEnd, - OpenLineBelow, - OpenLineAbove - }; - - IncrementalInserter(Editor& editor, Mode mode = Mode::Insert); + IncrementalInserter(Editor& editor, InsertMode mode = InsertMode::Insert); ~IncrementalInserter(); void insert(const String& string); @@ -127,8 +127,8 @@ public: Buffer& buffer() const { return m_editor.buffer(); } private: - Mode m_mode; - Editor& m_editor; + InsertMode m_mode; + Editor& m_editor; scoped_edition m_edition; }; diff --git a/src/main.cc b/src/main.cc index 494d6406..92e6247d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -33,7 +33,7 @@ namespace Kakoune bool quit_requested = false; -template +template void do_insert(Context& context) { context.client().insert(context.editor(), mode); @@ -156,7 +156,7 @@ void do_erase(Context& context) void do_change(Context& context) { RegisterManager::instance()['"'] = context.editor().selections_content(); - do_insert(context); + do_insert(context); } enum class PasteMode @@ -358,12 +358,12 @@ std::unordered_map> keymap = { { Key::Modifiers::None, 'd' }, do_erase }, { { Key::Modifiers::None, 'c' }, do_change }, - { { Key::Modifiers::None, 'i' }, do_insert }, - { { Key::Modifiers::None, 'I' }, do_insert }, - { { Key::Modifiers::None, 'a' }, do_insert }, - { { Key::Modifiers::None, 'A' }, do_insert }, - { { Key::Modifiers::None, 'o' }, do_insert }, - { { Key::Modifiers::None, 'O' }, do_insert }, + { { Key::Modifiers::None, 'i' }, do_insert }, + { { Key::Modifiers::None, 'I' }, do_insert }, + { { Key::Modifiers::None, 'a' }, do_insert }, + { { Key::Modifiers::None, 'A' }, do_insert }, + { { Key::Modifiers::None, 'o' }, do_insert }, + { { Key::Modifiers::None, 'O' }, do_insert }, { { Key::Modifiers::None, 'r' }, do_replace_with_char }, { { Key::Modifiers::None, 'g' }, do_go }, diff --git a/src/unit_tests.cc b/src/unit_tests.cc index 797f0946..22650edb 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -61,7 +61,7 @@ void test_incremental_inserter() editor.select(buffer.begin()); { - IncrementalInserter inserter(editor, IncrementalInserter::Mode::OpenLineAbove); + IncrementalInserter inserter(editor, InsertMode::OpenLineAbove); assert(editor.is_editing()); assert(editor.selections().size() == 1); assert(editor.selections().front().first() == buffer.begin());