logic: Improved code view performance

* Don't set stylesheet for every token activation
* Streamlined code area creation steps
* Delay most syntax highlighting until lines become visible
This commit is contained in:
Eberhard Graether
2017-06-05 23:34:20 +02:00
parent 30e6512d71
commit 71b3bdb26b
12 changed files with 217 additions and 123 deletions
-5
View File
@@ -141,11 +141,6 @@
font-size: <setting:font_size>px;
}
#code_file #code_snippet[isFirst=true] {
border: none;
padding-top: 0px;
}
#code_file #code_snippet #scope_name, #code_file #code_snippet #dots {
background-color: <color:code/snippet/title/background>;
border: none;
@@ -400,6 +400,8 @@ void CodeController::clear()
void CodeController::expandVisibleSnippets(std::vector<CodeSnippetParams>* snippets) const
{
TRACE();
bool inListMode = getView()->isInListMode();
size_t filesToExpand = inListMode ? std::min(int(snippets->size()), 3) : 1;
@@ -549,6 +551,8 @@ std::vector<CodeSnippetParams> CodeController::getSnippetsForCollection(
std::shared_ptr<SourceLocationCollection> collection, bool addSourceLocations
) const
{
TRACE();
std::vector<CodeSnippetParams> snippets;
collection->forEachSourceLocationFile(
+42 -39
View File
@@ -9,6 +9,7 @@
#include <QPainter>
#include <QPushButton>
#include <QScrollBar>
#include <QTextBlock>
#include <QToolTip>
#include "utility/messaging/type/MessageActivateLocalSymbols.h"
@@ -110,22 +111,7 @@ QtCodeArea::QtCodeArea(
viewport()->setCursor(Qt::ArrowCursor);
FilePath path = m_locationFile->getFilePath();
LanguageType language = LANGUAGE_UNKNOWN;
if (!path.empty())
{
if (path.extension() == ".java")
{
language = LANGUAGE_JAVA;
}
else
{
language = LANGUAGE_CPP;
}
}
m_lineNumberArea = new LineNumberArea(this);
m_highlighter = new QtHighlighter(document(), language);
std::string displayCode = m_code;
if (!displayCode.empty() && *displayCode.rbegin() == '\n')
@@ -149,10 +135,18 @@ QtCodeArea::QtCodeArea(
m_scrollSpeedChangeListener.setScrollBar(horizontalScrollBar());
createActions();
m_highlighter->highlightDocument();
createAnnotations(locationFile);
FilePath path = m_locationFile->getFilePath();
LanguageType language = LANGUAGE_UNKNOWN;
if (!path.empty())
{
language = (path.extension() == ".java" ? LANGUAGE_JAVA : LANGUAGE_CPP);
}
m_highlighter = new QtHighlighter(document(), language);
m_highlighter->highlightDocument();
}
QtCodeArea::~QtCodeArea()
@@ -406,15 +400,25 @@ void QtCodeArea::paintEvent(QPaintEvent* event)
{
if (firstVisibleLine < 0 && bottom >= event->rect().top())
{
firstVisibleLine = block.blockNumber();;
firstVisibleLine = block.blockNumber();
}
lastVisibleLine = block.blockNumber();;
lastVisibleLine = block.blockNumber();
}
block = block.next();
top = bottom;
bottom = top + static_cast<int>(blockBoundingRect(block).height());
}
std::vector<std::pair<int, int>> ranges;
for (size_t i : m_colorChangedAnnotationIndices)
{
Annotation& annotation = m_annotations[i];
ranges.push_back(std::pair<int, int>(annotation.start, annotation.end));
}
m_highlighter->highlightRange(firstVisibleLine, lastVisibleLine, ranges);
firstVisibleLine += m_startLineNumber;
lastVisibleLine += m_startLineNumber;
@@ -834,9 +838,13 @@ void QtCodeArea::annotateText()
const std::set<Id>& activeLocalSymbolIds = m_navigator->getActiveLocalSymbolIds();
const std::set<Id>& focusIds = m_navigator->getFocusedTokenIds();
std::vector<int> linesToRehighlight;
bool needsUpdate = false;
for (Annotation& annotation: m_annotations)
for (size_t i = 0; i < m_annotations.size(); i++)
{
Annotation& annotation = m_annotations[i];
bool wasActive = annotation.isActive;
bool wasFocused = annotation.isFocused;
const AnnotationColor& oldColor = getAnnotationColorForAnnotation(annotation);
@@ -860,31 +868,21 @@ void QtCodeArea::annotateText()
{
if (newColor.text.size() > 0 && newColor.text != "transparent")
{
bool isDuplicateAnnotation = false;
for (Annotation* a : m_colorChangedAnnotations)
if (!annotation.oldTextColor.isValid())
{
if (a->start == annotation.start && a->end == annotation.end && a->locationId != annotation.locationId)
{
isDuplicateAnnotation = true;
break;
}
annotation.oldTextColor = m_highlighter->getFormat(annotation.start, annotation.end).foreground().color();
}
if (!isDuplicateAnnotation)
{
if (!annotation.oldTextColor.isValid())
{
annotation.oldTextColor = m_highlighter->getFormat(annotation.start, annotation.end).foreground().color();
}
setTextColorForAnnotation(annotation, QColor(newColor.text.c_str()));
m_colorChangedAnnotations.push_back(&annotation);
}
setTextColorForAnnotation(annotation, QColor(newColor.text.c_str()));
m_colorChangedAnnotationIndices.insert(i);
}
else if (annotation.oldTextColor.isValid())
{
setTextColorForAnnotation(annotation, annotation.oldTextColor);
annotation.oldTextColor = QColor();
m_colorChangedAnnotationIndices.erase(i);
linesToRehighlight.push_back(annotation.startLine - 1);
}
}
@@ -894,7 +892,12 @@ void QtCodeArea::annotateText()
}
}
if (needsUpdate)
if (linesToRehighlight.size())
{
m_highlighter->rehighlightLines(linesToRehighlight);
}
if (m_wasAnnotated && needsUpdate)
{
m_lineNumberArea->update();
viewport()->update();
+1 -2
View File
@@ -188,8 +188,7 @@ private:
std::vector<Annotation> m_annotations;
std::vector<const Annotation*> m_hoveredAnnotations;
// Remove when annotations become unique regarding start and end, and store multiple tokenIds
std::vector<Annotation*> m_colorChangedAnnotations;
std::set<size_t> m_colorChangedAnnotationIndices;
int m_digits;
+12 -6
View File
@@ -30,6 +30,7 @@ QtCodeFile::QtCodeFile(const FilePath& filePath, QtCodeNavigator* navigator)
m_titleBar = new QPushButton(this);
m_titleBar->setObjectName("title_widget");
m_titleBar->setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
layout->addWidget(m_titleBar);
QHBoxLayout* titleLayout = new QHBoxLayout();
@@ -83,6 +84,8 @@ QtCodeFile::QtCodeFile(const FilePath& filePath, QtCodeNavigator* navigator)
connect(m_maximizeButton, SIGNAL(clicked()), this, SLOT(clickedMaximizeButton()));
m_snippetLayout = new QVBoxLayout();
m_snippetLayout->setContentsMargins(0, 0, 0, 0);
m_snippetLayout->setSpacing(0);
layout->addLayout(m_snippetLayout);
update();
@@ -122,8 +125,7 @@ QtCodeSnippet* QtCodeFile::addCodeSnippet(const CodeSnippetParams& params)
if (params.locationFile->isWhole())
{
snippet->setProperty("isFirst", true);
snippet->setProperty("isLast", true);
snippet->setStyleSheet("#code_snippet { border: none; }");
m_fileSnippet = snippet;
if (!m_snippets.size())
@@ -359,8 +361,10 @@ void QtCodeFile::updateSnippets()
int maxDigits = 1;
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
{
snippet->setProperty("isFirst", false);
snippet->setProperty("isLast", false);
if (snippet != m_snippets.front() && snippet->styleSheet().size())
{
snippet->setStyleSheet("");
}
maxDigits = qMax(maxDigits, snippet->lineNumberDigits());
}
@@ -370,8 +374,10 @@ void QtCodeFile::updateSnippets()
snippet->updateLineNumberAreaWidthForDigits(maxDigits);
}
m_snippets.front()->setProperty("isFirst", true);
m_snippets.back()->setProperty("isLast", true);
if (!m_snippets.front()->styleSheet().size())
{
m_snippets.front()->setStyleSheet("#code_snippet { border: none; }");
}
}
void QtCodeFile::updateTitleBar()
+22 -14
View File
@@ -29,15 +29,19 @@ void QtCodeNavigateable::ensureWidgetVisibleAnimated(
if (rect.height() > 0)
{
focusRect = QRect(childWidget->mapTo(parentWidget, rect.topLeft().toPoint()), rect.size().toSize());
focusRect.adjust(0, 0, 0, 100);
if (focusRect.height() < 100)
{
focusRect.adjust(0, 0, 0, 100);
}
}
QScrollBar* scrollBar = area->verticalScrollBar();
int value = focusRect.center().y() - visibleRect.center().y();
if (onTop)
if (onTop || focusRect.height() > visibleRect.height())
{
value = focusRect.top() - visibleRect.top();
value = focusRect.top() - visibleRect.top() - 20;
}
if (scrollBar && (value > 50 || value < -50))
@@ -113,17 +117,21 @@ void QtCodeNavigateable::ensurePercentVisibleAnimated(double percentA, double pe
int value = scrollHeight * scrollFactor;
if (animated && ApplicationSettings::getInstance()->getUseAnimations())
int diff = value - scrollBar->value();
if (diff > 5 || diff < -5)
{
QPropertyAnimation* anim = new QPropertyAnimation(scrollBar, "value");
anim->setDuration(300);
anim->setStartValue(scrollBar->value());
anim->setEndValue(value);
anim->setEasingCurve(QEasingCurve::InOutQuad);
anim->start();
}
else
{
scrollBar->setValue(value);
if (animated && ApplicationSettings::getInstance()->getUseAnimations())
{
QPropertyAnimation* anim = new QPropertyAnimation(scrollBar, "value");
anim->setDuration(300);
anim->setStartValue(scrollBar->value());
anim->setEndValue(value);
anim->setEasingCurve(QEasingCurve::InOutQuad);
anim->start();
}
else
{
scrollBar->setValue(value);
}
}
}
+1 -3
View File
@@ -20,7 +20,6 @@
#include "qt/utility/utilityQt.h"
#include "settings/ApplicationSettings.h"
QtCodeNavigator::QtCodeNavigator(QWidget* parent)
: QWidget(parent)
, m_mode(MODE_NONE)
@@ -586,7 +585,7 @@ void QtCodeNavigator::scrollToDefinition(bool animated, bool ignoreActiveReferen
if (!m_activeTokenId)
{
if (m_mode == MODE_SINGLE && m_references.size() && m_references.front().locationType != LOCATION_TOKEN)
if (m_references.size() && m_references.front().locationType != LOCATION_TOKEN)
{
requestScroll(m_references.front().filePath, 0, m_references.front().locationId, false, false);
emit scrollRequest();
@@ -596,7 +595,6 @@ void QtCodeNavigator::scrollToDefinition(bool animated, bool ignoreActiveReferen
if (m_mode == MODE_LIST)
{
std::cout << animated << std::endl;
std::pair<QtCodeSnippet*, Id> result = m_list->getFirstSnippetWithActiveLocationId(m_activeTokenId);
if (result.first != nullptr)
{
+2 -1
View File
@@ -19,7 +19,8 @@ std::shared_ptr<QtCodeSnippet> QtCodeSnippet::merged(
SourceLocationFile* aFile = a->m_codeArea->getSourceLocationFile().get();
SourceLocationFile* bFile = b->m_codeArea->getSourceLocationFile().get();
std::shared_ptr<SourceLocationFile> locationFile = std::make_shared<SourceLocationFile>(aFile->getFilePath(), aFile->isWhole(), aFile->isWhole());
std::shared_ptr<SourceLocationFile> locationFile =
std::make_shared<SourceLocationFile>(aFile->getFilePath(), aFile->isWhole(), aFile->isWhole());
aFile->forEachSourceLocation(
[&locationFile](SourceLocation* loc)
+114 -39
View File
@@ -1,5 +1,6 @@
#include "qt/utility/QtHighlighter.h"
#include <QTextBlock>
#include <QTextCursor>
#include <QTextDocument>
@@ -105,17 +106,9 @@ void QtHighlighter::clearHighlightingRules()
s_highlightingRules.clear();
}
QtHighlighter::QtHighlighter(QTextDocument *parent, LanguageType language)
: QSyntaxHighlighter(parent)
QtHighlighter::QtHighlighter(QTextDocument *document, LanguageType language)
: m_document(document)
, m_language(language)
{
}
void QtHighlighter::highlightBlock(const QString& text)
{
}
void QtHighlighter::highlightDocument()
{
if (m_language == LANGUAGE_UNKNOWN)
{
@@ -127,6 +120,25 @@ void QtHighlighter::highlightDocument()
createHighlightingRules();
}
m_highlightingRules = s_highlightingRules;
if (m_language == LANGUAGE_JAVA)
{
m_highlightingRules.append(s_highlightingRulesJava);
}
else
{
m_highlightingRules.append(s_highlightingRulesCpp);
}
}
void QtHighlighter::highlightDocument()
{
if (m_language == LANGUAGE_UNKNOWN)
{
return;
}
QTextDocument* doc = document();
int docStart = 0;
@@ -138,37 +150,106 @@ void QtHighlighter::highlightDocument()
docEnd -= 1;
applyFormat(docStart, docEnd, s_textFormat);
std::vector<std::pair<int, int>> ranges;
m_ranges.clear();
m_highlightedLines.clear();
m_highlightedLines.resize(document()->blockCount(), false);
for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
{
formatBlock(it, s_quotationRule, &ranges, true);
formatBlock(it, s_quotationRule, &m_ranges, true);
}
QVector<HighlightingRule> highlightingRules = s_highlightingRules;
if (m_language == LANGUAGE_JAVA)
highlightMultiLineComments(&m_ranges);
}
void QtHighlighter::highlightRange(int startLine, int endLine, std::vector<std::pair<int, int>> ranges)
{
if (m_language == LANGUAGE_UNKNOWN)
{
highlightingRules.append(s_highlightingRulesJava);
}
else
{
highlightingRules.append(s_highlightingRulesCpp);
return;
}
for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
if (startLine < 0 || endLine < 0 || startLine > endLine || endLine > int(m_highlightedLines.size()))
{
foreach (const HighlightingRule &rule, highlightingRules)
return;
}
bool hasUnhighlightedLines = false;
for (int i = startLine; i <= endLine; i++)
{
if (!m_highlightedLines[i])
{
formatBlock(it, rule, &ranges, false);
hasUnhighlightedLines = true;
break;
}
}
highlightMultiLineComments(&ranges);
for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
if (!hasUnhighlightedLines)
{
formatBlock(it, s_commentRule, &ranges, true);
return;
}
QTextDocument* doc = document();
QTextBlock start = doc->findBlockByLineNumber(startLine);
QTextBlock end = doc->findBlockByLineNumber(endLine + 1);
ranges.insert(ranges.end(), m_ranges.begin(), m_ranges.end());
int index = startLine;
for (QTextBlock it = start; it != end; it = it.next())
{
if (!m_highlightedLines[index])
{
foreach (const HighlightingRule &rule, m_highlightingRules)
{
formatBlock(it, rule, &ranges, false);
}
}
index++;
}
index = startLine;
for (QTextBlock it = start; it != end; it = it.next())
{
if (!m_highlightedLines[index])
{
formatBlock(it, s_commentRule, &ranges, false);
}
index++;
}
for (int i = startLine; i <= endLine; i++)
{
m_highlightedLines[i] = true;
}
}
void QtHighlighter::rehighlightLines(const std::vector<int>& lines)
{
for (int line : lines)
{
if (line >= 0 && line < int(m_highlightedLines.size()))
{
m_highlightedLines[line] = false;
}
}
}
void QtHighlighter::applyFormat(int startPosition, int endPosition, const QTextCharFormat& format)
{
QTextCursor cursor(document());
cursor.setPosition(startPosition);
cursor.setPosition(endPosition, QTextCursor::KeepAnchor);
cursor.setCharFormat(format);
}
QTextCharFormat QtHighlighter::getFormat(int startPosition, int endPosition) const
{
QTextCursor cursor(document());
cursor.setPosition(endPosition);
return cursor.charFormat();
}
void QtHighlighter::highlightMultiLineComments(std::vector<std::pair<int, int>>* ranges)
@@ -251,7 +332,11 @@ void QtHighlighter::formatBlock(
applyFormat(pos + index, pos + index + length, rule.format);
}
newRanges.push_back(std::pair<int, int>(pos + index, pos + index + length));
if (saveRange)
{
newRanges.push_back(std::pair<int, int>(pos + index, pos + index + length));
}
index = expression.indexIn(block.text(), index + length);
}
@@ -261,17 +346,7 @@ void QtHighlighter::formatBlock(
}
}
void QtHighlighter::applyFormat(int startPosition, int endPosition, const QTextCharFormat& format)
QTextDocument* QtHighlighter::document() const
{
QTextCursor cursor(document());
cursor.setPosition(startPosition);
cursor.setPosition(endPosition, QTextCursor::KeepAnchor);
cursor.setCharFormat(format);
}
QTextCharFormat QtHighlighter::getFormat(int startPosition, int endPosition) const
{
QTextCursor cursor(document());
cursor.setPosition(endPosition);
return cursor.charFormat();
return m_document;
}
+12 -7
View File
@@ -1,31 +1,28 @@
#ifndef QT_HIGHLIGHTER_H
#define QT_HIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include "settings/LanguageType.h"
class QTextBlock;
class QTextDocument;
class QtHighlighter
: public QSyntaxHighlighter
{
Q_OBJECT
public:
static void createHighlightingRules();
static void clearHighlightingRules();
QtHighlighter(QTextDocument *parent, LanguageType language);
void highlightDocument();
void highlightRange(int startLine, int endLine, std::vector<std::pair<int, int>> ranges);
void rehighlightLines(const std::vector<int>& lines);
void applyFormat(int startPosition, int endPosition, const QTextCharFormat& format);
QTextCharFormat getFormat(int startPosition, int endPosition) const;
protected:
void highlightBlock(const QString& text);
private:
struct HighlightingRule
{
@@ -41,6 +38,8 @@ private:
bool isInRange(int index, const std::vector<std::pair<int, int>>& ranges) const;
void formatBlock(const QTextBlock& block, const HighlightingRule& rule, std::vector<std::pair<int, int>>* ranges, bool saveRange);
QTextDocument* document() const;
static QVector<HighlightingRule> s_highlightingRules;
static QVector<HighlightingRule> s_highlightingRulesCpp;
static QVector<HighlightingRule> s_highlightingRulesJava;
@@ -48,7 +47,13 @@ private:
static HighlightingRule s_commentRule;
static QTextCharFormat s_textFormat;
QTextDocument* m_document;
LanguageType m_language;
QVector<HighlightingRule> m_highlightingRules;
std::vector<std::pair<int, int>> m_ranges;
std::vector<bool> m_highlightedLines;
};
#endif // QT_HIGHLIGHTER_H
+7 -5
View File
@@ -1,6 +1,7 @@
#include "qt/view/QtCodeView.h"
#include "utility/ResourcePaths.h"
#include "utility/tracing.h"
#include "qt/element/QtCodeArea.h"
#include "qt/element/QtCodeNavigator.h"
@@ -33,6 +34,8 @@ void QtCodeView::refreshView()
{
m_onQtThread([=]()
{
TRACE("refresh");
setStyleSheet();
m_widget->refreshStyle();
@@ -61,6 +64,8 @@ void QtCodeView::showCodeSnippets(const std::vector<CodeSnippetParams>& snippets
{
m_onQtThread([=]()
{
TRACE("show code snippets");
if (params.clearSnippets)
{
m_widget->clearCodeSnippets();
@@ -92,11 +97,6 @@ void QtCodeView::showCodeSnippets(const std::vector<CodeSnippetParams>& snippets
m_widget->updateFiles();
if (m_widget->isInListMode())
{
setStyleSheet(); // so property "isLast" of QtCodeSnippet is computed correctly
}
if (params.showContents)
{
m_widget->showContents();
@@ -134,6 +134,7 @@ void QtCodeView::showActiveSnippet(
{
m_onQtThread([=]()
{
TRACE("show active snippet");
m_widget->showActiveSnippet(activeTokenIds, collection, scrollTo);
});
}
@@ -178,6 +179,7 @@ void QtCodeView::showContents()
{
m_onQtThread([=]()
{
TRACE("show contents");
m_widget->showContents();
performScroll();
});
-2
View File
@@ -41,8 +41,6 @@ public:
virtual bool isInListMode() const;
virtual bool hasSingleFileCached(const FilePath& filePath) const;
private:
void performScroll();
void setStyleSheet() const;