فهرست منبع

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

Andreas Kling 6 سال پیش
والد
کامیت
f18c985546
6فایلهای تغییر یافته به همراه59 افزوده شده و 0 حذف شده
  1. 10 0
      Editor/Document.cpp
  2. 1 0
      Editor/Document.h
  3. 27 0
      Editor/Editor.cpp
  4. 6 0
      Editor/Editor.h
  5. 13 0
      Editor/Line.cpp
  6. 2 0
      Editor/Line.h

+ 10 - 0
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());

+ 1 - 0
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();
 

+ 27 - 0
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;

+ 6 - 0
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;

+ 13 - 0
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 });
+}
+

+ 2 - 0
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&);