forked from qt-creator/qt-creator
TextEditor: Cleanup TextDocumentLayout
* Use range-based for * Use nullptr * Use inline member initialization * Fix real/int implicit casts Change-Id: I17fc35382ef22c97b34faba741915b9b914e38ff Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
committed by
Orgad Shaneh
parent
32d665e3be
commit
3306c9c969
@@ -28,8 +28,7 @@
|
||||
#include <utils/qtcassert.h>
|
||||
#include <QDebug>
|
||||
|
||||
using namespace TextEditor;
|
||||
|
||||
namespace TextEditor {
|
||||
|
||||
CodeFormatterData::~CodeFormatterData()
|
||||
{
|
||||
@@ -37,13 +36,12 @@ CodeFormatterData::~CodeFormatterData()
|
||||
|
||||
TextBlockUserData::~TextBlockUserData()
|
||||
{
|
||||
foreach (TextMark *mrk, m_marks) {
|
||||
for (TextMark *mrk : qAsConst(m_marks)) {
|
||||
mrk->baseTextDocument()->removeMarkFromMarksCache(mrk);
|
||||
mrk->setBaseTextDocument(0);
|
||||
mrk->setBaseTextDocument(nullptr);
|
||||
mrk->removedFromEditor();
|
||||
}
|
||||
|
||||
if (m_codeFormatterData)
|
||||
delete m_codeFormatterData;
|
||||
}
|
||||
|
||||
@@ -333,11 +331,9 @@ TextBlockUserData::MatchType TextBlockUserData::matchCursorBackward(QTextCursor
|
||||
const Parentheses::const_iterator cend = parentheses.constEnd();
|
||||
for (Parentheses::const_iterator it = parentheses.constBegin();it != cend; ++it) {
|
||||
const Parenthesis &paren = *it;
|
||||
if (paren.pos == relPos - 1
|
||||
&& paren.type == Parenthesis::Closed) {
|
||||
if (paren.pos == relPos - 1 && paren.type == Parenthesis::Closed)
|
||||
return checkClosedParenthesis(cursor, paren.chr);
|
||||
}
|
||||
}
|
||||
return NoMatch;
|
||||
}
|
||||
|
||||
@@ -383,11 +379,7 @@ void TextBlockUserData::addMark(TextMark *mark)
|
||||
|
||||
|
||||
TextDocumentLayout::TextDocumentLayout(QTextDocument *doc)
|
||||
: QPlainTextDocumentLayout(doc),
|
||||
lastSaveRevision(0),
|
||||
hasMarks(false),
|
||||
maxMarkWidthFactor(1.0),
|
||||
m_requiredWidth(0)
|
||||
: QPlainTextDocumentLayout(doc)
|
||||
{}
|
||||
|
||||
TextDocumentLayout::~TextDocumentLayout()
|
||||
@@ -568,7 +560,7 @@ void TextDocumentLayout::setRequiredWidth(int width)
|
||||
{
|
||||
int oldw = m_requiredWidth;
|
||||
m_requiredWidth = width;
|
||||
int dw = QPlainTextDocumentLayout::documentSize().width();
|
||||
int dw = int(QPlainTextDocumentLayout::documentSize().width());
|
||||
if (oldw > dw || width > dw)
|
||||
emitDocumentSizeChanged();
|
||||
}
|
||||
@@ -577,25 +569,23 @@ void TextDocumentLayout::setRequiredWidth(int width)
|
||||
QSizeF TextDocumentLayout::documentSize() const
|
||||
{
|
||||
QSizeF size = QPlainTextDocumentLayout::documentSize();
|
||||
size.setWidth(qMax((qreal)m_requiredWidth, size.width()));
|
||||
size.setWidth(qMax(qreal(m_requiredWidth), size.width()));
|
||||
return size;
|
||||
}
|
||||
|
||||
TextMarks TextDocumentLayout::documentClosing()
|
||||
{
|
||||
TextMarks marks;
|
||||
QTextBlock block = document()->begin();
|
||||
while (block.isValid()) {
|
||||
for (QTextBlock block = document()->begin(); block.isValid(); block = block.next()) {
|
||||
if (TextBlockUserData *data = static_cast<TextBlockUserData *>(block.userData()))
|
||||
marks.append(data->documentClosing());
|
||||
block = block.next();
|
||||
}
|
||||
return marks;
|
||||
}
|
||||
|
||||
void TextDocumentLayout::documentReloaded(TextMarks marks, TextDocument *baseTextDocument)
|
||||
{
|
||||
foreach (TextMark *mark, marks) {
|
||||
for (TextMark *mark : qAsConst(marks)) {
|
||||
int blockNumber = mark->lineNumber() - 1;
|
||||
QTextBlock block = document()->findBlockByNumber(blockNumber);
|
||||
if (block.isValid()) {
|
||||
@@ -605,7 +595,7 @@ void TextDocumentLayout::documentReloaded(TextMarks marks, TextDocument *baseTex
|
||||
mark->updateBlock(block);
|
||||
} else {
|
||||
baseTextDocument->removeMarkFromMarksCache(mark);
|
||||
mark->setBaseTextDocument(0);
|
||||
mark->setBaseTextDocument(nullptr);
|
||||
mark->removedFromEditor();
|
||||
}
|
||||
}
|
||||
@@ -619,9 +609,10 @@ void TextDocumentLayout::updateMarksLineNumber()
|
||||
QTextBlock block = document()->begin();
|
||||
int blockNumber = 0;
|
||||
while (block.isValid()) {
|
||||
if (const TextBlockUserData *userData = testUserData(block))
|
||||
foreach (TextMark *mrk, userData->marks())
|
||||
if (const TextBlockUserData *userData = testUserData(block)) {
|
||||
for (TextMark *mrk : userData->marks())
|
||||
mrk->updateLineNumber(blockNumber + 1);
|
||||
}
|
||||
block = block.next();
|
||||
++blockNumber;
|
||||
}
|
||||
@@ -629,10 +620,11 @@ void TextDocumentLayout::updateMarksLineNumber()
|
||||
|
||||
void TextDocumentLayout::updateMarksBlock(const QTextBlock &block)
|
||||
{
|
||||
if (const TextBlockUserData *userData = testUserData(block))
|
||||
foreach (TextMark *mrk, userData->marks())
|
||||
if (const TextBlockUserData *userData = testUserData(block)) {
|
||||
for (TextMark *mrk : userData->marks())
|
||||
mrk->updateBlock(block);
|
||||
}
|
||||
}
|
||||
|
||||
QRectF TextDocumentLayout::blockBoundingRect(const QTextBlock &block) const
|
||||
{
|
||||
@@ -642,12 +634,6 @@ QRectF TextDocumentLayout::blockBoundingRect(const QTextBlock &block) const
|
||||
return boundingRect;
|
||||
}
|
||||
|
||||
TextDocumentLayout::FoldValidator::FoldValidator()
|
||||
: m_layout(0)
|
||||
, m_requestDocUpdate(false)
|
||||
, m_insideFold(0)
|
||||
{}
|
||||
|
||||
void TextDocumentLayout::FoldValidator::setup(TextDocumentLayout *layout)
|
||||
{
|
||||
m_layout = layout;
|
||||
@@ -701,3 +687,5 @@ void TextDocumentLayout::FoldValidator::finalize()
|
||||
m_layout->emitDocumentSizeChanged();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace TextEditor
|
||||
|
||||
@@ -41,12 +41,12 @@ struct TEXTEDITOR_EXPORT Parenthesis
|
||||
{
|
||||
enum Type : char { Opened, Closed };
|
||||
|
||||
inline Parenthesis() : pos(-1), type(Opened) {}
|
||||
inline Parenthesis() = default;
|
||||
inline Parenthesis(Type t, QChar c, int position)
|
||||
: pos(position), chr(c), type(t) {}
|
||||
int pos;
|
||||
int pos = -1;
|
||||
QChar chr;
|
||||
Type type;
|
||||
Type type = Opened;
|
||||
};
|
||||
|
||||
class TEXTEDITOR_EXPORT CodeFormatterData
|
||||
@@ -61,12 +61,12 @@ public:
|
||||
|
||||
inline TextBlockUserData()
|
||||
: m_foldingIndent(0)
|
||||
, m_lexerState(0)
|
||||
, m_folded(false)
|
||||
, m_ifdefedOut(false)
|
||||
, m_lexerState(0)
|
||||
, m_foldingStartIncluded(false)
|
||||
, m_foldingEndIncluded(false)
|
||||
, m_codeFormatterData(0)
|
||||
, m_codeFormatterData(nullptr)
|
||||
{}
|
||||
~TextBlockUserData();
|
||||
|
||||
@@ -75,9 +75,9 @@ public:
|
||||
inline bool removeMark(TextMark *mark) { return m_marks.removeAll(mark); }
|
||||
|
||||
inline TextMarks documentClosing() {
|
||||
TextMarks marks = m_marks;
|
||||
foreach (TextMark *mrk, m_marks)
|
||||
mrk->setBaseTextDocument(0);
|
||||
const TextMarks marks = m_marks;
|
||||
for (TextMark *mrk : marks)
|
||||
mrk->setBaseTextDocument(nullptr);
|
||||
m_marks.clear();
|
||||
return marks;
|
||||
}
|
||||
@@ -136,9 +136,9 @@ public:
|
||||
private:
|
||||
TextMarks m_marks;
|
||||
int m_foldingIndent : 16;
|
||||
int m_lexerState : 8;
|
||||
uint m_folded : 1;
|
||||
uint m_ifdefedOut : 1;
|
||||
uint m_lexerState : 8;
|
||||
uint m_foldingStartIncluded : 1;
|
||||
uint m_foldingEndIncluded : 1;
|
||||
int m_additionalAnnotationHeight = 0;
|
||||
@@ -153,7 +153,7 @@ class TEXTEDITOR_EXPORT TextDocumentLayout : public QPlainTextDocumentLayout
|
||||
|
||||
public:
|
||||
TextDocumentLayout(QTextDocument *doc);
|
||||
~TextDocumentLayout();
|
||||
~TextDocumentLayout() override;
|
||||
|
||||
static void setParentheses(const QTextBlock &block, const Parentheses &parentheses);
|
||||
static void clearParentheses(const QTextBlock &block) { setParentheses(block, Parentheses());}
|
||||
@@ -179,17 +179,15 @@ public:
|
||||
class TEXTEDITOR_EXPORT FoldValidator
|
||||
{
|
||||
public:
|
||||
FoldValidator();
|
||||
|
||||
void setup(TextDocumentLayout *layout);
|
||||
void reset();
|
||||
void process(QTextBlock block);
|
||||
void finalize();
|
||||
|
||||
private:
|
||||
TextDocumentLayout *m_layout;
|
||||
bool m_requestDocUpdate;
|
||||
int m_insideFold;
|
||||
TextDocumentLayout *m_layout = nullptr;
|
||||
bool m_requestDocUpdate = false;
|
||||
int m_insideFold = 0;
|
||||
};
|
||||
|
||||
static TextBlockUserData *testUserData(const QTextBlock &block) {
|
||||
@@ -206,11 +204,10 @@ public:
|
||||
|
||||
void emitDocumentSizeChanged() { emit documentSizeChanged(documentSize()); }
|
||||
|
||||
int lastSaveRevision;
|
||||
bool hasMarks;
|
||||
double maxMarkWidthFactor;
|
||||
|
||||
int m_requiredWidth;
|
||||
int lastSaveRevision = 0;
|
||||
bool hasMarks = false;
|
||||
double maxMarkWidthFactor = 1.0;
|
||||
int m_requiredWidth = 0;
|
||||
|
||||
void setRequiredWidth(int width);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user