From: hama Date: Mon, 27 Jul 2015 21:54:11 +0000 (+0200) Subject: ReEdit: split line works and is fully tested X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=4b2a662fd58387c4be6027e743faea3bab43380b;p=reqt ReEdit: split line works and is fully tested --- diff --git a/base/ReFile.cpp b/base/ReFile.cpp index d22a585..821dc32 100644 --- a/base/ReFile.cpp +++ b/base/ReFile.cpp @@ -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) { * false: 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) { * false: 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( - ignoreCase ? - memchr(start, first, restLength) : - memichr(start, first, restLength))) != NULL) { + && (ptr = reinterpret_cast( + 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(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(m_block)); m_block = reinterpret_cast(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--) diff --git a/cunit/cuReEdit.cpp b/cunit/cuReEdit.cpp index c537501..a82b8c3 100644 --- a/cunit/cuReEdit.cpp +++ b/cunit/cuReEdit.cpp @@ -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"); } diff --git a/gui/ReEdit.cpp b/gui/ReEdit.cpp index 6b9e7d7..0d23519 100644 --- a/gui/ReEdit.cpp +++ b/gui/ReEdit.cpp @@ -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); diff --git a/gui/ReEdit.hpp b/gui/ReEdit.hpp index f56fd3f..a6b04fa 100644 --- a/gui/ReEdit.hpp +++ b/gui/ReEdit.hpp @@ -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);