2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-02-07 18:56:02 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-10-27 18:10:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-10-27 20:00:07 +03:00
|
|
|
#include <AK/HashTable.h>
|
2019-10-27 18:10:07 +03:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
2020-02-15 01:28:42 +03:00
|
|
|
#include <AK/Optional.h>
|
2019-10-27 18:10:07 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2020-05-17 21:33:06 +03:00
|
|
|
#include <AK/Utf32View.h>
|
2020-02-16 11:17:49 +03:00
|
|
|
#include <LibCore/Forward.h>
|
|
|
|
#include <LibGUI/Command.h>
|
|
|
|
#include <LibGUI/Forward.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/TextRange.h>
|
|
|
|
#include <LibGUI/UndoStack.h>
|
2021-09-22 00:02:48 +03:00
|
|
|
#include <LibGUI/Widget.h>
|
2020-02-15 01:28:42 +03:00
|
|
|
#include <LibGfx/Color.h>
|
2021-01-02 22:31:45 +03:00
|
|
|
#include <LibGfx/TextAttributes.h>
|
2020-04-23 21:29:38 +03:00
|
|
|
#include <LibRegex/Regex.h>
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
struct TextDocumentSpan {
|
|
|
|
TextRange range;
|
2021-01-02 22:31:45 +03:00
|
|
|
Gfx::TextAttributes attributes;
|
2021-06-07 10:33:09 +03:00
|
|
|
u64 data { 0 };
|
2021-01-02 22:31:45 +03:00
|
|
|
bool is_skippable { false };
|
2019-10-27 18:10:07 +03:00
|
|
|
};
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
class TextDocument : public RefCounted<TextDocument> {
|
2019-10-27 18:10:07 +03:00
|
|
|
public:
|
2019-11-01 21:19:39 +03:00
|
|
|
enum class SearchShouldWrap {
|
|
|
|
No = 0,
|
|
|
|
Yes
|
|
|
|
};
|
|
|
|
|
2019-10-27 20:00:07 +03:00
|
|
|
class Client {
|
|
|
|
public:
|
|
|
|
virtual ~Client();
|
|
|
|
virtual void document_did_append_line() = 0;
|
2019-12-09 19:45:40 +03:00
|
|
|
virtual void document_did_insert_line(size_t) = 0;
|
|
|
|
virtual void document_did_remove_line(size_t) = 0;
|
2019-10-27 20:00:07 +03:00
|
|
|
virtual void document_did_remove_all_lines() = 0;
|
2021-09-22 00:02:48 +03:00
|
|
|
virtual void document_did_change(AllowCallback = AllowCallback::Yes) = 0;
|
|
|
|
virtual void document_did_set_text(AllowCallback = AllowCallback::Yes) = 0;
|
2020-02-02 17:07:41 +03:00
|
|
|
virtual void document_did_set_cursor(const TextPosition&) = 0;
|
2021-05-08 14:16:37 +03:00
|
|
|
virtual void document_did_update_undo_stack() = 0;
|
2020-01-23 23:04:59 +03:00
|
|
|
|
|
|
|
virtual bool is_automatic_indentation_enabled() const = 0;
|
|
|
|
virtual int soft_tab_width() const = 0;
|
2019-10-27 20:00:07 +03:00
|
|
|
};
|
|
|
|
|
2020-02-16 11:17:49 +03:00
|
|
|
static NonnullRefPtr<TextDocument> create(Client* client = nullptr);
|
2020-08-15 10:58:31 +03:00
|
|
|
virtual ~TextDocument();
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2020-03-01 14:35:09 +03:00
|
|
|
size_t line_count() const { return m_lines.size(); }
|
|
|
|
const TextDocumentLine& line(size_t line_index) const { return m_lines[line_index]; }
|
|
|
|
TextDocumentLine& line(size_t line_index) { return m_lines[line_index]; }
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2021-02-07 18:56:02 +03:00
|
|
|
void set_spans(Vector<TextDocumentSpan> spans) { m_spans = move(spans); }
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
bool set_text(StringView, AllowCallback = AllowCallback::Yes);
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
const NonnullOwnPtrVector<TextDocumentLine>& lines() const { return m_lines; }
|
|
|
|
NonnullOwnPtrVector<TextDocumentLine>& lines() { return m_lines; }
|
2019-10-27 18:10:07 +03:00
|
|
|
|
|
|
|
bool has_spans() const { return !m_spans.is_empty(); }
|
2020-03-12 17:36:25 +03:00
|
|
|
Vector<TextDocumentSpan>& spans() { return m_spans; }
|
2020-02-02 17:07:41 +03:00
|
|
|
const Vector<TextDocumentSpan>& spans() const { return m_spans; }
|
2020-03-01 14:35:09 +03:00
|
|
|
void set_span_at_index(size_t index, TextDocumentSpan span) { m_spans[index] = move(span); }
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2020-10-30 01:52:07 +03:00
|
|
|
const TextDocumentSpan* span_at(const TextPosition&) const;
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void append_line(NonnullOwnPtr<TextDocumentLine>);
|
2019-12-09 19:45:40 +03:00
|
|
|
void remove_line(size_t line_index);
|
2019-10-27 20:00:07 +03:00
|
|
|
void remove_all_lines();
|
2020-02-02 17:07:41 +03:00
|
|
|
void insert_line(size_t line_index, NonnullOwnPtr<TextDocumentLine>);
|
2019-10-27 20:00:07 +03:00
|
|
|
|
|
|
|
void register_client(Client&);
|
|
|
|
void unregister_client(Client&);
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void update_views(Badge<TextDocumentLine>);
|
2019-10-27 21:36:59 +03:00
|
|
|
|
2020-09-28 16:37:37 +03:00
|
|
|
String text() const;
|
2020-02-02 17:07:41 +03:00
|
|
|
String text_in_range(const TextRange&) const;
|
2019-10-29 23:36:47 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
Vector<TextRange> find_all(StringView needle, bool regmatch = false);
|
2019-11-01 21:19:39 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
void update_regex_matches(StringView);
|
|
|
|
TextRange find_next(StringView, const TextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes, bool regmatch = false, bool match_case = true);
|
|
|
|
TextRange find_previous(StringView, const TextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes, bool regmatch = false, bool match_case = true);
|
2019-11-01 21:19:39 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
TextPosition next_position_after(const TextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
|
|
|
|
TextPosition previous_position_before(const TextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
|
2019-11-01 21:19:39 +03:00
|
|
|
|
2020-08-05 23:31:20 +03:00
|
|
|
u32 code_point_at(const TextPosition&) const;
|
2019-11-01 21:19:39 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
TextRange range_for_entire_line(size_t line_index) const;
|
2019-12-11 00:06:31 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Optional<TextDocumentSpan> first_non_skippable_span_before(const TextPosition&) const;
|
|
|
|
Optional<TextDocumentSpan> first_non_skippable_span_after(const TextPosition&) const;
|
2019-11-15 22:36:45 +03:00
|
|
|
|
2020-05-24 20:32:08 +03:00
|
|
|
TextPosition first_word_break_before(const TextPosition&, bool start_at_column_before) const;
|
2020-05-24 01:40:14 +03:00
|
|
|
TextPosition first_word_break_after(const TextPosition&) const;
|
|
|
|
|
2021-07-14 06:12:07 +03:00
|
|
|
TextPosition first_word_before(const TextPosition&, bool start_at_column_before) const;
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand>);
|
2019-11-30 15:05:17 +03:00
|
|
|
|
2019-11-30 17:36:17 +03:00
|
|
|
bool can_undo() const { return m_undo_stack.can_undo(); }
|
|
|
|
bool can_redo() const { return m_undo_stack.can_redo(); }
|
2019-11-30 15:05:17 +03:00
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
|
2021-05-08 22:44:22 +03:00
|
|
|
UndoStack const& undo_stack() const { return m_undo_stack; }
|
|
|
|
|
2019-11-30 15:05:17 +03:00
|
|
|
void notify_did_change();
|
2020-02-02 17:07:41 +03:00
|
|
|
void set_all_cursors(const TextPosition&);
|
2019-11-30 15:05:17 +03:00
|
|
|
|
2020-05-17 21:33:06 +03:00
|
|
|
TextPosition insert_at(const TextPosition&, u32, const Client* = nullptr);
|
2021-11-11 02:55:02 +03:00
|
|
|
TextPosition insert_at(const TextPosition&, StringView, const Client* = nullptr);
|
2020-02-02 17:07:41 +03:00
|
|
|
void remove(const TextRange&);
|
2019-11-30 18:50:24 +03:00
|
|
|
|
2020-08-15 10:58:31 +03:00
|
|
|
virtual bool is_code_document() const { return false; }
|
|
|
|
|
2020-09-20 22:20:24 +03:00
|
|
|
bool is_empty() const;
|
2021-05-05 20:09:43 +03:00
|
|
|
bool is_modified() const { return m_undo_stack.is_current_modified(); }
|
|
|
|
void set_unmodified();
|
2020-09-20 22:20:24 +03:00
|
|
|
|
2020-08-15 10:58:31 +03:00
|
|
|
protected:
|
2020-02-02 17:07:41 +03:00
|
|
|
explicit TextDocument(Client* client);
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2020-08-15 10:58:31 +03:00
|
|
|
private:
|
2020-02-02 17:07:41 +03:00
|
|
|
NonnullOwnPtrVector<TextDocumentLine> m_lines;
|
|
|
|
Vector<TextDocumentSpan> m_spans;
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2019-10-27 20:00:07 +03:00
|
|
|
HashTable<Client*> m_clients;
|
2019-11-23 19:41:14 +03:00
|
|
|
bool m_client_notifications_enabled { true };
|
2019-11-30 15:05:17 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
UndoStack m_undo_stack;
|
2020-04-23 21:29:38 +03:00
|
|
|
|
|
|
|
RegexResult m_regex_result;
|
|
|
|
size_t m_regex_result_match_index { 0 };
|
|
|
|
size_t m_regex_result_match_capture_group_index { 0 };
|
|
|
|
|
|
|
|
bool m_regex_needs_update { true };
|
|
|
|
String m_regex_needle;
|
2019-10-27 18:10:07 +03:00
|
|
|
};
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
class TextDocumentLine {
|
2019-10-27 18:10:07 +03:00
|
|
|
public:
|
2020-02-02 17:07:41 +03:00
|
|
|
explicit TextDocumentLine(TextDocument&);
|
2021-11-11 02:55:02 +03:00
|
|
|
explicit TextDocumentLine(TextDocument&, StringView);
|
2019-10-27 18:10:07 +03:00
|
|
|
|
2020-05-17 21:33:06 +03:00
|
|
|
String to_utf8() const;
|
|
|
|
|
2020-08-05 23:31:20 +03:00
|
|
|
Utf32View view() const { return { code_points(), length() }; }
|
|
|
|
const u32* code_points() const { return m_text.data(); }
|
2020-05-17 21:33:06 +03:00
|
|
|
size_t length() const { return m_text.size(); }
|
2021-11-11 02:55:02 +03:00
|
|
|
bool set_text(TextDocument&, StringView);
|
2020-05-17 23:24:52 +03:00
|
|
|
void set_text(TextDocument&, Vector<u32>);
|
2020-05-17 21:33:06 +03:00
|
|
|
void append(TextDocument&, u32);
|
|
|
|
void prepend(TextDocument&, u32);
|
|
|
|
void insert(TextDocument&, size_t index, u32);
|
2020-02-02 17:07:41 +03:00
|
|
|
void remove(TextDocument&, size_t index);
|
2020-05-17 21:33:06 +03:00
|
|
|
void append(TextDocument&, const u32*, size_t);
|
2020-02-02 17:07:41 +03:00
|
|
|
void truncate(TextDocument&, size_t length);
|
|
|
|
void clear(TextDocument&);
|
2020-05-17 21:33:06 +03:00
|
|
|
void remove_range(TextDocument&, size_t start, size_t length);
|
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
size_t first_non_whitespace_column() const;
|
2020-09-01 20:10:55 +03:00
|
|
|
Optional<size_t> last_non_whitespace_column() const;
|
|
|
|
bool ends_in_whitespace() const;
|
2021-04-22 00:48:26 +03:00
|
|
|
bool can_select() const;
|
2020-09-20 22:20:24 +03:00
|
|
|
bool is_empty() const { return length() == 0; }
|
2020-12-17 18:44:02 +03:00
|
|
|
size_t leading_spaces() const;
|
2019-10-27 18:10:07 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// NOTE: This vector is null terminated.
|
2020-05-17 21:33:06 +03:00
|
|
|
Vector<u32> m_text;
|
2019-10-27 18:10:07 +03:00
|
|
|
};
|
2020-01-23 23:04:59 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
class TextDocumentUndoCommand : public Command {
|
2020-01-23 23:04:59 +03:00
|
|
|
public:
|
2020-02-02 17:07:41 +03:00
|
|
|
TextDocumentUndoCommand(TextDocument&);
|
|
|
|
virtual ~TextDocumentUndoCommand();
|
2020-12-17 18:44:02 +03:00
|
|
|
virtual void perform_formatting(const TextDocument::Client&) { }
|
2020-01-23 23:04:59 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void execute_from(const TextDocument::Client& client)
|
2020-01-23 23:04:59 +03:00
|
|
|
{
|
|
|
|
m_client = &client;
|
|
|
|
redo();
|
|
|
|
m_client = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-02-02 17:07:41 +03:00
|
|
|
TextDocument& m_document;
|
|
|
|
const TextDocument::Client* m_client { nullptr };
|
2020-01-23 23:04:59 +03:00
|
|
|
};
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
class InsertTextCommand : public TextDocumentUndoCommand {
|
2020-01-23 23:04:59 +03:00
|
|
|
public:
|
2020-02-02 17:07:41 +03:00
|
|
|
InsertTextCommand(TextDocument&, const String&, const TextPosition&);
|
2020-12-17 18:44:02 +03:00
|
|
|
virtual void perform_formatting(const TextDocument::Client&) override;
|
2020-01-23 23:04:59 +03:00
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
2021-05-08 17:52:08 +03:00
|
|
|
virtual bool merge_with(GUI::Command const&) override;
|
2021-05-08 22:44:22 +03:00
|
|
|
virtual String action_text() const override;
|
2020-09-28 16:37:37 +03:00
|
|
|
const String& text() const { return m_text; }
|
|
|
|
const TextRange& range() const { return m_range; }
|
2020-01-23 23:04:59 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
String m_text;
|
2020-02-02 17:07:41 +03:00
|
|
|
TextRange m_range;
|
2020-01-23 23:04:59 +03:00
|
|
|
};
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
class RemoveTextCommand : public TextDocumentUndoCommand {
|
2020-01-23 23:04:59 +03:00
|
|
|
public:
|
2020-02-02 17:07:41 +03:00
|
|
|
RemoveTextCommand(TextDocument&, const String&, const TextRange&);
|
2020-01-23 23:04:59 +03:00
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
2020-09-28 16:37:37 +03:00
|
|
|
const TextRange& range() const { return m_range; }
|
2021-05-08 17:53:08 +03:00
|
|
|
virtual bool merge_with(GUI::Command const&) override;
|
2021-05-08 22:44:22 +03:00
|
|
|
virtual String action_text() const override;
|
2020-01-23 23:04:59 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
String m_text;
|
2020-02-02 17:07:41 +03:00
|
|
|
TextRange m_range;
|
2020-01-23 23:04:59 +03:00
|
|
|
};
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|