qscilexerpython.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // This defines the interface to the QsciLexerPython 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 QSCILEXERPYTHON_H
  20. #define QSCILEXERPYTHON_H
  21. #include <QObject>
  22. #include <Qsci/qsciglobal.h>
  23. #include <Qsci/qscilexer.h>
  24. #include "Qsci/qsciscintillabase.h"
  25. //! \brief The QsciLexerPython class encapsulates the Scintilla Python lexer.
  26. class QSCINTILLA_EXPORT QsciLexerPython : public QsciLexer
  27. {
  28. Q_OBJECT
  29. public:
  30. //! This enum defines the meanings of the different styles used by the
  31. //! Python lexer.
  32. enum {
  33. //! The default.
  34. Default = 0,
  35. //! A comment.
  36. Comment = 1,
  37. //! A number.
  38. Number = 2,
  39. //! A double-quoted string.
  40. DoubleQuotedString = 3,
  41. //! A single-quoted string.
  42. SingleQuotedString = 4,
  43. //! A keyword.
  44. Keyword = 5,
  45. //! A triple single-quoted string.
  46. TripleSingleQuotedString = 6,
  47. //! A triple double-quoted string.
  48. TripleDoubleQuotedString = 7,
  49. //! The name of a class.
  50. ClassName = 8,
  51. //! The name of a function or method.
  52. FunctionMethodName = 9,
  53. //! An operator.
  54. Operator = 10,
  55. //! An identifier
  56. Identifier = 11,
  57. //! A comment block.
  58. CommentBlock = 12,
  59. //! The end of a line where a string is not closed.
  60. UnclosedString = 13,
  61. //! A highlighted identifier. These are defined by keyword set
  62. //! 2. Reimplement keywords() to define keyword set 2.
  63. HighlightedIdentifier = 14,
  64. //! A decorator.
  65. Decorator = 15
  66. };
  67. //! This enum defines the different conditions that can cause
  68. //! indentations to be displayed as being bad.
  69. enum IndentationWarning {
  70. //! Bad indentation is not displayed differently.
  71. NoWarning = 0,
  72. //! The indentation is inconsistent when compared to the
  73. //! previous line, ie. it is made up of a different combination
  74. //! of tabs and/or spaces.
  75. Inconsistent = 1,
  76. //! The indentation is made up of spaces followed by tabs.
  77. TabsAfterSpaces = 2,
  78. //! The indentation contains spaces.
  79. Spaces = 3,
  80. //! The indentation contains tabs.
  81. Tabs = 4
  82. };
  83. //! Construct a QsciLexerPython with parent \a parent. \a parent is
  84. //! typically the QsciScintilla instance.
  85. QsciLexerPython(QObject *parent = 0);
  86. //! Destroys the QsciLexerPython instance.
  87. virtual ~QsciLexerPython();
  88. //! Returns the name of the language.
  89. const char *language() const;
  90. //! Returns the name of the lexer. Some lexers support a number of
  91. //! languages.
  92. const char *lexer() const;
  93. //! \internal Returns the character sequences that can separate
  94. //! auto-completion words.
  95. QStringList autoCompletionWordSeparators() const;
  96. //! \internal Returns the number of lines prior to the current one when
  97. //! determining the scope of a block when auto-indenting.
  98. int blockLookback() const;
  99. //! \internal Returns a space separated list of words or characters in
  100. //! a particular style that define the start of a block for
  101. //! auto-indentation. The styles is returned via \a style.
  102. const char *blockStart(int *style = 0) const;
  103. //! \internal Returns the style used for braces for brace matching.
  104. int braceStyle() const;
  105. //! Returns the foreground colour of the text for style number \a style.
  106. //!
  107. //! \sa defaultPaper()
  108. QColor defaultColor(int style) const;
  109. //! Returns the end-of-line fill for style number \a style.
  110. bool defaultEolFill(int style) const;
  111. //! Returns the font for style number \a style.
  112. QFont defaultFont(int style) const;
  113. //! Returns the background colour of the text for style number \a style.
  114. //!
  115. //! \sa defaultColor()
  116. QColor defaultPaper(int style) const;
  117. //! \internal Returns the view used for indentation guides.
  118. virtual int indentationGuideView() const;
  119. //! Returns the set of keywords for the keyword set \a set recognised
  120. //! by the lexer as a space separated string.
  121. const char *keywords(int set) const;
  122. //! Returns the descriptive name for style number \a style. If the
  123. //! style is invalid for this language then an empty QString is returned.
  124. //! This is intended to be used in user preference dialogs.
  125. QString description(int style) const;
  126. //! Causes all properties to be refreshed by emitting the
  127. //! propertyChanged() signal as required.
  128. void refreshProperties();
  129. //! Returns true if indented comment blocks can be folded.
  130. //!
  131. //! \sa setFoldComments()
  132. bool foldComments() const {return fold_comments;}
  133. //! If \a fold is true then trailing blank lines are included in a fold
  134. //! block. The default is true.
  135. //!
  136. //! \sa foldCompact()
  137. void setFoldCompact(bool fold);
  138. //! Returns true if trailing blank lines are included in a fold block.
  139. //!
  140. //! \sa setFoldCompact()
  141. bool foldCompact() const {return fold_compact;}
  142. //! Returns true if triple quoted strings can be folded.
  143. //!
  144. //! \sa setFoldQuotes()
  145. bool foldQuotes() const {return fold_quotes;}
  146. //! Returns the condition that will cause bad indentations to be
  147. //! displayed.
  148. //!
  149. //! \sa setIndentationWarning()
  150. QsciLexerPython::IndentationWarning indentationWarning() const {return indent_warn;}
  151. //! If \a enabled is true then sub-identifiers defined in keyword set 2
  152. //! will be highlighted. For example, if it is false and "open" is defined
  153. //! in keyword set 2 then "foo.open" will not be highlighted. The default
  154. //! is true.
  155. //!
  156. //! \sa highlightSubidentifiers()
  157. void setHighlightSubidentifiers(bool enabled);
  158. //! Returns true if string literals are allowed to span newline characters.
  159. //!
  160. //! \sa setHighlightSubidentifiers()
  161. bool highlightSubidentifiers() const {return highlight_subids;}
  162. //! If \a allowed is true then string literals are allowed to span newline
  163. //! characters. The default is false.
  164. //!
  165. //! \sa stringsOverNewlineAllowed()
  166. void setStringsOverNewlineAllowed(bool allowed);
  167. //! Returns true if string literals are allowed to span newline characters.
  168. //!
  169. //! \sa setStringsOverNewlineAllowed()
  170. bool stringsOverNewlineAllowed() const {return strings_over_newline;}
  171. //! If \a allowed is true then Python v2 unicode string literals (e.g.
  172. //! u"utf8") are allowed. The default is true.
  173. //!
  174. //! \sa v2UnicodeAllowed()
  175. void setV2UnicodeAllowed(bool allowed);
  176. //! Returns true if Python v2 unicode string literals (e.g. u"utf8") are
  177. //! allowed.
  178. //!
  179. //! \sa setV2UnicodeAllowed()
  180. bool v2UnicodeAllowed() const {return v2_unicode;}
  181. //! If \a allowed is true then Python v3 binary and octal literals (e.g.
  182. //! 0b1011, 0o712) are allowed. The default is true.
  183. //!
  184. //! \sa v3BinaryOctalAllowed()
  185. void setV3BinaryOctalAllowed(bool allowed);
  186. //! Returns true if Python v3 binary and octal literals (e.g. 0b1011,
  187. //! 0o712) are allowed.
  188. //!
  189. //! \sa setV3BinaryOctalAllowed()
  190. bool v3BinaryOctalAllowed() const {return v3_binary_octal;}
  191. //! If \a allowed is true then Python v3 bytes string literals (e.g.
  192. //! b"bytes") are allowed. The default is true.
  193. //!
  194. //! \sa v3BytesAllowed()
  195. void setV3BytesAllowed(bool allowed);
  196. //! Returns true if Python v3 bytes string literals (e.g. b"bytes") are
  197. //! allowed.
  198. //!
  199. //! \sa setV3BytesAllowed()
  200. bool v3BytesAllowed() const {return v3_bytes;}
  201. public slots:
  202. //! If \a fold is true then indented comment blocks can be folded. The
  203. //! default is false.
  204. //!
  205. //! \sa foldComments()
  206. virtual void setFoldComments(bool fold);
  207. //! If \a fold is true then triple quoted strings can be folded. The
  208. //! default is false.
  209. //!
  210. //! \sa foldQuotes()
  211. virtual void setFoldQuotes(bool fold);
  212. //! Sets the condition that will cause bad indentations to be
  213. //! displayed.
  214. //!
  215. //! \sa indentationWarning()
  216. virtual void setIndentationWarning(QsciLexerPython::IndentationWarning warn);
  217. protected:
  218. //! The lexer's properties are read from the settings \a qs. \a prefix
  219. //! (which has a trailing '/') should be used as a prefix to the key of
  220. //! each setting. true is returned if there is no error.
  221. //!
  222. bool readProperties(QSettings &qs,const QString &prefix);
  223. //! The lexer's properties are written to the settings \a qs.
  224. //! \a prefix (which has a trailing '/') should be used as a prefix to
  225. //! the key of each setting. true is returned if there is no error.
  226. //!
  227. bool writeProperties(QSettings &qs,const QString &prefix) const;
  228. private:
  229. void setCommentProp();
  230. void setCompactProp();
  231. void setQuotesProp();
  232. void setTabWhingeProp();
  233. void setStringsOverNewlineProp();
  234. void setV2UnicodeProp();
  235. void setV3BinaryOctalProp();
  236. void setV3BytesProp();
  237. void setHighlightSubidsProp();
  238. bool fold_comments;
  239. bool fold_compact;
  240. bool fold_quotes;
  241. IndentationWarning indent_warn;
  242. bool strings_over_newline;
  243. bool v2_unicode;
  244. bool v3_binary_octal;
  245. bool v3_bytes;
  246. bool highlight_subids;
  247. friend class QsciLexerHTML;
  248. static const char *keywordClass;
  249. QsciLexerPython(const QsciLexerPython &);
  250. QsciLexerPython &operator=(const QsciLexerPython &);
  251. };
  252. #endif