Warning, file /education/cantor/src/lib/defaulthighlighter.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #ifndef DEFAULTHIGHLIGHTER_H
0007 #define DEFAULTHIGHLIGHTER_H
0008 
0009 #include "cantor_export.h"
0010 
0011 #include <QRegularExpression>
0012 #include <QSyntaxHighlighter>
0013 
0014 class QGraphicsTextItem;
0015 
0016 namespace Cantor
0017 {
0018 class DefaultHighlighterPrivate;
0019 class Session;
0020 
0021 /**
0022  * The DefaultHighlighter is an implementation QSyntaxHighlighter.
0023  * It covers most common cases of syntax highlighting for Cantor's command entries.
0024  *
0025  * When creating a custom highlighter, for example for a new backend, you should use
0026  * the provided functions addPairs(), addRule() and/or addRules().
0027  *
0028  * If you need more specific functionality, subclass highlightBlock(). Usually it's a good idea to also call
0029  * DefaultHighlighter's implementation from it.
0030  *
0031  * @author Alexander Rieder
0032  */
0033 
0034 class CANTOR_EXPORT DefaultHighlighter : public QSyntaxHighlighter
0035 {
0036   Q_OBJECT
0037   public:
0038     explicit DefaultHighlighter(QObject* parent);
0039     explicit DefaultHighlighter(QObject* parent, Session* session);
0040     virtual ~DefaultHighlighter() override;
0041 
0042     /**
0043      * Change the item being highlighted.
0044      */
0045     void setTextItem(QGraphicsTextItem* item);
0046 
0047   public Q_SLOTS:
0048     /**
0049      * Called when the cursor moved. Rehighlights accordingly.
0050      */
0051     void positionChanged(const QTextCursor&);
0052 
0053   protected Q_SLOTS:
0054     /**
0055      * Convenience method, equivalent to @code addRules(functions, functionFormat()) @endcode
0056      */
0057     void addFunctions(const QStringList& functions);
0058     /**
0059      * Convenience method, equivalent to @code addRules(variables, variableFormat()) @endcode
0060      */
0061     void addVariables(const QStringList& variables);
0062     /**
0063      * Removes any rules previously added for the word @p word
0064      */
0065     void removeRule(const QString& word);
0066     /**
0067      * Convenience method, removes all rules with conditions from @p conditions
0068      * @sa removeRule, addRules
0069      */
0070     void removeRules(const QStringList& conditions);
0071 
0072   protected:
0073     /**
0074      * This method is called by Cantor's KTextEdit and is where all the highlighting must take place.
0075      * The default implementation calls highlightPairs(), highlightWords() and highlightRegExps().
0076      *
0077      */
0078     void highlightBlock(const QString& text) override;
0079 
0080     virtual QStringList parseBlockTextToWords(const QString& text);
0081 
0082     bool skipHighlighting(const QString& text);
0083 
0084     QTextCharFormat functionFormat() const;
0085     QTextCharFormat variableFormat() const;
0086     QTextCharFormat objectFormat() const;
0087     QTextCharFormat keywordFormat() const;
0088     QTextCharFormat numberFormat() const;
0089     QTextCharFormat operatorFormat() const;
0090     QTextCharFormat errorFormat() const;
0091     QTextCharFormat commentFormat() const;
0092     QTextCharFormat stringFormat() const;
0093     QTextCharFormat matchingPairFormat() const;
0094     QTextCharFormat mismatchingPairFormat() const;
0095 
0096     /**
0097      * Call this to add a pair of symbols for highlighting.
0098      * The default implementation of the class already adds (), {} and [], so no need to add those.
0099      * For example, if you wanted to highlight angle-brackets, you would use:
0100      * @code
0101      * addPair('<', '>');
0102      * @endcode
0103      * @param openSymbol the opening symbol of the pair
0104      * @param closeSymbol the closing symbol of the pair
0105      * @sa highlightPairs
0106      */
0107     void addPair(QChar openSymbol, QChar closeSymbol);
0108     /**
0109      * Highlights all instances of the @p word in the text with the specified @p format
0110      * @param word the word to highlight
0111      * @param format the format to be used for displaying the word
0112      */
0113     void addRule(const QString& word, const QTextCharFormat& format);
0114     /**
0115      * Highlights all parts of the text matched by the regular expression @p regexp in the text
0116      * with the specified @p format
0117      * @param regexp the regular expression used to look for matches
0118      * @param format the format used to display the matching parts of the text
0119      */
0120     void addRule(const QRegularExpression& regexp, const QTextCharFormat& format);
0121 
0122     /**
0123      * Convenience method, highlights all items in @p conditions with the specified @p format
0124      * @code
0125      * QStringList greenWords;
0126      * greenWords << "tree" << "forest" << "grass";
0127      * addRules(greenWords, greenWordFormat);
0128      * @endcode
0129      * @param conditions any Qt container of QRegularExpression or QString.
0130      * @param format the format used to display the matching parts of the text
0131      */
0132     void addRules(const QStringList& conditions, const QTextCharFormat& format);
0133     /**
0134      * Convenience method, equivalent to @code addRules(keywords, keywordFormat()) @endcode
0135      */
0136     void addKeywords(const QStringList& keywords);
0137     /**
0138      * Removes any rules previously added for the regular expression @p regexp
0139      */
0140     void removeRule(const QRegularExpression& regex);
0141 
0142     /**
0143      * Highlight pairs added with addPair()
0144      * @sa addPair
0145      */
0146     void highlightPairs(const QString& text);
0147     /**
0148      * Highlights words added with addRule()
0149      * @sa addRule, addRules
0150      */
0151     void highlightWords(const QString& text);
0152     /**
0153      * Highlights all matches from regular expressions added with addRule()
0154      * @sa addRule, addRules
0155      */
0156     void highlightRegExps(const QString& text);
0157 
0158     /**
0159      * Returns a string  that contains a regular expression that matches for characters thar are allowed inside
0160      * words for this backend. For example, maxima or scilab allow % at the beginning of variable names
0161      */
0162     virtual QString nonSeparatingCharacters() const;
0163 
0164   private Q_SLOTS:
0165     void updateFormats();
0166 
0167   Q_SIGNALS:
0168     void rulesChanged();
0169 
0170   private:
0171     DefaultHighlighterPrivate* d;
0172 };
0173 }
0174 
0175 #endif