2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-03-29 16:33:46 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-03-07 00:31:06 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2019-07-24 09:12:23 +02:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2019-08-25 21:33:08 +02:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2020-02-15 00:24:14 +01:00
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2021-01-01 17:29:11 +03:30
|
|
|
#include <LibCore/Timer.h>
|
2021-05-03 20:31:58 +02:00
|
|
|
#include <LibGUI/AbstractScrollableWidget.h>
|
2021-07-27 03:19:56 +08:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/Clipboard.h>
|
2020-12-30 13:55:06 +03:30
|
|
|
#include <LibGUI/Forward.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/TextDocument.h>
|
|
|
|
|
#include <LibGUI/TextRange.h>
|
2020-02-15 00:24:14 +01:00
|
|
|
#include <LibGfx/TextAlignment.h>
|
2021-02-07 15:15:10 +01:00
|
|
|
#include <LibSyntax/Forward.h>
|
2021-02-07 16:56:02 +01:00
|
|
|
#include <LibSyntax/HighlighterClient.h>
|
2019-03-07 00:31:06 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
class TextEditor
|
2021-05-03 20:31:58 +02:00
|
|
|
: public AbstractScrollableWidget
|
2021-02-07 16:56:02 +01:00
|
|
|
, public TextDocument::Client
|
2021-07-27 03:19:56 +08:00
|
|
|
, public Syntax::HighlighterClient
|
|
|
|
|
, public Clipboard::ClipboardClient {
|
2021-02-07 16:56:02 +01:00
|
|
|
C_OBJECT(TextEditor);
|
2021-01-02 11:59:55 +01:00
|
|
|
|
2019-03-07 00:31:06 +01:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum Type {
|
2019-05-28 11:53:16 +02:00
|
|
|
MultiLine,
|
|
|
|
|
SingleLine
|
|
|
|
|
};
|
2020-07-14 17:02:46 -04:00
|
|
|
|
|
|
|
|
enum Mode {
|
|
|
|
|
Editable,
|
|
|
|
|
ReadOnly,
|
|
|
|
|
DisplayOnly
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-09 22:47:48 +10:00
|
|
|
enum WrappingMode {
|
|
|
|
|
NoWrap,
|
|
|
|
|
WrapAnywhere,
|
|
|
|
|
WrapAtWords
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual ~TextEditor() override;
|
2019-03-07 00:31:06 +01:00
|
|
|
|
2021-07-09 21:34:20 +10:00
|
|
|
TextDocument const& document() const { return *m_document; }
|
2020-02-02 15:07:41 +01:00
|
|
|
TextDocument& document() { return *m_document; }
|
2021-06-25 12:37:30 +03:00
|
|
|
bool has_document() const { return !!m_document; }
|
2019-10-27 16:10:07 +01:00
|
|
|
|
2020-08-15 10:58:31 +03:00
|
|
|
virtual void set_document(TextDocument&);
|
2019-10-27 19:36:59 +01:00
|
|
|
|
2021-07-09 21:34:20 +10:00
|
|
|
String const& placeholder() const { return m_placeholder; }
|
2021-11-11 00:55:02 +01:00
|
|
|
void set_placeholder(StringView placeholder) { m_placeholder = placeholder; }
|
2020-09-20 12:20:24 -07:00
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
TextDocumentLine& current_line() { return line(m_cursor.line()); }
|
2021-07-09 21:34:20 +10:00
|
|
|
TextDocumentLine const& current_line() const { return line(m_cursor.line()); }
|
2021-01-02 11:59:55 +01:00
|
|
|
|
2020-09-01 19:10:55 +02:00
|
|
|
void set_visualize_trailing_whitespace(bool);
|
|
|
|
|
bool visualize_trailing_whitespace() const { return m_visualize_trailing_whitespace; }
|
|
|
|
|
|
2021-03-17 13:52:42 -03:00
|
|
|
void set_visualize_leading_whitespace(bool);
|
|
|
|
|
bool visualize_leading_whitespace() const { return m_visualize_leading_whitespace; }
|
|
|
|
|
|
2021-09-11 11:08:01 -04:00
|
|
|
bool is_cursor_line_highlighted() const { return m_cursor_line_highlighting; }
|
|
|
|
|
void set_cursor_line_highlighting(bool);
|
|
|
|
|
|
2020-01-23 21:04:59 +01:00
|
|
|
virtual bool is_automatic_indentation_enabled() const final { return m_automatic_indentation_enabled; }
|
2019-04-25 22:56:09 +02:00
|
|
|
void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; }
|
|
|
|
|
|
2020-01-23 21:04:59 +01:00
|
|
|
virtual int soft_tab_width() const final { return m_soft_tab_width; }
|
2021-03-15 18:28:00 -03:00
|
|
|
void set_soft_tab_width(int width) { m_soft_tab_width = width; };
|
2020-01-23 21:04:59 +01:00
|
|
|
|
2021-01-09 22:47:48 +10:00
|
|
|
WrappingMode wrapping_mode() const { return m_wrapping_mode; }
|
|
|
|
|
bool is_wrapping_enabled() const { return m_wrapping_mode != WrappingMode::NoWrap; }
|
|
|
|
|
void set_wrapping_mode(WrappingMode);
|
2019-08-25 08:43:01 +02:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
|
|
|
|
|
void set_text_alignment(Gfx::TextAlignment);
|
2019-04-24 22:24:16 +02:00
|
|
|
|
2019-03-15 17:37:13 +01:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
bool is_single_line() const { return m_type == SingleLine; }
|
|
|
|
|
bool is_multi_line() const { return m_type == MultiLine; }
|
|
|
|
|
|
2020-07-14 17:02:46 -04:00
|
|
|
Mode mode() const { return m_mode; }
|
|
|
|
|
bool is_editable() const { return m_mode == Editable; }
|
|
|
|
|
bool is_readonly() const { return m_mode == ReadOnly; }
|
|
|
|
|
bool is_displayonly() const { return m_mode == DisplayOnly; }
|
|
|
|
|
void set_mode(const Mode);
|
|
|
|
|
|
2019-04-11 04:01:17 +02:00
|
|
|
bool is_ruler_visible() const { return m_ruler_visible; }
|
2021-04-10 00:09:44 +02:00
|
|
|
void set_ruler_visible(bool);
|
2019-04-11 04:01:17 +02:00
|
|
|
|
2021-06-12 04:50:23 +03:00
|
|
|
bool is_gutter_visible() const { return m_gutter_visible; }
|
|
|
|
|
void set_gutter_visible(bool);
|
|
|
|
|
|
2021-07-09 21:34:20 +10:00
|
|
|
void set_icon(Gfx::Bitmap const*);
|
|
|
|
|
Gfx::Bitmap const* icon() const { return m_icon; }
|
2020-06-29 20:34:42 +02:00
|
|
|
|
2019-04-10 03:08:29 +02:00
|
|
|
Function<void()> on_cursor_change;
|
2019-04-12 02:52:34 +02:00
|
|
|
Function<void()> on_selection_change;
|
2020-07-04 22:37:55 +04:30
|
|
|
Function<void()> on_focusin;
|
|
|
|
|
Function<void()> on_focusout;
|
2022-02-22 15:00:56 -05:00
|
|
|
Function<void()> on_highlighter_change;
|
2019-03-07 00:31:06 +01:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
void set_text(StringView, AllowCallback = AllowCallback::Yes);
|
2019-03-07 13:13:25 +01:00
|
|
|
void scroll_cursor_into_view();
|
2021-07-09 21:34:20 +10:00
|
|
|
void scroll_position_into_view(TextPosition const&);
|
2019-12-09 17:45:40 +01:00
|
|
|
size_t line_count() const { return document().line_count(); }
|
2021-01-02 11:59:55 +01:00
|
|
|
TextDocumentLine& line(size_t index) { return document().line(index); }
|
2021-07-09 21:34:20 +10:00
|
|
|
TextDocumentLine const& line(size_t index) const { return document().line(index); }
|
2021-01-02 11:59:55 +01:00
|
|
|
NonnullOwnPtrVector<TextDocumentLine>& lines() { return document().lines(); }
|
2021-07-09 21:34:20 +10:00
|
|
|
NonnullOwnPtrVector<TextDocumentLine> const& lines() const { return document().lines(); }
|
2020-02-15 00:24:14 +01:00
|
|
|
int line_height() const;
|
2020-02-02 15:07:41 +01:00
|
|
|
TextPosition cursor() const { return m_cursor; }
|
|
|
|
|
TextRange normalized_selection() const { return m_selection.normalized(); }
|
2019-03-07 00:31:06 +01:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
void insert_at_cursor_or_replace_selection(StringView);
|
2022-04-13 16:14:59 +02:00
|
|
|
void replace_all_text_without_resetting_undo_stack(StringView text);
|
2021-06-30 06:13:06 -07:00
|
|
|
bool write_to_file(String const& path);
|
2022-01-16 22:43:40 -05:00
|
|
|
bool write_to_file(Core::File&);
|
2019-03-08 18:28:24 +01:00
|
|
|
bool has_selection() const { return m_selection.is_valid(); }
|
2019-03-08 01:59:59 +01:00
|
|
|
String selected_text() const;
|
2021-09-18 16:31:47 -04:00
|
|
|
size_t number_of_words() const;
|
2021-05-25 20:58:55 -04:00
|
|
|
size_t number_of_selected_words() const;
|
2021-07-09 21:34:20 +10:00
|
|
|
void set_selection(TextRange const&);
|
2019-12-01 18:48:32 -08:00
|
|
|
void clear_selection();
|
2019-11-30 13:05:17 +01:00
|
|
|
bool can_undo() const { return document().can_undo(); }
|
|
|
|
|
bool can_redo() const { return document().can_redo(); }
|
2019-08-21 21:23:17 +02:00
|
|
|
|
2019-03-15 17:37:13 +01:00
|
|
|
String text() const;
|
|
|
|
|
|
|
|
|
|
void clear();
|
2019-03-08 01:59:59 +01:00
|
|
|
|
2019-03-08 14:08:15 +01:00
|
|
|
void cut();
|
|
|
|
|
void copy();
|
|
|
|
|
void paste();
|
2019-03-20 23:11:00 +01:00
|
|
|
void do_delete();
|
2019-03-25 13:13:46 +01:00
|
|
|
void delete_current_line();
|
2021-07-13 22:12:07 -05:00
|
|
|
void delete_previous_word();
|
2021-07-15 15:50:03 -05:00
|
|
|
void delete_previous_char();
|
2021-07-15 16:35:45 -05:00
|
|
|
void delete_from_line_start_to_cursor();
|
2019-06-22 10:38:38 +02:00
|
|
|
void select_all();
|
2021-08-15 15:48:53 +10:00
|
|
|
void select_current_line();
|
2021-05-08 23:38:01 +02:00
|
|
|
virtual void undo();
|
|
|
|
|
virtual void redo();
|
2022-06-18 12:02:51 -07:00
|
|
|
bool is_indenting_selection();
|
|
|
|
|
void indent_selection();
|
2022-06-18 12:03:49 -07:00
|
|
|
void unindent_selection();
|
2019-03-08 14:08:15 +01:00
|
|
|
|
2019-04-09 16:20:36 +02:00
|
|
|
Function<void()> on_change;
|
2021-05-08 13:40:33 +02:00
|
|
|
Function<void(bool modified)> on_modified_change;
|
2020-07-14 17:18:12 -04:00
|
|
|
Function<void()> on_mousedown;
|
2019-04-10 03:08:29 +02:00
|
|
|
Function<void()> on_return_pressed;
|
2021-06-02 13:21:17 +02:00
|
|
|
Function<void()> on_shift_return_pressed;
|
2022-03-19 12:09:44 +01:00
|
|
|
Function<void()> on_ctrl_return_pressed;
|
2019-04-10 03:08:29 +02:00
|
|
|
Function<void()> on_escape_pressed;
|
2020-07-14 17:18:12 -04:00
|
|
|
Function<void()> on_up_pressed;
|
|
|
|
|
Function<void()> on_down_pressed;
|
|
|
|
|
Function<void()> on_pageup_pressed;
|
|
|
|
|
Function<void()> on_pagedown_pressed;
|
2019-03-15 17:37:13 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Action& undo_action() { return *m_undo_action; }
|
|
|
|
|
Action& redo_action() { return *m_redo_action; }
|
|
|
|
|
Action& cut_action() { return *m_cut_action; }
|
|
|
|
|
Action& copy_action() { return *m_copy_action; }
|
|
|
|
|
Action& paste_action() { return *m_paste_action; }
|
|
|
|
|
Action& go_to_line_action() { return *m_go_to_line_action; }
|
2020-04-20 21:10:03 +02:00
|
|
|
Action& select_all_action() { return *m_select_all_action; }
|
2019-04-18 12:25:00 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void add_custom_context_menu_action(Action&);
|
2019-08-25 21:33:08 +02:00
|
|
|
|
2020-11-10 08:53:50 +00:00
|
|
|
void set_cursor_and_focus_line(size_t line, size_t column);
|
2019-12-09 17:45:40 +01:00
|
|
|
void set_cursor(size_t line, size_t column);
|
2021-07-09 21:34:20 +10:00
|
|
|
virtual void set_cursor(TextPosition const&);
|
2019-10-21 18:58:27 +02:00
|
|
|
|
2022-02-07 08:08:09 +02:00
|
|
|
Syntax::Highlighter* syntax_highlighter();
|
2021-07-09 21:34:20 +10:00
|
|
|
Syntax::Highlighter const* syntax_highlighter() const;
|
2021-02-07 15:15:10 +01:00
|
|
|
void set_syntax_highlighter(OwnPtr<Syntax::Highlighter>);
|
2020-02-07 20:07:15 +01:00
|
|
|
|
2021-07-09 21:34:20 +10:00
|
|
|
AutocompleteProvider const* autocomplete_provider() const;
|
2020-12-30 13:55:06 +03:30
|
|
|
void set_autocomplete_provider(OwnPtr<AutocompleteProvider>&&);
|
|
|
|
|
|
2021-07-09 21:34:20 +10:00
|
|
|
EditingEngine const* editing_engine() const;
|
2021-01-02 11:59:55 +01:00
|
|
|
void set_editing_engine(OwnPtr<EditingEngine>);
|
|
|
|
|
|
2021-01-01 17:29:11 +03:30
|
|
|
bool should_autocomplete_automatically() const { return m_autocomplete_timer; }
|
|
|
|
|
void set_should_autocomplete_automatically(bool);
|
|
|
|
|
|
2022-05-07 16:03:33 +02:00
|
|
|
Optional<u32> const& substitution_code_point() const { return m_substitution_code_point; }
|
|
|
|
|
void set_substitution_code_point(Optional<u32> code_point);
|
2021-06-25 17:48:51 +02:00
|
|
|
|
2020-05-30 02:01:35 -04:00
|
|
|
bool is_in_drag_select() const { return m_in_drag_select; }
|
|
|
|
|
|
2021-10-11 22:51:40 +02:00
|
|
|
TextRange& selection() { return m_selection; };
|
2021-01-02 11:59:55 +01:00
|
|
|
void did_update_selection();
|
2021-09-21 17:02:48 -04:00
|
|
|
void did_change(AllowCallback = AllowCallback::Yes);
|
2021-01-02 11:59:55 +01:00
|
|
|
void update_cursor();
|
|
|
|
|
|
|
|
|
|
void add_code_point(u32 code_point);
|
|
|
|
|
void reset_cursor_blink();
|
2021-04-26 20:29:05 +00:00
|
|
|
void update_selection(bool is_selecting);
|
2021-01-02 11:59:55 +01:00
|
|
|
|
|
|
|
|
int number_of_visible_lines() const;
|
|
|
|
|
Gfx::IntRect cursor_content_rect() const;
|
2021-07-09 21:34:20 +10:00
|
|
|
TextPosition text_position_at_content_position(Gfx::IntPoint const&) const;
|
2021-01-02 11:59:55 +01:00
|
|
|
|
2021-01-27 21:57:39 +10:00
|
|
|
void delete_text_range(TextRange);
|
|
|
|
|
|
2021-08-02 11:10:56 +02:00
|
|
|
bool text_is_secret() const { return m_text_is_secret; }
|
|
|
|
|
void set_text_is_secret(bool text_is_secret);
|
2022-02-07 08:08:09 +02:00
|
|
|
void force_rehighlight();
|
2021-08-02 11:10:56 +02:00
|
|
|
|
2022-03-29 16:33:46 +03:00
|
|
|
enum class SearchDirection {
|
|
|
|
|
Forward,
|
|
|
|
|
Backward,
|
|
|
|
|
};
|
|
|
|
|
TextRange find_text(StringView needle, SearchDirection, GUI::TextDocument::SearchShouldWrap, bool use_regex, bool match_case);
|
|
|
|
|
void reset_search_results();
|
|
|
|
|
Optional<size_t> search_result_index() const { return m_search_result_index; }
|
|
|
|
|
Vector<TextRange> const& search_results() const { return m_search_results; }
|
|
|
|
|
|
2022-02-22 20:25:30 +01:00
|
|
|
virtual Optional<UISize> calculated_min_size() const override;
|
|
|
|
|
|
2022-06-07 22:32:35 +02:00
|
|
|
template<class T, class... Args>
|
|
|
|
|
inline void execute(Badge<EditingEngine>, Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
execute<T>(forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-01 12:26:35 +02:00
|
|
|
protected:
|
2020-02-23 12:07:13 +01:00
|
|
|
explicit TextEditor(Type = Type::MultiLine);
|
2019-09-01 12:26:35 +02:00
|
|
|
|
2019-09-21 15:43:52 +02:00
|
|
|
virtual void did_change_font() override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void paint_event(PaintEvent&) override;
|
|
|
|
|
virtual void mousedown_event(MouseEvent&) override;
|
|
|
|
|
virtual void mouseup_event(MouseEvent&) override;
|
|
|
|
|
virtual void mousemove_event(MouseEvent&) override;
|
|
|
|
|
virtual void doubleclick_event(MouseEvent&) override;
|
|
|
|
|
virtual void keydown_event(KeyEvent&) override;
|
2020-08-14 19:56:40 +02:00
|
|
|
virtual void focusin_event(FocusEvent&) override;
|
|
|
|
|
virtual void focusout_event(FocusEvent&) override;
|
2020-02-02 12:34:39 +01:00
|
|
|
virtual void timer_event(Core::TimerEvent&) override;
|
|
|
|
|
virtual void enter_event(Core::Event&) override;
|
|
|
|
|
virtual void leave_event(Core::Event&) override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void context_menu_event(ContextMenuEvent&) override;
|
|
|
|
|
virtual void resize_event(ResizeEvent&) override;
|
2020-03-16 13:36:21 +02:00
|
|
|
virtual void theme_change_event(ThemeChangeEvent&) override;
|
2021-10-27 23:39:55 -05:00
|
|
|
virtual void cursor_did_change();
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect ruler_content_rect(size_t line) const;
|
2021-06-12 04:50:23 +03:00
|
|
|
Gfx::IntRect gutter_content_rect(size_t line) const;
|
2019-03-31 23:52:02 +02:00
|
|
|
|
2021-07-09 21:34:20 +10:00
|
|
|
TextPosition text_position_at(Gfx::IntPoint const&) const;
|
2020-04-24 23:48:25 +03:00
|
|
|
bool ruler_visible() const { return m_ruler_visible; }
|
2021-06-12 04:50:23 +03:00
|
|
|
bool gutter_visible() const { return m_gutter_visible; }
|
2021-07-09 21:34:20 +10:00
|
|
|
Gfx::IntRect content_rect_for_position(TextPosition const&) const;
|
2020-09-20 20:58:46 +03:00
|
|
|
int ruler_width() const;
|
2021-06-12 04:50:23 +03:00
|
|
|
int gutter_width() const;
|
2019-10-29 21:37:46 +01:00
|
|
|
|
2022-03-29 16:31:26 +03:00
|
|
|
virtual void highlighter_did_set_spans(Vector<TextDocumentSpan> spans) final { document().set_spans(Syntax::HighlighterClient::span_collection_index, move(spans)); }
|
|
|
|
|
|
2019-09-21 15:43:52 +02:00
|
|
|
private:
|
2020-02-02 15:07:41 +01:00
|
|
|
friend class TextDocumentLine;
|
2019-10-27 16:10:07 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
// ^TextDocument::Client
|
2019-10-27 18:00:07 +01:00
|
|
|
virtual void document_did_append_line() override;
|
2019-12-09 17:45:40 +01:00
|
|
|
virtual void document_did_insert_line(size_t) override;
|
|
|
|
|
virtual void document_did_remove_line(size_t) override;
|
2019-10-27 18:00:07 +01:00
|
|
|
virtual void document_did_remove_all_lines() override;
|
2021-09-21 17:02:48 -04:00
|
|
|
virtual void document_did_change(AllowCallback = AllowCallback::Yes) override;
|
|
|
|
|
virtual void document_did_set_text(AllowCallback = AllowCallback::Yes) override;
|
2021-07-09 21:34:20 +10:00
|
|
|
virtual void document_did_set_cursor(TextPosition const&) override;
|
2021-05-08 13:16:37 +02:00
|
|
|
virtual void document_did_update_undo_stack() override;
|
2019-10-27 18:00:07 +01:00
|
|
|
|
2021-02-07 16:56:02 +01:00
|
|
|
// ^Syntax::HighlighterClient
|
|
|
|
|
virtual Vector<TextDocumentSpan>& spans() final { return document().spans(); }
|
2021-07-09 21:34:20 +10:00
|
|
|
virtual Vector<TextDocumentSpan> const& spans() const final { return document().spans(); }
|
2021-02-07 16:56:02 +01:00
|
|
|
virtual void set_span_at_index(size_t index, TextDocumentSpan span) final { document().set_span_at_index(index, move(span)); }
|
|
|
|
|
virtual void highlighter_did_request_update() final { update(); }
|
|
|
|
|
virtual String highlighter_did_request_text() const final { return text(); }
|
|
|
|
|
virtual GUI::TextDocument& highlighter_did_request_document() final { return document(); }
|
|
|
|
|
virtual GUI::TextPosition highlighter_did_request_cursor() const final { return m_cursor; }
|
|
|
|
|
|
2021-07-27 03:19:56 +08:00
|
|
|
// ^Clipboard::ClipboardClient
|
|
|
|
|
virtual void clipboard_content_did_change(String const& mime_type) override;
|
|
|
|
|
|
2019-04-18 12:25:00 +02:00
|
|
|
void create_actions();
|
2019-03-15 17:54:05 +01:00
|
|
|
void paint_ruler(Painter&);
|
2019-03-16 16:54:51 +01:00
|
|
|
void update_content_size();
|
2020-05-18 16:38:28 +02:00
|
|
|
int fixed_glyph_width() const;
|
2019-03-07 00:31:06 +01:00
|
|
|
|
2020-05-27 19:18:40 +02:00
|
|
|
void defer_reflow();
|
|
|
|
|
void undefer_reflow();
|
|
|
|
|
|
2021-10-27 23:13:49 -05:00
|
|
|
enum UserRequestedAutocomplete {
|
|
|
|
|
No,
|
|
|
|
|
Yes
|
|
|
|
|
};
|
|
|
|
|
void try_show_autocomplete(UserRequestedAutocomplete);
|
2021-10-28 02:04:33 -05:00
|
|
|
void try_update_autocomplete(Function<void()> callback = {});
|
|
|
|
|
void force_update_autocomplete(Function<void()> callback = {});
|
2021-10-27 23:39:55 -05:00
|
|
|
void hide_autocomplete_if_needed();
|
2021-10-28 20:23:58 -05:00
|
|
|
void hide_autocomplete();
|
2021-01-01 17:29:11 +03:30
|
|
|
|
2020-06-29 20:34:42 +02:00
|
|
|
int icon_size() const { return 16; }
|
|
|
|
|
int icon_padding() const { return 2; }
|
|
|
|
|
|
2020-05-27 19:18:40 +02:00
|
|
|
class ReflowDeferrer {
|
|
|
|
|
public:
|
|
|
|
|
ReflowDeferrer(TextEditor& editor)
|
|
|
|
|
: m_editor(editor)
|
|
|
|
|
{
|
|
|
|
|
m_editor.defer_reflow();
|
|
|
|
|
}
|
|
|
|
|
~ReflowDeferrer()
|
|
|
|
|
{
|
|
|
|
|
m_editor.undefer_reflow();
|
|
|
|
|
}
|
2020-07-14 17:02:46 -04:00
|
|
|
|
2020-05-27 19:18:40 +02:00
|
|
|
private:
|
|
|
|
|
TextEditor& m_editor;
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-25 17:48:51 +02:00
|
|
|
int text_width_for_font(auto const& text_view, Gfx::Font const&) const;
|
|
|
|
|
Utf32View substitution_code_point_view(size_t length) const;
|
|
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect line_content_rect(size_t item_index) const;
|
|
|
|
|
Gfx::IntRect line_widget_rect(size_t line_index) const;
|
2019-03-20 23:11:00 +01:00
|
|
|
void delete_selection();
|
2021-07-09 21:34:20 +10:00
|
|
|
int content_x_for_position(TextPosition const&) const;
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect ruler_rect_in_inner_coordinates() const;
|
2021-06-12 04:50:23 +03:00
|
|
|
Gfx::IntRect gutter_rect_in_inner_coordinates() const;
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect visible_text_rect_in_inner_coordinates() const;
|
2019-08-25 08:43:01 +02:00
|
|
|
void recompute_all_visual_lines();
|
2019-11-05 16:37:47 -06:00
|
|
|
void ensure_cursor_is_valid();
|
2021-06-29 11:22:57 +02:00
|
|
|
void rehighlight_if_needed();
|
2019-03-07 13:21:51 +01:00
|
|
|
|
2019-12-09 17:45:40 +01:00
|
|
|
size_t visual_line_containing(size_t line_index, size_t column) const;
|
|
|
|
|
void recompute_visual_lines(size_t line_index);
|
2019-10-27 18:00:07 +01:00
|
|
|
|
2020-04-20 21:31:49 +02:00
|
|
|
void automatic_selection_scroll_timer_fired();
|
|
|
|
|
|
2019-11-30 16:50:24 +01:00
|
|
|
template<class T, class... Args>
|
|
|
|
|
inline void execute(Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
auto command = make<T>(*m_document, forward<Args>(args)...);
|
2020-12-18 02:44:02 +11:00
|
|
|
command->perform_formatting(*this);
|
2021-05-08 15:52:37 +02:00
|
|
|
will_execute(*command);
|
2020-01-23 21:04:59 +01:00
|
|
|
command->execute_from(*this);
|
2019-11-30 16:50:24 +01:00
|
|
|
m_document->add_to_undo_stack(move(command));
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 15:52:37 +02:00
|
|
|
virtual void will_execute(TextDocumentUndoCommand const&) { }
|
2022-03-29 16:33:46 +03:00
|
|
|
void on_search_results(GUI::TextRange current, Vector<GUI::TextRange> all_results);
|
|
|
|
|
|
|
|
|
|
static constexpr auto search_results_span_collection_index = 1;
|
2020-09-27 00:11:15 +03:00
|
|
|
|
2019-03-15 17:37:13 +01:00
|
|
|
Type m_type { MultiLine };
|
2020-07-14 17:02:46 -04:00
|
|
|
Mode m_mode { Editable };
|
2019-03-15 17:37:13 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
TextPosition m_cursor;
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::CenterLeft };
|
2019-03-07 01:05:35 +01:00
|
|
|
bool m_cursor_state { true };
|
2019-03-08 17:53:02 +01:00
|
|
|
bool m_in_drag_select { false };
|
2019-04-24 23:06:44 +02:00
|
|
|
bool m_ruler_visible { false };
|
2021-06-12 04:50:23 +03:00
|
|
|
bool m_gutter_visible { false };
|
2021-06-29 11:22:57 +02:00
|
|
|
bool m_needs_rehighlight { false };
|
2019-04-25 22:56:09 +02:00
|
|
|
bool m_automatic_indentation_enabled { false };
|
2022-02-26 15:22:30 -05:00
|
|
|
WrappingMode m_wrapping_mode { WrappingMode::NoWrap };
|
2020-09-01 19:10:55 +02:00
|
|
|
bool m_visualize_trailing_whitespace { true };
|
2021-03-17 13:52:42 -03:00
|
|
|
bool m_visualize_leading_whitespace { false };
|
2021-09-11 11:08:01 -04:00
|
|
|
bool m_cursor_line_highlighting { true };
|
2019-12-09 17:45:40 +01:00
|
|
|
size_t m_soft_tab_width { 4 };
|
2020-05-18 17:47:01 +02:00
|
|
|
int m_horizontal_content_padding { 3 };
|
2022-05-17 20:40:00 +02:00
|
|
|
TextRange m_selection {};
|
2021-06-25 17:48:51 +02:00
|
|
|
|
2022-05-07 16:03:33 +02:00
|
|
|
Optional<u32> m_substitution_code_point;
|
2021-06-25 17:48:51 +02:00
|
|
|
mutable OwnPtr<Vector<u32>> m_substitution_string_data; // Used to avoid repeated String construction.
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
RefPtr<Menu> m_context_menu;
|
|
|
|
|
RefPtr<Action> m_undo_action;
|
|
|
|
|
RefPtr<Action> m_redo_action;
|
|
|
|
|
RefPtr<Action> m_cut_action;
|
|
|
|
|
RefPtr<Action> m_copy_action;
|
|
|
|
|
RefPtr<Action> m_paste_action;
|
|
|
|
|
RefPtr<Action> m_go_to_line_action;
|
2020-04-20 21:10:03 +02:00
|
|
|
RefPtr<Action> m_select_all_action;
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::ElapsedTimer m_triple_click_timer;
|
2020-02-02 15:07:41 +01:00
|
|
|
NonnullRefPtrVector<Action> m_custom_context_menu_actions;
|
2019-10-25 21:05:06 +02:00
|
|
|
|
2020-05-27 19:18:40 +02:00
|
|
|
size_t m_reflow_deferred { 0 };
|
2020-06-13 00:31:46 +02:00
|
|
|
bool m_reflow_requested { false };
|
|
|
|
|
|
|
|
|
|
bool is_visual_data_up_to_date() const { return !m_reflow_requested; }
|
2020-05-27 19:18:40 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
RefPtr<TextDocument> m_document;
|
2019-10-27 18:00:07 +01:00
|
|
|
|
2020-09-20 12:20:24 -07:00
|
|
|
String m_placeholder { "" };
|
|
|
|
|
|
2019-10-27 18:00:07 +01:00
|
|
|
template<typename Callback>
|
2019-12-09 17:45:40 +01:00
|
|
|
void for_each_visual_line(size_t line_index, Callback) const;
|
2019-10-27 18:00:07 +01:00
|
|
|
|
|
|
|
|
struct LineVisualData {
|
2019-12-09 17:45:40 +01:00
|
|
|
Vector<size_t, 1> visual_line_breaks;
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect visual_rect;
|
2019-10-27 18:00:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
|
2020-02-07 20:07:15 +01:00
|
|
|
|
2021-02-07 15:15:10 +01:00
|
|
|
OwnPtr<Syntax::Highlighter> m_highlighter;
|
2020-12-30 13:55:06 +03:30
|
|
|
OwnPtr<AutocompleteProvider> m_autocomplete_provider;
|
|
|
|
|
OwnPtr<AutocompleteBox> m_autocomplete_box;
|
2021-01-01 17:29:11 +03:30
|
|
|
bool m_should_keep_autocomplete_box { false };
|
|
|
|
|
size_t m_automatic_autocomplete_delay_ms { 800 };
|
2020-04-20 21:31:49 +02:00
|
|
|
|
|
|
|
|
RefPtr<Core::Timer> m_automatic_selection_scroll_timer;
|
2021-01-01 17:29:11 +03:30
|
|
|
RefPtr<Core::Timer> m_autocomplete_timer;
|
|
|
|
|
|
2021-01-02 11:59:55 +01:00
|
|
|
OwnPtr<EditingEngine> m_editing_engine;
|
|
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntPoint m_last_mousemove_position;
|
2020-06-29 20:34:42 +02:00
|
|
|
|
|
|
|
|
RefPtr<Gfx::Bitmap> m_icon;
|
2021-08-02 11:10:56 +02:00
|
|
|
|
|
|
|
|
bool m_text_is_secret { false };
|
2022-03-29 16:33:46 +03:00
|
|
|
|
|
|
|
|
Optional<size_t> m_search_result_index;
|
|
|
|
|
Vector<GUI::TextRange> m_search_results;
|
2019-03-07 00:31:06 +01:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|