Introduction/setup
I'm working on a text editor project, and I currently have a working syntax highlighter set up. But I feel that my design approach is not lending itself well to maintainable code.
Here is the syntax highlighter class's declaration (don't worry about the language-specific types preceded by "Q"; those are just defined by the Qt framework for C++):
class Highlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
Highlighter(QTextDocument *parent = nullptr) : QSyntaxHighlighter (parent) {}
virtual void addKeywords(QStringList keywords);
virtual void setKeywordFormat();
virtual void setClassPattern(QRegularExpression classPattern);
virtual void setClassFormat();
virtual void setFunctionPattern(QRegularExpression functionPattern);
virtual void setFunctionFormat();
virtual void setQuotePattern(QRegularExpression quotePattern);
virtual void setQuoteFormat();
virtual void setInlineCommentPattern(QRegularExpression inlineCommentPattern);
virtual void setInlineCommentFormat();
virtual void setBlockCommentStartPattern(QRegularExpression blockCommentStart);
virtual void setBlockCommentEndPattern(QRegularExpression blockCommentEnd);
virtual void setBlockCommentFormat();
virtual void addRule(QRegularExpression pattern, QTextCharFormat format);
protected:
virtual void highlightBlock(const QString &text) override;
virtual void highlightMultilineComments(const QString &text);
private:
struct HighlightingRule
{
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> rules;
QRegularExpression blockCommentStart;
QRegularExpression blockCommentEnd;
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;
QTextCharFormat inlineCommentFormat;
QTextCharFormat blockCommentFormat;
QTextCharFormat quoteFormat;
QTextCharFormat functionFormat;
};
I declared many of the functions as virtual with inheritance in mind. However, whether or not I should use inheritance is really at the core of this problem (more on that in a bit).
Additionally, the header includes the following functions not part of the class:
Highlighter *cHighlighter(QTextDocument *doc);
Highlighter *cppHighlighter(QTextDocument *doc);
Highlighter *javaHighlighter(QTextDocument *doc);
Highlighter *pythonHighlighter(QTextDocument *doc);
Each of these functions assembles the respective type of Highlighter. Below are the function definitions:
/* Returns a Highlighter object specific to the C language and its grammar and syntax.
*/
Highlighter *cHighlighter(QTextDocument *doc)
{
QStringList keywords;
keywords << "\\bauto\\b" << "\\bbreak\\b" << "\\bcase\\b" << "\\bchar\\b" << "\\bconst\\b"
<< "\\bcontinue\\b" << "\\bdefault\\b" << "\\bdo\\b" << "\\bdouble\\b" << "\\belse\\b"
<< "\\benum\\b" << "\\bextern\\b" << "\\bfloat\\b" << "\\bfor\\b" << "\\bgoto\\b"
<< "\\bif\\b" << "\\bint\\b" << "\\blong\\b" << "\\bregister\\b" << "\\breturn\\b"
<< "\\bshort\\b" << "\\bsigned\\b" << "\\bsizeof\\b" << "\\bstatic\\b" << "\\bstruct\\b"
<< "\\bswitch\\b" << "\\btypedef\\b" << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvoid\\b"
<< "\\bvolatile\\b" << "\\bwhile\\b";
QRegularExpression classPattern("\\b[A-Z_][a-zA-Z0-9_]*\\b");
QRegularExpression quotePattern("(\".*\")|('\\\\.')|('.{0,1}')");
QRegularExpression functionPattern("\\b[A-Za-z_][A-Za-z0-9_]*(?=\\()");
QRegularExpression inlineCommentPattern("//.*");
QRegularExpression blockCommentStart("/\\*");
QRegularExpression blockCommentEnd("\\*/");
Highlighter *highlighter = new Highlighter(doc);
highlighter->addKeywords(keywords);
highlighter->setClassPattern(classPattern);
highlighter->setQuotePattern(quotePattern);
highlighter->setFunctionPattern(functionPattern);
highlighter->setInlineCommentPattern(inlineCommentPattern);
highlighter->setBlockCommentStartPattern(blockCommentStart);
highlighter->setBlockCommentEndPattern(blockCommentEnd);
return highlighter;
}
/* Returns a Highlighter object specific to the C++ language and its grammar and syntax.
*/
Highlighter *cppHighlighter(QTextDocument *doc)
{
Highlighter *cLanguage = cHighlighter(doc);
QStringList cppOnlyKeywords;
cppOnlyKeywords << "\\basm\\b" << "\\bbool\\b" << "\\bcatch\\b" <<
"\\bclass\\b" << "\\bconst_cast\\b" << "\\bdelete\\b" <<
"\\bdynamic_cast\\b" << "\\bexplicit\\b" << "\\bfalse\\b" <<
"\\bfriend\\b" << "\\binline\\b" << "\\bmutable\\b" <<
"\\bnamespace\\b" << "\\bnew\\b" << "\\boperator\\b" <<
"\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b" <<
"\\breinterpret_cast\\b" << "\\bstatic_cast\\b" <<
"\\btemplate\\b" << "\\bthis\\b" << "\\bthrow\\b" <<
"\\btrue\\b" << "\\btry\\b" << "\\btypeid\\b" << "\\btypename\\b" <<
"\\bvirtual\\b" << "\\busing\\b" << "\\bwchar_t\\b";
cLanguage->addKeywords(cppOnlyKeywords);
return cLanguage;
}
/* Returns a Highlighter object specific to the Java language and its grammar and syntax.
*/
Highlighter *javaHighlighter(QTextDocument *doc)
{
QStringList keywords;
keywords << "\\babstract\\b" << "\\bassert\\b" << "\\bboolean\\b" << "\\bbreak\\b" << "\\bbyte\\b"
<< "\\bcase\\b" << "\\bcatch\\b" << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b" << "\\bcontinue\\b"
<< "\\bdefault\\b" << "\\bdo\\b" << "\\bdouble\\b" << "\\belse\\b" << "\\benum\\b" << "\\bextends\\b"
<< "\\bfinal\\b" << "\\bfinally\\b" << "\\bfloat\\b" << "\\bfor\\b" << "\\bgoto\\b" << "\\bif\\b"
<< "\\bimplements\\b" << "\\bimport\\b" << "\\binstanceof\\b" << "\\bint\\b" << "\\binterface\\b"
<< "\\blong\\b" << "\\bnative\\b" << "\\bnew\\b" << "\\bpackage\\b" << "\\bprivate\\b" << "\\bprotected\\b"
<< "\\bpublic\\b" << "\\breturn\\b" << "\\bshort\\b" << "\\bstatic\\b" << "\\bstrictfp\\b" << "\\bsuper\\b"
<< "\\bswitch\\b" << "\\bsynchronized\\b" << "\\bthis\\b" << "\\bthrow\\b" << "\\bthrows\\b" << "\\btransient\\b"
<< "\\btry\\b" << "\\bvoid\\b" << "\\bvolatile\\b" << "\\bwhile\\b" << "\\btrue\\b" << "\\bfalse\\b" << "\\bnull\\b";
QRegularExpression classPattern("\\b[A-Z_][a-zA-Z0-9_]*\\b");
QRegularExpression quotePattern("(\".*\")|('\\\\.')|('.{0,1}')");
QRegularExpression functionPattern("\\b[A-Za-z_][A-Za-z0-9_]*(?=\\()");
QRegularExpression inlineCommentPattern("//.*");
QRegularExpression blockCommentStart("/\\*");
QRegularExpression blockCommentEnd("\\*/");
Highlighter *highlighter = new Highlighter(doc);
highlighter->addKeywords(keywords);
highlighter->setClassPattern(classPattern);
highlighter->setQuotePattern(quotePattern);
highlighter->setFunctionPattern(functionPattern);
highlighter->setInlineCommentPattern(inlineCommentPattern);
highlighter->setBlockCommentStartPattern(blockCommentStart);
highlighter->setBlockCommentEndPattern(blockCommentEnd);
return highlighter;
}
/* Returns a Highlighter object specific to the Python language and its grammar and syntax.
*/
Highlighter *pythonHighlighter(QTextDocument *doc)
{
QStringList keywords;
keywords << "\\band\\b" << "\\bas\\b" << "\\bassert\\b" << "\\bbreak\\b" << "\\bclass\\b" << "\\bcontinue\\b"
<< "\\bdef\\b" << "\\bdel\\b" << "\\belif\\b" << "\\belse\\b" << "\\bexcept\\b" << "\\bFalse\\b"
<< "\\bfinally\\b" << "\\bfor\\b" << "\\bfrom\\b" << "\\bglobal\\b" << "\\bif\\b" << "\\bimport\\b"
<< "\\bin\\b" << "\\bis\\b" << "\\blambda\\b" << "\\bNone\\b" << "\\bnonlocal\\b" << "\\bnot\\b"
<< "\\bor\\b" << "\\bpass\\b" << "\\braise\\b" << "\\breturn\\b" << "\\bTrue\\b" << "\\btry\\b"
<< "\\bwhile\\b" << "\\bwith\\b" << "\\byield\\b";
QRegularExpression classPattern("\\b[A-Z_][a-zA-Z0-9_]*\\b");
QRegularExpression quotePattern("(\".*\")|('.*')");
QRegularExpression functionPattern("\\b[A-Za-z_][A-Za-z0-9_]*(?=\\()");
QRegularExpression inlineCommentPattern("#.*");
QRegularExpression blockCommentStart("'''");
QRegularExpression blockCommentEnd("'''");
Highlighter *highlighter = new Highlighter(doc);
highlighter->addKeywords(keywords);
highlighter->setClassPattern(classPattern);
highlighter->setQuotePattern(quotePattern);
highlighter->setFunctionPattern(functionPattern);
highlighter->setInlineCommentPattern(inlineCommentPattern);
highlighter->setBlockCommentStartPattern(blockCommentStart);
highlighter->setBlockCommentEndPattern(blockCommentEnd);
return highlighter;
}
The problem
Notice the protected method named highlightMultilineComments
. By default, because of how syntax highlighting is performed in Qt, this method assumes that the Highlighter's blockCommentStart
and blockCommentEnd
regular expressions are not identical. In the case of languages like Python, that's obviously not the case, since the starting and ending comment delimeters are the same (triple single or double quotes). And in that case, the function does not perform as expected. That's all you need to know.
The Problem
I made this method virtual with the intent of creating a subclass, say PythonHighlighter
, that overrides nothing but that specific function to define custom logic to handle that special case. Other languages can, in theory, override everything and customize how they want the highlighter to be set up (if I go with inheritance).
Issues with Inheritance
But if I were to create a subclass for Python, that would mean I'd have to create one for C, C++, Java, and any other language that I'd like to add in the future (for consistency). This is obviously more difficult to manage than my current approach, where I have functions that merely assemble the highlighters. If I add a class for every language, the source files will significantly grow in number.
Issues with builder functions
So using the builder functions has its benefits. But that approach does not allow me to override the highlightMultilineComments
method. So it's clearly not ideal in that respect.
Question
How can I leverage the benefits of inheritance—being able to override methods like highlightMultilineComments
depending on the language—without sacrificing the relative maintainability of "builder functions"
Additional things I've considered
I've also considered adding a function like highlightSymmetricMultilineComments
. Then, highlightMultilineComments
could check if blockCommentStart
and blockCommentEnd
have the same regex pattern. If it does, it would simply call that method.
This presents an obvious problem—it does not make sense for this to be in Highlighter, considering that not all languages have symmetric multiline comments (Python is the only one currently supported by the text editor that does).
Aucun commentaire:
Enregistrer un commentaire