2019-10-27 16:10:07 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-10-27 19:36:59 +01:00
|
|
|
#include <AK/Badge.h>
|
2019-10-27 18:00:07 +01:00
|
|
|
#include <AK/HashTable.h>
|
2019-10-27 16:10:07 +01:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <AK/RefCounted.h>
|
2019-11-30 13:05:17 +01:00
|
|
|
#include <LibCore/CTimer.h>
|
2019-10-27 16:10:07 +01:00
|
|
|
#include <LibDraw/Color.h>
|
|
|
|
#include <LibDraw/Font.h>
|
|
|
|
#include <LibGUI/GTextRange.h>
|
2019-11-30 15:36:17 +01:00
|
|
|
#include <LibGUI/GUndoStack.h>
|
2019-10-27 16:10:07 +01:00
|
|
|
|
|
|
|
class GTextEditor;
|
2019-11-30 13:05:17 +01:00
|
|
|
class GTextDocument;
|
2019-10-27 16:10:07 +01:00
|
|
|
class GTextDocumentLine;
|
|
|
|
|
|
|
|
struct GTextDocumentSpan {
|
|
|
|
GTextRange range;
|
|
|
|
Color color;
|
2019-11-18 19:10:06 +01:00
|
|
|
Optional<Color> background_color;
|
2019-11-15 20:36:45 +01:00
|
|
|
bool is_skippable { false };
|
2019-10-27 16:10:07 +01:00
|
|
|
const Font* font { nullptr };
|
2019-11-18 19:10:06 +01:00
|
|
|
void* data { nullptr };
|
2019-10-27 16:10:07 +01:00
|
|
|
};
|
|
|
|
|
2019-11-30 15:36:17 +01:00
|
|
|
class GTextDocumentUndoCommand : public GCommand {
|
2019-11-30 13:05:17 +01:00
|
|
|
public:
|
|
|
|
GTextDocumentUndoCommand(GTextDocument&);
|
|
|
|
virtual ~GTextDocumentUndoCommand();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GTextDocument& m_document;
|
|
|
|
};
|
|
|
|
|
2019-11-30 16:50:24 +01:00
|
|
|
class InsertTextCommand : public GTextDocumentUndoCommand {
|
2019-11-30 13:05:17 +01:00
|
|
|
public:
|
2019-11-30 16:50:24 +01:00
|
|
|
InsertTextCommand(GTextDocument&, const String&, const GTextPosition&);
|
2019-11-30 13:05:17 +01:00
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
|
|
|
|
|
|
|
private:
|
2019-11-30 16:50:24 +01:00
|
|
|
String m_text;
|
|
|
|
GTextRange m_range;
|
2019-11-30 13:05:17 +01:00
|
|
|
};
|
|
|
|
|
2019-11-30 16:50:24 +01:00
|
|
|
class RemoveTextCommand : public GTextDocumentUndoCommand {
|
2019-11-30 13:05:17 +01:00
|
|
|
public:
|
2019-11-30 16:50:24 +01:00
|
|
|
RemoveTextCommand(GTextDocument&, const String&, const GTextRange&);
|
2019-11-30 13:05:17 +01:00
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
|
|
|
|
|
|
|
private:
|
2019-11-30 16:50:24 +01:00
|
|
|
String m_text;
|
|
|
|
GTextRange m_range;
|
2019-11-30 13:05:17 +01:00
|
|
|
};
|
|
|
|
|
2019-10-27 16:10:07 +01:00
|
|
|
class GTextDocument : public RefCounted<GTextDocument> {
|
|
|
|
public:
|
2019-11-01 19:19:39 +01:00
|
|
|
enum class SearchShouldWrap {
|
|
|
|
No = 0,
|
|
|
|
Yes
|
|
|
|
};
|
|
|
|
|
2019-10-27 18:00:07 +01:00
|
|
|
class Client {
|
|
|
|
public:
|
|
|
|
virtual ~Client();
|
|
|
|
virtual void document_did_append_line() = 0;
|
2019-12-09 17:45:40 +01:00
|
|
|
virtual void document_did_insert_line(size_t) = 0;
|
|
|
|
virtual void document_did_remove_line(size_t) = 0;
|
2019-10-27 18:00:07 +01:00
|
|
|
virtual void document_did_remove_all_lines() = 0;
|
2019-10-27 19:36:59 +01:00
|
|
|
virtual void document_did_change() = 0;
|
2019-11-23 17:41:14 +01:00
|
|
|
virtual void document_did_set_text() = 0;
|
2019-11-30 13:05:17 +01:00
|
|
|
virtual void document_did_set_cursor(const GTextPosition&) = 0;
|
2019-10-27 18:00:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static NonnullRefPtr<GTextDocument> create(Client* client = nullptr)
|
2019-10-27 16:10:07 +01:00
|
|
|
{
|
2019-10-27 18:00:07 +01:00
|
|
|
return adopt(*new GTextDocument(client));
|
2019-10-27 16:10:07 +01:00
|
|
|
}
|
|
|
|
|
2019-12-09 17:45:40 +01:00
|
|
|
size_t line_count() const { return (size_t)m_lines.size(); }
|
|
|
|
const GTextDocumentLine& line(size_t line_index) const { return m_lines[(int)line_index]; }
|
|
|
|
GTextDocumentLine& line(size_t line_index) { return m_lines[(int)line_index]; }
|
2019-10-27 16:10:07 +01:00
|
|
|
|
|
|
|
void set_spans(const Vector<GTextDocumentSpan>& spans) { m_spans = spans; }
|
|
|
|
|
|
|
|
void set_text(const StringView&);
|
|
|
|
|
|
|
|
const NonnullOwnPtrVector<GTextDocumentLine>& lines() const { return m_lines; }
|
|
|
|
NonnullOwnPtrVector<GTextDocumentLine>& lines() { return m_lines; }
|
|
|
|
|
|
|
|
bool has_spans() const { return !m_spans.is_empty(); }
|
|
|
|
const Vector<GTextDocumentSpan>& spans() const { return m_spans; }
|
2019-12-09 17:45:40 +01:00
|
|
|
void set_span_at_index(size_t index, GTextDocumentSpan span) { m_spans[(int)index] = move(span); }
|
2019-10-27 16:10:07 +01:00
|
|
|
|
2019-10-27 18:00:07 +01:00
|
|
|
void append_line(NonnullOwnPtr<GTextDocumentLine>);
|
2019-12-09 17:45:40 +01:00
|
|
|
void remove_line(size_t line_index);
|
2019-10-27 18:00:07 +01:00
|
|
|
void remove_all_lines();
|
2019-12-09 17:45:40 +01:00
|
|
|
void insert_line(size_t line_index, NonnullOwnPtr<GTextDocumentLine>);
|
2019-10-27 18:00:07 +01:00
|
|
|
|
|
|
|
void register_client(Client&);
|
|
|
|
void unregister_client(Client&);
|
|
|
|
|
2019-10-27 19:36:59 +01:00
|
|
|
void update_views(Badge<GTextDocumentLine>);
|
|
|
|
|
2019-10-29 21:36:47 +01:00
|
|
|
String text_in_range(const GTextRange&) const;
|
|
|
|
|
2019-11-01 19:19:39 +01:00
|
|
|
Vector<GTextRange> find_all(const StringView& needle) const;
|
|
|
|
|
|
|
|
GTextRange find_next(const StringView&, const GTextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes) const;
|
|
|
|
GTextRange find_previous(const StringView&, const GTextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes) const;
|
|
|
|
|
|
|
|
GTextPosition next_position_after(const GTextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
|
|
|
|
GTextPosition previous_position_before(const GTextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
|
|
|
|
|
|
|
|
char character_at(const GTextPosition&) const;
|
|
|
|
|
2019-11-15 20:36:45 +01:00
|
|
|
Optional<GTextDocumentSpan> first_non_skippable_span_before(const GTextPosition&) const;
|
|
|
|
Optional<GTextDocumentSpan> first_non_skippable_span_after(const GTextPosition&) const;
|
|
|
|
|
2019-11-30 13:05:17 +01:00
|
|
|
void add_to_undo_stack(NonnullOwnPtr<GTextDocumentUndoCommand>);
|
|
|
|
|
2019-11-30 15:36:17 +01:00
|
|
|
bool can_undo() const { return m_undo_stack.can_undo(); }
|
|
|
|
bool can_redo() const { return m_undo_stack.can_redo(); }
|
2019-11-30 13:05:17 +01:00
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
|
|
|
|
void notify_did_change();
|
|
|
|
void set_all_cursors(const GTextPosition&);
|
|
|
|
|
2019-11-30 16:50:24 +01:00
|
|
|
GTextPosition insert_at(const GTextPosition&, char);
|
|
|
|
GTextPosition insert_at(const GTextPosition&, const StringView&);
|
|
|
|
void remove(const GTextRange&);
|
|
|
|
|
2019-10-27 16:10:07 +01:00
|
|
|
private:
|
2019-10-27 18:00:07 +01:00
|
|
|
explicit GTextDocument(Client* client);
|
2019-10-27 16:10:07 +01:00
|
|
|
|
2019-11-30 13:05:17 +01:00
|
|
|
void update_undo_timer();
|
|
|
|
|
2019-10-27 16:10:07 +01:00
|
|
|
NonnullOwnPtrVector<GTextDocumentLine> m_lines;
|
|
|
|
Vector<GTextDocumentSpan> m_spans;
|
|
|
|
|
2019-10-27 18:00:07 +01:00
|
|
|
HashTable<Client*> m_clients;
|
2019-11-23 17:41:14 +01:00
|
|
|
bool m_client_notifications_enabled { true };
|
2019-11-30 13:05:17 +01:00
|
|
|
|
2019-11-30 15:36:17 +01:00
|
|
|
GUndoStack m_undo_stack;
|
2019-11-30 13:05:17 +01:00
|
|
|
RefPtr<CTimer> m_undo_timer;
|
2019-10-27 16:10:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class GTextDocumentLine {
|
|
|
|
friend class GTextEditor;
|
|
|
|
friend class GTextDocument;
|
|
|
|
|
|
|
|
public:
|
2019-10-27 19:36:59 +01:00
|
|
|
explicit GTextDocumentLine(GTextDocument&);
|
|
|
|
explicit GTextDocumentLine(GTextDocument&, const StringView&);
|
2019-10-27 16:10:07 +01:00
|
|
|
|
2019-12-09 17:45:40 +01:00
|
|
|
StringView view() const { return { characters(), (size_t)length() }; }
|
2019-10-27 16:10:07 +01:00
|
|
|
const char* characters() const { return m_text.data(); }
|
2019-12-09 17:45:40 +01:00
|
|
|
size_t length() const { return (size_t)m_text.size() - 1; }
|
2019-10-27 19:36:59 +01:00
|
|
|
void set_text(GTextDocument&, const StringView&);
|
|
|
|
void append(GTextDocument&, char);
|
|
|
|
void prepend(GTextDocument&, char);
|
2019-12-09 17:45:40 +01:00
|
|
|
void insert(GTextDocument&, size_t index, char);
|
|
|
|
void remove(GTextDocument&, size_t index);
|
|
|
|
void append(GTextDocument&, const char*, size_t);
|
|
|
|
void truncate(GTextDocument&, size_t length);
|
2019-10-27 19:36:59 +01:00
|
|
|
void clear(GTextDocument&);
|
2019-12-09 17:45:40 +01:00
|
|
|
size_t first_non_whitespace_column() const;
|
2019-10-27 16:10:07 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
// NOTE: This vector is null terminated.
|
|
|
|
Vector<char> m_text;
|
|
|
|
};
|