diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-12-06 23:59:55 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-12-06 23:59:55 +0100 |
commit | f18c985546341f45276adf2e01dc9ab3765c26cf (patch) | |
tree | b20f9c282e634a35e867e9f58be39474c0d5c626 /Editor | |
parent | eb4bbc337fd4bb9a4407b1ce2732f0712abf71a7 (diff) | |
download | serenity-f18c985546341f45276adf2e01dc9ab3765c26cf.zip |
Add basic 'x' and 'X' right/left deletion commands.
Diffstat (limited to 'Editor')
-rw-r--r-- | Editor/Document.cpp | 10 | ||||
-rw-r--r-- | Editor/Document.h | 1 | ||||
-rw-r--r-- | Editor/Editor.cpp | 27 | ||||
-rw-r--r-- | Editor/Editor.h | 6 | ||||
-rw-r--r-- | Editor/Line.cpp | 13 | ||||
-rw-r--r-- | Editor/Line.h | 2 |
6 files changed, 59 insertions, 0 deletions
diff --git a/Editor/Document.cpp b/Editor/Document.cpp index 696bd9de4e..89f5df0aa9 100644 --- a/Editor/Document.cpp +++ b/Editor/Document.cpp @@ -27,6 +27,16 @@ bool Document::backspace_at(Position) return false; } +bool Document::erase_at(Position position, int count) +{ + ASSERT(position.is_valid()); + ASSERT(position.line() < line_count()); + if (count == 0) + return false; + line(position.line()).erase(position.column(), count); + return true; +} + bool Document::newline_at(Position position) { ASSERT(position.is_valid()); diff --git a/Editor/Document.h b/Editor/Document.h index 7e474e484f..10b7b07fad 100644 --- a/Editor/Document.h +++ b/Editor/Document.h @@ -22,6 +22,7 @@ public: bool insert_at(Position, const std::string&); bool newline_at(Position); bool backspace_at(Position); + bool erase_at(Position, int count); void dump(); diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index e1a539f7bb..28fd89dc50 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -155,6 +155,8 @@ int Editor::exec() case '0': move_to_start_of_line(); break; case '$': move_to_end_of_line(); break; case 'a': move_right(); set_mode(EditingDocument); break; + case 'x': erase_right(); break; + case 'X': erase_left(); break; case '\\': set_mode(EditingCommand); break; } } @@ -360,6 +362,31 @@ bool Editor::remove_text_at_cursor(const std::string& text) return false; } +void Editor::erase_left() +{ + if (m_cursor.column() == 0) + return; + m_document->erase_at(m_cursor, -1); + m_cursor.move_by(0, -1); +} + +Line& Editor::current_line() +{ + return m_document->line(m_cursor.line()); +} + +const Line& Editor::current_line() const +{ + return m_document->line(m_cursor.line()); +} + +void Editor::erase_right() +{ + if (m_cursor.column() == current_line().length()) + return; + m_document->erase_at(m_cursor, 1); +} + void Editor::set_status_text(const std::string& text) { m_status_text = text; diff --git a/Editor/Editor.h b/Editor/Editor.h index ff637fd667..ea8106ed5b 100644 --- a/Editor/Editor.h +++ b/Editor/Editor.h @@ -6,6 +6,7 @@ #include <string> class Document; +class Line; class Editor { public: @@ -38,12 +39,17 @@ public: void run(OwnPtr<Operation>&&); private: + Line& current_line(); + const Line& current_line() const; + void move_left(); void move_down(); void move_up(); void move_right(); void move_to_end_of_line(); void move_to_start_of_line(); + void erase_left(); + void erase_right(); size_t max_line() const; size_t max_column() const; diff --git a/Editor/Line.cpp b/Editor/Line.cpp index fbd6721934..0a5f6e989d 100644 --- a/Editor/Line.cpp +++ b/Editor/Line.cpp @@ -117,3 +117,16 @@ void Line::coalesce() m_chunks.clear(); m_chunks.push_back(Chunk{ contents }); } + +void Line::erase(size_t column, int count) +{ + coalesce(); + auto str = data(); + if (count < 0) + str.erase(str.begin() + column + count, str.begin() + column); + else + str.erase(str.begin() + column, str.begin() + column + count); + m_chunks.clear(); + m_chunks.push_back(Chunk{ str }); +} + diff --git a/Editor/Line.h b/Editor/Line.h index dc6a19f2ba..399184860c 100644 --- a/Editor/Line.h +++ b/Editor/Line.h @@ -34,6 +34,8 @@ public: void coalesce(); + void erase(size_t column, int count); + private: void append(const std::string&); void prepend(const std::string&); |