qscilexeryaml.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // This module implements the QsciLexerYAML 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/qscilexeryaml.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerYAML::QsciLexerYAML(QObject *parent)
  25. : QsciLexer(parent), fold_comments(false)
  26. {
  27. }
  28. // The dtor.
  29. QsciLexerYAML::~QsciLexerYAML()
  30. {
  31. }
  32. // Returns the language name.
  33. const char *QsciLexerYAML::language() const
  34. {
  35. return "YAML";
  36. }
  37. // Returns the lexer name.
  38. const char *QsciLexerYAML::lexer() const
  39. {
  40. return "yaml";
  41. }
  42. // Returns the foreground colour of the text for a style.
  43. QColor QsciLexerYAML::defaultColor(int style) const
  44. {
  45. switch (style)
  46. {
  47. case Default:
  48. return QColor(0x00,0x00,0x00);
  49. case Comment:
  50. return QColor(0x00,0x88,0x00);
  51. case Identifier:
  52. return QColor(0x00,0x00,0x88);
  53. case Keyword:
  54. return QColor(0x88,0x00,0x88);
  55. case Number:
  56. return QColor(0x88,0x00,0x00);
  57. case Reference:
  58. return QColor(0x00,0x88,0x88);
  59. case DocumentDelimiter:
  60. case SyntaxErrorMarker:
  61. return QColor(0xff,0xff,0xff);
  62. case TextBlockMarker:
  63. return QColor(0x33,0x33,0x66);
  64. }
  65. return QsciLexer::defaultColor(style);
  66. }
  67. // Returns the end-of-line fill for a style.
  68. bool QsciLexerYAML::defaultEolFill(int style) const
  69. {
  70. if (style == DocumentDelimiter || style == SyntaxErrorMarker)
  71. return true;
  72. return QsciLexer::defaultEolFill(style);
  73. }
  74. // Returns the font of the text for a style.
  75. QFont QsciLexerYAML::defaultFont(int style) const
  76. {
  77. QFont f;
  78. switch (style)
  79. {
  80. case Default:
  81. case TextBlockMarker:
  82. #if defined(Q_OS_WIN)
  83. f = QFont("Times New Roman", 11);
  84. #elif defined(Q_OS_MAC)
  85. f = QFont("Times New Roman", 12);
  86. #else
  87. f = QFont("Bitstream Charter", 10);
  88. #endif
  89. break;
  90. case Identifier:
  91. f = QsciLexer::defaultFont(style);
  92. f.setBold(true);
  93. break;
  94. case DocumentDelimiter:
  95. #if defined(Q_OS_WIN)
  96. f = QFont("Comic Sans MS",9);
  97. #elif defined(Q_OS_MAC)
  98. f = QFont("Comic Sans MS", 12);
  99. #else
  100. f = QFont("Bitstream Vera Serif",9);
  101. #endif
  102. f.setBold(true);
  103. break;
  104. case SyntaxErrorMarker:
  105. #if defined(Q_OS_WIN)
  106. f = QFont("Times New Roman", 11);
  107. #elif defined(Q_OS_MAC)
  108. f = QFont("Times New Roman", 12);
  109. #else
  110. f = QFont("Bitstream Charter", 10);
  111. #endif
  112. f.setBold(true);
  113. f.setItalic(true);
  114. break;
  115. default:
  116. f = QsciLexer::defaultFont(style);
  117. }
  118. return f;
  119. }
  120. // Returns the set of keywords.
  121. const char *QsciLexerYAML::keywords(int set) const
  122. {
  123. if (set == 1)
  124. return "true false yes no";
  125. return 0;
  126. }
  127. // Returns the user name of a style.
  128. QString QsciLexerYAML::description(int style) const
  129. {
  130. switch (style)
  131. {
  132. case Default:
  133. return tr("Default");
  134. case Comment:
  135. return tr("Comment");
  136. case Identifier:
  137. return tr("Identifier");
  138. case Keyword:
  139. return tr("Keyword");
  140. case Number:
  141. return tr("Number");
  142. case Reference:
  143. return tr("Reference");
  144. case DocumentDelimiter:
  145. return tr("Document delimiter");
  146. case TextBlockMarker:
  147. return tr("Text block marker");
  148. case SyntaxErrorMarker:
  149. return tr("Syntax error marker");
  150. case Operator:
  151. return tr("Operator");
  152. }
  153. return QString();
  154. }
  155. // Returns the background colour of the text for a style.
  156. QColor QsciLexerYAML::defaultPaper(int style) const
  157. {
  158. switch (style)
  159. {
  160. case DocumentDelimiter:
  161. return QColor(0x00,0x00,0x88);
  162. case SyntaxErrorMarker:
  163. return QColor(0xff,0x00,0x00);
  164. }
  165. return QsciLexer::defaultPaper(style);
  166. }
  167. // Refresh all properties.
  168. void QsciLexerYAML::refreshProperties()
  169. {
  170. setCommentProp();
  171. }
  172. // Read properties from the settings.
  173. bool QsciLexerYAML::readProperties(QSettings &qs,const QString &prefix)
  174. {
  175. int rc = true;
  176. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  177. return rc;
  178. }
  179. // Write properties to the settings.
  180. bool QsciLexerYAML::writeProperties(QSettings &qs,const QString &prefix) const
  181. {
  182. int rc = true;
  183. qs.setValue(prefix + "foldcomments", fold_comments);
  184. return rc;
  185. }
  186. // Return true if comments can be folded.
  187. bool QsciLexerYAML::foldComments() const
  188. {
  189. return fold_comments;
  190. }
  191. // Set if comments can be folded.
  192. void QsciLexerYAML::setFoldComments(bool fold)
  193. {
  194. fold_comments = fold;
  195. setCommentProp();
  196. }
  197. // Set the "fold.comment.yaml" property.
  198. void QsciLexerYAML::setCommentProp()
  199. {
  200. emit propertyChanged("fold.comment.yaml",(fold_comments ? "1" : "0"));
  201. }