qscilexer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // This defines the interface to the QsciLexer class.
  2. //
  3. // Copyright (c) 2017 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. #ifndef QSCILEXER_H
  20. #define QSCILEXER_H
  21. #include <QColor>
  22. #include <QFont>
  23. #include <QMap>
  24. #include <QObject>
  25. #include <QString>
  26. #include <Qsci/qsciglobal.h>
  27. QT_BEGIN_NAMESPACE
  28. class QSettings;
  29. QT_END_NAMESPACE
  30. class QsciAbstractAPIs;
  31. class QsciScintilla;
  32. //! \brief The QsciLexer class is an abstract class used as a base for language
  33. //! lexers.
  34. //!
  35. //! A lexer scans the text breaking it up into separate language objects, e.g.
  36. //! keywords, strings, operators. The lexer then uses a different style to
  37. //! draw each object. A style is identified by a style number and has a number
  38. //! of attributes, including colour and font. A specific language lexer will
  39. //! implement appropriate default styles which can be overriden by an
  40. //! application by further sub-classing the specific language lexer.
  41. //!
  42. //! A lexer may provide one or more sets of words to be recognised as keywords.
  43. //! Most lexers only provide one set, but some may support languages embedded
  44. //! in other languages and provide several sets.
  45. //!
  46. //! QsciLexer provides convenience methods for saving and restoring user
  47. //! preferences for fonts and colours.
  48. //!
  49. //! If you want to write a lexer for a new language then you can add it to the
  50. //! underlying Scintilla code and implement a corresponding QsciLexer sub-class
  51. //! to manage the different styles used. Alternatively you can implement a
  52. //! sub-class of QsciLexerCustom.
  53. class QSCINTILLA_EXPORT QsciLexer : public QObject
  54. {
  55. Q_OBJECT
  56. public:
  57. //! Construct a QsciLexer with parent \a parent. \a parent is typically
  58. //! the QsciScintilla instance.
  59. QsciLexer(QObject *parent = 0);
  60. //! Destroy the QSciLexer.
  61. virtual ~QsciLexer();
  62. //! Returns the name of the language. It must be re-implemented by a
  63. //! sub-class.
  64. virtual const char *language() const = 0;
  65. //! Returns the name of the lexer. If 0 is returned then the lexer's
  66. //! numeric identifier is used. The default implementation returns 0.
  67. //!
  68. //! \sa lexerId()
  69. virtual const char *lexer() const;
  70. //! Returns the identifier (i.e. a QsciScintillaBase::SCLEX_* value) of the
  71. //! lexer. This is only used if lexer() returns 0. The default
  72. //! implementation returns QsciScintillaBase::SCLEX_CONTAINER.
  73. //!
  74. //! \sa lexer()
  75. virtual int lexerId() const;
  76. //! Returns the current API set or 0 if there isn't one.
  77. //!
  78. //! \sa setAPIs()
  79. QsciAbstractAPIs *apis() const;
  80. //! Returns the characters that can fill up auto-completion.
  81. virtual const char *autoCompletionFillups() const;
  82. //! Returns the list of character sequences that can separate
  83. //! auto-completion words. The first in the list is assumed to be the
  84. //! sequence used to separate words in the lexer's API files.
  85. virtual QStringList autoCompletionWordSeparators() const;
  86. //! Returns the auto-indentation style. The default is 0 if the
  87. //! language is block structured, or QsciScintilla::AiMaintain if not.
  88. //!
  89. //! \sa setAutoIndentStyle(), QsciScintilla::AiMaintain,
  90. //! QsciScintilla::AiOpening, QsciScintilla::AiClosing
  91. int autoIndentStyle();
  92. //! Returns a space separated list of words or characters in a particular
  93. //! style that define the end of a block for auto-indentation. The style
  94. //! is returned via \a style.
  95. virtual const char *blockEnd(int *style = 0) const;
  96. //! Returns the number of lines prior to the current one when determining
  97. //! the scope of a block when auto-indenting.
  98. virtual int blockLookback() const;
  99. //! Returns a space separated list of words or characters in a particular
  100. //! style that define the start of a block for auto-indentation. The style
  101. //! is returned via \a style.
  102. virtual const char *blockStart(int *style = 0) const;
  103. //! Returns a space separated list of keywords in a particular style that
  104. //! define the start of a block for auto-indentation. The style is
  105. //! returned via \a style.
  106. virtual const char *blockStartKeyword(int *style = 0) const;
  107. //! Returns the style used for braces for brace matching.
  108. virtual int braceStyle() const;
  109. //! Returns true if the language is case sensitive. The default is true.
  110. virtual bool caseSensitive() const;
  111. //! Returns the foreground colour of the text for style number \a style.
  112. //! The default colour is that returned by defaultColor().
  113. //!
  114. //! \sa defaultColor(), paper()
  115. virtual QColor color(int style) const;
  116. //! Returns the end-of-line for style number \a style. The default is
  117. //! false.
  118. virtual bool eolFill(int style) const;
  119. //! Returns the font for style number \a style. The default font is
  120. //! that returned by defaultFont().
  121. //!
  122. //! \sa defaultFont()
  123. virtual QFont font(int style) const;
  124. //! Returns the view used for indentation guides.
  125. virtual int indentationGuideView() const;
  126. //! Returns the set of keywords for the keyword set \a set recognised
  127. //! by the lexer as a space separated string. 0 is returned if there
  128. //! is no such set.
  129. virtual const char *keywords(int set) const;
  130. //! Returns the number of the style used for whitespace. The default
  131. //! implementation returns 0 which is the convention adopted by most
  132. //! lexers.
  133. virtual int defaultStyle() const;
  134. //! Returns the descriptive name for style number \a style. For a valid
  135. //! style number for this language a non-empty QString must be returned.
  136. //! If the style number is invalid then an empty QString must be returned.
  137. //! This is intended to be used in user preference dialogs.
  138. virtual QString description(int style) const = 0;
  139. //! Returns the background colour of the text for style number
  140. //! \a style.
  141. //!
  142. //! \sa defaultPaper(), color()
  143. virtual QColor paper(int style) const;
  144. //! Returns the default text colour.
  145. //!
  146. //! \sa setDefaultColor()
  147. QColor defaultColor() const;
  148. //! Returns the default text colour for style number \a style.
  149. virtual QColor defaultColor(int style) const;
  150. //! Returns the default end-of-line for style number \a style. The default
  151. //! is false.
  152. virtual bool defaultEolFill(int style) const;
  153. //! Returns the default font.
  154. //!
  155. //! \sa setDefaultFont()
  156. QFont defaultFont() const;
  157. //! Returns the default font for style number \a style.
  158. virtual QFont defaultFont(int style) const;
  159. //! Returns the default paper colour.
  160. //!
  161. //! \sa setDefaultPaper()
  162. QColor defaultPaper() const;
  163. //! Returns the default paper colour for style number \a style.
  164. virtual QColor defaultPaper(int style) const;
  165. //! Returns the QsciScintilla instance that the lexer is currently attached
  166. //! to or 0 if it is unattached.
  167. QsciScintilla *editor() const {return attached_editor;}
  168. //! The current set of APIs is set to \a apis. If \a apis is 0 then any
  169. //! existing APIs for this lexer are removed.
  170. //!
  171. //! \sa apis()
  172. void setAPIs(QsciAbstractAPIs *apis);
  173. //! The default text colour is set to \a c.
  174. //!
  175. //! \sa defaultColor(), color()
  176. void setDefaultColor(const QColor &c);
  177. //! The default font is set to \a f.
  178. //!
  179. //! \sa defaultFont(), font()
  180. void setDefaultFont(const QFont &f);
  181. //! The default paper colour is set to \a c.
  182. //!
  183. //! \sa defaultPaper(), paper()
  184. void setDefaultPaper(const QColor &c);
  185. //! \internal Set the QsciScintilla instance that the lexer is attached to.
  186. virtual void setEditor(QsciScintilla *editor);
  187. //! The colour, paper, font and end-of-line for each style number, and
  188. //! all lexer specific properties are read from the settings \a qs.
  189. //! \a prefix is prepended to the key of each entry. true is returned
  190. //! if there was no error.
  191. //!
  192. //! \sa writeSettings(), QsciScintilla::setLexer()
  193. bool readSettings(QSettings &qs,const char *prefix = "/Scintilla");
  194. //! Causes all properties to be refreshed by emitting the
  195. //! propertyChanged() signal as required.
  196. virtual void refreshProperties();
  197. //! Returns the number of style bits needed by the lexer. Normally this
  198. //! should only be re-implemented by custom lexers.
  199. virtual int styleBitsNeeded() const;
  200. //! Returns the string of characters that comprise a word. The default is
  201. //! 0 which implies the upper and lower case alphabetic characters and
  202. //! underscore.
  203. virtual const char *wordCharacters() const;
  204. //! The colour, paper, font and end-of-line for each style number, and
  205. //! all lexer specific properties are written to the settings \a qs.
  206. //! \a prefix is prepended to the key of each entry. true is returned
  207. //! if there was no error.
  208. //!
  209. //! \sa readSettings()
  210. bool writeSettings(QSettings &qs,
  211. const char *prefix = "/Scintilla") const;
  212. public slots:
  213. //! The auto-indentation style is set to \a autoindentstyle.
  214. //!
  215. //! \sa autoIndentStyle(), QsciScintilla::AiMaintain,
  216. //! QsciScintilla::AiOpening, QsciScintilla::AiClosing
  217. virtual void setAutoIndentStyle(int autoindentstyle);
  218. //! The foreground colour for style number \a style is set to \a c. If
  219. //! \a style is -1 then the colour is set for all styles.
  220. virtual void setColor(const QColor &c,int style = -1);
  221. //! The end-of-line fill for style number \a style is set to
  222. //! \a eoffill. If \a style is -1 then the fill is set for all styles.
  223. virtual void setEolFill(bool eoffill,int style = -1);
  224. //! The font for style number \a style is set to \a f. If \a style is
  225. //! -1 then the font is set for all styles.
  226. virtual void setFont(const QFont &f,int style = -1);
  227. //! The background colour for style number \a style is set to \a c. If
  228. //! \a style is -1 then the colour is set for all styles.
  229. virtual void setPaper(const QColor &c,int style = -1);
  230. signals:
  231. //! This signal is emitted when the foreground colour of style number
  232. //! \a style has changed. The new colour is \a c.
  233. void colorChanged(const QColor &c,int style);
  234. //! This signal is emitted when the end-of-file fill of style number
  235. //! \a style has changed. The new fill is \a eolfilled.
  236. void eolFillChanged(bool eolfilled,int style);
  237. //! This signal is emitted when the font of style number \a style has
  238. //! changed. The new font is \a f.
  239. void fontChanged(const QFont &f,int style);
  240. //! This signal is emitted when the background colour of style number
  241. //! \a style has changed. The new colour is \a c.
  242. void paperChanged(const QColor &c,int style);
  243. //! This signal is emitted when the value of the lexer property \a prop
  244. //! needs to be changed. The new value is \a val.
  245. void propertyChanged(const char *prop, const char *val);
  246. protected:
  247. //! The lexer's properties are read from the settings \a qs. \a prefix
  248. //! (which has a trailing '/') should be used as a prefix to the key of
  249. //! each setting. true is returned if there is no error.
  250. //!
  251. virtual bool readProperties(QSettings &qs,const QString &prefix);
  252. //! The lexer's properties are written to the settings \a qs.
  253. //! \a prefix (which has a trailing '/') should be used as a prefix to
  254. //! the key of each setting. true is returned if there is no error.
  255. //!
  256. virtual bool writeProperties(QSettings &qs,const QString &prefix) const;
  257. private:
  258. struct StyleData {
  259. QFont font;
  260. QColor color;
  261. QColor paper;
  262. bool eol_fill;
  263. };
  264. struct StyleDataMap {
  265. bool style_data_set;
  266. QMap<int, StyleData> style_data;
  267. };
  268. StyleDataMap *style_map;
  269. int autoIndStyle;
  270. QFont defFont;
  271. QColor defColor;
  272. QColor defPaper;
  273. QsciAbstractAPIs *apiSet;
  274. QsciScintilla *attached_editor;
  275. void setStyleDefaults() const;
  276. StyleData &styleData(int style) const;
  277. QsciLexer(const QsciLexer &);
  278. QsciLexer &operator=(const QsciLexer &);
  279. };
  280. #endif