Move last insert state from context to input handler

This commit is contained in:
Maxime Coste 2013-02-18 14:07:30 +01:00
parent 0e081a76c1
commit fc2dd599a3
3 changed files with 13 additions and 11 deletions

View File

@ -97,9 +97,6 @@ struct Context
ui().print_status(status); ui().print_status(status);
} }
using Insertion = std::pair<InsertMode, std::vector<Key>>;
Insertion& last_insert() { return m_last_insert; }
void push_jump() void push_jump()
{ {
const SelectionList& jump = editor().selections(); const SelectionList& jump = editor().selections();
@ -162,7 +159,6 @@ private:
InputHandler* m_input_handler = nullptr; InputHandler* m_input_handler = nullptr;
safe_ptr<UserInterface> m_ui; safe_ptr<UserInterface> m_ui;
Insertion m_last_insert = {InsertMode::Insert, {}};
int m_numeric_param = 0; int m_numeric_param = 0;
using JumpList = std::vector<DynamicSelectionList>; using JumpList = std::vector<DynamicSelectionList>;

View File

@ -23,6 +23,10 @@ public:
virtual void on_key(const Key& key) = 0; virtual void on_key(const Key& key) = 0;
Context& context() const { return m_input_handler.context(); } Context& context() const { return m_input_handler.context(); }
using Insertion = InputHandler::Insertion;
Insertion& last_insert() { return m_input_handler.m_last_insert; }
protected: protected:
void reset_normal_mode(); void reset_normal_mode();
private: private:
@ -535,14 +539,14 @@ public:
} }
}} }}
{ {
context().last_insert().first = mode; last_insert().first = mode;
context().last_insert().second.clear(); last_insert().second.clear();
context().hooks().run_hook("InsertBegin", "", context()); context().hooks().run_hook("InsertBegin", "", context());
} }
void on_key(const Key& key) override void on_key(const Key& key) override
{ {
context().last_insert().second.push_back(key); last_insert().second.push_back(key);
if (m_insert_reg) if (m_insert_reg)
{ {
if (key.modifiers == Key::Modifiers::None) if (key.modifiers == Key::Modifiers::None)
@ -640,16 +644,15 @@ void InputHandler::insert(InsertMode mode)
void InputHandler::repeat_last_insert() void InputHandler::repeat_last_insert()
{ {
Context::Insertion& last_insert = m_context.last_insert(); if (m_last_insert.second.empty())
if (last_insert.second.empty())
return; return;
std::vector<Key> keys; std::vector<Key> keys;
swap(keys, last_insert.second); swap(keys, m_last_insert.second);
// context.last_insert will be refilled by the new Insert // context.last_insert will be refilled by the new Insert
// this is very inefficient. // this is very inefficient.
m_mode_trash.emplace_back(std::move(m_mode)); m_mode_trash.emplace_back(std::move(m_mode));
m_mode.reset(new InputModes::Insert(*this, last_insert.first)); m_mode.reset(new InputModes::Insert(*this, m_last_insert.first));
for (auto& key : keys) for (auto& key : keys)
m_mode->on_key(key); m_mode->on_key(key);
assert(dynamic_cast<InputModes::Normal*>(m_mode.get()) != nullptr); assert(dynamic_cast<InputModes::Normal*>(m_mode.get()) != nullptr);

View File

@ -71,6 +71,9 @@ private:
friend class InputMode; friend class InputMode;
std::unique_ptr<InputMode> m_mode; std::unique_ptr<InputMode> m_mode;
std::vector<std::unique_ptr<InputMode>> m_mode_trash; std::vector<std::unique_ptr<InputMode>> m_mode_trash;
using Insertion = std::pair<InsertMode, std::vector<Key>>;
Insertion m_last_insert = {InsertMode::Insert, {}};
}; };
struct prompt_aborted {}; struct prompt_aborted {};