File indexing completed on 2024-05-05 11:56:01

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2011 Filipe Saraiva <filipe@kde.org>
0004 */
0005 
0006 #include "scilabhighlighter.h"
0007 #include "scilabkeywords.h"
0008 #include "result.h"
0009 #include "textresult.h"
0010 #include "session.h"
0011 
0012 #include <QTextEdit>
0013 #include <QDebug>
0014 
0015 ScilabHighlighter::ScilabHighlighter(QObject* parent, Cantor::Session* session) : Cantor::DefaultHighlighter(parent), m_session(session)
0016 {
0017     addKeywords(ScilabKeywords::instance()->keywords());
0018     addFunctions(ScilabKeywords::instance()->functions());
0019     addVariables(ScilabKeywords::instance()->variables());
0020 
0021     addRule(QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()")), functionFormat());
0022 
0023     addRule(QLatin1String("FIXME"), commentFormat());
0024     addRule(QLatin1String("TODO"), commentFormat());
0025 
0026     addRule(QRegularExpression(QStringLiteral("\"[^\"]*\"")), stringFormat());
0027     addRule(QRegularExpression(QStringLiteral("'[^']*'")), stringFormat());
0028     addRule(QRegularExpression(QStringLiteral("//[^\n]*")), commentFormat());
0029 
0030     commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
0031     commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
0032 }
0033 
0034 void ScilabHighlighter::highlightBlock(const QString& text)
0035 {
0036     if (skipHighlighting(text)){
0037         return;
0038     }
0039 
0040     //Do some backend independent highlighting (brackets etc.)
0041     DefaultHighlighter::highlightBlock(text);
0042 
0043     setCurrentBlockState(0);
0044 
0045     int startIndex = 0;
0046     if (previousBlockState() != 1)
0047         startIndex = text.indexOf(commentStartExpression);
0048 
0049     while (startIndex >= 0){
0050         QRegularExpressionMatch endMatch;
0051         const int endIndex = text.indexOf(commentEndExpression, startIndex, &endMatch);
0052         int commentLength;
0053         if (endIndex == -1) { // no match found
0054             setCurrentBlockState(1);
0055             commentLength = text.length() - startIndex;
0056         } else { // match found
0057             // endMatch.catpuredEnd(0) is endIndex + endMatch.capturedLength(0)
0058             commentLength = endMatch.capturedEnd(0) - startIndex;
0059         }
0060         setFormat(startIndex,  commentLength,  commentFormat());
0061         startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
0062     }
0063 }
0064 
0065 QString ScilabHighlighter::nonSeparatingCharacters() const
0066 {
0067     return QLatin1String("[%]");
0068 }