2021-01-02 11:59:55 +01:00
|
|
|
/*
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2021-2022, the SerenityOS developers.
|
2021-01-02 11:59:55 +01:00
|
|
|
*
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-07 22:32:35 +02:00
|
|
|
class MoveLineUpOrDownCommand;
|
|
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
class EditingEngine {
|
|
|
|
|
AK_MAKE_NONCOPYABLE(EditingEngine);
|
|
|
|
|
AK_MAKE_NONMOVABLE(EditingEngine);
|
|
|
|
|
|
|
|
|
|
public:
|
2022-02-26 10:50:04 -07:00
|
|
|
virtual ~EditingEngine() = default;
|
2021-01-02 11:59:55 +01:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual bool on_key(KeyEvent const& event);
|
2021-01-02 11:59:55 +01:00
|
|
|
|
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; }
|
|
|
|
|
|
2022-06-07 22:32:35 +02:00
|
|
|
void get_selection_line_boundaries(Badge<MoveLineUpOrDownCommand>, size_t& first_line, size_t& last_line);
|
|
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
protected:
|
2022-02-26 10:50:04 -07:00
|
|
|
EditingEngine() = default;
|
2021-01-02 11:59:55 +01:00
|
|
|
|
|
|
|
|
WeakPtr<TextEditor> m_editor;
|
|
|
|
|
|
2022-05-17 20:43:22 +02:00
|
|
|
enum class DidMoveALine {
|
|
|
|
|
No,
|
|
|
|
|
Yes,
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-26 20:29:05 +00:00
|
|
|
void move_one_left();
|
|
|
|
|
void move_one_right();
|
2022-05-17 20:43:22 +02:00
|
|
|
void move_one_helper(KeyEvent const& event, VerticalDirection direction);
|
|
|
|
|
DidMoveALine move_one_up(KeyEvent const& event);
|
|
|
|
|
DidMoveALine move_one_down(KeyEvent const& 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-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;
|
2022-06-07 22:32:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MoveLineUpOrDownCommand : public TextDocumentUndoCommand {
|
|
|
|
|
public:
|
|
|
|
|
MoveLineUpOrDownCommand(TextDocument&, KeyEvent event, EditingEngine&);
|
|
|
|
|
virtual void undo() override;
|
|
|
|
|
virtual void redo() override;
|
|
|
|
|
bool merge_with(GUI::Command const&) override;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString action_text() const override;
|
2022-06-07 22:32:35 +02:00
|
|
|
|
|
|
|
|
static bool valid_operation(EditingEngine& engine, VerticalDirection direction);
|
2021-12-08 16:26:20 -06:00
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
private:
|
2022-06-07 22:32:35 +02:00
|
|
|
void move_lines(VerticalDirection);
|
|
|
|
|
TextRange retrieve_selection(VerticalDirection);
|
|
|
|
|
|
|
|
|
|
KeyEvent m_event;
|
|
|
|
|
VerticalDirection m_direction;
|
|
|
|
|
EditingEngine& m_engine;
|
|
|
|
|
TextRange m_selection;
|
|
|
|
|
TextPosition m_cursor;
|
2021-01-02 11:59:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|