Skip to content

Commit

Permalink
Don't match parentheses within comments and quotes sections.
Browse files Browse the repository at this point in the history
This default behavior can be changed in settings.
  • Loading branch information
adeas31 committed Dec 11, 2015
1 parent 1abab71 commit 2ee015c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
49 changes: 27 additions & 22 deletions OMEdit/OMEditGUI/Editors/ModelicaTextEditor.cpp
Expand Up @@ -596,17 +596,25 @@ void ModelicaTextHighlighter::initializeSettings()
mHighlightingRules.append(rule);
}

//! Highlights the multilines text.
//! Quoted text or multiline comments.
/*!
* \brief ModelicaTextHighlighter::highlightMultiLine
* Highlights the multilines text.
* Quoted text or multiline comments.
* \param text
* \param text
*/
void ModelicaTextHighlighter::highlightMultiLine(const QString &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 (index < text.length())
{
// store parentheses info
Parentheses parentheses;
TextBlockUserData *pTextBlockUserData = BaseEditorDocumentLayout::userData(currentBlock());
if (pTextBlockUserData) {
pTextBlockUserData->clearParentheses();
}
while (index < text.length()) {
switch (blockState) {
/* if the block already has single line comment then don't check for multi line comment and quotes. */
case 1:
Expand Down Expand Up @@ -644,8 +652,21 @@ void ModelicaTextHighlighter::highlightMultiLine(const QString &text)
blockState = 3;
}
}
// if no single line comment, no multi line comment and no quotes then store the parentheses
if (pTextBlockUserData && (blockState < 1 || blockState > 3 || mpModelicaTextEditorPage->getMatchParenthesesCommentsQuotesCheckBox()->isChecked())) {
if (text[index] == '(' || text[index] == '{' || text[index] == '[') {
parentheses.append(Parenthesis(Parenthesis::Opened, text[index], index));
} else if (text[index] == ')' || text[index] == '}' || text[index] == ']') {
parentheses.append(Parenthesis(Parenthesis::Closed, text[index], index));
}
}
index++;
}
if (pTextBlockUserData) {
pTextBlockUserData->setParentheses(parentheses);
// set text block user data
setCurrentBlockUserData(pTextBlockUserData);
}
switch (blockState) {
case 2:
setFormat(startIndex, text.length()-startIndex, mMultiLineCommentFormat);
Expand All @@ -665,22 +686,6 @@ void ModelicaTextHighlighter::highlightBlock(const QString &text)
if (!mpModelicaTextEditorPage->getSyntaxHighlightingCheckbox()->isChecked()) {
return;
}
// store parentheses info
TextBlockUserData *pTextBlockUserData = BaseEditorDocumentLayout::userData(currentBlock());
if (pTextBlockUserData) {
pTextBlockUserData->clearParentheses();
Parentheses parentheses;
for (int i = 0 ; i < text.length() ; i++) {
if (text.at(i) == '(' || text.at(i) == '{' || text.at(i) == '[') {
parentheses.append(Parenthesis(Parenthesis::Opened, text.at(i), i));
} else if (text.at(i) == ')' || text.at(i) == '}' || text.at(i) == ']') {
parentheses.append(Parenthesis(Parenthesis::Closed, text.at(i), i));
}
}
pTextBlockUserData->setParentheses(parentheses);
// set text block user data
setCurrentBlockUserData(pTextBlockUserData);
}
// set text block state
setCurrentBlockState(0);
setFormat(0, text.length(), mpModelicaTextEditorPage->getTextRuleColor());
Expand Down
10 changes: 9 additions & 1 deletion OMEdit/OMEditGUI/Options/OptionsDialog.cpp
Expand Up @@ -226,6 +226,9 @@ void OptionsDialog::readModelicaTextSettings()
if (mpSettings->contains("textEditor/enableSyntaxHighlighting")) {
mpModelicaTextEditorPage->getSyntaxHighlightingCheckbox()->setChecked(mpSettings->value("textEditor/enableSyntaxHighlighting").toBool());
}
if (mpSettings->contains("textEditor/matchParenthesesCommentsQuotes")) {
mpModelicaTextEditorPage->getMatchParenthesesCommentsQuotesCheckBox()->setChecked(mpSettings->value("textEditor/matchParenthesesCommentsQuotes").toBool());
}
if (mpSettings->contains("textEditor/enableLineWrapping")) {
mpModelicaTextEditorPage->getLineWrappingCheckbox()->setChecked(mpSettings->value("textEditor/enableLineWrapping").toBool());
}
Expand Down Expand Up @@ -701,6 +704,7 @@ void OptionsDialog::saveModelicaTextSettings()
mpSettings->setValue("textEditor/tabSize", mpModelicaTextEditorPage->getTabSizeSpinBox()->value());
mpSettings->setValue("textEditor/indentSize", mpModelicaTextEditorPage->getIndentSpinBox()->value());
mpSettings->setValue("textEditor/enableSyntaxHighlighting", mpModelicaTextEditorPage->getSyntaxHighlightingCheckbox()->isChecked());
mpSettings->setValue("textEditor/matchParenthesesCommentsQuotes", mpModelicaTextEditorPage->getMatchParenthesesCommentsQuotesCheckBox()->isChecked());
mpSettings->setValue("textEditor/enableLineWrapping", mpModelicaTextEditorPage->getLineWrappingCheckbox()->isChecked());
mpSettings->setValue("textEditor/fontFamily", mpModelicaTextEditorPage->getFontFamilyComboBox()->currentFont().family());
mpSettings->setValue("textEditor/fontSize", mpModelicaTextEditorPage->getFontSizeSpinBox()->value());
Expand Down Expand Up @@ -1930,14 +1934,17 @@ ModelicaTextEditorPage::ModelicaTextEditorPage(OptionsDialog *pOptionsDialog)
// syntax highlighting checkbox
mpSyntaxHighlightingCheckbox = new QCheckBox(tr("Enable Syntax Highlighting"));
mpSyntaxHighlightingCheckbox->setChecked(true);
// syntax highlighting checkbox
mpMatchParenthesesCommentsQuotesCheckBox = new QCheckBox(tr("Match Parentheses within Comments and Quotes"));
// line wrap checkbox
mpLineWrappingCheckbox = new QCheckBox(tr("Enable Line Wrapping"));
mpLineWrappingCheckbox->setChecked(true);
connect(mpLineWrappingCheckbox, SIGNAL(toggled(bool)), this, SLOT(setLineWrapping()));
// set Syntax Highlight & Text Wrapping groupbox layout
QGridLayout *pSyntaxHighlightAndTextWrappingGroupBoxLayout = new QGridLayout;
pSyntaxHighlightAndTextWrappingGroupBoxLayout->addWidget(mpSyntaxHighlightingCheckbox, 0, 0);
pSyntaxHighlightAndTextWrappingGroupBoxLayout->addWidget(mpLineWrappingCheckbox, 1, 0);
pSyntaxHighlightAndTextWrappingGroupBoxLayout->addWidget(mpMatchParenthesesCommentsQuotesCheckBox, 1, 0);
pSyntaxHighlightAndTextWrappingGroupBoxLayout->addWidget(mpLineWrappingCheckbox, 2, 0);
mpSyntaxHighlightAndTextWrappingGroupBox->setLayout(pSyntaxHighlightAndTextWrappingGroupBoxLayout);
// fonts & colors groupbox
mpFontColorsGroupBox = new QGroupBox(Helper::fontAndColors);
Expand Down Expand Up @@ -1988,6 +1995,7 @@ ModelicaTextEditorPage::ModelicaTextEditorPage(OptionsDialog *pOptionsDialog)
ModelicaTextHighlighter *pModelicaTextHighlighter = new ModelicaTextHighlighter(this, mpPreviewPlainTextBox);
connect(this, SIGNAL(updatePreview()), pModelicaTextHighlighter, SLOT(settingsChanged()));
connect(mpSyntaxHighlightingCheckbox, SIGNAL(toggled(bool)), pModelicaTextHighlighter, SLOT(settingsChanged()));
connect(mpMatchParenthesesCommentsQuotesCheckBox, SIGNAL(toggled(bool)), pModelicaTextHighlighter, SLOT(settingsChanged()));
// set fonts & colors groupbox layout
QGridLayout *pFontsColorsGroupBoxLayout = new QGridLayout;
pFontsColorsGroupBoxLayout->addWidget(mpFontFamilyLabel, 0, 0);
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Options/OptionsDialog.h
Expand Up @@ -308,6 +308,7 @@ class ModelicaTextEditorPage : public QWidget
QSpinBox *getTabSizeSpinBox() {return mpTabSizeSpinBox;}
QSpinBox *getIndentSpinBox() {return mpIndentSpinBox;}
QCheckBox* getSyntaxHighlightingCheckbox() {return mpSyntaxHighlightingCheckbox;}
QCheckBox* getMatchParenthesesCommentsQuotesCheckBox() {return mpMatchParenthesesCommentsQuotesCheckBox;}
QCheckBox* getLineWrappingCheckbox() {return mpLineWrappingCheckbox;}
QFontComboBox* getFontFamilyComboBox() {return mpFontFamilyComboBox;}
DoubleSpinBox* getFontSizeSpinBox() {return mpFontSizeSpinBox;}
Expand Down Expand Up @@ -337,6 +338,7 @@ class ModelicaTextEditorPage : public QWidget
QSpinBox *mpIndentSpinBox;
QGroupBox *mpSyntaxHighlightAndTextWrappingGroupBox;
QCheckBox *mpSyntaxHighlightingCheckbox;
QCheckBox *mpMatchParenthesesCommentsQuotesCheckBox;
QCheckBox *mpLineWrappingCheckbox;
QGroupBox *mpFontColorsGroupBox;
Label *mpFontFamilyLabel;
Expand Down

0 comments on commit 2ee015c

Please sign in to comment.