mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-05-17 17:58:23 -05:00
Error indicators in JSON mode editor
Improve the feedback to the user for invalid JSON data using error indicators like the SQL editor. Do not select all the text in the JSON mode, in order to see the syntax highlighting while browsing. Fix the logic in the case of not compacting/prettifying. Change "Indent" to "Pretty print" in preferences for coherence to JSON Export dialog (actually it is not only indentation). Do not underline error style font, so it does not overlap with error indicators. See issue #1173
This commit is contained in:
+19
-6
@@ -166,7 +166,8 @@ void EditDialog::loadData(const QByteArray& data)
|
||||
// The JSON widget buffer is now the main data source
|
||||
dataSource = JsonBuffer;
|
||||
{
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(QByteArray(data.constData(), data.size()));
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(QByteArray(data.constData(), data.size()), &parseError);
|
||||
|
||||
if (mustIndentAndCompact && !jsonDoc.isNull()) {
|
||||
// Load indented JSON into the JSON editor
|
||||
@@ -177,9 +178,12 @@ void EditDialog::loadData(const QByteArray& data)
|
||||
textData = QString::fromUtf8(data.constData(), data.size());
|
||||
jsonEdit->setText(textData);
|
||||
}
|
||||
|
||||
jsonEdit->clearErrorIndicators();
|
||||
if (parseError.error != QJsonParseError::NoError)
|
||||
jsonEdit->setErrorIndicator(parseError.offset-1);
|
||||
|
||||
}
|
||||
// Select all of the text by default
|
||||
jsonEdit->selectAll();
|
||||
|
||||
break;
|
||||
|
||||
@@ -432,10 +436,19 @@ void EditDialog::accept()
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonEdit->text().toUtf8(), &parseError);
|
||||
bool proceed;
|
||||
if (mustIndentAndCompact && !jsonDoc.isNull()) {
|
||||
// Compact the JSON data before storing
|
||||
newData = QString(jsonDoc.toJson(QJsonDocument::Compact));
|
||||
|
||||
jsonEdit->clearErrorIndicators();
|
||||
if (parseError.error != QJsonParseError::NoError)
|
||||
jsonEdit->setErrorIndicator(parseError.offset-1);
|
||||
|
||||
if (!jsonDoc.isNull()) {
|
||||
if (mustIndentAndCompact)
|
||||
// Compact the JSON data before storing
|
||||
newData = QString(jsonDoc.toJson(QJsonDocument::Compact));
|
||||
else
|
||||
newData = jsonEdit->text();
|
||||
proceed = (oldData != newData);
|
||||
|
||||
} else {
|
||||
newData = jsonEdit->text();
|
||||
proceed = (oldData != newData && promptInvalidData("JSON", parseError.errorString()));
|
||||
|
||||
@@ -388,7 +388,7 @@
|
||||
<item row="1" column="1">
|
||||
<widget class="QCheckBox" name="checkIndentCompact">
|
||||
<property name="text">
|
||||
<string>Indent text on loading. Compact on saving.</string>
|
||||
<string>Pretty print on loading. Compact on saving.</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
||||
+35
-2
@@ -32,7 +32,11 @@ JsonTextEdit::JsonTextEdit(QWidget* parent) :
|
||||
// Enable folding
|
||||
setFolding(QsciScintilla::BoxedTreeFoldStyle);
|
||||
jsonLexer->setFoldCompact(false);
|
||||
|
||||
|
||||
// Create error indicator
|
||||
errorIndicatorNumber = indicatorDefine(QsciScintilla::SquiggleIndicator);
|
||||
setIndicatorForegroundColor(Qt::red, errorIndicatorNumber);
|
||||
|
||||
// Set a sensible scroll width, so the scroll bar is avoided in
|
||||
// most cases.
|
||||
setScrollWidth(80);
|
||||
@@ -136,7 +140,6 @@ void JsonTextEdit::reloadSettings()
|
||||
QFont errorFont(Settings::getValue("editor", "font").toString());
|
||||
errorFont.setPointSize(Settings::getValue("editor", "fontsize").toInt());
|
||||
errorFont.setItalic(true);
|
||||
errorFont.setUnderline(true);
|
||||
jsonLexer->setFont(errorFont, QsciLexerJSON::Error);
|
||||
jsonLexer->setFont(errorFont, QsciLexerJSON::UnclosedString);
|
||||
jsonLexer->setPaper(jsonLexer->defaultPaper(QsciLexerJSON::String), QsciLexerJSON::Error);
|
||||
@@ -164,4 +167,34 @@ void JsonTextEdit::reloadSettings()
|
||||
setTabWidth(Settings::getValue("editor", "tabsize").toInt());
|
||||
jsonLexer->refreshProperties();
|
||||
|
||||
// Check if error indicators are enabled and clear them if they just got disabled
|
||||
showErrorIndicators = Settings::getValue("editor", "error_indicators").toBool();
|
||||
if(!showErrorIndicators)
|
||||
clearErrorIndicators();
|
||||
|
||||
}
|
||||
|
||||
void JsonTextEdit::clearErrorIndicators()
|
||||
{
|
||||
// Clear any error indicators from position (0,0) to the last column of the last line
|
||||
clearIndicatorRange(0, 0, lines(), lineLength(lines()), errorIndicatorNumber);
|
||||
}
|
||||
|
||||
void JsonTextEdit::setErrorIndicator(int fromRow, int fromIndex, int toRow, int toIndex)
|
||||
{
|
||||
// Set error indicator for the specified range but only if they're enabled
|
||||
if(showErrorIndicators)
|
||||
fillIndicatorRange(fromRow, fromIndex, toRow, toIndex, errorIndicatorNumber);
|
||||
}
|
||||
|
||||
void JsonTextEdit::setErrorIndicator(int position)
|
||||
{
|
||||
// Set error indicator for the position until end of line, but only if they're enabled
|
||||
if(showErrorIndicators) {
|
||||
|
||||
int column = SendScintilla(QsciScintillaBase::SCI_GETCOLUMN, position);
|
||||
int line = SendScintilla(QsciScintillaBase::SCI_LINEFROMPOSITION, position);
|
||||
|
||||
fillIndicatorRange(line, column, line+1, 0, errorIndicatorNumber);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@ public:
|
||||
public slots:
|
||||
void reloadKeywords();
|
||||
void reloadSettings();
|
||||
void clearErrorIndicators();
|
||||
void setErrorIndicator(int fromRow, int fromIndex, int toRow, int toIndex);
|
||||
// Set error indicator from position to end of line
|
||||
void setErrorIndicator(int position);
|
||||
|
||||
protected:
|
||||
void dropEvent(QDropEvent* e);
|
||||
@@ -28,6 +32,9 @@ protected:
|
||||
private:
|
||||
void setupSyntaxHighlightingFormat(const QString& settings_name, int style);
|
||||
|
||||
int errorIndicatorNumber;
|
||||
bool showErrorIndicators;
|
||||
|
||||
private slots:
|
||||
void updateLineNumberAreaWidth();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user