Commit a7143664 authored by mae's avatar mae
Browse files

removed the folding ribbon from the default configuration, instead

turned a new block highlighting mechanism on.
parent f933f521
...@@ -188,6 +188,10 @@ BaseTextEditor::BaseTextEditor(QWidget *parent) ...@@ -188,6 +188,10 @@ BaseTextEditor::BaseTextEditor(QWidget *parent)
d->m_parenthesesMatchingTimer->setSingleShot(true); d->m_parenthesesMatchingTimer->setSingleShot(true);
connect(d->m_parenthesesMatchingTimer, SIGNAL(timeout()), this, SLOT(_q_matchParentheses())); connect(d->m_parenthesesMatchingTimer, SIGNAL(timeout()), this, SLOT(_q_matchParentheses()));
d->m_highlightBlocksTimer = new QTimer(this);
d->m_highlightBlocksTimer->setSingleShot(true);
connect(d->m_highlightBlocksTimer, SIGNAL(timeout()), this, SLOT(_q_highlightBlocks()));
d->m_searchResultFormat.setBackground(QColor(0xffef0b)); d->m_searchResultFormat.setBackground(QColor(0xffef0b));
...@@ -429,6 +433,23 @@ bool DocumentMarker::addMark(TextEditor::ITextMark *mark, int line) ...@@ -429,6 +433,23 @@ bool DocumentMarker::addMark(TextEditor::ITextMark *mark, int line)
return false; return false;
} }
int BaseTextEditorPrivate::visualIndent(const QTextBlock &block) const
{
if (!block.isValid())
return 0;
const QTextDocument *document = block.document();
int i = 0;
while (i < block.length()) {
if (!document->characterAt(block.position() + i).isSpace()) {
QTextCursor cursor(block);
cursor.setPosition(block.position() + i);
return q->cursorRect(cursor).x();
}
++i;
}
return 0;
}
TextEditor::TextMarks DocumentMarker::marksAt(int line) const TextEditor::TextMarks DocumentMarker::marksAt(int line) const
{ {
...@@ -1198,8 +1219,11 @@ bool BaseTextEditor::lineSeparatorsAllowed() const ...@@ -1198,8 +1219,11 @@ bool BaseTextEditor::lineSeparatorsAllowed() const
void BaseTextEditor::setHighlightBlocks(bool b) void BaseTextEditor::setHighlightBlocks(bool b)
{ {
d->m_highlightBlocks = b & d->m_codeFoldingSupported; if (d->m_highlightBlocks == b)
viewport()->update(); return;
d->m_highlightBlocks = b;
d->m_highlightBlocksInfo = BaseTextEditorPrivateHighlightBlocks();
_q_highlightBlocks();
} }
bool BaseTextEditor::highlightBlocks() const bool BaseTextEditor::highlightBlocks() const
...@@ -1755,22 +1779,28 @@ void BaseTextEditor::paintEvent(QPaintEvent *e) ...@@ -1755,22 +1779,28 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
QTextBlock visibleCollapsedBlock; QTextBlock visibleCollapsedBlock;
QPointF visibleCollapsedBlockOffset; QPointF visibleCollapsedBlockOffset;
while (block.isValid()) { while (block.isValid()) {
QRectF r = blockBoundingRect(block).translated(offset); QRectF r = blockBoundingRect(block).translated(offset);
if (d->m_highlightBlocks) { if (d->m_highlightBlocks) {
QTextBlock previousBlock = block.previous();
if (previousBlock.isValid()){ int n = block.blockNumber();
int thisBraceDepth = block.userState(); int depth = 0;
if (thisBraceDepth >= 0) foreach (int i, d->m_highlightBlocksInfo.open)
thisBraceDepth >>= 8; if (n >= i)
int braceDepth = block.previous().userState(); ++depth;
if (braceDepth >= 0) foreach (int i, d->m_highlightBlocksInfo.close)
braceDepth >>= 8; if (n > i)
int minBraceDepth = qMin(thisBraceDepth, braceDepth); --depth;
if (minBraceDepth > 0) {
painter.fillRect(r, calcBlendColor(baseColor, minBraceDepth)); int count = d->m_highlightBlocksInfo.visualIndent.size();
if (count) {
for(int i = 0; i <= depth; ++i) {
int vi = i > 0 ? d->m_highlightBlocksInfo.visualIndent.at(i-1) : 0;
painter.fillRect(r.adjusted(vi, 0, 0, 0), calcBlendColor(baseColor, count - i));
} }
} }
} }
...@@ -1881,7 +1911,6 @@ void BaseTextEditor::paintEvent(QPaintEvent *e) ...@@ -1881,7 +1911,6 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
// invisible blocks do have zero line count // invisible blocks do have zero line count
block = doc->findBlockByLineNumber(block.firstLineNumber()); block = doc->findBlockByLineNumber(block.firstLineNumber());
} }
} }
if (backgroundVisible() && !block.isValid() && offset.y() <= er.bottom() if (backgroundVisible() && !block.isValid() && offset.y() <= er.bottom()
...@@ -2390,6 +2419,10 @@ void BaseTextEditor::slotCursorPositionChanged() ...@@ -2390,6 +2419,10 @@ void BaseTextEditor::slotCursorPositionChanged()
} }
setExtraSelections(CurrentLineSelection, extraSelections); setExtraSelections(CurrentLineSelection, extraSelections);
if (d->m_highlightBlocks) {
d->m_highlightBlocksTimer->start(100);
}
} }
void BaseTextEditor::slotUpdateBlockNotify(const QTextBlock &block) void BaseTextEditor::slotUpdateBlockNotify(const QTextBlock &block)
...@@ -3139,6 +3172,67 @@ bool TextBlockUserData::findNextClosingParenthesis(QTextCursor *cursor, bool sel ...@@ -3139,6 +3172,67 @@ bool TextBlockUserData::findNextClosingParenthesis(QTextCursor *cursor, bool sel
return false; return false;
} }
bool TextBlockUserData::findPreviousBlockOpenParenthesis(QTextCursor *cursor)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = parenList.count()-1; i >= 0; --i) {
Parenthesis paren = parenList.at(i);
if (paren.chr != QLatin1Char('{') && paren.chr != QLatin1Char('}')
&& paren.chr != QLatin1Char('+') && paren.chr != QLatin1Char('-'))
continue;
if (block == cursor->block() &&
(position - block.position() <= paren.pos))
continue;
if (paren.type == Parenthesis::Closed) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos);
return true;
}
}
}
block = block.previous();
}
return false;
}
bool TextBlockUserData::findNextBlockClosingParenthesis(QTextCursor *cursor)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = 0; i < parenList.count(); ++i) {
Parenthesis paren = parenList.at(i);
if (paren.chr != QLatin1Char('{') && paren.chr != QLatin1Char('}')
&& paren.chr != QLatin1Char('+') && paren.chr != QLatin1Char('-'))
continue;
if (block == cursor->block() && position - block.position() >= paren.pos)
continue;
if (paren.type == Parenthesis::Opened) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos+1);
return true;
}
}
}
block = block.next();
}
return false;
}
TextBlockUserData::MatchType TextBlockUserData::matchCursorBackward(QTextCursor *cursor) TextBlockUserData::MatchType TextBlockUserData::matchCursorBackward(QTextCursor *cursor)
{ {
cursor->clearSelection(); cursor->clearSelection();
...@@ -3274,6 +3368,23 @@ void BaseTextEditor::_q_matchParentheses() ...@@ -3274,6 +3368,23 @@ void BaseTextEditor::_q_matchParentheses()
setExtraSelections(ParenthesesMatchingSelection, extraSelections); setExtraSelections(ParenthesesMatchingSelection, extraSelections);
} }
void BaseTextEditor::_q_highlightBlocks()
{
QTextCursor cursor = textCursor();
QTextCursor closeCursor = cursor;
BaseTextEditorPrivateHighlightBlocks highlightBlocksInfo;
while (TextBlockUserData::findPreviousBlockOpenParenthesis(&cursor)) {
highlightBlocksInfo.open.prepend(cursor.blockNumber());
highlightBlocksInfo.visualIndent.prepend(d->visualIndent(cursor.block()));
if (TextBlockUserData::findNextBlockClosingParenthesis(&closeCursor))
highlightBlocksInfo.close.append(closeCursor.blockNumber());
}
if (d->m_highlightBlocksInfo != highlightBlocksInfo) {
d->m_highlightBlocksInfo = highlightBlocksInfo;
viewport()->update();
}
}
void BaseTextEditor::setActionHack(QObject *hack) void BaseTextEditor::setActionHack(QObject *hack)
{ {
d->m_actionHack = hack; d->m_actionHack = hack;
......
...@@ -167,6 +167,9 @@ public: ...@@ -167,6 +167,9 @@ public:
static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false); static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false);
static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false); static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false);
static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor);
static bool findNextBlockClosingParenthesis(QTextCursor *cursor);
private: private:
TextMarks m_marks; TextMarks m_marks;
...@@ -464,6 +467,7 @@ private: ...@@ -464,6 +467,7 @@ private:
// parentheses matcher // parentheses matcher
private slots: private slots:
void _q_matchParentheses(); void _q_matchParentheses();
void _q_highlightBlocks();
void slotSelectionChanged(); void slotSelectionChanged();
}; };
......
...@@ -114,6 +114,17 @@ private: ...@@ -114,6 +114,17 @@ private:
//================BaseTextEditorPrivate============== //================BaseTextEditorPrivate==============
struct BaseTextEditorPrivateHighlightBlocks
{
QList<int> open;
QList<int> close;
QList<int> visualIndent;
inline bool operator==(const BaseTextEditorPrivateHighlightBlocks &o) const {
return (open == o.open && close == o.close && visualIndent == o.visualIndent);
}
inline bool operator!=(const BaseTextEditorPrivateHighlightBlocks &o) const { return !(*this == o); }
};
class BaseTextEditorPrivate class BaseTextEditorPrivate
{ {
BaseTextEditorPrivate(const BaseTextEditorPrivate &); BaseTextEditorPrivate(const BaseTextEditorPrivate &);
...@@ -219,6 +230,11 @@ public: ...@@ -219,6 +230,11 @@ public:
QTextCursor m_selectBlockAnchor; QTextCursor m_selectBlockAnchor;
void moveCursorVisible(bool ensureVisible = true); void moveCursorVisible(bool ensureVisible = true);
int visualIndent(const QTextBlock &block) const;
BaseTextEditorPrivateHighlightBlocks m_highlightBlocksInfo;
QTimer *m_highlightBlocksTimer;
}; };
} // namespace Internal } // namespace Internal
......
...@@ -39,9 +39,9 @@ static const char * const textWrappingKey = "TextWrapping"; ...@@ -39,9 +39,9 @@ static const char * const textWrappingKey = "TextWrapping";
static const char * const showWrapColumnKey = "ShowWrapColumn"; static const char * const showWrapColumnKey = "ShowWrapColumn";
static const char * const wrapColumnKey = "WrapColumn"; static const char * const wrapColumnKey = "WrapColumn";
static const char * const visualizeWhitespaceKey = "VisualizeWhitespace"; static const char * const visualizeWhitespaceKey = "VisualizeWhitespace";
static const char * const displayFoldingMarkersKey = "DisplayFoldingMarkers"; static const char * const displayFoldingMarkersKey = "DisplayFoldingMarkersV2";
static const char * const highlightCurrentLineKey = "HighlightCurrentLineKey"; static const char * const highlightCurrentLineKey = "HighlightCurrentLineKey";
static const char * const highlightBlocksKey = "HighlightBlocksKey"; static const char * const highlightBlocksKey = "HighlightBlocksKeyV2";
static const char * const groupPostfix = "DisplaySettings"; static const char * const groupPostfix = "DisplaySettings";
namespace TextEditor { namespace TextEditor {
...@@ -52,9 +52,9 @@ DisplaySettings::DisplaySettings() : ...@@ -52,9 +52,9 @@ DisplaySettings::DisplaySettings() :
m_showWrapColumn(false), m_showWrapColumn(false),
m_wrapColumn(80), m_wrapColumn(80),
m_visualizeWhitespace(false), m_visualizeWhitespace(false),
m_displayFoldingMarkers(true), m_displayFoldingMarkers(false),
m_highlightCurrentLine(true), m_highlightCurrentLine(true),
m_highlightBlocks(false) m_highlightBlocks(true)
{ {
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment