qscilexerjson.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // This module implements the QsciLexerJSON 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/qscilexerjson.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerJSON::QsciLexerJSON(QObject *parent)
  25. : QsciLexer(parent),
  26. allow_comments(true), escape_sequence(true), fold_compact(true)
  27. {
  28. }
  29. // The dtor.
  30. QsciLexerJSON::~QsciLexerJSON()
  31. {
  32. }
  33. // Returns the language name.
  34. const char *QsciLexerJSON::language() const
  35. {
  36. return "JSON";
  37. }
  38. // Returns the lexer name.
  39. const char *QsciLexerJSON::lexer() const
  40. {
  41. return "json";
  42. }
  43. // Returns the foreground colour of the text for a style.
  44. QColor QsciLexerJSON::defaultColor(int style) const
  45. {
  46. switch (style)
  47. {
  48. case UnclosedString:
  49. case Error:
  50. return QColor(0xff, 0xff, 0xff);
  51. case Number:
  52. return QColor(0x00, 0x7f, 0x7f);
  53. case String:
  54. return QColor(0x7f, 0x00, 0x00);
  55. case Property:
  56. return QColor(0x88, 0x0a, 0xe8);
  57. case EscapeSequence:
  58. return QColor(0x0b, 0x98, 0x2e);
  59. case CommentLine:
  60. case CommentBlock:
  61. return QColor(0x05, 0xbb, 0xae);
  62. case Operator:
  63. return QColor(0x18, 0x64, 0x4a);
  64. case IRI:
  65. return QColor(0x00, 0x00, 0xff);
  66. case IRICompact:
  67. return QColor(0xd1, 0x37, 0xc1);
  68. case Keyword:
  69. return QColor(0x0b, 0xce, 0xa7);
  70. case KeywordLD:
  71. return QColor(0xec, 0x28, 0x06);
  72. }
  73. return QsciLexer::defaultColor(style);
  74. }
  75. // Returns the end-of-line fill for a style.
  76. bool QsciLexerJSON::defaultEolFill(int style) const
  77. {
  78. switch (style)
  79. {
  80. case UnclosedString:
  81. return true;
  82. }
  83. return QsciLexer::defaultEolFill(style);
  84. }
  85. // Returns the font of the text for a style.
  86. QFont QsciLexerJSON::defaultFont(int style) const
  87. {
  88. QFont f;
  89. switch (style)
  90. {
  91. case CommentLine:
  92. f = QsciLexer::defaultFont(style);
  93. f.setItalic(true);
  94. break;
  95. case Keyword:
  96. f = QsciLexer::defaultFont(style);
  97. f.setBold(true);
  98. break;
  99. default:
  100. f = QsciLexer::defaultFont(style);
  101. }
  102. return f;
  103. }
  104. // Returns the set of keywords.
  105. const char *QsciLexerJSON::keywords(int set) const
  106. {
  107. if (set == 1)
  108. return "false true null";
  109. if (set == 2)
  110. return
  111. "@id @context @type @value @language @container @list @set "
  112. "@reverse @index @base @vocab @graph";
  113. return 0;
  114. }
  115. // Returns the user name of a style.
  116. QString QsciLexerJSON::description(int style) const
  117. {
  118. switch (style)
  119. {
  120. case Default:
  121. return tr("Default");
  122. case Number:
  123. return tr("Number");
  124. case String:
  125. return tr("String");
  126. case UnclosedString:
  127. return tr("Unclosed string");
  128. case Property:
  129. return tr("Property");
  130. case EscapeSequence:
  131. return tr("Escape sequence");
  132. case CommentLine:
  133. return tr("Line comment");
  134. case CommentBlock:
  135. return tr("Block comment");
  136. case Operator:
  137. return tr("Operator");
  138. case IRI:
  139. return tr("IRI");
  140. case IRICompact:
  141. return tr("JSON-LD compact IRI");
  142. case Keyword:
  143. return tr("JSON keyword");
  144. case KeywordLD:
  145. return tr("JSON-LD keyword");
  146. case Error:
  147. return tr("Parsing error");
  148. }
  149. return QString();
  150. }
  151. // Returns the background colour of the text for a style.
  152. QColor QsciLexerJSON::defaultPaper(int style) const
  153. {
  154. switch (style)
  155. {
  156. case UnclosedString:
  157. case Error:
  158. return QColor(0xff, 0x00, 0x00);
  159. }
  160. return QsciLexer::defaultPaper(style);
  161. }
  162. // Refresh all properties.
  163. void QsciLexerJSON::refreshProperties()
  164. {
  165. setAllowCommentsProp();
  166. setEscapeSequenceProp();
  167. setCompactProp();
  168. }
  169. // Read properties from the settings.
  170. bool QsciLexerJSON::readProperties(QSettings &qs,const QString &prefix)
  171. {
  172. allow_comments = qs.value(prefix + "allowcomments", true).toBool();
  173. escape_sequence = qs.value(prefix + "escapesequence", true).toBool();
  174. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  175. return true;
  176. }
  177. // Write properties to the settings.
  178. bool QsciLexerJSON::writeProperties(QSettings &qs,const QString &prefix) const
  179. {
  180. qs.setValue(prefix + "allowcomments", allow_comments);
  181. qs.setValue(prefix + "escapesequence", escape_sequence);
  182. qs.setValue(prefix + "foldcompact", fold_compact);
  183. return true;
  184. }
  185. // Set if comments are highlighted
  186. void QsciLexerJSON::setHighlightComments(bool highlight)
  187. {
  188. allow_comments = highlight;
  189. setAllowCommentsProp();
  190. }
  191. // Set the "lexer.json.allow.comments" property.
  192. void QsciLexerJSON::setAllowCommentsProp()
  193. {
  194. emit propertyChanged("lexer.json.allow.comments",
  195. (allow_comments ? "1" : "0"));
  196. }
  197. // Set if escape sequences are highlighted.
  198. void QsciLexerJSON::setHighlightEscapeSequences(bool highlight)
  199. {
  200. escape_sequence = highlight;
  201. setEscapeSequenceProp();
  202. }
  203. // Set the "lexer.json.escape.sequence" property.
  204. void QsciLexerJSON::setEscapeSequenceProp()
  205. {
  206. emit propertyChanged("lexer.json.escape.sequence",
  207. (escape_sequence ? "1" : "0"));
  208. }
  209. // Set if folds are compact.
  210. void QsciLexerJSON::setFoldCompact(bool fold)
  211. {
  212. fold_compact = fold;
  213. setCompactProp();
  214. }
  215. // Set the "fold.compact" property.
  216. void QsciLexerJSON::setCompactProp()
  217. {
  218. emit propertyChanged("fold.compact", (fold_compact ? "1" : "0"));
  219. }