Move Insertion Hooks handing to the input handler
This commit is contained in:
parent
cedb0d18a3
commit
c45838cc57
|
@ -408,7 +408,6 @@ using utf8_it = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy::Pas
|
||||||
IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
|
IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
|
||||||
: m_editor(editor), m_edition(editor), m_mode(mode)
|
: m_editor(editor), m_edition(editor), m_mode(mode)
|
||||||
{
|
{
|
||||||
m_editor.on_incremental_insertion_begin();
|
|
||||||
Buffer& buffer = *editor.m_buffer;
|
Buffer& buffer = *editor.m_buffer;
|
||||||
|
|
||||||
if (mode == InsertMode::Replace)
|
if (mode == InsertMode::Replace)
|
||||||
|
@ -488,8 +487,6 @@ IncrementalInserter::~IncrementalInserter()
|
||||||
sel.last() = utf8::previous(sel.last());
|
sel.last() = utf8::previous(sel.last());
|
||||||
sel.avoid_eol();
|
sel.avoid_eol();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_editor.on_incremental_insertion_end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IncrementalInserter::insert(String content)
|
void IncrementalInserter::insert(String content)
|
||||||
|
|
|
@ -96,8 +96,6 @@ private:
|
||||||
void check_invariant() const;
|
void check_invariant() const;
|
||||||
|
|
||||||
friend class IncrementalInserter;
|
friend class IncrementalInserter;
|
||||||
virtual void on_incremental_insertion_begin() {}
|
|
||||||
virtual void on_incremental_insertion_end() {}
|
|
||||||
|
|
||||||
safe_ptr<Buffer> m_buffer;
|
safe_ptr<Buffer> m_buffer;
|
||||||
DynamicSelectionList m_selections;
|
DynamicSelectionList m_selections;
|
||||||
|
|
|
@ -31,6 +31,8 @@ private:
|
||||||
namespace InputModes
|
namespace InputModes
|
||||||
{
|
{
|
||||||
|
|
||||||
|
static constexpr std::chrono::milliseconds idle_timeout{100};
|
||||||
|
|
||||||
class Normal : public InputMode
|
class Normal : public InputMode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -513,12 +515,10 @@ private:
|
||||||
class Insert : public InputMode
|
class Insert : public InputMode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr std::chrono::milliseconds idle_timeout(){ return std::chrono::milliseconds{100}; }
|
|
||||||
|
|
||||||
Insert(Context& context, InsertMode mode)
|
Insert(Context& context, InsertMode mode)
|
||||||
: InputMode(context.input_handler()),
|
: InputMode(context.input_handler()),
|
||||||
m_inserter(context.editor(), mode),
|
m_inserter(context.editor(), mode),
|
||||||
m_idle_timer{Clock::time_point::max(),
|
m_idle_timer{Clock::now() + idle_timeout,
|
||||||
[this, &context](Timer& timer) {
|
[this, &context](Timer& timer) {
|
||||||
context.hooks().run_hook("InsertIdle", "", context);
|
context.hooks().run_hook("InsertIdle", "", context);
|
||||||
m_completer.reset(context);
|
m_completer.reset(context);
|
||||||
|
@ -532,7 +532,7 @@ public:
|
||||||
{
|
{
|
||||||
context.last_insert().first = mode;
|
context.last_insert().first = mode;
|
||||||
context.last_insert().second.clear();
|
context.last_insert().second.clear();
|
||||||
m_idle_timer.set_next_date(Clock::now() + idle_timeout());
|
context.hooks().run_hook("InsertBegin", "", context);
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_key(const Key& key, Context& context) override
|
void on_key(const Key& key, Context& context) override
|
||||||
|
@ -546,8 +546,10 @@ public:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bool reset_completer = true;
|
bool reset_completer = true;
|
||||||
|
bool moved = false;
|
||||||
if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' })
|
if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' })
|
||||||
{
|
{
|
||||||
|
context.hooks().run_hook("InsertEnd", "", context);
|
||||||
m_completer.reset(context);
|
m_completer.reset(context);
|
||||||
reset_normal_mode();
|
reset_normal_mode();
|
||||||
}
|
}
|
||||||
|
@ -556,11 +558,20 @@ public:
|
||||||
else if (key == Key::Left)
|
else if (key == Key::Left)
|
||||||
m_inserter.move_cursors(-1_char);
|
m_inserter.move_cursors(-1_char);
|
||||||
else if (key == Key::Right)
|
else if (key == Key::Right)
|
||||||
|
{
|
||||||
m_inserter.move_cursors(1_char);
|
m_inserter.move_cursors(1_char);
|
||||||
|
moved = true;
|
||||||
|
}
|
||||||
else if (key == Key::Up)
|
else if (key == Key::Up)
|
||||||
|
{
|
||||||
m_inserter.move_cursors(-1_line);
|
m_inserter.move_cursors(-1_line);
|
||||||
|
moved = true;
|
||||||
|
}
|
||||||
else if (key == Key::Down)
|
else if (key == Key::Down)
|
||||||
|
{
|
||||||
m_inserter.move_cursors(1_line);
|
m_inserter.move_cursors(1_line);
|
||||||
|
moved = true;
|
||||||
|
}
|
||||||
else if (key.modifiers == Key::Modifiers::None)
|
else if (key.modifiers == Key::Modifiers::None)
|
||||||
{
|
{
|
||||||
m_inserter.insert(codepoint_to_str(key.key));
|
m_inserter.insert(codepoint_to_str(key.key));
|
||||||
|
@ -585,8 +596,10 @@ public:
|
||||||
if (reset_completer)
|
if (reset_completer)
|
||||||
{
|
{
|
||||||
// m_completer.reset(context);
|
// m_completer.reset(context);
|
||||||
m_idle_timer.set_next_date(Clock::now() + idle_timeout());
|
m_idle_timer.set_next_date(Clock::now() + idle_timeout);
|
||||||
}
|
}
|
||||||
|
if (moved)
|
||||||
|
context.hooks().run_hook("InsertMove", "", context);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
bool m_insert_reg = false;
|
bool m_insert_reg = false;
|
||||||
|
@ -639,7 +652,7 @@ void InputHandler::repeat_last_insert(Context& context)
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputHandler::prompt(const String& prompt, Completer completer,
|
void InputHandler::prompt(const String& prompt, Completer completer,
|
||||||
PromptCallback callback, Context& context)
|
PromptCallback callback, Context& context)
|
||||||
{
|
{
|
||||||
assert(&context.input_handler() == this);
|
assert(&context.input_handler() == this);
|
||||||
m_mode_trash.emplace_back(std::move(m_mode));
|
m_mode_trash.emplace_back(std::move(m_mode));
|
||||||
|
@ -647,7 +660,7 @@ void InputHandler::prompt(const String& prompt, Completer completer,
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputHandler::menu(const memoryview<String>& choices,
|
void InputHandler::menu(const memoryview<String>& choices,
|
||||||
MenuCallback callback, Context& context)
|
MenuCallback callback, Context& context)
|
||||||
{
|
{
|
||||||
assert(&context.input_handler() == this);
|
assert(&context.input_handler() == this);
|
||||||
m_mode_trash.emplace_back(std::move(m_mode));
|
m_mode_trash.emplace_back(std::move(m_mode));
|
||||||
|
|
|
@ -22,7 +22,7 @@ hook global WinSetOption filetype=cpp %~
|
||||||
addfilter -group cpp-filters regex ^(\h+)([^\n]*[^([{]\h*|$) \n \n$1
|
addfilter -group cpp-filters regex ^(\h+)([^\n]*[^([{]\h*|$) \n \n$1
|
||||||
addfilter -group cpp-filters regex ^(\h*)[^\n]*[([{]\h* \n '\n$1 '
|
addfilter -group cpp-filters regex ^(\h*)[^\n]*[([{]\h* \n '\n$1 '
|
||||||
addfilter -group cpp-filters cleanup_whitespaces
|
addfilter -group cpp-filters cleanup_whitespaces
|
||||||
hook window InsertEnd .* %{ exec xs\h+(?=\n)<ret>d }
|
hook window InsertEnd .* %{ exec -restore-selections <a-x>s\h+$<ret>d }
|
||||||
~
|
~
|
||||||
|
|
||||||
hook global WinSetOption filetype=(?!cpp).* %{
|
hook global WinSetOption filetype=(?!cpp).* %{
|
||||||
|
|
|
@ -176,13 +176,6 @@ String Window::status_line() const
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::on_incremental_insertion_end()
|
|
||||||
{
|
|
||||||
DynamicSelectionList backup(buffer(), selections());
|
|
||||||
hooks().run_hook("InsertEnd", "", Context(*this));
|
|
||||||
select((SelectionList)backup);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::on_option_changed(const String& name, const Option& option)
|
void Window::on_option_changed(const String& name, const Option& option)
|
||||||
{
|
{
|
||||||
String desc = name + "=" + option.as_string();
|
String desc = name + "=" + option.as_string();
|
||||||
|
|
|
@ -53,7 +53,6 @@ public:
|
||||||
private:
|
private:
|
||||||
Window(const Window&) = delete;
|
Window(const Window&) = delete;
|
||||||
|
|
||||||
void on_incremental_insertion_end() override;
|
|
||||||
void on_option_changed(const String& name, const Option& option) override;
|
void on_option_changed(const String& name, const Option& option) override;
|
||||||
|
|
||||||
void scroll_to_keep_cursor_visible_ifn();
|
void scroll_to_keep_cursor_visible_ifn();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user