1 // This defines the interface to the QsciLexer class.
2 //
3 // Copyright (c) 2019 Riverbank Computing Limited <info@riverbankcomputing.com>
4 //
5 // This file is part of QScintilla.
6 //
7 // This file may be used under the terms of the GNU General Public License
8 // version 3.0 as published by the Free Software Foundation and appearing in
9 // the file LICENSE included in the packaging of this file.  Please review the
10 // following information to ensure the GNU General Public License version 3.0
11 // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 //
13 // If you do not wish to use this file under the terms of the GPL version 3.0
14 // then you may purchase a commercial license.  For more information contact
15 // info@riverbankcomputing.com.
16 //
17 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 
20 
21 #ifndef QSCILEXER_H
22 #define QSCILEXER_H
23 
24 #include <QColor>
25 #include <QFont>
26 #include <QMap>
27 #include <QObject>
28 #include <QString>
29 
30 #include <Qsci/qsciglobal.h>
31 
32 
33 QT_BEGIN_NAMESPACE
34 class QSettings;
35 QT_END_NAMESPACE
36 
37 class QsciAbstractAPIs;
38 class QsciScintilla;
39 
40 
41 //! \brief The QsciLexer class is an abstract class used as a base for language
42 //! lexers.
43 //!
44 //! A lexer scans the text breaking it up into separate language objects, e.g.
45 //! keywords, strings, operators.  The lexer then uses a different style to
46 //! draw each object.  A style is identified by a style number and has a number
47 //! of attributes, including colour and font.  A specific language lexer will
48 //! implement appropriate default styles which can be overriden by an
49 //! application by further sub-classing the specific language lexer.
50 //!
51 //! A lexer may provide one or more sets of words to be recognised as keywords.
52 //! Most lexers only provide one set, but some may support languages embedded
53 //! in other languages and provide several sets.
54 //!
55 //! QsciLexer provides convenience methods for saving and restoring user
56 //! preferences for fonts and colours.
57 //!
58 //! If you want to write a lexer for a new language then you can add it to the
59 //! underlying Scintilla code and implement a corresponding QsciLexer sub-class
60 //! to manage the different styles used.  Alternatively you can implement a
61 //! sub-class of QsciLexerCustom.
62 class QSCINTILLA_EXPORT QsciLexer : public QObject
63 {
64     Q_OBJECT
65 
66 public:
67     //! Construct a QsciLexer with parent \a parent.  \a parent is typically
68     //! the QsciScintilla instance.
69     QsciLexer(QObject *parent = 0);
70 
71     //! Destroy the QSciLexer.
72     virtual ~QsciLexer();
73 
74     //! Returns the name of the language.  It must be re-implemented by a
75     //! sub-class.
76     virtual const char *language() const = 0;
77 
78     //! Returns the name of the lexer.  If 0 is returned then the lexer's
79     //! numeric identifier is used.  The default implementation returns 0.
80     //!
81     //! \sa lexerId()
82     virtual const char *lexer() const;
83 
84     //! Returns the identifier (i.e. a QsciScintillaBase::SCLEX_* value) of the
85     //! lexer.  This is only used if lexer() returns 0.  The default
86     //! implementation returns QsciScintillaBase::SCLEX_CONTAINER.
87     //!
88     //! \sa lexer()
89     virtual int lexerId() const;
90 
91     //! Returns the current API set or 0 if there isn't one.
92     //!
93     //! \sa setAPIs()
94     QsciAbstractAPIs *apis() const;
95 
96     //! Returns the characters that can fill up auto-completion.
97     virtual const char *autoCompletionFillups() const;
98 
99     //! Returns the list of character sequences that can separate
100     //! auto-completion words.  The first in the list is assumed to be the
101     //! sequence used to separate words in the lexer's API files.
102     virtual QStringList autoCompletionWordSeparators() const;
103 
104     //! Returns the auto-indentation style.  The default is 0 if the
105     //! language is block structured, or QsciScintilla::AiMaintain if not.
106     //!
107     //! \sa setAutoIndentStyle(), QsciScintilla::AiMaintain,
108     //! QsciScintilla::AiOpening, QsciScintilla::AiClosing
109     int autoIndentStyle();
110 
111     //! Returns a space separated list of words or characters in a particular
112     //! style that define the end of a block for auto-indentation.  The style
113     //! is returned via \a style.
114     virtual const char *blockEnd(int *style = 0) const;
115 
116     //! Returns the number of lines prior to the current one when determining
117     //! the scope of a block when auto-indenting.
118     virtual int blockLookback() const;
119 
120     //! Returns a space separated list of words or characters in a particular
121     //! style that define the start of a block for auto-indentation.  The style
122     //! is returned via \a style.
123     virtual const char *blockStart(int *style = 0) const;
124 
125     //! Returns a space separated list of keywords in a particular style that
126     //! define the start of a block for auto-indentation.  The style is
127     //! returned via \a style.
128     virtual const char *blockStartKeyword(int *style = 0) const;
129 
130     //! Returns the style used for braces for brace matching.
131     virtual int braceStyle() const;
132 
133     //! Returns true if the language is case sensitive.  The default is true.
134     virtual bool caseSensitive() const;
135 
136     //! Returns the foreground colour of the text for style number \a style.
137     //! The default colour is that returned by defaultColor().
138     //!
139     //! \sa defaultColor(), paper()
140     virtual QColor color(int style) const;
141 
142     //! Returns the end-of-line for style number \a style.  The default is
143     //! false.
144     virtual bool eolFill(int style) const;
145 
146     //! Returns the font for style number \a style.  The default font is
147     //! that returned by defaultFont().
148     //!
149     //! \sa defaultFont()
150     virtual QFont font(int style) const;
151 
152     //! Returns the view used for indentation guides.
153     virtual int indentationGuideView() const;
154 
155     //! Returns the set of keywords for the keyword set \a set recognised
156     //! by the lexer as a space separated string.  Keyword sets are numbered
157     //! from 1.  0 is returned if there is no such set.
158     virtual const char *keywords(int set) const;
159 
160     //! Returns the number of the style used for whitespace.  The default
161     //! implementation returns 0 which is the convention adopted by most
162     //! lexers.
163     virtual int defaultStyle() const;
164 
165     //! Returns the descriptive name for style number \a style.  For a valid
166     //! style number for this language a non-empty QString must be returned.
167     //! If the style number is invalid then an empty QString must be returned.
168     //! This is intended to be used in user preference dialogs.
169     virtual QString description(int style) const = 0;
170 
171     //! Returns the background colour of the text for style number
172     //! \a style.
173     //!
174     //! \sa defaultPaper(), color()
175     virtual QColor paper(int style) const;
176 
177     //! Returns the default text colour.
178     //!
179     //! \sa setDefaultColor()
180     QColor defaultColor() const;
181 
182     //! Returns the default text colour for style number \a style.
183     virtual QColor defaultColor(int style) const;
184 
185     //! Returns the default end-of-line for style number \a style.  The default
186     //! is false.
187     virtual bool defaultEolFill(int style) const;
188 
189     //! Returns the default font.
190     //!
191     //! \sa setDefaultFont()
192     QFont defaultFont() const;
193 
194     //! Returns the default font for style number \a style.
195     virtual QFont defaultFont(int style) const;
196 
197     //! Returns the default paper colour.
198     //!
199     //! \sa setDefaultPaper()
200     QColor defaultPaper() const;
201 
202     //! Returns the default paper colour for style number \a style.
203     virtual QColor defaultPaper(int style) const;
204 
205     //! Returns the QsciScintilla instance that the lexer is currently attached
206     //! to or 0 if it is unattached.
editor()207     QsciScintilla *editor() const {return attached_editor;}
208 
209     //! The current set of APIs is set to \a apis.  If \a apis is 0 then any
210     //! existing APIs for this lexer are removed.
211     //!
212     //! \sa apis()
213     void setAPIs(QsciAbstractAPIs *apis);
214 
215     //! The default text colour is set to \a c.
216     //!
217     //! \sa defaultColor(), color()
218     void setDefaultColor(const QColor &c);
219 
220     //! The default font is set to \a f.
221     //!
222     //! \sa defaultFont(), font()
223     void setDefaultFont(const QFont &f);
224 
225     //! The default paper colour is set to \a c.
226     //!
227     //! \sa defaultPaper(), paper()
228     void setDefaultPaper(const QColor &c);
229 
230     //! \internal Set the QsciScintilla instance that the lexer is attached to.
231     virtual void setEditor(QsciScintilla *editor);
232 
233     //! The colour, paper, font and end-of-line for each style number, and
234     //! all lexer specific properties are read from the settings \a qs.
235     //! \a prefix is prepended to the key of each entry.  true is returned
236     //! if there was no error.
237     //!
238     //! \sa writeSettings(), QsciScintilla::setLexer()
239     bool readSettings(QSettings &qs,const char *prefix = "/Scintilla");
240 
241     //! Causes all properties to be refreshed by emitting the
242     //! propertyChanged() signal as required.
243     virtual void refreshProperties();
244 
245     //! Returns the number of style bits needed by the lexer.  Normally this
246     //! should only be re-implemented by custom lexers.  This is deprecated and
247     //! no longer has any effect.
248     virtual int styleBitsNeeded() const;
249 
250     //! Returns the string of characters that comprise a word.  The default is
251     //! 0 which implies the upper and lower case alphabetic characters and
252     //! underscore.
253     virtual const char *wordCharacters() const;
254 
255     //! The colour, paper, font and end-of-line for each style number, and
256     //! all lexer specific properties are written to the settings \a qs.
257     //! \a prefix is prepended to the key of each entry.  true is returned
258     //! if there was no error.
259     //!
260     //! \sa readSettings()
261     bool writeSettings(QSettings &qs,
262                const char *prefix = "/Scintilla") const;
263 
264 public slots:
265     //! The auto-indentation style is set to \a autoindentstyle.
266     //!
267     //! \sa autoIndentStyle(), QsciScintilla::AiMaintain,
268     //! QsciScintilla::AiOpening, QsciScintilla::AiClosing
269     virtual void setAutoIndentStyle(int autoindentstyle);
270 
271     //! The foreground colour for style number \a style is set to \a c.  If
272     //! \a style is -1 then the colour is set for all styles.
273     virtual void setColor(const QColor &c,int style = -1);
274 
275     //! The end-of-line fill for style number \a style is set to
276     //! \a eoffill.  If \a style is -1 then the fill is set for all styles.
277     virtual void setEolFill(bool eoffill,int style = -1);
278 
279     //! The font for style number \a style is set to \a f.  If \a style is
280     //! -1 then the font is set for all styles.
281     virtual void setFont(const QFont &f,int style = -1);
282 
283     //! The background colour for style number \a style is set to \a c.  If
284     //! \a style is -1 then the colour is set for all styles.
285     virtual void setPaper(const QColor &c,int style = -1);
286 
287 signals:
288     //! This signal is emitted when the foreground colour of style number
289     //! \a style has changed.  The new colour is \a c.
290     void colorChanged(const QColor &c,int style);
291 
292     //! This signal is emitted when the end-of-file fill of style number
293     //! \a style has changed.  The new fill is \a eolfilled.
294     void eolFillChanged(bool eolfilled,int style);
295 
296     //! This signal is emitted when the font of style number \a style has
297     //! changed.  The new font is \a f.
298     void fontChanged(const QFont &f,int style);
299 
300     //! This signal is emitted when the background colour of style number
301     //! \a style has changed.  The new colour is \a c.
302     void paperChanged(const QColor &c,int style);
303 
304     //! This signal is emitted when the value of the lexer property \a prop
305     //! needs to be changed.  The new value is \a val.
306     void propertyChanged(const char *prop, const char *val);
307 
308 protected:
309     //! The lexer's properties are read from the settings \a qs.  \a prefix
310     //! (which has a trailing '/') should be used as a prefix to the key of
311     //! each setting.  true is returned if there is no error.
312     //!
313     virtual bool readProperties(QSettings &qs,const QString &prefix);
314 
315     //! The lexer's properties are written to the settings \a qs.
316     //! \a prefix (which has a trailing '/') should be used as a prefix to
317     //! the key of each setting.  true is returned if there is no error.
318     //!
319     virtual bool writeProperties(QSettings &qs,const QString &prefix) const;
320 
321 private:
322     struct StyleData {
323         QFont font;
324         QColor color;
325         QColor paper;
326         bool eol_fill;
327     };
328 
329     struct StyleDataMap {
330         bool style_data_set;
331         QMap<int, StyleData> style_data;
332     };
333 
334     StyleDataMap *style_map;
335 
336     int autoIndStyle;
337     QFont defFont;
338     QColor defColor;
339     QColor defPaper;
340     QsciAbstractAPIs *apiSet;
341     QsciScintilla *attached_editor;
342 
343     void setStyleDefaults() const;
344     StyleData &styleData(int style) const;
345 
346     QsciLexer(const QsciLexer &);
347     QsciLexer &operator=(const QsciLexer &);
348 };
349 
350 #endif
351