Used slightly different approach. This tab is fully build in source code using subclasses to adjust behaviour of widgets for reuse in the other tabs. Uses custom proxy model for filtering triggers for correct table and supporting out of the box sorting by QTableView. SqlCodePreview: QPlainTextEditor which sql highlighter and in readonly mode but allows copy.
82 lines
2.2 KiB
C++
82 lines
2.2 KiB
C++
#include "SqlSyntaxHighlighter.h"
|
|
|
|
#include "PgTypeContainer.h"
|
|
#include "SqlLexer.h"
|
|
|
|
|
|
|
|
SqlSyntaxHighlighter::SqlSyntaxHighlighter(QTextDocument *parent)
|
|
: QSyntaxHighlighter(parent)
|
|
{
|
|
m_keywordFormat.setForeground(QColor(32, 32, 192));
|
|
m_keywordFormat.setFontWeight(QFont::Bold);
|
|
|
|
m_commentFormat.setForeground(QColor(128, 128, 128));
|
|
|
|
m_quotedStringFormat.setForeground(QColor(192, 32, 192));
|
|
|
|
m_quotedIdentifierFormat.setForeground(QColor(192, 128, 32));
|
|
|
|
m_typeFormat.setForeground(QColor(32, 192, 32));
|
|
m_typeFormat.setFontWeight(QFont::Bold);
|
|
|
|
m_parameterFormat.setForeground(QColor(192, 32, 32));
|
|
m_parameterFormat.setFontWeight(QFont::Bold);
|
|
}
|
|
|
|
SqlSyntaxHighlighter::~SqlSyntaxHighlighter()
|
|
{
|
|
}
|
|
|
|
void SqlSyntaxHighlighter::setTypes(const PgTypeContainer& types)
|
|
{
|
|
m_typeNames.clear();
|
|
for (auto&& e : types) {
|
|
m_typeNames.insert(e.name);
|
|
}
|
|
rehighlight();
|
|
}
|
|
|
|
void SqlSyntaxHighlighter::highlightBlock(const QString &text)
|
|
{
|
|
int previous_state = previousBlockState();
|
|
if (previous_state <= 0)
|
|
previous_state = 0;
|
|
SqlLexer lexer(text, (LexerState)previous_state);
|
|
int startpos, length;
|
|
BasicTokenType tokentype;
|
|
QString s;
|
|
while (lexer.nextBasicToken(startpos, length, tokentype, s)) {
|
|
switch (tokentype) {
|
|
case BasicTokenType::None:
|
|
case BasicTokenType::End: // End of input
|
|
case BasicTokenType::DollarQuote:
|
|
break;
|
|
case BasicTokenType::Symbol: { // can be many things, keyword, object name, operator, ..
|
|
auto kw = getPgsqlKeyword(s);
|
|
if (kw != nullptr) {
|
|
setFormat(startpos, length, m_keywordFormat);
|
|
}
|
|
else if (m_typeNames.count(s.toLower()) > 0) {
|
|
setFormat(startpos, length, m_typeFormat);
|
|
}
|
|
}
|
|
break;
|
|
case BasicTokenType::OpenBlockComment:
|
|
setCurrentBlockState((int)lexer.currentState());
|
|
case BasicTokenType::BlockComment:
|
|
case BasicTokenType::Comment:
|
|
setFormat(startpos, length, m_commentFormat);
|
|
break;
|
|
case BasicTokenType::QuotedString:
|
|
setFormat(startpos, length, m_quotedStringFormat);
|
|
break;
|
|
case BasicTokenType::QuotedIdentifier:
|
|
setFormat(startpos, length, m_quotedIdentifierFormat);
|
|
break;
|
|
case BasicTokenType::Parameter:
|
|
setFormat(startpos, length, m_parameterFormat);
|
|
break;
|
|
}
|
|
}
|
|
}
|