qscilexerlua.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // This module implements the QsciLexerLua 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/qscilexerlua.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerLua::QsciLexerLua(QObject *parent)
  25. : QsciLexer(parent), fold_compact(true)
  26. {
  27. }
  28. // The dtor.
  29. QsciLexerLua::~QsciLexerLua()
  30. {
  31. }
  32. // Returns the language name.
  33. const char *QsciLexerLua::language() const
  34. {
  35. return "Lua";
  36. }
  37. // Returns the lexer name.
  38. const char *QsciLexerLua::lexer() const
  39. {
  40. return "lua";
  41. }
  42. // Return the set of character sequences that can separate auto-completion
  43. // words.
  44. QStringList QsciLexerLua::autoCompletionWordSeparators() const
  45. {
  46. QStringList wl;
  47. wl << ":" << ".";
  48. return wl;
  49. }
  50. // Return the list of characters that can start a block.
  51. const char *QsciLexerLua::blockStart(int *style) const
  52. {
  53. if (style)
  54. *style = Operator;
  55. return "";
  56. }
  57. // Return the style used for braces.
  58. int QsciLexerLua::braceStyle() const
  59. {
  60. return Operator;
  61. }
  62. // Returns the foreground colour of the text for a style.
  63. QColor QsciLexerLua::defaultColor(int style) const
  64. {
  65. switch (style)
  66. {
  67. case Default:
  68. return QColor(0x00,0x00,0x00);
  69. case Comment:
  70. case LineComment:
  71. return QColor(0x00,0x7f,0x00);
  72. case Number:
  73. return QColor(0x00,0x7f,0x7f);
  74. case Keyword:
  75. case BasicFunctions:
  76. case StringTableMathsFunctions:
  77. case CoroutinesIOSystemFacilities:
  78. return QColor(0x00,0x00,0x7f);
  79. case String:
  80. case Character:
  81. case LiteralString:
  82. return QColor(0x7f,0x00,0x7f);
  83. case Preprocessor:
  84. case Label:
  85. return QColor(0x7f,0x7f,0x00);
  86. case Operator:
  87. case Identifier:
  88. break;
  89. }
  90. return QsciLexer::defaultColor(style);
  91. }
  92. // Returns the end-of-line fill for a style.
  93. bool QsciLexerLua::defaultEolFill(int style) const
  94. {
  95. if (style == Comment || style == UnclosedString)
  96. return true;
  97. return QsciLexer::defaultEolFill(style);
  98. }
  99. // Returns the font of the text for a style.
  100. QFont QsciLexerLua::defaultFont(int style) const
  101. {
  102. QFont f;
  103. switch (style)
  104. {
  105. case Comment:
  106. case LineComment:
  107. case LiteralString:
  108. #if defined(Q_OS_WIN)
  109. f = QFont("Comic Sans MS",9);
  110. #elif defined(Q_OS_MAC)
  111. f = QFont("Comic Sans MS", 12);
  112. #else
  113. f = QFont("Bitstream Vera Serif",9);
  114. #endif
  115. break;
  116. default:
  117. f = QsciLexer::defaultFont(style);
  118. }
  119. return f;
  120. }
  121. // Returns the set of keywords.
  122. const char *QsciLexerLua::keywords(int set) const
  123. {
  124. if (set == 1)
  125. // Keywords.
  126. return
  127. "and break do else elseif end false for function if "
  128. "in local nil not or repeat return then true until "
  129. "while";
  130. if (set == 2)
  131. // Basic functions.
  132. return
  133. "_ALERT _ERRORMESSAGE _INPUT _PROMPT _OUTPUT _STDERR "
  134. "_STDIN _STDOUT call dostring foreach foreachi getn "
  135. "globals newtype rawget rawset require sort tinsert "
  136. "tremove "
  137. "G getfenv getmetatable ipairs loadlib next pairs "
  138. "pcall rawegal rawget rawset require setfenv "
  139. "setmetatable xpcall string table math coroutine io "
  140. "os debug";
  141. if (set == 3)
  142. // String, table and maths functions.
  143. return
  144. "abs acos asin atan atan2 ceil cos deg exp floor "
  145. "format frexp gsub ldexp log log10 max min mod rad "
  146. "random randomseed sin sqrt strbyte strchar strfind "
  147. "strlen strlower strrep strsub strupper tan "
  148. "string.byte string.char string.dump string.find "
  149. "string.len string.lower string.rep string.sub "
  150. "string.upper string.format string.gfind string.gsub "
  151. "table.concat table.foreach table.foreachi table.getn "
  152. "table.sort table.insert table.remove table.setn "
  153. "math.abs math.acos math.asin math.atan math.atan2 "
  154. "math.ceil math.cos math.deg math.exp math.floor "
  155. "math.frexp math.ldexp math.log math.log10 math.max "
  156. "math.min math.mod math.pi math.rad math.random "
  157. "math.randomseed math.sin math.sqrt math.tan";
  158. if (set == 4)
  159. // Coroutine, I/O and system facilities.
  160. return
  161. "openfile closefile readfrom writeto appendto remove "
  162. "rename flush seek tmpfile tmpname read write clock "
  163. "date difftime execute exit getenv setlocale time "
  164. "coroutine.create coroutine.resume coroutine.status "
  165. "coroutine.wrap coroutine.yield io.close io.flush "
  166. "io.input io.lines io.open io.output io.read "
  167. "io.tmpfile io.type io.write io.stdin io.stdout "
  168. "io.stderr os.clock os.date os.difftime os.execute "
  169. "os.exit os.getenv os.remove os.rename os.setlocale "
  170. "os.time os.tmpname";
  171. return 0;
  172. }
  173. // Returns the user name of a style.
  174. QString QsciLexerLua::description(int style) const
  175. {
  176. switch (style)
  177. {
  178. case Default:
  179. return tr("Default");
  180. case Comment:
  181. return tr("Comment");
  182. case LineComment:
  183. return tr("Line comment");
  184. case Number:
  185. return tr("Number");
  186. case Keyword:
  187. return tr("Keyword");
  188. case String:
  189. return tr("String");
  190. case Character:
  191. return tr("Character");
  192. case LiteralString:
  193. return tr("Literal string");
  194. case Preprocessor:
  195. return tr("Preprocessor");
  196. case Operator:
  197. return tr("Operator");
  198. case Identifier:
  199. return tr("Identifier");
  200. case UnclosedString:
  201. return tr("Unclosed string");
  202. case BasicFunctions:
  203. return tr("Basic functions");
  204. case StringTableMathsFunctions:
  205. return tr("String, table and maths functions");
  206. case CoroutinesIOSystemFacilities:
  207. return tr("Coroutines, i/o and system facilities");
  208. case KeywordSet5:
  209. return tr("User defined 1");
  210. case KeywordSet6:
  211. return tr("User defined 2");
  212. case KeywordSet7:
  213. return tr("User defined 3");
  214. case KeywordSet8:
  215. return tr("User defined 4");
  216. case Label:
  217. return tr("Label");
  218. }
  219. return QString();
  220. }
  221. // Returns the background colour of the text for a style.
  222. QColor QsciLexerLua::defaultPaper(int style) const
  223. {
  224. switch (style)
  225. {
  226. case Comment:
  227. return QColor(0xd0,0xf0,0xf0);
  228. case LiteralString:
  229. return QColor(0xe0,0xff,0xff);
  230. case UnclosedString:
  231. return QColor(0xe0,0xc0,0xe0);
  232. case BasicFunctions:
  233. return QColor(0xd0,0xff,0xd0);
  234. case StringTableMathsFunctions:
  235. return QColor(0xd0,0xd0,0xff);
  236. case CoroutinesIOSystemFacilities:
  237. return QColor(0xff,0xd0,0xd0);
  238. }
  239. return QsciLexer::defaultPaper(style);
  240. }
  241. // Refresh all properties.
  242. void QsciLexerLua::refreshProperties()
  243. {
  244. setCompactProp();
  245. }
  246. // Read properties from the settings.
  247. bool QsciLexerLua::readProperties(QSettings &qs,const QString &prefix)
  248. {
  249. int rc = true;
  250. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  251. return rc;
  252. }
  253. // Write properties to the settings.
  254. bool QsciLexerLua::writeProperties(QSettings &qs,const QString &prefix) const
  255. {
  256. int rc = true;
  257. qs.setValue(prefix + "foldcompact", fold_compact);
  258. return rc;
  259. }
  260. // Return true if folds are compact.
  261. bool QsciLexerLua::foldCompact() const
  262. {
  263. return fold_compact;
  264. }
  265. // Set if folds are compact.
  266. void QsciLexerLua::setFoldCompact(bool fold)
  267. {
  268. fold_compact = fold;
  269. setCompactProp();
  270. }
  271. // Set the "fold.compact" property.
  272. void QsciLexerLua::setCompactProp()
  273. {
  274. emit propertyChanged("fold.compact",(fold_compact ? "1" : "0"));
  275. }