qscilexer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // This module implements the QsciLexer 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/qscilexer.h"
  20. #include <qapplication.h>
  21. #include <qcolor.h>
  22. #include <qfont.h>
  23. #include <qsettings.h>
  24. #include "Qsci/qsciapis.h"
  25. #include "Qsci/qsciscintilla.h"
  26. #include "Qsci/qsciscintillabase.h"
  27. // The ctor.
  28. QsciLexer::QsciLexer(QObject *parent)
  29. : QObject(parent),
  30. autoIndStyle(-1), apiSet(0), attached_editor(0)
  31. {
  32. #if defined(Q_OS_WIN)
  33. defFont = QFont("Verdana", 10);
  34. #elif defined(Q_OS_MAC)
  35. defFont = QFont("Menlo", 12);
  36. #else
  37. defFont = QFont("Bitstream Vera Sans", 9);
  38. #endif
  39. // Set the default fore and background colours.
  40. QPalette pal = QApplication::palette();
  41. defColor = pal.text().color();
  42. defPaper = pal.base().color();
  43. // Putting this on the heap means we can keep the style getters const.
  44. style_map = new StyleDataMap;
  45. style_map->style_data_set = false;
  46. }
  47. // The dtor.
  48. QsciLexer::~QsciLexer()
  49. {
  50. delete style_map;
  51. }
  52. // Set the attached editor.
  53. void QsciLexer::setEditor(QsciScintilla *editor)
  54. {
  55. attached_editor = editor;
  56. if (attached_editor)
  57. {
  58. attached_editor->SendScintilla(QsciScintillaBase::SCI_SETSTYLEBITS,
  59. styleBitsNeeded());
  60. }
  61. }
  62. // Return the lexer name.
  63. const char *QsciLexer::lexer() const
  64. {
  65. return 0;
  66. }
  67. // Return the lexer identifier.
  68. int QsciLexer::lexerId() const
  69. {
  70. return QsciScintillaBase::SCLEX_CONTAINER;
  71. }
  72. // Return the number of style bits needed by the lexer.
  73. int QsciLexer::styleBitsNeeded() const
  74. {
  75. if (!attached_editor)
  76. return 5;
  77. return attached_editor->SendScintilla(QsciScintillaBase::SCI_GETSTYLEBITSNEEDED);
  78. }
  79. // Make sure the style defaults have been set.
  80. void QsciLexer::setStyleDefaults() const
  81. {
  82. if (!style_map->style_data_set)
  83. {
  84. for (int i = 0; i < 128; ++i)
  85. if (!description(i).isEmpty())
  86. styleData(i);
  87. style_map->style_data_set = true;
  88. }
  89. }
  90. // Return a reference to a style's data, setting up the defaults if needed.
  91. QsciLexer::StyleData &QsciLexer::styleData(int style) const
  92. {
  93. StyleData &sd = style_map->style_data[style];
  94. // See if this is a new style by checking if the colour is valid.
  95. if (!sd.color.isValid())
  96. {
  97. sd.color = defaultColor(style);
  98. sd.paper = defaultPaper(style);
  99. sd.font = defaultFont(style);
  100. sd.eol_fill = defaultEolFill(style);
  101. }
  102. return sd;
  103. }
  104. // Set the APIs associated with the lexer.
  105. void QsciLexer::setAPIs(QsciAbstractAPIs *apis)
  106. {
  107. apiSet = apis;
  108. }
  109. // Return a pointer to the current APIs if there are any.
  110. QsciAbstractAPIs *QsciLexer::apis() const
  111. {
  112. return apiSet;
  113. }
  114. // Default implementation to return the set of fill up characters that can end
  115. // auto-completion.
  116. const char *QsciLexer::autoCompletionFillups() const
  117. {
  118. return "(";
  119. }
  120. // Default implementation to return the view used for indentation guides.
  121. int QsciLexer::indentationGuideView() const
  122. {
  123. return QsciScintillaBase::SC_IV_LOOKBOTH;
  124. }
  125. // Default implementation to return the list of character sequences that can
  126. // separate auto-completion words.
  127. QStringList QsciLexer::autoCompletionWordSeparators() const
  128. {
  129. return QStringList();
  130. }
  131. // Default implementation to return the list of keywords that can start a
  132. // block.
  133. const char *QsciLexer::blockStartKeyword(int *) const
  134. {
  135. return 0;
  136. }
  137. // Default implementation to return the list of characters that can start a
  138. // block.
  139. const char *QsciLexer::blockStart(int *) const
  140. {
  141. return 0;
  142. }
  143. // Default implementation to return the list of characters that can end a
  144. // block.
  145. const char *QsciLexer::blockEnd(int *) const
  146. {
  147. return 0;
  148. }
  149. // Default implementation to return the style used for braces.
  150. int QsciLexer::braceStyle() const
  151. {
  152. return -1;
  153. }
  154. // Default implementation to return the number of lines to look back when
  155. // auto-indenting.
  156. int QsciLexer::blockLookback() const
  157. {
  158. return 20;
  159. }
  160. // Default implementation to return the case sensitivity of the language.
  161. bool QsciLexer::caseSensitive() const
  162. {
  163. return true;
  164. }
  165. // Default implementation to return the characters that make up a word.
  166. const char *QsciLexer::wordCharacters() const
  167. {
  168. return 0;
  169. }
  170. // Default implementation to return the style used for whitespace.
  171. int QsciLexer::defaultStyle() const
  172. {
  173. return 0;
  174. }
  175. // Returns the foreground colour of the text for a style.
  176. QColor QsciLexer::color(int style) const
  177. {
  178. return styleData(style).color;
  179. }
  180. // Returns the background colour of the text for a style.
  181. QColor QsciLexer::paper(int style) const
  182. {
  183. return styleData(style).paper;
  184. }
  185. // Returns the font for a style.
  186. QFont QsciLexer::font(int style) const
  187. {
  188. return styleData(style).font;
  189. }
  190. // Returns the end-of-line fill for a style.
  191. bool QsciLexer::eolFill(int style) const
  192. {
  193. return styleData(style).eol_fill;
  194. }
  195. // Returns the set of keywords.
  196. const char *QsciLexer::keywords(int) const
  197. {
  198. return 0;
  199. }
  200. // Returns the default EOL fill for a style.
  201. bool QsciLexer::defaultEolFill(int) const
  202. {
  203. return false;
  204. }
  205. // Returns the default font for a style.
  206. QFont QsciLexer::defaultFont(int) const
  207. {
  208. return defaultFont();
  209. }
  210. // Returns the default font.
  211. QFont QsciLexer::defaultFont() const
  212. {
  213. return defFont;
  214. }
  215. // Sets the default font.
  216. void QsciLexer::setDefaultFont(const QFont &f)
  217. {
  218. defFont = f;
  219. }
  220. // Returns the default text colour for a style.
  221. QColor QsciLexer::defaultColor(int) const
  222. {
  223. return defaultColor();
  224. }
  225. // Returns the default text colour.
  226. QColor QsciLexer::defaultColor() const
  227. {
  228. return defColor;
  229. }
  230. // Sets the default text colour.
  231. void QsciLexer::setDefaultColor(const QColor &c)
  232. {
  233. defColor = c;
  234. }
  235. // Returns the default paper colour for a styles.
  236. QColor QsciLexer::defaultPaper(int) const
  237. {
  238. return defaultPaper();
  239. }
  240. // Returns the default paper colour.
  241. QColor QsciLexer::defaultPaper() const
  242. {
  243. return defPaper;
  244. }
  245. // Sets the default paper colour.
  246. void QsciLexer::setDefaultPaper(const QColor &c)
  247. {
  248. defPaper = c;
  249. // Normally the default values are only intended to provide defaults when a
  250. // lexer is first setup because once a style has been referenced then a
  251. // copy of the default is made. However the default paper is a special
  252. // case because there is no other way to set the background colour used
  253. // where there is no text. Therefore we also actively set it.
  254. setPaper(c, QsciScintillaBase::STYLE_DEFAULT);
  255. }
  256. // Read properties from the settings.
  257. bool QsciLexer::readProperties(QSettings &,const QString &)
  258. {
  259. return true;
  260. }
  261. // Refresh all properties.
  262. void QsciLexer::refreshProperties()
  263. {
  264. }
  265. // Write properties to the settings.
  266. bool QsciLexer::writeProperties(QSettings &,const QString &) const
  267. {
  268. return true;
  269. }
  270. // Restore the user settings.
  271. bool QsciLexer::readSettings(QSettings &qs,const char *prefix)
  272. {
  273. bool ok, flag, rc = true;
  274. int num;
  275. QString key, full_key;
  276. QStringList fdesc;
  277. setStyleDefaults();
  278. // Read the styles.
  279. for (int i = 0; i < 128; ++i)
  280. {
  281. // Ignore invalid styles.
  282. if (description(i).isEmpty())
  283. continue;
  284. key.sprintf("%s/%s/style%d/",prefix,language(),i);
  285. // Read the foreground colour.
  286. full_key = key + "color";
  287. ok = qs.contains(full_key);
  288. num = qs.value(full_key).toInt();
  289. if (ok)
  290. setColor(QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i);
  291. else
  292. rc = false;
  293. // Read the end-of-line fill.
  294. full_key = key + "eolfill";
  295. ok = qs.contains(full_key);
  296. flag = qs.value(full_key, false).toBool();
  297. if (ok)
  298. setEolFill(flag, i);
  299. else
  300. rc = false;
  301. // Read the font. First try the deprecated format that uses an integer
  302. // point size.
  303. full_key = key + "font";
  304. ok = qs.contains(full_key);
  305. fdesc = qs.value(full_key).toStringList();
  306. if (ok && fdesc.count() == 5)
  307. {
  308. QFont f;
  309. f.setFamily(fdesc[0]);
  310. f.setPointSize(fdesc[1].toInt());
  311. f.setBold(fdesc[2].toInt());
  312. f.setItalic(fdesc[3].toInt());
  313. f.setUnderline(fdesc[4].toInt());
  314. setFont(f, i);
  315. }
  316. else
  317. rc = false;
  318. // Now try the newer font format that uses a floating point point size.
  319. // It is not an error if it doesn't exist.
  320. full_key = key + "font2";
  321. ok = qs.contains(full_key);
  322. fdesc = qs.value(full_key).toStringList();
  323. if (ok)
  324. {
  325. // Allow for future versions with more fields.
  326. if (fdesc.count() >= 5)
  327. {
  328. QFont f;
  329. f.setFamily(fdesc[0]);
  330. f.setPointSizeF(fdesc[1].toDouble());
  331. f.setBold(fdesc[2].toInt());
  332. f.setItalic(fdesc[3].toInt());
  333. f.setUnderline(fdesc[4].toInt());
  334. setFont(f, i);
  335. }
  336. else
  337. {
  338. rc = false;
  339. }
  340. }
  341. // Read the background colour.
  342. full_key = key + "paper";
  343. ok = qs.contains(full_key);
  344. num = qs.value(full_key).toInt();
  345. if (ok)
  346. setPaper(QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i);
  347. else
  348. rc = false;
  349. }
  350. // Read the properties.
  351. key.sprintf("%s/%s/properties/",prefix,language());
  352. if (!readProperties(qs,key))
  353. rc = false;
  354. refreshProperties();
  355. // Read the rest.
  356. key.sprintf("%s/%s/",prefix,language());
  357. // Read the default foreground colour.
  358. full_key = key + "defaultcolor";
  359. ok = qs.contains(full_key);
  360. num = qs.value(full_key).toInt();
  361. if (ok)
  362. setDefaultColor(QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff));
  363. else
  364. rc = false;
  365. // Read the default background colour.
  366. full_key = key + "defaultpaper";
  367. ok = qs.contains(full_key);
  368. num = qs.value(full_key).toInt();
  369. if (ok)
  370. setDefaultPaper(QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff));
  371. else
  372. rc = false;
  373. // Read the default font. First try the deprecated format that uses an
  374. // integer point size.
  375. full_key = key + "defaultfont";
  376. ok = qs.contains(full_key);
  377. fdesc = qs.value(full_key).toStringList();
  378. if (ok && fdesc.count() == 5)
  379. {
  380. QFont f;
  381. f.setFamily(fdesc[0]);
  382. f.setPointSize(fdesc[1].toInt());
  383. f.setBold(fdesc[2].toInt());
  384. f.setItalic(fdesc[3].toInt());
  385. f.setUnderline(fdesc[4].toInt());
  386. setDefaultFont(f);
  387. }
  388. else
  389. rc = false;
  390. // Now try the newer font format that uses a floating point point size. It
  391. // is not an error if it doesn't exist.
  392. full_key = key + "defaultfont2";
  393. ok = qs.contains(full_key);
  394. fdesc = qs.value(full_key).toStringList();
  395. if (ok)
  396. {
  397. // Allow for future versions with more fields.
  398. if (fdesc.count() >= 5)
  399. {
  400. QFont f;
  401. f.setFamily(fdesc[0]);
  402. f.setPointSizeF(fdesc[1].toDouble());
  403. f.setBold(fdesc[2].toInt());
  404. f.setItalic(fdesc[3].toInt());
  405. f.setUnderline(fdesc[4].toInt());
  406. setDefaultFont(f);
  407. }
  408. else
  409. {
  410. rc = false;
  411. }
  412. }
  413. full_key = key + "autoindentstyle";
  414. ok = qs.contains(full_key);
  415. num = qs.value(full_key).toInt();
  416. if (ok)
  417. setAutoIndentStyle(num);
  418. else
  419. rc = false;
  420. return rc;
  421. }
  422. // Save the user settings.
  423. bool QsciLexer::writeSettings(QSettings &qs,const char *prefix) const
  424. {
  425. bool rc = true;
  426. QString key, fmt("%1");
  427. int num;
  428. QStringList fdesc;
  429. setStyleDefaults();
  430. // Write the styles.
  431. for (int i = 0; i < 128; ++i)
  432. {
  433. // Ignore invalid styles.
  434. if (description(i).isEmpty())
  435. continue;
  436. QColor c;
  437. key.sprintf("%s/%s/style%d/",prefix,language(),i);
  438. // Write the foreground colour.
  439. c = color(i);
  440. num = (c.red() << 16) | (c.green() << 8) | c.blue();
  441. qs.setValue(key + "color", num);
  442. // Write the end-of-line fill.
  443. qs.setValue(key + "eolfill", eolFill(i));
  444. // Write the font using the deprecated format.
  445. QFont f = font(i);
  446. fdesc.clear();
  447. fdesc += f.family();
  448. fdesc += fmt.arg(f.pointSize());
  449. // The casts are for Borland.
  450. fdesc += fmt.arg((int)f.bold());
  451. fdesc += fmt.arg((int)f.italic());
  452. fdesc += fmt.arg((int)f.underline());
  453. qs.setValue(key + "font", fdesc);
  454. // Write the font using the newer format.
  455. fdesc[1] = fmt.arg(f.pointSizeF());
  456. qs.setValue(key + "font2", fdesc);
  457. // Write the background colour.
  458. c = paper(i);
  459. num = (c.red() << 16) | (c.green() << 8) | c.blue();
  460. qs.setValue(key + "paper", num);
  461. }
  462. // Write the properties.
  463. key.sprintf("%s/%s/properties/",prefix,language());
  464. if (!writeProperties(qs,key))
  465. rc = false;
  466. // Write the rest.
  467. key.sprintf("%s/%s/",prefix,language());
  468. // Write the default foreground colour.
  469. num = (defColor.red() << 16) | (defColor.green() << 8) | defColor.blue();
  470. qs.setValue(key + "defaultcolor", num);
  471. // Write the default background colour.
  472. num = (defPaper.red() << 16) | (defPaper.green() << 8) | defPaper.blue();
  473. qs.setValue(key + "defaultpaper", num);
  474. // Write the default font using the deprecated format.
  475. fdesc.clear();
  476. fdesc += defFont.family();
  477. fdesc += fmt.arg(defFont.pointSize());
  478. // The casts are for Borland.
  479. fdesc += fmt.arg((int)defFont.bold());
  480. fdesc += fmt.arg((int)defFont.italic());
  481. fdesc += fmt.arg((int)defFont.underline());
  482. qs.setValue(key + "defaultfont", fdesc);
  483. // Write the font using the newer format.
  484. fdesc[1] = fmt.arg(defFont.pointSizeF());
  485. qs.setValue(key + "defaultfont2", fdesc);
  486. qs.setValue(key + "autoindentstyle", autoIndStyle);
  487. return rc;
  488. }
  489. // Return the auto-indentation style.
  490. int QsciLexer::autoIndentStyle()
  491. {
  492. // We can't do this in the ctor because we want the virtuals to work.
  493. if (autoIndStyle < 0)
  494. autoIndStyle = (blockStartKeyword() || blockStart() || blockEnd()) ?
  495. 0 : QsciScintilla::AiMaintain;
  496. return autoIndStyle;
  497. }
  498. // Set the auto-indentation style.
  499. void QsciLexer::setAutoIndentStyle(int autoindentstyle)
  500. {
  501. autoIndStyle = autoindentstyle;
  502. }
  503. // Set the foreground colour for a style.
  504. void QsciLexer::setColor(const QColor &c, int style)
  505. {
  506. if (style >= 0)
  507. {
  508. styleData(style).color = c;
  509. emit colorChanged(c, style);
  510. }
  511. else
  512. for (int i = 0; i < 128; ++i)
  513. if (!description(i).isEmpty())
  514. setColor(c, i);
  515. }
  516. // Set the end-of-line fill for a style.
  517. void QsciLexer::setEolFill(bool eolfill, int style)
  518. {
  519. if (style >= 0)
  520. {
  521. styleData(style).eol_fill = eolfill;
  522. emit eolFillChanged(eolfill, style);
  523. }
  524. else
  525. for (int i = 0; i < 128; ++i)
  526. if (!description(i).isEmpty())
  527. setEolFill(eolfill, i);
  528. }
  529. // Set the font for a style.
  530. void QsciLexer::setFont(const QFont &f, int style)
  531. {
  532. if (style >= 0)
  533. {
  534. styleData(style).font = f;
  535. emit fontChanged(f, style);
  536. }
  537. else
  538. for (int i = 0; i < 128; ++i)
  539. if (!description(i).isEmpty())
  540. setFont(f, i);
  541. }
  542. // Set the background colour for a style.
  543. void QsciLexer::setPaper(const QColor &c, int style)
  544. {
  545. if (style >= 0)
  546. {
  547. styleData(style).paper = c;
  548. emit paperChanged(c, style);
  549. }
  550. else
  551. {
  552. for (int i = 0; i < 128; ++i)
  553. if (!description(i).isEmpty())
  554. setPaper(c, i);
  555. emit paperChanged(c, QsciScintillaBase::STYLE_DEFAULT);
  556. }
  557. }