diff --git a/src/lib_gui/qt/element/QtCodeArea.cpp b/src/lib_gui/qt/element/QtCodeArea.cpp index 02f3cfab..11a09c80 100644 --- a/src/lib_gui/qt/element/QtCodeArea.cpp +++ b/src/lib_gui/qt/element/QtCodeArea.cpp @@ -98,6 +98,7 @@ QtCodeArea::QtCodeArea( , m_isActiveFile(false) , m_lineNumbersHidden(false) , m_wasAnnotated(false) + , m_endTextEditPosition(0) { setObjectName("code_area"); setReadOnly(true); @@ -117,6 +118,7 @@ QtCodeArea::QtCodeArea( } setPlainText(QString::fromUtf8(displayCode.c_str())); + createLineLengthCache(); m_digits = lineNumberDigits(); updateLineNumberAreaWidth(); @@ -656,7 +658,7 @@ void QtCodeArea::createAnnotations(std::shared_ptr locationFi { annotation.end = endTextEditPosition(); annotation.endLine = endLineNumber; - annotation.endCol = document()->findBlockByLineNumber(document()->blockCount() - 1).length(); + annotation.endCol = m_lineLengths[document()->blockCount() - 1]; } else { @@ -770,7 +772,7 @@ int QtCodeArea::toTextEditPosition(int lineNumber, int columnNumber) const for (int i = 0; i < lineNumber - 1; i++) { - position += document()->findBlockByLineNumber(i).length(); + position += m_lineLengths[i]; } position += columnNumber; @@ -782,7 +784,7 @@ std::pair QtCodeArea::toLineColumn(int textEditPosition) const int lineNumber = m_startLineNumber; for (int i = 0; i < document()->lineCount(); i++) { - int nextTextEditPosition = textEditPosition - document()->findBlockByLineNumber(i).length(); + int nextTextEditPosition = textEditPosition - m_lineLengths[i]; if (nextTextEditPosition >= 0) { textEditPosition = nextTextEditPosition; @@ -803,14 +805,7 @@ int QtCodeArea::startTextEditPosition() const int QtCodeArea::endTextEditPosition() const { - int position = 0; - - for (int i = 0; i < document()->blockCount(); i++) - { - position += document()->findBlockByLineNumber(i).length(); - } - - return position - 1; + return m_endTextEditPosition; } std::set QtCodeArea::getActiveLineNumbers() const @@ -867,14 +862,14 @@ std::vector QtCodeArea::getCursorRectsForAnnotation(const Annotation& ann { // Avoid that annotations at line end span down to first column of the next line. if (annotation.startLine != annotation.endLine || - document()->findBlockByLineNumber(line - m_startLineNumber).length() != annotation.endCol) + m_lineLengths[line - m_startLineNumber] != annotation.endCol) { cursor.setPosition(annotation.end); } } else { - cursor.setPosition(toTextEditPosition(line, document()->findBlockByLineNumber(line - m_startLineNumber).length() - 1)); + cursor.setPosition(toTextEditPosition(line, m_lineLengths[line - m_startLineNumber] - 1)); } rectEnd = cursorRect(cursor); @@ -963,3 +958,16 @@ void QtCodeArea::createActions() m_setIDECursorPositionAction->setToolTip(tr("Set the IDE Cursor to this code position")); connect(m_setIDECursorPositionAction, SIGNAL(triggered()), this, SLOT(setIDECursorPosition())); } + +void QtCodeArea::createLineLengthCache() +{ + m_endTextEditPosition = -1; + + m_lineLengths.clear(); + + for (QTextBlock it = document()->begin(); it != document()->end(); it = it.next()) + { + m_lineLengths.push_back(it.length()); + m_endTextEditPosition += it.length(); + } +} diff --git a/src/lib_gui/qt/element/QtCodeArea.h b/src/lib_gui/qt/element/QtCodeArea.h index 0f3f3267..299a247c 100644 --- a/src/lib_gui/qt/element/QtCodeArea.h +++ b/src/lib_gui/qt/element/QtCodeArea.h @@ -162,6 +162,8 @@ private: void createActions(); + void createLineLengthCache(); + static std::vector s_annotationColors; QtCodeFile* m_fileWidget; @@ -189,6 +191,9 @@ private: bool m_isActiveFile; bool m_lineNumbersHidden; bool m_wasAnnotated; + + std::vector m_lineLengths; + int m_endTextEditPosition; }; #endif // QT_CODE_AREA_H