]> gitweb.hamatoma.de Git - reqt/commitdiff
ReEdit: split line works and is fully tested
authorhama <hama@siduction.net>
Mon, 27 Jul 2015 21:54:11 +0000 (23:54 +0200)
committerhama <hama@siduction.net>
Mon, 27 Jul 2015 21:54:11 +0000 (23:54 +0200)
base/ReFile.cpp
cunit/cuReEdit.cpp
gui/ReEdit.cpp
gui/ReEdit.hpp

index d22a5853355a08092a5fc770db41680572e6ae08..821dc32a4f851e28bf056e0a7db35fc951b50513 100644 (file)
@@ -44,8 +44,8 @@ int memicmp(const void* str1, const void* str2, size_t length) {
  * Constructor.
  */
 ReLines::ReLines() :
-               QStringList(),
-               m_empty() {
+           QStringList(),
+           m_empty() {
 }
 /**
  * Destructor.
@@ -134,7 +134,7 @@ void ReLines::insertLines(int lineNo, const QString& text, bool withUndo) {
  *                                     <code>false</code>: undo is impossible
  */
 void ReLines::insertPart(int lineNo, int col, const QString& text,
-       bool withUndo) {
+    bool withUndo) {
        if (lineNo >= 0 && lineNo < lineCount() && col >= 0) {
                if (withUndo)
                        storeInsertPart(lineNo, col, text.length());
@@ -175,7 +175,7 @@ void ReLines::insertText(int lineNo, int col, const QString& text) {
                if (lastEoLn != endOfLine) {
                        int oldCount = lineCount();
                        insertLines(lineNo + 1,
-                               text.mid(endOfLine + 1, lastEoLn - endOfLine), true);
+                           text.mid(endOfLine + 1, lastEoLn - endOfLine), true);
                        newLines = lineCount() - oldCount;
                }
                if (lastEoLn != text.length() - 1) {
@@ -244,7 +244,7 @@ bool ReLines::removePart(int lineNo, int col, int count, bool withUndo) {
                                        replace(lineNo, current.mid(0, col));
                                else
                                        replace(lineNo,
-                                               current.mid(0, col) + current.mid(col + count));
+                                           current.mid(0, col) + current.mid(col + count));
                        }
                }
        }
@@ -280,23 +280,25 @@ void ReLines::removeLines(int start, int count, bool withUndo) {
  *                                     <code>false</code>: undo is impossible
  */
 void ReLines::splitLine(int lineNo, int col, bool withUndo) {
-       if (lineNo >= 0 && lineNo < length() && col >= 0) {
+       int count = length();
+       if (lineNo >= 0 && lineNo < count && col >= 0) {
                QString current = at(lineNo);
                if (withUndo)
                        storeSplit(lineNo, col);
-               int count = length();
-               if (lineNo >= count - 1) {
-                       if (col >= current.length())
+               if (col >= current.length()) {
+                       if (lineNo >= count - 1)
                                append("");
                        else
-                               append(current.mid(col));
+                               insert(lineNo + 1, "");
                } else {
-                       if (col >= current.length())
-                               insert(lineNo, "");
+                       QString x = current.mid(col);
+                       if (lineNo >= count - 1)
+                               append(current.mid(col));
                        else
                                insert(lineNo + 1, current.mid(col));
+                       x = current.mid(0, col);
+                       replace(lineNo, current.mid(0, col));
                }
-               replace(lineNo, current.mid(0, col));
        }
 }
 /**
@@ -342,26 +344,26 @@ void ReLines::undo(int& lineNo, int& col) {
  * @param filename  name of the file
  */
 ReFile::ReFile(const QString& filename, bool readOnly, ReLogger* logger) :
-               ReLineSource(),
-               ReLines(),
-               m_endOfLine(),
-               m_filename(filename),
-               m_file(filename),
-               m_block(NULL),
-               // in 32-bit address space we allocate only 10 MByte, in 64-bit environments 100 GByte
-               m_blocksize(
-                       sizeof(void*) <= 4 ?
-                               10 * 1024 * 1024ll : 0x100ll * 0x10000 * 0x10000),
-               m_blockOffset(0),
-               m_filesize(0),
-               m_startOfLine(NULL),
-               m_lineLength(0),
-               m_lineOffset(0),
-               m_currentLineNo(0),
-               m_maxLineLength(0x10000),
-               m_content(),
-               m_readOnly(readOnly),
-               m_logger(logger) {
+           ReLineSource(),
+           ReLines(),
+           m_endOfLine(),
+           m_filename(filename),
+           m_file(filename),
+           m_block(NULL),
+           // in 32-bit address space we allocate only 10 MByte, in 64-bit environments 100 GByte
+           m_blocksize(
+               sizeof(void*) <= 4 ?
+                   10 * 1024 * 1024ll : 0x100ll * 0x10000 * 0x10000),
+           m_blockOffset(0),
+           m_filesize(0),
+           m_startOfLine(NULL),
+           m_lineLength(0),
+           m_lineOffset(0),
+           m_currentLineNo(0),
+           m_maxLineLength(0x10000),
+           m_content(),
+           m_readOnly(readOnly),
+           m_logger(logger) {
 #if defined __linux__
        setEndOfLine("\n");
 #elif defined __WIN32__
@@ -418,7 +420,7 @@ QString ReFile::filename() const {
  *                                             false: a line has not been found
  */
 bool ReFile::findLine(const char* toFind, bool ignoreCase, int& lineNo,
-       QString* line) {
+    QString* line) {
        bool rc = false;
        int length;
        int sourceLength = strlen(toFind);
@@ -429,14 +431,14 @@ bool ReFile::findLine(const char* toFind, bool ignoreCase, int& lineNo,
                const char* ptr = start;
                int restLength = length - sourceLength + 1;
                while (restLength > 0
-                       && (ptr = reinterpret_cast<const char*>(
-                               ignoreCase ?
-                                       memchr(start, first, restLength) :
-                                       memichr(start, first, restLength))) != NULL) {
+                   && (ptr = reinterpret_cast<const char*>(
+                       ignoreCase ?
+                           memchr(start, first, restLength) :
+                           memichr(start, first, restLength))) != NULL) {
                        if ((
-                               ignoreCase ?
-                                       _memicmp(ptr, toFind, sourceLength) :
-                                       memcmp(ptr, toFind, sourceLength)) == 0) {
+                           ignoreCase ?
+                               _memicmp(ptr, toFind, sourceLength) :
+                               memcmp(ptr, toFind, sourceLength)) == 0) {
                                rc = true;
                                lineNo = m_currentLineNo;
                                QByteArray buffer(m_startOfLine, m_lineLength);
@@ -466,8 +468,8 @@ bool ReFile::findLine(const char* toFind, bool ignoreCase, int& lineNo,
  *                          false: a line has not been found
  */
 bool ReFile::findLine(const QString& includePattern, bool includeIsRegExpr,
-       bool includeIgnoreCase, const QString& excludePattern,
-       bool excludeIsRegExpr, bool excludeIgnoreCase, int& lineNo, QString* line) {
+    bool includeIgnoreCase, const QString& excludePattern,
+    bool excludeIsRegExpr, bool excludeIgnoreCase, int& lineNo, QString* line) {
        bool rc = false;
        if (line != NULL)
                *line = "";
@@ -513,9 +515,9 @@ char* ReFile::nextLine(int& length) {
                if (m_currentLineNo == 65639)
                        m_currentLineNo += 0;
                rc = m_startOfLine = remap(m_lineOffset += m_lineLength,
-                       m_maxLineLength, lineLength);
+                   m_maxLineLength, lineLength);
                const char* ptr = reinterpret_cast<const char*>(memchr(rc, '\n',
-                       lineLength));
+                   lineLength));
                if (ptr != NULL)
                        lineLength = ptr - rc + 1;
                length = m_lineLength = lineLength;
@@ -637,7 +639,7 @@ char* ReFile::remap(int64_t offset, int size, int& length) {
                        size = m_filesize - offset;
                // Note: size <= m_blocksize
                if (m_block != NULL && offset >= m_blockOffset
-                       && offset + size <= m_blockOffset + m_blocksize) {
+                   && offset + size <= m_blockOffset + m_blocksize) {
                        // new block is inside the internal block:
                        // no remapping needed
                        rc = m_block + (offset - m_blockOffset);
@@ -652,7 +654,7 @@ char* ReFile::remap(int64_t offset, int size, int& length) {
                        if (m_block != NULL)
                                m_file.unmap(reinterpret_cast<uchar*>(m_block));
                        m_block = reinterpret_cast<char*>(m_file.map(m_blockOffset,
-                               m_blocksize));
+                           m_blocksize));
                        rc = m_block + (offset - m_blockOffset);
                        length = m_blocksize - (rc - m_block);
                        if (length > size)
@@ -703,7 +705,7 @@ void ReFile::setFilename(const QString &filename) {
  * @return              the name of an existing directory
  */
 QByteArray ReFile::tempDir(const char* node, const char* parent,
-       bool withSeparator) {
+    bool withSeparator) {
 #if defined __linux__
        QByteArray temp("/tmp");
        static const char* firstVar = "TMP";
@@ -750,7 +752,7 @@ QByteArray ReFile::tempDir(const char* node, const char* parent,
  * @return                  the full name of a temporary file
  */
 QByteArray ReFile::tempFile(const char* node, const char* parent,
-       bool deleteIfExists) {
+    bool deleteIfExists) {
        QByteArray rc(tempDir(parent));
        if (!rc.endsWith('/'))
                rc += '/';
@@ -798,7 +800,7 @@ bool ReFile::write(const QString& filename) {
  * @param mode          file write mode: "w" (write) or "a" (append)
  */
 void ReFile::writeToFile(const char* filename, const char* content,
-       size_t contentLength, const char* mode) {
+    size_t contentLength, const char* mode) {
        FILE* fp = fopen(filename, mode);
        if (fp != NULL) {
                if (contentLength == (size_t) - 1)
@@ -812,12 +814,12 @@ void ReFile::writeToFile(const char* filename, const char* content,
  * Constructor.
  */
 ReUndoList::ReUndoList() :
-               m_list(),
-               m_current(NULL),
-               m_lastLine(-1),
-               m_lastPosition(-1),
-               m_maxUndoSize(10 * 1024 * 1024),
-               m_currentUndoSize(0) {
+           m_list(),
+           m_current(NULL),
+           m_lastLine(-1),
+           m_lastPosition(-1),
+           m_maxUndoSize(10 * 1024 * 1024),
+           m_currentUndoSize(0) {
 }
 
 /**
@@ -956,7 +958,7 @@ void ReUndoList::storeSplit(int lineNo, int col) {
  * @param list the list containing the lines for extracting the line content
  */
 void ReUndoList::storeRemoveLines(int lineNo, int count,
-       const QStringList& list) {
+    const QStringList& list) {
        qint64 size = 0;
        // Calculate the additional space
        for (int ii = lineNo + count - 1; ii >= lineNo; ii--)
index c537501281227c86252c5cad0a1917c9b4efa34f..a82b8c3ee5b18be8e5870187d7ebf34f5d9c0994 100644 (file)
@@ -15,8 +15,8 @@
 class TestReEdit: public ReTest, protected ReEdit {
 public:
        TestReEdit() :
-                   ReTest("ReEdit"),
-                   ReEdit(NULL) {
+                       ReTest("ReEdit"),
+                       ReEdit(NULL) {
                doIt();
        }
 
@@ -28,7 +28,7 @@ public:
                setLines(&m_lines);
        }
        void pushKey(int key, const QString& text = "",
-           Qt::KeyboardModifiers modifiers = Qt::NoModifier) {
+               Qt::KeyboardModifiers modifiers = Qt::NoModifier) {
                QKeyEvent event(QEvent::KeyPress, key, modifiers, text);
                keyPressEvent(&event);
        }
@@ -66,7 +66,7 @@ public:
                checkEqu(expectedCol, m_cursorCol);
        }
        void checkCursorPos(EditorAction action, int expectedLineNo,
-           int expectedCol) {
+               int expectedCol) {
                editorAction(action);
                checkEqu(expectedLineNo, m_cursorLineNo);
                checkEqu(expectedCol, m_cursorCol);
@@ -150,7 +150,6 @@ public:
                m_screenWidth = 5;
                m_firstLine = 0;
                m_firstCol = 0;
-
                // Split line at the end:
                m_cursorLineNo = 0;
                m_cursorCol = 2;
@@ -158,7 +157,6 @@ public:
                checkEqu("abc", m_lines.lineAt(0));
                checkEqu("", m_lines.lineAt(1));
                checkEqu("1234", m_lines.lineAt(2));
-
                // Split line in the middle of the line:
                m_cursorLineNo = 0;
                m_cursorCol = 1;
@@ -166,7 +164,23 @@ public:
                pushKey(Qt::Key_Enter, "\n");
                checkEqu("ab", m_lines.lineAt(0));
                checkEqu("c", m_lines.lineAt(1));
-               checkEqu("1234", m_lines.lineAt(1));
+               checkEqu("1234", m_lines.lineAt(2));
+
+               // ... in the last line:
+               // Split line at the end:
+               m_cursorLineNo = 2;
+               m_cursorCol = 2;
+               init("abc\n1234\nxy");
+               pushKey(Qt::Key_Return, "\n");
+               checkEqu("xy", m_lines.lineAt(2));
+               checkEqu("", m_lines.lineAt(3));
+               // Split line in the middle of the line:
+               m_cursorLineNo = 2;
+               m_cursorCol = 1;
+               init("abc\n1234\nxyz");
+               pushKey(Qt::Key_Return, "\n");
+               checkEqu("xy", m_lines.lineAt(2));
+               checkEqu("z", m_lines.lineAt(3));
                log("ok");
        }
 
index 6b9e7d78a17cd7c9644f71c85fe2063713adfa2d..0d2351983f1a8ffbd748df184f2606ed42b8f765 100644 (file)
@@ -210,6 +210,8 @@ void ReEdit::assignKeysStandard() {
        m_keyAltControl[Qt::Key_Right] = EA_VIEW_RIGHT;
        m_keyAltControlShift[Qt::Key_Left] = EA_PAGE_LEFT;
        m_keyAltControlShift[Qt::Key_Right] = EA_PAGE_RIGHT;
+       m_keyRaw[Qt::Key_Return] = EA_NEWLINE;
+       m_keyRaw[Qt::Key_Enter] = EA_NEWLINE;
 }
 
 /**
@@ -316,6 +318,11 @@ void ReEdit::editorAction(ReEdit::EditorAction action) {
        switch (action) {
        case EA_UNDEF:
                break;
+       case EA_NEWLINE:
+               m_lines->splitLine(m_cursorLineNo, m_cursorCol + 1, true);
+               m_cursorCol = -1;
+               m_cursorLineNo++;
+               break;
        case EA_CHAR_LEFT:
                if (--m_cursorCol < -1) {
                        if (m_cursorLineNo == 0)
@@ -453,16 +460,9 @@ void ReEdit::keyPressEvent(QKeyEvent* event) {
                switch (key) {
                case Qt::Key_Enter:
                case Qt::Key_Return:
-                       m_lines->splitLine(m_cursorLineNo, m_cursorCol + 1, true);
-                       m_cursorCol = -1;
-                       m_cursorLineNo++;
-                       break;
-               case Qt::Key_Backspace: {
-                       editorAction(EA_BACKSPACE);
-                       break;
-               }
                case Qt::Key_Delete:
-                       editorAction(EA_DEL_CHAR);
+               case Qt::Key_Backspace:
+                       editorAction(m_keyRaw[key]);
                        break;
                default:
                        m_lines->insertText(m_cursorLineNo, m_cursorCol + 1, keyText);
index f56fd3fc0d5b440dd67229ea3c6963b7bc95303c..a6b04fac7423650c79d79a8231845d58318b3b98 100644 (file)
@@ -85,8 +85,8 @@ public:
         * @param source    source to copy
         */
        inline ReEditText(const ReEditText& source) :
-                   m_text(source.m_text),
-                   m_look(source.m_look) {
+                       m_text(source.m_text),
+                       m_look(source.m_look) {
        }
        /** Assignment operator.
         * @param source    source to copy
@@ -146,14 +146,14 @@ public:
 class ReParagraphBuilder {
 public:
        virtual void buildParagraph(ReParagraph& paragraph, int lineNo,
-           ReEdit* edit);
+               ReEdit* edit);
 };
 
 class ReCursortLineBuilder: public ReParagraphBuilder {
        // ReParagraphBuilder interface
 public:
        virtual void buildParagraph(ReParagraph& paragraph, int lineNo,
-           ReEdit* edit);
+               ReEdit* edit);
 };
 
 /**
@@ -234,10 +234,10 @@ public:
        class ClickPosition: public QRect {
        public:
                ClickPosition(ClickObjType type) :
-                           QRect(0, 0, 0, 0),
-                           m_type(type),
-                           m_title(),
-                           m_object(NULL) {
+                               QRect(0, 0, 0, 0),
+                               m_type(type),
+                               m_title(),
+                               m_object(NULL) {
                }
        public:
                bool operator <(const ClickPosition& op) {
@@ -298,6 +298,7 @@ public:
                EA_VIEW_RIGHT,
                EA_PAGE_LEFT,
                EA_PAGE_RIGHT,
+               EA_NEWLINE,
        };
 public:
        explicit ReEdit(QWidget *parent = 0);
@@ -308,7 +309,7 @@ public:
        void editorAction(EditorAction action);
        ReLines& lines();
        ReLook* lookOf(ReLook::ForeGround foreground,
-           ReLook::BackGround background);
+               ReLook::BackGround background);
        /** Returns the current page size.
         * return    number of visible lines in the edit field
         */
@@ -333,8 +334,8 @@ public:
 protected:
        QBrush* createBrush(ReLook::BackGround background);
        void drawScrollbars(QPainter& painter, const QRect& rect,
-           double sizeVertical, double posVertical, double sizeHorizontal,
-           double posHorizontal);
+               double sizeVertical, double posVertical, double sizeHorizontal,
+               double posHorizontal);
        void ensureCursorVisible();
 protected slots:
        void keyPressEvent(QKeyEvent* event);