Add basic 'x' and 'X' right/left deletion commands.

This commit is contained in:
Andreas Kling 2018-12-06 23:59:55 +01:00
parent eb4bbc337f
commit f18c985546
Notes: sideshowbarker 2024-07-19 16:08:24 +09:00
6 changed files with 59 additions and 0 deletions

View File

@ -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());

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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 });
}

View File

@ -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&);