qscilexercoffeescript.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // This module implements the QsciLexerCoffeeScript 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/qscilexercoffeescript.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerCoffeeScript::QsciLexerCoffeeScript(QObject *parent)
  25. : QsciLexer(parent),
  26. fold_comments(false), fold_compact(true), style_preproc(false),
  27. dollars(true)
  28. {
  29. }
  30. // The dtor.
  31. QsciLexerCoffeeScript::~QsciLexerCoffeeScript()
  32. {
  33. }
  34. // Returns the language name.
  35. const char *QsciLexerCoffeeScript::language() const
  36. {
  37. return "CoffeeScript";
  38. }
  39. // Returns the lexer name.
  40. const char *QsciLexerCoffeeScript::lexer() const
  41. {
  42. return "coffeescript";
  43. }
  44. // Return the set of character sequences that can separate auto-completion
  45. // words.
  46. QStringList QsciLexerCoffeeScript::autoCompletionWordSeparators() const
  47. {
  48. QStringList wl;
  49. wl << ".";
  50. return wl;
  51. }
  52. // Return the list of keywords that can start a block.
  53. const char *QsciLexerCoffeeScript::blockStartKeyword(int *style) const
  54. {
  55. if (style)
  56. *style = Keyword;
  57. return "catch class do else finally for if try until when while";
  58. }
  59. // Return the list of characters that can start a block.
  60. const char *QsciLexerCoffeeScript::blockStart(int *style) const
  61. {
  62. if (style)
  63. *style = Operator;
  64. return "{";
  65. }
  66. // Return the list of characters that can end a block.
  67. const char *QsciLexerCoffeeScript::blockEnd(int *style) const
  68. {
  69. if (style)
  70. *style = Operator;
  71. return "}";
  72. }
  73. // Return the style used for braces.
  74. int QsciLexerCoffeeScript::braceStyle() const
  75. {
  76. return Operator;
  77. }
  78. // Return the string of characters that comprise a word.
  79. const char *QsciLexerCoffeeScript::wordCharacters() const
  80. {
  81. return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_#";
  82. }
  83. // Returns the foreground colour of the text for a style.
  84. QColor QsciLexerCoffeeScript::defaultColor(int style) const
  85. {
  86. switch (style)
  87. {
  88. case Default:
  89. return QColor(0x80, 0x80, 0x80);
  90. case Comment:
  91. case CommentLine:
  92. case CommentBlock:
  93. case BlockRegexComment:
  94. return QColor(0x00, 0x7f, 0x00);
  95. case CommentDoc:
  96. case CommentLineDoc:
  97. return QColor(0x3f, 0x70, 0x3f);
  98. case Number:
  99. return QColor(0x00, 0x7f, 0x7f);
  100. case Keyword:
  101. return QColor(0x00, 0x00, 0x7f);
  102. case DoubleQuotedString:
  103. case SingleQuotedString:
  104. return QColor(0x7f, 0x00, 0x7f);
  105. case PreProcessor:
  106. return QColor(0x7f, 0x7f, 0x00);
  107. case Operator:
  108. case UnclosedString:
  109. return QColor(0x00, 0x00, 0x00);
  110. case VerbatimString:
  111. return QColor(0x00, 0x7f, 0x00);
  112. case Regex:
  113. case BlockRegex:
  114. return QColor(0x3f, 0x7f, 0x3f);
  115. case CommentDocKeyword:
  116. return QColor(0x30, 0x60, 0xa0);
  117. case CommentDocKeywordError:
  118. return QColor(0x80, 0x40, 0x20);
  119. case InstanceProperty:
  120. return QColor(0xc0, 0x60, 0x00);
  121. }
  122. return QsciLexer::defaultColor(style);
  123. }
  124. // Returns the end-of-line fill for a style.
  125. bool QsciLexerCoffeeScript::defaultEolFill(int style) const
  126. {
  127. switch (style)
  128. {
  129. case UnclosedString:
  130. case VerbatimString:
  131. case Regex:
  132. return true;
  133. }
  134. return QsciLexer::defaultEolFill(style);
  135. }
  136. // Returns the font of the text for a style.
  137. QFont QsciLexerCoffeeScript::defaultFont(int style) const
  138. {
  139. QFont f;
  140. switch (style)
  141. {
  142. case Comment:
  143. case CommentLine:
  144. case CommentDoc:
  145. case CommentLineDoc:
  146. case CommentDocKeyword:
  147. case CommentDocKeywordError:
  148. case CommentBlock:
  149. case BlockRegexComment:
  150. #if defined(Q_OS_WIN)
  151. f = QFont("Comic Sans MS",9);
  152. #elif defined(Q_OS_MAC)
  153. f = QFont("Comic Sans MS", 12);
  154. #else
  155. f = QFont("Bitstream Vera Serif",9);
  156. #endif
  157. break;
  158. case Keyword:
  159. case Operator:
  160. f = QsciLexer::defaultFont(style);
  161. f.setBold(true);
  162. break;
  163. case DoubleQuotedString:
  164. case SingleQuotedString:
  165. case UnclosedString:
  166. case VerbatimString:
  167. case Regex:
  168. case BlockRegex:
  169. #if defined(Q_OS_WIN)
  170. f = QFont("Courier New",10);
  171. #elif defined(Q_OS_MAC)
  172. f = QFont("Courier", 12);
  173. #else
  174. f = QFont("Bitstream Vera Sans Mono",9);
  175. #endif
  176. break;
  177. default:
  178. f = QsciLexer::defaultFont(style);
  179. }
  180. return f;
  181. }
  182. // Returns the set of keywords.
  183. const char *QsciLexerCoffeeScript::keywords(int set) const
  184. {
  185. if (set == 1)
  186. return
  187. "true false null this new delete typeof in instanceof return "
  188. "throw break continue debugger if else switch for while do try "
  189. "catch finally class extends super "
  190. "undefined then unless until loop of by when and or is isnt not "
  191. "yes no on off";
  192. return 0;
  193. }
  194. // Returns the user name of a style.
  195. QString QsciLexerCoffeeScript::description(int style) const
  196. {
  197. switch (style)
  198. {
  199. case Default:
  200. return tr("Default");
  201. case Comment:
  202. return tr("C-style comment");
  203. case CommentLine:
  204. return tr("C++-style comment");
  205. case CommentDoc:
  206. return tr("JavaDoc C-style comment");
  207. case Number:
  208. return tr("Number");
  209. case Keyword:
  210. return tr("Keyword");
  211. case DoubleQuotedString:
  212. return tr("Double-quoted string");
  213. case SingleQuotedString:
  214. return tr("Single-quoted string");
  215. case UUID:
  216. return tr("IDL UUID");
  217. case PreProcessor:
  218. return tr("Pre-processor block");
  219. case Operator:
  220. return tr("Operator");
  221. case Identifier:
  222. return tr("Identifier");
  223. case UnclosedString:
  224. return tr("Unclosed string");
  225. case VerbatimString:
  226. return tr("C# verbatim string");
  227. case Regex:
  228. return tr("Regular expression");
  229. case CommentLineDoc:
  230. return tr("JavaDoc C++-style comment");
  231. case KeywordSet2:
  232. return tr("Secondary keywords and identifiers");
  233. case CommentDocKeyword:
  234. return tr("JavaDoc keyword");
  235. case CommentDocKeywordError:
  236. return tr("JavaDoc keyword error");
  237. case GlobalClass:
  238. return tr("Global classes");
  239. case CommentBlock:
  240. return tr("Block comment");
  241. case BlockRegex:
  242. return tr("Block regular expression");
  243. case BlockRegexComment:
  244. return tr("Block regular expression comment");
  245. case InstanceProperty:
  246. return tr("Instance property");
  247. }
  248. return QString();
  249. }
  250. // Returns the background colour of the text for a style.
  251. QColor QsciLexerCoffeeScript::defaultPaper(int style) const
  252. {
  253. switch (style)
  254. {
  255. case UnclosedString:
  256. return QColor(0xe0,0xc0,0xe0);
  257. case VerbatimString:
  258. return QColor(0xe0,0xff,0xe0);
  259. case Regex:
  260. return QColor(0xe0,0xf0,0xe0);
  261. }
  262. return QsciLexer::defaultPaper(style);
  263. }
  264. // Refresh all properties.
  265. void QsciLexerCoffeeScript::refreshProperties()
  266. {
  267. setCommentProp();
  268. setCompactProp();
  269. setStylePreprocProp();
  270. setDollarsProp();
  271. }
  272. // Read properties from the settings.
  273. bool QsciLexerCoffeeScript::readProperties(QSettings &qs,const QString &prefix)
  274. {
  275. int rc = true;
  276. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  277. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  278. style_preproc = qs.value(prefix + "stylepreprocessor", false).toBool();
  279. dollars = qs.value(prefix + "dollars", true).toBool();
  280. return rc;
  281. }
  282. // Write properties to the settings.
  283. bool QsciLexerCoffeeScript::writeProperties(QSettings &qs,const QString &prefix) const
  284. {
  285. int rc = true;
  286. qs.setValue(prefix + "foldcomments", fold_comments);
  287. qs.setValue(prefix + "foldcompact", fold_compact);
  288. qs.setValue(prefix + "stylepreprocessor", style_preproc);
  289. qs.setValue(prefix + "dollars", dollars);
  290. return rc;
  291. }
  292. // Set if comments can be folded.
  293. void QsciLexerCoffeeScript::setFoldComments(bool fold)
  294. {
  295. fold_comments = fold;
  296. setCommentProp();
  297. }
  298. // Set the "fold.comment" property.
  299. void QsciLexerCoffeeScript::setCommentProp()
  300. {
  301. emit propertyChanged("fold.coffeescript.comment",
  302. (fold_comments ? "1" : "0"));
  303. }
  304. // Set if folds are compact
  305. void QsciLexerCoffeeScript::setFoldCompact(bool fold)
  306. {
  307. fold_compact = fold;
  308. setCompactProp();
  309. }
  310. // Set the "fold.compact" property.
  311. void QsciLexerCoffeeScript::setCompactProp()
  312. {
  313. emit propertyChanged("fold.compact", (fold_compact ? "1" : "0"));
  314. }
  315. // Set if preprocessor lines are styled.
  316. void QsciLexerCoffeeScript::setStylePreprocessor(bool style)
  317. {
  318. style_preproc = style;
  319. setStylePreprocProp();
  320. }
  321. // Set the "styling.within.preprocessor" property.
  322. void QsciLexerCoffeeScript::setStylePreprocProp()
  323. {
  324. emit propertyChanged("styling.within.preprocessor",
  325. (style_preproc ? "1" : "0"));
  326. }
  327. // Set if '$' characters are allowed.
  328. void QsciLexerCoffeeScript::setDollarsAllowed(bool allowed)
  329. {
  330. dollars = allowed;
  331. setDollarsProp();
  332. }
  333. // Set the "lexer.cpp.allow.dollars" property.
  334. void QsciLexerCoffeeScript::setDollarsProp()
  335. {
  336. emit propertyChanged("lexer.cpp.allow.dollars", (dollars ? "1" : "0"));
  337. }