show location of error in gutter and as selection

Close #3
This commit is contained in:
eelke 2018-04-09 21:44:00 +02:00
parent 47ee1857cd
commit 45b7d4fcbc
4 changed files with 105 additions and 14 deletions

View file

@ -17,6 +17,7 @@ CodeEditor::CodeEditor(QWidget *parent)
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateGutterAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateGutterArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
updateGutterAreaWidth(0);
highlightCurrentLine();
@ -31,7 +32,7 @@ int CodeEditor::gutterAreaWidth()
++digits;
}
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
int space = 3 + fontMetrics().width(QLatin1Char('9')) * (digits + 2);
return space;
}
@ -61,20 +62,55 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
}
void CodeEditor::highlightCurrentLine()
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
currentLine = selection;
updateExtraSelections();
}
void CodeEditor::onTextChanged()
{
clearErrorMarkers();
}
void CodeEditor::addErrorMarker(int position, int length)
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::red).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setFontItalic(true);
selection.cursor = textCursor();
selection.cursor.setPosition(position);
int lineno = selection.cursor.blockNumber();
errorLines.insert(lineno);
selection.cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, length);
errorMarkers.append(selection);
update();
updateExtraSelections();
}
void CodeEditor::clearErrorMarkers()
{
errorMarkers.clear();
errorLines.clear();
update();
updateExtraSelections();
}
void CodeEditor::updateExtraSelections()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
extraSelections.append(currentLine);
for (auto e : errorMarkers)
extraSelections.append(e);
setExtraSelections(extraSelections);
}
@ -102,6 +138,12 @@ void CodeEditor::gutterAreaPaintEvent(QPaintEvent *event)
painter.setPen(Qt::black);
painter.drawText(0, top, gutterArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
if (errorLines.count(blockNumber) > 0) {
int s = fontMetrics().height() - 8;
painter.setBrush(QBrush(Qt::red));
painter.drawEllipse(4, top + 4, s, s);
}
}
block = block.next();