Skip to content

Commit

Permalink
- Fix multi-line strings in the OMEdit ModelicaEditor
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8403 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Mar 29, 2011
1 parent 3daead3 commit fb49433
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
69 changes: 43 additions & 26 deletions OMEdit/OMEditGUI/ModelicaEditor.cpp
Expand Up @@ -276,42 +276,62 @@ void ModelicaTextHighlighter::initializeSettings()
mHighlightingRules.append(rule);

mQuotationFormat.setForeground(QColor(mpModelicaTextSettings->getQuotesRuleColor()));
rule.mPattern = QRegExp("\"([^\"]|\\\\\")*\"");
rule.mFormat = mQuotationFormat;
mHighlightingRules.append(rule);

mQuotesExpression = QRegExp("\"");
mCommentStartExpression = QRegExp("/\\*");
mCommentEndExpression = QRegExp("\\*/");
}

void ModelicaTextHighlighter::highlightMultiLine(const QString &text, QRegExp &startExpression, QRegExp &endExpression,
QTextCharFormat &format)
void ModelicaTextHighlighter::highlightMultiLine(const QString &text)
{
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = startExpression.indexIn(text);
/* Hand-written recognizer beats the crap known as QRegEx ;) */
int index = 0, startIndex = 0;
int blockState = previousBlockState();
// fprintf(stderr, "%s with blockState %d\n", text.toStdString().c_str(), blockState);

while (startIndex >= 0)
while (index < text.length())
{
int endIndex = endExpression.indexIn(text, startIndex);
int textLength;
if (endIndex == -1)
{
setCurrentBlockState(1);
textLength = text.length() - startIndex;
}
else
{
textLength = endIndex - startIndex + endExpression.matchedLength();
switch (blockState) {
case 1:
if (text[index] == '*' && index+1<text.length() && text[index+1] == '/') {
index++;
setFormat(startIndex, index-startIndex+1, mMultiLineCommentFormat);
blockState = 0;
}
break;
case 2:
if (text[index] == '\\') {
index++;
} else if (text[index] == '"') {
setFormat(startIndex, index-startIndex+1, mQuotationFormat);
blockState = 0;
}
break;
default:
if (text[index] == '/' && index+1<text.length() && text[index+1] == '*') {
startIndex = index++;
blockState = 1;
} else if (text[index] == '"') {
startIndex = index;
blockState = 2;
}
}
setFormat(startIndex, textLength, format);
startIndex = startExpression.indexIn(text, startIndex + textLength);
index++;
}
switch (blockState) {
case 1:
setFormat(startIndex, text.length()-startIndex, mMultiLineCommentFormat);
setCurrentBlockState(1);
break;
case 2:
setFormat(startIndex, text.length()-startIndex, mQuotationFormat);
setCurrentBlockState(2);
break;
}
}

void ModelicaTextHighlighter::highlightBlock(const QString &text)
{
setCurrentBlockState(0);
setFormat(0, text.length(), mpModelicaTextSettings->getTextRuleColor());
foreach (const HighlightingRule &rule, mHighlightingRules)
{
Expand All @@ -324,10 +344,7 @@ void ModelicaTextHighlighter::highlightBlock(const QString &text)
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
highlightMultiLine(text, mCommentStartExpression, mCommentEndExpression, mMultiLineCommentFormat);
// setCurrentBlockState(0);
// highlightMultiLine(text, mQuotesExpression, mQuotesExpression, mQuotationFormat);
highlightMultiLine(text);
}

void ModelicaTextHighlighter::settingsChanged()
Expand Down
6 changes: 3 additions & 3 deletions OMEdit/OMEditGUI/ModelicaEditor.h
Expand Up @@ -76,8 +76,7 @@ class ModelicaTextHighlighter : public QSyntaxHighlighter
public:
ModelicaTextHighlighter(ModelicaTextSettings *pSettings, QTextDocument *pParent = 0);
void initializeSettings();
void highlightMultiLine(const QString &text, QRegExp &startExpression, QRegExp &endExpression,
QTextCharFormat &format);
void highlightMultiLine(const QString &text);

ModelicaTextSettings *mpModelicaTextSettings;
protected:
Expand All @@ -90,9 +89,10 @@ class ModelicaTextHighlighter : public QSyntaxHighlighter
};
QVector<HighlightingRule> mHighlightingRules;

QRegExp mQuotesExpression;
QRegExp mCommentStartExpression;
QRegExp mCommentEndExpression;
QRegExp mStringStartExpression;
QRegExp mStringEndExpression;

QTextCharFormat mKeywordFormat;
QTextCharFormat mTypeFormat;
Expand Down

0 comments on commit fb49433

Please sign in to comment.