mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 17:05:10 +00:00
LibGUI: Give GTextDocument access to the GTextEditor during commands
It's useful for the GTextDocument to have access to the initiating GTextEditor widget during the initial execution of a command. Since commands are executed via calls to GUndoCommand::redo(), we do this by wrapping the invocation of redo() in a new helper called GTextDocumentUndoCommand::execute_from(GTextDocument::Client). This is then used to fetch the current auto-indentation feature state and the soft tab width, both used by text insertion.
This commit is contained in:
parent
03d73cbaae
commit
14b83ee12f
3 changed files with 57 additions and 43 deletions
|
@ -40,6 +40,7 @@
|
|||
class GTextEditor;
|
||||
class GTextDocument;
|
||||
class GTextDocumentLine;
|
||||
class GTextDocumentUndoCommand;
|
||||
|
||||
struct GTextDocumentSpan {
|
||||
GTextRange range;
|
||||
|
@ -50,37 +51,6 @@ struct GTextDocumentSpan {
|
|||
void* data { nullptr };
|
||||
};
|
||||
|
||||
class GTextDocumentUndoCommand : public GCommand {
|
||||
public:
|
||||
GTextDocumentUndoCommand(GTextDocument&);
|
||||
virtual ~GTextDocumentUndoCommand();
|
||||
|
||||
protected:
|
||||
GTextDocument& m_document;
|
||||
};
|
||||
|
||||
class InsertTextCommand : public GTextDocumentUndoCommand {
|
||||
public:
|
||||
InsertTextCommand(GTextDocument&, const String&, const GTextPosition&);
|
||||
virtual void undo() override;
|
||||
virtual void redo() override;
|
||||
|
||||
private:
|
||||
String m_text;
|
||||
GTextRange m_range;
|
||||
};
|
||||
|
||||
class RemoveTextCommand : public GTextDocumentUndoCommand {
|
||||
public:
|
||||
RemoveTextCommand(GTextDocument&, const String&, const GTextRange&);
|
||||
virtual void undo() override;
|
||||
virtual void redo() override;
|
||||
|
||||
private:
|
||||
String m_text;
|
||||
GTextRange m_range;
|
||||
};
|
||||
|
||||
class GTextDocument : public RefCounted<GTextDocument> {
|
||||
public:
|
||||
enum class SearchShouldWrap {
|
||||
|
@ -98,6 +68,9 @@ public:
|
|||
virtual void document_did_change() = 0;
|
||||
virtual void document_did_set_text() = 0;
|
||||
virtual void document_did_set_cursor(const GTextPosition&) = 0;
|
||||
|
||||
virtual bool is_automatic_indentation_enabled() const = 0;
|
||||
virtual int soft_tab_width() const = 0;
|
||||
};
|
||||
|
||||
static NonnullRefPtr<GTextDocument> create(Client* client = nullptr)
|
||||
|
@ -157,8 +130,8 @@ public:
|
|||
void notify_did_change();
|
||||
void set_all_cursors(const GTextPosition&);
|
||||
|
||||
GTextPosition insert_at(const GTextPosition&, char);
|
||||
GTextPosition insert_at(const GTextPosition&, const StringView&);
|
||||
GTextPosition insert_at(const GTextPosition&, char, const Client* = nullptr);
|
||||
GTextPosition insert_at(const GTextPosition&, const StringView&, const Client* = nullptr);
|
||||
void remove(const GTextRange&);
|
||||
|
||||
private:
|
||||
|
@ -201,3 +174,42 @@ private:
|
|||
// NOTE: This vector is null terminated.
|
||||
Vector<char> m_text;
|
||||
};
|
||||
|
||||
class GTextDocumentUndoCommand : public GCommand {
|
||||
public:
|
||||
GTextDocumentUndoCommand(GTextDocument&);
|
||||
virtual ~GTextDocumentUndoCommand();
|
||||
|
||||
void execute_from(const GTextDocument::Client& client)
|
||||
{
|
||||
m_client = &client;
|
||||
redo();
|
||||
m_client = nullptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
GTextDocument& m_document;
|
||||
const GTextDocument::Client* m_client { nullptr };
|
||||
};
|
||||
|
||||
class InsertTextCommand : public GTextDocumentUndoCommand {
|
||||
public:
|
||||
InsertTextCommand(GTextDocument&, const String&, const GTextPosition&);
|
||||
virtual void undo() override;
|
||||
virtual void redo() override;
|
||||
|
||||
private:
|
||||
String m_text;
|
||||
GTextRange m_range;
|
||||
};
|
||||
|
||||
class RemoveTextCommand : public GTextDocumentUndoCommand {
|
||||
public:
|
||||
RemoveTextCommand(GTextDocument&, const String&, const GTextRange&);
|
||||
virtual void undo() override;
|
||||
virtual void redo() override;
|
||||
|
||||
private:
|
||||
String m_text;
|
||||
GTextRange m_range;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue