qscilexerruby.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // This module implements the QsciLexerRuby 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/qscilexerruby.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerRuby::QsciLexerRuby(QObject *parent)
  25. : QsciLexer(parent), fold_comments(false), fold_compact(true)
  26. {
  27. }
  28. // The dtor.
  29. QsciLexerRuby::~QsciLexerRuby()
  30. {
  31. }
  32. // Returns the language name.
  33. const char *QsciLexerRuby::language() const
  34. {
  35. return "Ruby";
  36. }
  37. // Returns the lexer name.
  38. const char *QsciLexerRuby::lexer() const
  39. {
  40. return "ruby";
  41. }
  42. // Return the list of words that can start a block.
  43. const char *QsciLexerRuby::blockStart(int *style) const
  44. {
  45. if (style)
  46. *style = Keyword;
  47. return "do";
  48. }
  49. // Return the list of words that can start end a block.
  50. const char *QsciLexerRuby::blockEnd(int *style) const
  51. {
  52. if (style)
  53. *style = Keyword;
  54. return "end";
  55. }
  56. // Return the list of words that can start end a block.
  57. const char *QsciLexerRuby::blockStartKeyword(int *style) const
  58. {
  59. if (style)
  60. *style = Keyword;
  61. return "def class if do elsif else case while for";
  62. }
  63. // Return the style used for braces.
  64. int QsciLexerRuby::braceStyle() const
  65. {
  66. return Operator;
  67. }
  68. // Returns the foreground colour of the text for a style.
  69. QColor QsciLexerRuby::defaultColor(int style) const
  70. {
  71. switch (style)
  72. {
  73. case Default:
  74. return QColor(0x80,0x80,0x80);
  75. case Comment:
  76. return QColor(0x00,0x7f,0x00);
  77. case POD:
  78. return QColor(0x00,0x40,0x00);
  79. case Number:
  80. case FunctionMethodName:
  81. return QColor(0x00,0x7f,0x7f);
  82. case Keyword:
  83. case DemotedKeyword:
  84. return QColor(0x00,0x00,0x7f);
  85. case DoubleQuotedString:
  86. case SingleQuotedString:
  87. case HereDocument:
  88. case PercentStringq:
  89. case PercentStringQ:
  90. return QColor(0x7f,0x00,0x7f);
  91. case ClassName:
  92. return QColor(0x00,0x00,0xff);
  93. case Regex:
  94. case HereDocumentDelimiter:
  95. case PercentStringr:
  96. case PercentStringw:
  97. return QColor(0x00,0x00,0x00);
  98. case Global:
  99. return QColor(0x80,0x00,0x80);
  100. case Symbol:
  101. return QColor(0xc0,0xa0,0x30);
  102. case ModuleName:
  103. return QColor(0xa0,0x00,0xa0);
  104. case InstanceVariable:
  105. return QColor(0xb0,0x00,0x80);
  106. case ClassVariable:
  107. return QColor(0x80,0x00,0xb0);
  108. case Backticks:
  109. case PercentStringx:
  110. return QColor(0xff,0xff,0x00);
  111. case DataSection:
  112. return QColor(0x60,0x00,0x00);
  113. }
  114. return QsciLexer::defaultColor(style);
  115. }
  116. // Returns the end-of-line fill for a style.
  117. bool QsciLexerRuby::defaultEolFill(int style) const
  118. {
  119. bool fill;
  120. switch (style)
  121. {
  122. case POD:
  123. case DataSection:
  124. case HereDocument:
  125. fill = true;
  126. break;
  127. default:
  128. fill = QsciLexer::defaultEolFill(style);
  129. }
  130. return fill;
  131. }
  132. // Returns the font of the text for a style.
  133. QFont QsciLexerRuby::defaultFont(int style) const
  134. {
  135. QFont f;
  136. switch (style)
  137. {
  138. case Comment:
  139. #if defined(Q_OS_WIN)
  140. f = QFont("Comic Sans MS",9);
  141. #elif defined(Q_OS_MAC)
  142. f = QFont("Comic Sans MS", 12);
  143. #else
  144. f = QFont("Bitstream Vera Serif",9);
  145. #endif
  146. break;
  147. case POD:
  148. case DoubleQuotedString:
  149. case SingleQuotedString:
  150. case PercentStringq:
  151. case PercentStringQ:
  152. #if defined(Q_OS_WIN)
  153. f = QFont("Courier New",10);
  154. #elif defined(Q_OS_MAC)
  155. f = QFont("Courier", 12);
  156. #else
  157. f = QFont("Bitstream Vera Sans Mono",9);
  158. #endif
  159. break;
  160. case Keyword:
  161. case ClassName:
  162. case FunctionMethodName:
  163. case Operator:
  164. case ModuleName:
  165. case DemotedKeyword:
  166. f = QsciLexer::defaultFont(style);
  167. f.setBold(true);
  168. break;
  169. default:
  170. f = QsciLexer::defaultFont(style);
  171. }
  172. return f;
  173. }
  174. // Returns the set of keywords.
  175. const char *QsciLexerRuby::keywords(int set) const
  176. {
  177. if (set == 1)
  178. return
  179. "__FILE__ and def end in or self unless __LINE__ "
  180. "begin defined? ensure module redo super until BEGIN "
  181. "break do false next rescue then when END case else "
  182. "for nil require retry true while alias class elsif "
  183. "if not return undef yield";
  184. return 0;
  185. }
  186. // Returns the user name of a style.
  187. QString QsciLexerRuby::description(int style) const
  188. {
  189. switch (style)
  190. {
  191. case Default:
  192. return tr("Default");
  193. case Error:
  194. return tr("Error");
  195. case Comment:
  196. return tr("Comment");
  197. case POD:
  198. return tr("POD");
  199. case Number:
  200. return tr("Number");
  201. case Keyword:
  202. return tr("Keyword");
  203. case DoubleQuotedString:
  204. return tr("Double-quoted string");
  205. case SingleQuotedString:
  206. return tr("Single-quoted string");
  207. case ClassName:
  208. return tr("Class name");
  209. case FunctionMethodName:
  210. return tr("Function or method name");
  211. case Operator:
  212. return tr("Operator");
  213. case Identifier:
  214. return tr("Identifier");
  215. case Regex:
  216. return tr("Regular expression");
  217. case Global:
  218. return tr("Global");
  219. case Symbol:
  220. return tr("Symbol");
  221. case ModuleName:
  222. return tr("Module name");
  223. case InstanceVariable:
  224. return tr("Instance variable");
  225. case ClassVariable:
  226. return tr("Class variable");
  227. case Backticks:
  228. return tr("Backticks");
  229. case DataSection:
  230. return tr("Data section");
  231. case HereDocumentDelimiter:
  232. return tr("Here document delimiter");
  233. case HereDocument:
  234. return tr("Here document");
  235. case PercentStringq:
  236. return tr("%q string");
  237. case PercentStringQ:
  238. return tr("%Q string");
  239. case PercentStringx:
  240. return tr("%x string");
  241. case PercentStringr:
  242. return tr("%r string");
  243. case PercentStringw:
  244. return tr("%w string");
  245. case DemotedKeyword:
  246. return tr("Demoted keyword");
  247. case Stdin:
  248. return tr("stdin");
  249. case Stdout:
  250. return tr("stdout");
  251. case Stderr:
  252. return tr("stderr");
  253. }
  254. return QString();
  255. }
  256. // Returns the background colour of the text for a style.
  257. QColor QsciLexerRuby::defaultPaper(int style) const
  258. {
  259. switch (style)
  260. {
  261. case Error:
  262. return QColor(0xff,0x00,0x00);
  263. case POD:
  264. return QColor(0xc0,0xff,0xc0);
  265. case Regex:
  266. case PercentStringr:
  267. return QColor(0xa0,0xff,0xa0);
  268. case Backticks:
  269. case PercentStringx:
  270. return QColor(0xa0,0x80,0x80);
  271. case DataSection:
  272. return QColor(0xff,0xf0,0xd8);
  273. case HereDocumentDelimiter:
  274. case HereDocument:
  275. return QColor(0xdd,0xd0,0xdd);
  276. case PercentStringw:
  277. return QColor(0xff,0xff,0xe0);
  278. case Stdin:
  279. case Stdout:
  280. case Stderr:
  281. return QColor(0xff,0x80,0x80);
  282. }
  283. return QsciLexer::defaultPaper(style);
  284. }
  285. // Refresh all properties.
  286. void QsciLexerRuby::refreshProperties()
  287. {
  288. setCommentProp();
  289. setCompactProp();
  290. }
  291. // Read properties from the settings.
  292. bool QsciLexerRuby::readProperties(QSettings &qs, const QString &prefix)
  293. {
  294. int rc = true;
  295. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  296. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  297. return rc;
  298. }
  299. // Write properties to the settings.
  300. bool QsciLexerRuby::writeProperties(QSettings &qs, const QString &prefix) const
  301. {
  302. int rc = true;
  303. qs.value(prefix + "foldcomments", fold_comments);
  304. qs.value(prefix + "foldcompact", fold_compact);
  305. return rc;
  306. }
  307. // Set if comments can be folded.
  308. void QsciLexerRuby::setFoldComments(bool fold)
  309. {
  310. fold_comments = fold;
  311. setCommentProp();
  312. }
  313. // Set the "fold.comment" property.
  314. void QsciLexerRuby::setCommentProp()
  315. {
  316. emit propertyChanged("fold.comment", (fold_comments ? "1" : "0"));
  317. }
  318. // Set if folds are compact
  319. void QsciLexerRuby::setFoldCompact(bool fold)
  320. {
  321. fold_compact = fold;
  322. setCompactProp();
  323. }
  324. // Set the "fold.compact" property.
  325. void QsciLexerRuby::setCompactProp()
  326. {
  327. emit propertyChanged("fold.compact", (fold_compact ? "1" : "0"));
  328. }