GUI: Updated code viewer
This commit is contained in:
parent
21bb973702
commit
feef8f3ebe
|
@ -49,15 +49,31 @@ Highlighter::Highlighter(QTextDocument *parent)
|
||||||
rule.format = singleLineCommentFormat;
|
rule.format = singleLineCommentFormat;
|
||||||
highlightingRules.append(rule);
|
highlightingRules.append(rule);
|
||||||
|
|
||||||
|
highlightingRulesWithSymbols = highlightingRules;
|
||||||
|
|
||||||
multiLineCommentFormat.setForeground(Qt::gray);
|
multiLineCommentFormat.setForeground(Qt::gray);
|
||||||
|
|
||||||
|
symbolFormat.setForeground(Qt::red);
|
||||||
|
symbolFormat.setBackground(QColor(220,220,255));
|
||||||
|
|
||||||
commentStartExpression = QRegularExpression("/\\*");
|
commentStartExpression = QRegularExpression("/\\*");
|
||||||
commentEndExpression = QRegularExpression("\\*/");
|
commentEndExpression = QRegularExpression("\\*/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Highlighter::setSymbols(const QStringList &symbols)
|
||||||
|
{
|
||||||
|
highlightingRulesWithSymbols = highlightingRules;
|
||||||
|
foreach (const QString &sym, symbols) {
|
||||||
|
HighlightingRule rule;
|
||||||
|
rule.pattern = QRegularExpression("\\b" + sym + "\\b");
|
||||||
|
rule.format = symbolFormat;
|
||||||
|
highlightingRulesWithSymbols.append(rule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Highlighter::highlightBlock(const QString &text)
|
void Highlighter::highlightBlock(const QString &text)
|
||||||
{
|
{
|
||||||
foreach (const HighlightingRule &rule, highlightingRules) {
|
foreach (const HighlightingRule &rule, highlightingRulesWithSymbols) {
|
||||||
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
|
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
|
||||||
while (matchIterator.hasNext()) {
|
while (matchIterator.hasNext()) {
|
||||||
QRegularExpressionMatch match = matchIterator.next();
|
QRegularExpressionMatch match = matchIterator.next();
|
||||||
|
@ -94,6 +110,8 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
|
||||||
highlighter = new Highlighter(this->document());
|
highlighter = new Highlighter(this->document());
|
||||||
mErrorPosition = -1;
|
mErrorPosition = -1;
|
||||||
|
|
||||||
|
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
|
||||||
|
|
||||||
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
||||||
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
|
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
|
||||||
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
|
||||||
|
@ -103,19 +121,25 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
|
||||||
|
|
||||||
static int getPos(const QString &fileData, int lineNumber)
|
static int getPos(const QString &fileData, int lineNumber)
|
||||||
{
|
{
|
||||||
|
if (lineNumber <= 1)
|
||||||
|
return 0;
|
||||||
for (int pos = 0, line = 1; pos < fileData.size(); ++pos) {
|
for (int pos = 0, line = 1; pos < fileData.size(); ++pos) {
|
||||||
if (fileData[pos] != '\n')
|
if (fileData[pos] != '\n')
|
||||||
continue;
|
continue;
|
||||||
++line;
|
++line;
|
||||||
if (line == lineNumber)
|
if (line >= lineNumber)
|
||||||
return pos + 1;
|
return pos + 1;
|
||||||
}
|
}
|
||||||
return fileData.size();
|
return fileData.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodeEditor::setErrorLine(int errorLine)
|
void CodeEditor::setError(const QString &code, int errorLine, const QStringList &symbols)
|
||||||
{
|
{
|
||||||
mErrorPosition = getPos(toPlainText(), errorLine);
|
highlighter->setSymbols(symbols);
|
||||||
|
|
||||||
|
setPlainText(code);
|
||||||
|
|
||||||
|
mErrorPosition = getPos(code, errorLine);
|
||||||
QTextCursor tc = textCursor();
|
QTextCursor tc = textCursor();
|
||||||
tc.setPosition(mErrorPosition);
|
tc.setPosition(mErrorPosition);
|
||||||
setTextCursor(tc);
|
setTextCursor(tc);
|
||||||
|
|
|
@ -20,6 +20,8 @@ class Highlighter : public QSyntaxHighlighter {
|
||||||
public:
|
public:
|
||||||
explicit Highlighter(QTextDocument *parent);
|
explicit Highlighter(QTextDocument *parent);
|
||||||
|
|
||||||
|
void setSymbols(const QStringList &symbols);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void highlightBlock(const QString &text) override;
|
void highlightBlock(const QString &text) override;
|
||||||
|
|
||||||
|
@ -29,6 +31,7 @@ private:
|
||||||
QTextCharFormat format;
|
QTextCharFormat format;
|
||||||
};
|
};
|
||||||
QVector<HighlightingRule> highlightingRules;
|
QVector<HighlightingRule> highlightingRules;
|
||||||
|
QVector<HighlightingRule> highlightingRulesWithSymbols;
|
||||||
|
|
||||||
QRegularExpression commentStartExpression;
|
QRegularExpression commentStartExpression;
|
||||||
QRegularExpression commentEndExpression;
|
QRegularExpression commentEndExpression;
|
||||||
|
@ -39,6 +42,7 @@ private:
|
||||||
QTextCharFormat multiLineCommentFormat;
|
QTextCharFormat multiLineCommentFormat;
|
||||||
QTextCharFormat quotationFormat;
|
QTextCharFormat quotationFormat;
|
||||||
QTextCharFormat functionFormat;
|
QTextCharFormat functionFormat;
|
||||||
|
QTextCharFormat symbolFormat;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CodeEditor : public QPlainTextEdit {
|
class CodeEditor : public QPlainTextEdit {
|
||||||
|
@ -50,7 +54,7 @@ public:
|
||||||
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
||||||
int lineNumberAreaWidth();
|
int lineNumberAreaWidth();
|
||||||
|
|
||||||
void setErrorLine(int errorLine);
|
void setError(const QString &code, int errorLine, const QStringList &symbols);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void resizeEvent(QResizeEvent *event) override;
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
|
|
|
@ -406,9 +406,16 @@ void ResultsView::updateDetails(const QModelIndex &index)
|
||||||
|
|
||||||
QFile file(filepath);
|
QFile file(filepath);
|
||||||
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
QStringList symbols;
|
||||||
|
QRegularExpression re(".*: ([A-Za-z_][A-Za-z0-9_]*)$");
|
||||||
|
const QString errorMessage = data["message"].toString();
|
||||||
|
QRegularExpressionMatch match = re.match(errorMessage);
|
||||||
|
if (match.hasMatch()) {
|
||||||
|
symbols << match.captured(1);
|
||||||
|
}
|
||||||
|
|
||||||
QTextStream in(&file);
|
QTextStream in(&file);
|
||||||
mUI.mCode->setPlainText(in.readAll());
|
mUI.mCode->setError(in.readAll(), lineNumber, symbols);
|
||||||
mUI.mCode->setErrorLine(lineNumber);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue