qscilexerd.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // This module implements the QsciLexerD 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. #include "Qsci/qscilexerd.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerD::QsciLexerD(QObject *parent)
  25. : QsciLexer(parent),
  26. fold_atelse(false), fold_comments(false), fold_compact(true)
  27. {
  28. }
  29. // The dtor.
  30. QsciLexerD::~QsciLexerD()
  31. {
  32. }
  33. // Returns the language name.
  34. const char *QsciLexerD::language() const
  35. {
  36. return "D";
  37. }
  38. // Returns the lexer name.
  39. const char *QsciLexerD::lexer() const
  40. {
  41. return "d";
  42. }
  43. // Return the set of character sequences that can separate auto-completion
  44. // words.
  45. QStringList QsciLexerD::autoCompletionWordSeparators() const
  46. {
  47. QStringList wl;
  48. wl << ".";
  49. return wl;
  50. }
  51. // Return the list of keywords that can start a block.
  52. const char *QsciLexerD::blockStartKeyword(int *style) const
  53. {
  54. if (style)
  55. *style = Keyword;
  56. return "case catch class default do else finally for foreach "
  57. "foreach_reverse if private protected public struct try union "
  58. "while";
  59. }
  60. // Return the list of characters that can start a block.
  61. const char *QsciLexerD::blockStart(int *style) const
  62. {
  63. if (style)
  64. *style = Operator;
  65. return "{";
  66. }
  67. // Return the list of characters that can end a block.
  68. const char *QsciLexerD::blockEnd(int *style) const
  69. {
  70. if (style)
  71. *style = Operator;
  72. return "}";
  73. }
  74. // Return the style used for braces.
  75. int QsciLexerD::braceStyle() const
  76. {
  77. return Operator;
  78. }
  79. // Return the string of characters that comprise a word.
  80. const char *QsciLexerD::wordCharacters() const
  81. {
  82. return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_#";
  83. }
  84. // Returns the foreground colour of the text for a style.
  85. QColor QsciLexerD::defaultColor(int style) const
  86. {
  87. switch (style)
  88. {
  89. case Default:
  90. return QColor(0x80,0x80,0x80);
  91. case Comment:
  92. case CommentLine:
  93. return QColor(0x00,0x7f,0x00);
  94. case CommentDoc:
  95. case CommentLineDoc:
  96. return QColor(0x3f,0x70,0x3f);
  97. case CommentNested:
  98. return QColor(0xa0,0xc0,0xa0);
  99. case Number:
  100. return QColor(0x00,0x7f,0x7f);
  101. case Keyword:
  102. case KeywordSecondary:
  103. case KeywordDoc:
  104. case Typedefs:
  105. return QColor(0x00,0x00,0x7f);
  106. case String:
  107. return QColor(0x7f,0x00,0x7f);
  108. case Character:
  109. return QColor(0x7f,0x00,0x7f);
  110. case Operator:
  111. case UnclosedString:
  112. return QColor(0x00,0x00,0x00);
  113. case Identifier:
  114. break;
  115. case CommentDocKeyword:
  116. return QColor(0x30,0x60,0xa0);
  117. case CommentDocKeywordError:
  118. return QColor(0x80,0x40,0x20);
  119. }
  120. return QsciLexer::defaultColor(style);
  121. }
  122. // Returns the end-of-line fill for a style.
  123. bool QsciLexerD::defaultEolFill(int style) const
  124. {
  125. if (style == UnclosedString)
  126. return true;
  127. return QsciLexer::defaultEolFill(style);
  128. }
  129. // Returns the font of the text for a style.
  130. QFont QsciLexerD::defaultFont(int style) const
  131. {
  132. QFont f;
  133. switch (style)
  134. {
  135. case Comment:
  136. case CommentLine:
  137. case CommentDoc:
  138. case CommentNested:
  139. case CommentLineDoc:
  140. case CommentDocKeyword:
  141. case CommentDocKeywordError:
  142. #if defined(Q_OS_WIN)
  143. f = QFont("Comic Sans MS",9);
  144. #elif defined(Q_OS_MAC)
  145. f = QFont("Comic Sans MS", 12);
  146. #else
  147. f = QFont("Bitstream Vera Serif",9);
  148. #endif
  149. break;
  150. case Keyword:
  151. case KeywordSecondary:
  152. case KeywordDoc:
  153. case Typedefs:
  154. case Operator:
  155. f = QsciLexer::defaultFont(style);
  156. f.setBold(true);
  157. break;
  158. case String:
  159. case UnclosedString:
  160. #if defined(Q_OS_WIN)
  161. f = QFont("Courier New",10);
  162. #elif defined(Q_OS_MAC)
  163. f = QFont("Courier", 12);
  164. #else
  165. f = QFont("Bitstream Vera Sans Mono",9);
  166. #endif
  167. break;
  168. default:
  169. f = QsciLexer::defaultFont(style);
  170. }
  171. return f;
  172. }
  173. // Returns the set of keywords.
  174. const char *QsciLexerD::keywords(int set) const
  175. {
  176. if (set == 1)
  177. return
  178. "abstract alias align asm assert auto body bool break byte case "
  179. "cast catch cdouble cent cfloat char class const continue creal "
  180. "dchar debug default delegate delete deprecated do double else "
  181. "enum export extern false final finally float for foreach "
  182. "foreach_reverse function goto idouble if ifloat import in inout "
  183. "int interface invariant ireal is lazy long mixin module new null "
  184. "out override package pragma private protected public real return "
  185. "scope short static struct super switch synchronized template "
  186. "this throw true try typedef typeid typeof ubyte ucent uint ulong "
  187. "union unittest ushort version void volatile wchar while with";
  188. if (set == 3)
  189. return
  190. "a addindex addtogroup anchor arg attention author b brief bug c "
  191. "class code date def defgroup deprecated dontinclude e em endcode "
  192. "endhtmlonly endif endlatexonly endlink endverbatim enum example "
  193. "exception f$ f[ f] file fn hideinitializer htmlinclude htmlonly "
  194. "if image include ingroup internal invariant interface latexonly "
  195. "li line link mainpage name namespace nosubgrouping note overload "
  196. "p page par param post pre ref relates remarks return retval sa "
  197. "section see showinitializer since skip skipline struct "
  198. "subsection test throw todo typedef union until var verbatim "
  199. "verbinclude version warning weakgroup $ @ \\ & < > # { }";
  200. return 0;
  201. }
  202. // Returns the user name of a style.
  203. QString QsciLexerD::description(int style) const
  204. {
  205. switch (style)
  206. {
  207. case Default:
  208. return tr("Default");
  209. case Comment:
  210. return tr("Block comment");
  211. case CommentLine:
  212. return tr("Line comment");
  213. case CommentDoc:
  214. return tr("DDoc style block comment");
  215. case CommentNested:
  216. return tr("Nesting comment");
  217. case Number:
  218. return tr("Number");
  219. case Keyword:
  220. return tr("Keyword");
  221. case KeywordSecondary:
  222. return tr("Secondary keyword");
  223. case KeywordDoc:
  224. return tr("Documentation keyword");
  225. case Typedefs:
  226. return tr("Type definition");
  227. case String:
  228. return tr("String");
  229. case UnclosedString:
  230. return tr("Unclosed string");
  231. case Character:
  232. return tr("Character");
  233. case Operator:
  234. return tr("Operator");
  235. case Identifier:
  236. return tr("Identifier");
  237. case CommentLineDoc:
  238. return tr("DDoc style line comment");
  239. case CommentDocKeyword:
  240. return tr("DDoc keyword");
  241. case CommentDocKeywordError:
  242. return tr("DDoc keyword error");
  243. case BackquoteString:
  244. return tr("Backquoted string");
  245. case RawString:
  246. return tr("Raw string");
  247. case KeywordSet5:
  248. return tr("User defined 1");
  249. case KeywordSet6:
  250. return tr("User defined 2");
  251. case KeywordSet7:
  252. return tr("User defined 3");
  253. }
  254. return QString();
  255. }
  256. // Returns the background colour of the text for a style.
  257. QColor QsciLexerD::defaultPaper(int style) const
  258. {
  259. if (style == UnclosedString)
  260. return QColor(0xe0,0xc0,0xe0);
  261. return QsciLexer::defaultPaper(style);
  262. }
  263. // Refresh all properties.
  264. void QsciLexerD::refreshProperties()
  265. {
  266. setAtElseProp();
  267. setCommentProp();
  268. setCompactProp();
  269. }
  270. // Read properties from the settings.
  271. bool QsciLexerD::readProperties(QSettings &qs,const QString &prefix)
  272. {
  273. int rc = true;
  274. fold_atelse = qs.value(prefix + "foldatelse", false).toBool();
  275. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  276. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  277. return rc;
  278. }
  279. // Write properties to the settings.
  280. bool QsciLexerD::writeProperties(QSettings &qs,const QString &prefix) const
  281. {
  282. int rc = true;
  283. qs.setValue(prefix + "foldatelse", fold_atelse);
  284. qs.setValue(prefix + "foldcomments", fold_comments);
  285. qs.setValue(prefix + "foldcompact", fold_compact);
  286. return rc;
  287. }
  288. // Return true if else can be folded.
  289. bool QsciLexerD::foldAtElse() const
  290. {
  291. return fold_atelse;
  292. }
  293. // Set if else can be folded.
  294. void QsciLexerD::setFoldAtElse(bool fold)
  295. {
  296. fold_atelse = fold;
  297. setAtElseProp();
  298. }
  299. // Set the "fold.at.else" property.
  300. void QsciLexerD::setAtElseProp()
  301. {
  302. emit propertyChanged("fold.at.else",(fold_atelse ? "1" : "0"));
  303. }
  304. // Return true if comments can be folded.
  305. bool QsciLexerD::foldComments() const
  306. {
  307. return fold_comments;
  308. }
  309. // Set if comments can be folded.
  310. void QsciLexerD::setFoldComments(bool fold)
  311. {
  312. fold_comments = fold;
  313. setCommentProp();
  314. }
  315. // Set the "fold.comment" property.
  316. void QsciLexerD::setCommentProp()
  317. {
  318. emit propertyChanged("fold.comment",(fold_comments ? "1" : "0"));
  319. }
  320. // Return true if folds are compact.
  321. bool QsciLexerD::foldCompact() const
  322. {
  323. return fold_compact;
  324. }
  325. // Set if folds are compact
  326. void QsciLexerD::setFoldCompact(bool fold)
  327. {
  328. fold_compact = fold;
  329. setCompactProp();
  330. }
  331. // Set the "fold.compact" property.
  332. void QsciLexerD::setCompactProp()
  333. {
  334. emit propertyChanged("fold.compact",(fold_compact ? "1" : "0"));
  335. }