2021-01-02 11:59:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-02 11:59:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <LibGUI/Event.h>
|
|
|
|
#include <LibGUI/TextDocument.h>
|
|
|
|
|
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
enum CursorWidth {
|
|
|
|
NARROW,
|
|
|
|
WIDE
|
|
|
|
};
|
|
|
|
|
2021-12-08 16:26:20 -06:00
|
|
|
enum EngineType {
|
|
|
|
Regular,
|
|
|
|
Vim,
|
|
|
|
};
|
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
class EditingEngine {
|
|
|
|
AK_MAKE_NONCOPYABLE(EditingEngine);
|
|
|
|
AK_MAKE_NONMOVABLE(EditingEngine);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~EditingEngine();
|
|
|
|
|
|
|
|
virtual CursorWidth cursor_width() const { return NARROW; }
|
|
|
|
|
|
|
|
void attach(TextEditor& editor);
|
|
|
|
void detach();
|
|
|
|
|
LibGUI: Implement Vim motion system
This patch implements Vim motions. The VimMotion class will accept
keycodes from the editing engine to build up a motion, and will
signal when a motion is complete via VimMotion::is_complete(). The
editing engine can then call VimMotion::get_range() to obtain a
TextRange object which can be used to perform operations on the text,
or VimMotion::get_position() to obtain a TextPosition which is the
new position of the cursor after the motion.
Currently, the following motions are supported:
- h/j/k/l, regular Vim line and character movements
- 0/^/$, start/end of line and start of non-blank
- w/e/b/ge, word-related movements
- W/E/B/gE, WORD (anything non-blank) versions of the above motions
- gg/G, document related movements
- t/f, to/find character
All motions except gg/G accept a number prefix to repeat the motion that
many times.
This patch updates insert, normal and visual modes to use this motion
system for movement.
2021-04-18 02:32:42 +00:00
|
|
|
TextEditor& editor()
|
|
|
|
{
|
|
|
|
VERIFY(!m_editor.is_null());
|
|
|
|
return *m_editor.unsafe_ptr();
|
|
|
|
}
|
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
virtual bool on_key(const KeyEvent& event);
|
|
|
|
|
2021-12-08 16:26:20 -06:00
|
|
|
bool is_regular() const { return engine_type() == EngineType::Regular; }
|
|
|
|
bool is_vim() const { return engine_type() == EngineType::Vim; }
|
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
protected:
|
|
|
|
EditingEngine() { }
|
|
|
|
|
|
|
|
WeakPtr<TextEditor> m_editor;
|
|
|
|
|
2021-04-26 20:29:05 +00:00
|
|
|
void move_one_left();
|
|
|
|
void move_one_right();
|
2021-01-02 11:59:55 +01:00
|
|
|
void move_one_up(const KeyEvent& event);
|
|
|
|
void move_one_down(const KeyEvent& event);
|
2021-04-26 20:29:05 +00:00
|
|
|
void move_to_previous_span();
|
2021-10-12 20:40:38 +02:00
|
|
|
void move_to_next_span();
|
2021-07-10 21:48:49 +01:00
|
|
|
void move_to_logical_line_beginning();
|
|
|
|
void move_to_logical_line_end();
|
2021-04-26 20:29:05 +00:00
|
|
|
void move_to_line_beginning();
|
|
|
|
void move_to_line_end();
|
|
|
|
void move_page_up();
|
|
|
|
void move_page_down();
|
2021-01-02 11:59:55 +01:00
|
|
|
void move_to_first_line();
|
|
|
|
void move_to_last_line();
|
2021-01-27 21:57:39 +10:00
|
|
|
TextPosition find_beginning_of_next_word();
|
2021-01-25 04:24:56 +10:00
|
|
|
void move_to_beginning_of_next_word();
|
2021-01-27 21:57:39 +10:00
|
|
|
TextPosition find_end_of_next_word();
|
2021-01-25 04:24:56 +10:00
|
|
|
void move_to_end_of_next_word();
|
2021-01-27 21:57:39 +10:00
|
|
|
TextPosition find_end_of_previous_word();
|
2021-01-25 04:24:56 +10:00
|
|
|
void move_to_end_of_previous_word();
|
2021-01-27 21:57:39 +10:00
|
|
|
TextPosition find_beginning_of_previous_word();
|
2021-01-25 04:24:56 +10:00
|
|
|
void move_to_beginning_of_previous_word();
|
2021-01-02 11:59:55 +01:00
|
|
|
|
2021-04-26 20:29:05 +00:00
|
|
|
void move_up(double page_height_factor);
|
|
|
|
void move_down(double page_height_factor);
|
2021-01-02 11:59:55 +01:00
|
|
|
|
|
|
|
void get_selection_line_boundaries(size_t& first_line, size_t& last_line);
|
|
|
|
|
|
|
|
void delete_line();
|
|
|
|
void delete_char();
|
|
|
|
|
2021-12-08 16:26:20 -06:00
|
|
|
virtual EngineType engine_type() const = 0;
|
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
private:
|
|
|
|
void move_selected_lines_up();
|
|
|
|
void move_selected_lines_down();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|