Hide more Client Mode code in the cpp
This commit is contained in:
parent
a312ce6dc2
commit
747c14c19f
101
src/client.cc
101
src/client.cc
|
@ -10,24 +10,28 @@ namespace Kakoune
|
||||||
|
|
||||||
extern std::unordered_map<Key, std::function<void (Context& context)>> keymap;
|
extern std::unordered_map<Key, std::function<void (Context& context)>> keymap;
|
||||||
|
|
||||||
class Client::Mode
|
class ClientMode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Mode(Client& client) : m_client(client) {}
|
ClientMode(Client& client) : m_client(client) {}
|
||||||
virtual ~Mode() {}
|
virtual ~ClientMode() {}
|
||||||
Mode(const Mode&) = delete;
|
ClientMode(const ClientMode&) = delete;
|
||||||
Mode& operator=(const Mode&) = delete;
|
ClientMode& operator=(const ClientMode&) = delete;
|
||||||
|
|
||||||
virtual void on_key(const Key& key, Context& context) = 0;
|
virtual void on_key(const Key& key, Context& context) = 0;
|
||||||
protected:
|
protected:
|
||||||
|
UserInterface& ui() { return *m_client.m_ui; }
|
||||||
|
void reset_normal_mode();
|
||||||
|
std::pair<IncrementalInserter::Mode, std::vector<Key>>& last_insert() { return m_client.m_last_insert; }
|
||||||
|
private:
|
||||||
Client& m_client;
|
Client& m_client;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Client::NormalMode : public Client::Mode
|
class NormalMode : public ClientMode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NormalMode(Client& client)
|
NormalMode(Client& client)
|
||||||
: Client::Mode(client)
|
: ClientMode(client)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,19 +59,24 @@ private:
|
||||||
int m_count = 0;
|
int m_count = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Client::MenuMode : public Client::Mode
|
void ClientMode::reset_normal_mode()
|
||||||
|
{
|
||||||
|
m_client.m_mode.reset(new NormalMode(m_client));
|
||||||
|
}
|
||||||
|
|
||||||
|
class MenuMode : public ClientMode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MenuMode(Client& client, const memoryview<String>& choices, MenuCallback callback)
|
MenuMode(Client& client, const memoryview<String>& choices, MenuCallback callback)
|
||||||
: Client::Mode(client),
|
: ClientMode(client),
|
||||||
m_callback(callback), m_choice_count(choices.size()), m_selected(0)
|
m_callback(callback), m_choice_count(choices.size()), m_selected(0)
|
||||||
{
|
{
|
||||||
client.m_ui->menu_show(choices);
|
ui().menu_show(choices);
|
||||||
}
|
}
|
||||||
|
|
||||||
~MenuMode()
|
~MenuMode()
|
||||||
{
|
{
|
||||||
m_client.m_ui->menu_hide();
|
ui().menu_hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_key(const Key& key, Context& context) override
|
void on_key(const Key& key, Context& context) override
|
||||||
|
@ -79,7 +88,7 @@ public:
|
||||||
{
|
{
|
||||||
if (++m_selected >= m_choice_count)
|
if (++m_selected >= m_choice_count)
|
||||||
m_selected = 0;
|
m_selected = 0;
|
||||||
m_client.m_ui->menu_select(m_selected);
|
ui().menu_select(m_selected);
|
||||||
}
|
}
|
||||||
if (key == Key::Up or
|
if (key == Key::Up or
|
||||||
key == Key::BackTab or
|
key == Key::BackTab or
|
||||||
|
@ -88,27 +97,27 @@ public:
|
||||||
{
|
{
|
||||||
if (--m_selected < 0)
|
if (--m_selected < 0)
|
||||||
m_selected = m_choice_count-1;
|
m_selected = m_choice_count-1;
|
||||||
m_client.m_ui->menu_select(m_selected);
|
ui().menu_select(m_selected);
|
||||||
}
|
}
|
||||||
if (key == Key(Key::Modifiers::Control, 'm'))
|
if (key == Key(Key::Modifiers::Control, 'm'))
|
||||||
{
|
{
|
||||||
// save callback as reset_normal_mode will delete this
|
// save callback as reset_normal_mode will delete this
|
||||||
MenuCallback callback = std::move(m_callback);
|
MenuCallback callback = std::move(m_callback);
|
||||||
int selected = m_selected;
|
int selected = m_selected;
|
||||||
m_client.reset_normal_mode();
|
reset_normal_mode();
|
||||||
callback(selected, context);
|
callback(selected, context);
|
||||||
}
|
}
|
||||||
if (key == Key::Escape)
|
if (key == Key::Escape)
|
||||||
{
|
{
|
||||||
m_client.reset_normal_mode();
|
reset_normal_mode();
|
||||||
}
|
}
|
||||||
if (key.modifiers == Key::Modifiers::None and
|
if (key.modifiers == Key::Modifiers::None and
|
||||||
key.key >= '0' and key.key <= '9')
|
key.key >= '0' and key.key <= '9')
|
||||||
{
|
{
|
||||||
m_client.m_ui->menu_hide();
|
ui().menu_hide();
|
||||||
// save callback as reset_normal_mode will delete this
|
// save callback as reset_normal_mode will delete this
|
||||||
MenuCallback callback = std::move(m_callback);
|
MenuCallback callback = std::move(m_callback);
|
||||||
m_client.reset_normal_mode();
|
reset_normal_mode();
|
||||||
callback(key.key - '0' - 1, context);
|
callback(key.key - '0' - 1, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,21 +128,21 @@ private:
|
||||||
int m_choice_count;
|
int m_choice_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Client::PromptMode : public Client::Mode
|
class PromptMode : public ClientMode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PromptMode(Client& client, const String& prompt,
|
PromptMode(Client& client, const String& prompt,
|
||||||
Completer completer, PromptCallback callback)
|
Completer completer, PromptCallback callback)
|
||||||
: Client::Mode(client), m_prompt(prompt),
|
: ClientMode(client), m_prompt(prompt),
|
||||||
m_completer(completer), m_callback(callback)
|
m_completer(completer), m_callback(callback)
|
||||||
{
|
{
|
||||||
m_history_it = ms_history[m_prompt].end();
|
m_history_it = ms_history[m_prompt].end();
|
||||||
m_client.print_status(m_prompt, m_prompt.length());
|
ui().print_status(m_prompt, m_prompt.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
~PromptMode()
|
~PromptMode()
|
||||||
{
|
{
|
||||||
m_client.m_ui->menu_hide();
|
ui().menu_hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_key(const Key& key, Context& context) override
|
void on_key(const Key& key, Context& context) override
|
||||||
|
@ -146,11 +155,11 @@ public:
|
||||||
history.erase(it);
|
history.erase(it);
|
||||||
|
|
||||||
history.push_back(m_result);
|
history.push_back(m_result);
|
||||||
m_client.print_status("");
|
ui().print_status("");
|
||||||
// save callback as reset_normal_mode will delete this
|
// save callback as reset_normal_mode will delete this
|
||||||
PromptCallback callback = std::move(m_callback);
|
PromptCallback callback = std::move(m_callback);
|
||||||
String result = std::move(m_result);
|
String result = std::move(m_result);
|
||||||
m_client.reset_normal_mode();
|
reset_normal_mode();
|
||||||
// call callback after reset_normal_mode so that callback
|
// call callback after reset_normal_mode so that callback
|
||||||
// may change the mode
|
// may change the mode
|
||||||
callback(result, context);
|
callback(result, context);
|
||||||
|
@ -158,8 +167,8 @@ public:
|
||||||
}
|
}
|
||||||
else if (key == Key::Escape)
|
else if (key == Key::Escape)
|
||||||
{
|
{
|
||||||
m_client.print_status("");
|
ui().print_status("");
|
||||||
m_client.reset_normal_mode();
|
reset_normal_mode();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (key == Key::Up or
|
else if (key == Key::Up or
|
||||||
|
@ -226,14 +235,14 @@ public:
|
||||||
--m_cursor_pos;
|
--m_cursor_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_client.m_ui->menu_hide();
|
ui().menu_hide();
|
||||||
m_current_completion = -1;
|
m_current_completion = -1;
|
||||||
}
|
}
|
||||||
else if (key == Key(Key::Modifiers::Control, 'r'))
|
else if (key == Key(Key::Modifiers::Control, 'r'))
|
||||||
{
|
{
|
||||||
Key k = m_client.m_ui->get_key();
|
Key k = ui().get_key();
|
||||||
String reg = RegisterManager::instance()[k.key].values(context)[0];
|
String reg = RegisterManager::instance()[k.key].values(context)[0];
|
||||||
m_client.m_ui->menu_hide();
|
ui().menu_hide();
|
||||||
m_current_completion = -1;
|
m_current_completion = -1;
|
||||||
m_result = m_result.substr(0, m_cursor_pos) + reg
|
m_result = m_result.substr(0, m_cursor_pos) + reg
|
||||||
+ m_result.substr(m_cursor_pos, String::npos);
|
+ m_result.substr(m_cursor_pos, String::npos);
|
||||||
|
@ -251,8 +260,8 @@ public:
|
||||||
if (candidates.empty())
|
if (candidates.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_client.m_ui->menu_hide();
|
ui().menu_hide();
|
||||||
m_client.m_ui->menu_show(candidates);
|
ui().menu_show(candidates);
|
||||||
String prefix = m_result.substr(m_completions.start,
|
String prefix = m_result.substr(m_completions.start,
|
||||||
m_completions.end - m_completions.start);
|
m_completions.end - m_completions.start);
|
||||||
if (not contains(candidates, prefix))
|
if (not contains(candidates, prefix))
|
||||||
|
@ -265,19 +274,19 @@ public:
|
||||||
m_current_completion = candidates.size()-1;
|
m_current_completion = candidates.size()-1;
|
||||||
|
|
||||||
const String& completion = candidates[m_current_completion];
|
const String& completion = candidates[m_current_completion];
|
||||||
m_client.m_ui->menu_select(m_current_completion);
|
ui().menu_select(m_current_completion);
|
||||||
m_result = m_result.substr(0, m_completions.start) + completion
|
m_result = m_result.substr(0, m_completions.start) + completion
|
||||||
+ m_result.substr(m_cursor_pos);
|
+ m_result.substr(m_cursor_pos);
|
||||||
m_cursor_pos = m_completions.start + completion.length();
|
m_cursor_pos = m_completions.start + completion.length();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_client.m_ui->menu_hide();
|
ui().menu_hide();
|
||||||
m_current_completion = -1;
|
m_current_completion = -1;
|
||||||
m_result = m_result.substr(0, m_cursor_pos) + key.key + m_result.substr(m_cursor_pos, String::npos);
|
m_result = m_result.substr(0, m_cursor_pos) + key.key + m_result.substr(m_cursor_pos, String::npos);
|
||||||
++m_cursor_pos;
|
++m_cursor_pos;
|
||||||
}
|
}
|
||||||
m_client.print_status(m_prompt + m_result, m_prompt.length() + m_cursor_pos);
|
ui().print_status(m_prompt + m_result, m_prompt.length() + m_cursor_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -293,19 +302,19 @@ private:
|
||||||
static std::unordered_map<String, std::vector<String>> ms_history;
|
static std::unordered_map<String, std::vector<String>> ms_history;
|
||||||
std::vector<String>::iterator m_history_it;
|
std::vector<String>::iterator m_history_it;
|
||||||
};
|
};
|
||||||
std::unordered_map<String, std::vector<String>> Client::PromptMode::ms_history;
|
std::unordered_map<String, std::vector<String>> PromptMode::ms_history;
|
||||||
|
|
||||||
class Client::NextKeyMode : public Client::Mode
|
class NextKeyMode : public ClientMode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NextKeyMode(Client& client, KeyCallback callback)
|
NextKeyMode(Client& client, KeyCallback callback)
|
||||||
: Client::Mode(client), m_callback(callback) {}
|
: ClientMode(client), m_callback(callback) {}
|
||||||
|
|
||||||
void on_key(const Key& key, Context& context) override
|
void on_key(const Key& key, Context& context) override
|
||||||
{
|
{
|
||||||
// save callback as reset_normal_mode will delete this
|
// save callback as reset_normal_mode will delete this
|
||||||
KeyCallback callback = std::move(m_callback);
|
KeyCallback callback = std::move(m_callback);
|
||||||
m_client.reset_normal_mode();
|
reset_normal_mode();
|
||||||
callback(key, context);
|
callback(key, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,19 +322,19 @@ private:
|
||||||
KeyCallback m_callback;
|
KeyCallback m_callback;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Client::InsertMode : public Client::Mode
|
class InsertMode : public ClientMode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InsertMode(Client& client, Editor& editor, IncrementalInserter::Mode mode)
|
InsertMode(Client& client, Editor& editor, IncrementalInserter::Mode mode)
|
||||||
: Client::Mode(client), m_inserter(editor, mode)
|
: ClientMode(client), m_inserter(editor, mode)
|
||||||
{
|
{
|
||||||
m_client.m_last_insert.first = mode;
|
last_insert().first = mode;
|
||||||
m_client.m_last_insert.second.clear();
|
last_insert().second.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_key(const Key& key, Context& context) override
|
void on_key(const Key& key, Context& context) override
|
||||||
{
|
{
|
||||||
m_client.m_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)
|
||||||
|
@ -339,7 +348,7 @@ public:
|
||||||
switch (key.key)
|
switch (key.key)
|
||||||
{
|
{
|
||||||
case Key::Escape:
|
case Key::Escape:
|
||||||
m_client.reset_normal_mode();
|
reset_normal_mode();
|
||||||
return;
|
return;
|
||||||
case Key::Backspace:
|
case Key::Backspace:
|
||||||
m_inserter.erase();
|
m_inserter.erase();
|
||||||
|
@ -445,10 +454,4 @@ void Client::draw_window(Window& window)
|
||||||
m_ui->draw_window(window);
|
m_ui->draw_window(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Client::reset_normal_mode()
|
|
||||||
{
|
|
||||||
m_mode.reset(new NormalMode(*this));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@ using MenuCallback = std::function<void (int, Context&)>;
|
||||||
using PromptCallback = std::function<void (const String&, Context&)>;
|
using PromptCallback = std::function<void (const String&, Context&)>;
|
||||||
using KeyCallback = std::function<void (const Key&, Context&)>;
|
using KeyCallback = std::function<void (const Key&, Context&)>;
|
||||||
|
|
||||||
|
class ClientMode;
|
||||||
|
|
||||||
class Client : public SafeCountable
|
class Client : public SafeCountable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -42,19 +44,10 @@ public:
|
||||||
void handle_next_input(Context& context);
|
void handle_next_input(Context& context);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void reset_normal_mode();
|
friend class ClientMode;
|
||||||
std::pair<IncrementalInserter::Mode, std::vector<Key>> m_last_insert;
|
std::unique_ptr<ClientMode> m_mode;
|
||||||
|
|
||||||
std::unique_ptr<UserInterface> m_ui;
|
std::unique_ptr<UserInterface> m_ui;
|
||||||
|
std::pair<IncrementalInserter::Mode, std::vector<Key>> m_last_insert;
|
||||||
class Mode;
|
|
||||||
std::unique_ptr<Mode> m_mode;
|
|
||||||
|
|
||||||
class NormalMode;
|
|
||||||
class MenuMode;
|
|
||||||
class PromptMode;
|
|
||||||
class NextKeyMode;
|
|
||||||
class InsertMode;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct prompt_aborted {};
|
struct prompt_aborted {};
|
||||||
|
|
|
@ -15,7 +15,7 @@ class UserInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~UserInterface() {}
|
virtual ~UserInterface() {}
|
||||||
virtual void print_status(const String& status, CharCount cursor_pos) = 0;
|
virtual void print_status(const String& status, CharCount cursor_pos = -1) = 0;
|
||||||
virtual void menu_show(const memoryview<String>& choices) = 0;
|
virtual void menu_show(const memoryview<String>& choices) = 0;
|
||||||
virtual void menu_select(int selected) = 0;
|
virtual void menu_select(int selected) = 0;
|
||||||
virtual void menu_hide() = 0;
|
virtual void menu_hide() = 0;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user