qscilexercss.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // This module implements the QsciLexerCSS 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/qscilexercss.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerCSS::QsciLexerCSS(QObject *parent)
  25. : QsciLexer(parent),
  26. fold_comments(false), fold_compact(true), hss_language(false),
  27. less_language(false), scss_language(false)
  28. {
  29. }
  30. // The dtor.
  31. QsciLexerCSS::~QsciLexerCSS()
  32. {
  33. }
  34. // Returns the language name.
  35. const char *QsciLexerCSS::language() const
  36. {
  37. return "CSS";
  38. }
  39. // Returns the lexer name.
  40. const char *QsciLexerCSS::lexer() const
  41. {
  42. return "css";
  43. }
  44. // Return the list of characters that can start a block.
  45. const char *QsciLexerCSS::blockStart(int *style) const
  46. {
  47. if (style)
  48. *style = Operator;
  49. return "{";
  50. }
  51. // Return the list of characters that can end a block.
  52. const char *QsciLexerCSS::blockEnd(int *style) const
  53. {
  54. if (style)
  55. *style = Operator;
  56. return "}";
  57. }
  58. // Return the string of characters that comprise a word.
  59. const char *QsciLexerCSS::wordCharacters() const
  60. {
  61. return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-";
  62. }
  63. // Returns the foreground colour of the text for a style.
  64. QColor QsciLexerCSS::defaultColor(int style) const
  65. {
  66. switch (style)
  67. {
  68. case Default:
  69. return QColor(0xff,0x00,0x80);
  70. case Tag:
  71. return QColor(0x00,0x00,0x7f);
  72. case PseudoClass:
  73. case Attribute:
  74. return QColor(0x80,0x00,0x00);
  75. case UnknownPseudoClass:
  76. case UnknownProperty:
  77. return QColor(0xff,0x00,0x00);
  78. case Operator:
  79. return QColor(0x00,0x00,0x00);
  80. case CSS1Property:
  81. return QColor(0x00,0x40,0xe0);
  82. case Value:
  83. case DoubleQuotedString:
  84. case SingleQuotedString:
  85. return QColor(0x7f,0x00,0x7f);
  86. case Comment:
  87. return QColor(0x00,0x7f,0x00);
  88. case IDSelector:
  89. return QColor(0x00,0x7f,0x7f);
  90. case Important:
  91. return QColor(0xff,0x80,0x00);
  92. case AtRule:
  93. case MediaRule:
  94. return QColor(0x7f,0x7f,0x00);
  95. case CSS2Property:
  96. return QColor(0x00,0xa0,0xe0);
  97. }
  98. return QsciLexer::defaultColor(style);
  99. }
  100. // Returns the font of the text for a style.
  101. QFont QsciLexerCSS::defaultFont(int style) const
  102. {
  103. QFont f;
  104. if (style == Comment)
  105. #if defined(Q_OS_WIN)
  106. f = QFont("Comic Sans MS",9);
  107. #elif defined(Q_OS_MAC)
  108. f = QFont("Comic Sans MS", 12);
  109. #else
  110. f = QFont("Bitstream Vera Serif",9);
  111. #endif
  112. else
  113. {
  114. f = QsciLexer::defaultFont(style);
  115. switch (style)
  116. {
  117. case Tag:
  118. case Important:
  119. case MediaRule:
  120. f.setBold(true);
  121. break;
  122. case IDSelector:
  123. f.setItalic(true);
  124. break;
  125. }
  126. }
  127. return f;
  128. }
  129. // Returns the set of keywords.
  130. const char *QsciLexerCSS::keywords(int set) const
  131. {
  132. if (set == 1)
  133. return
  134. "color background-color background-image "
  135. "background-repeat background-attachment "
  136. "background-position background font-family "
  137. "font-style font-variant font-weight font-size font "
  138. "word-spacing letter-spacing text-decoration "
  139. "vertical-align text-transform text-align "
  140. "text-indent line-height margin-top margin-right "
  141. "margin-bottom margin-left margin padding-top "
  142. "padding-right padding-bottom padding-left padding "
  143. "border-top-width border-right-width "
  144. "border-bottom-width border-left-width border-width "
  145. "border-top border-right border-bottom border-left "
  146. "border border-color border-style width height float "
  147. "clear display white-space list-style-type "
  148. "list-style-image list-style-position list-style";
  149. if (set == 2)
  150. return
  151. "first-letter first-line link active visited "
  152. "first-child focus hover lang before after left "
  153. "right first";
  154. if (set == 3)
  155. return
  156. "border-top-color border-right-color "
  157. "border-bottom-color border-left-color border-color "
  158. "border-top-style border-right-style "
  159. "border-bottom-style border-left-style border-style "
  160. "top right bottom left position z-index direction "
  161. "unicode-bidi min-width max-width min-height "
  162. "max-height overflow clip visibility content quotes "
  163. "counter-reset counter-increment marker-offset size "
  164. "marks page-break-before page-break-after "
  165. "page-break-inside page orphans widows font-stretch "
  166. "font-size-adjust unicode-range units-per-em src "
  167. "panose-1 stemv stemh slope cap-height x-height "
  168. "ascent descent widths bbox definition-src baseline "
  169. "centerline mathline topline text-shadow "
  170. "caption-side table-layout border-collapse "
  171. "border-spacing empty-cells speak-header cursor "
  172. "outline outline-width outline-style outline-color "
  173. "volume speak pause-before pause-after pause "
  174. "cue-before cue-after cue play-during azimuth "
  175. "elevation speech-rate voice-family pitch "
  176. "pitch-range stress richness speak-punctuation "
  177. "speak-numeral";
  178. return 0;
  179. }
  180. // Returns the user name of a style.
  181. QString QsciLexerCSS::description(int style) const
  182. {
  183. switch (style)
  184. {
  185. case Default:
  186. return tr("Default");
  187. case Tag:
  188. return tr("Tag");
  189. case ClassSelector:
  190. return tr("Class selector");
  191. case PseudoClass:
  192. return tr("Pseudo-class");
  193. case UnknownPseudoClass:
  194. return tr("Unknown pseudo-class");
  195. case Operator:
  196. return tr("Operator");
  197. case CSS1Property:
  198. return tr("CSS1 property");
  199. case UnknownProperty:
  200. return tr("Unknown property");
  201. case Value:
  202. return tr("Value");
  203. case IDSelector:
  204. return tr("ID selector");
  205. case Important:
  206. return tr("Important");
  207. case AtRule:
  208. return tr("@-rule");
  209. case DoubleQuotedString:
  210. return tr("Double-quoted string");
  211. case SingleQuotedString:
  212. return tr("Single-quoted string");
  213. case CSS2Property:
  214. return tr("CSS2 property");
  215. case Attribute:
  216. return tr("Attribute");
  217. case CSS3Property:
  218. return tr("CSS3 property");
  219. case PseudoElement:
  220. return tr("Pseudo-element");
  221. case ExtendedCSSProperty:
  222. return tr("Extended CSS property");
  223. case ExtendedPseudoClass:
  224. return tr("Extended pseudo-class");
  225. case ExtendedPseudoElement:
  226. return tr("Extended pseudo-element");
  227. case MediaRule:
  228. return tr("Media rule");
  229. case Variable:
  230. return tr("Variable");
  231. }
  232. return QString();
  233. }
  234. // Refresh all properties.
  235. void QsciLexerCSS::refreshProperties()
  236. {
  237. setCommentProp();
  238. setCompactProp();
  239. setHSSProp();
  240. setLessProp();
  241. setSCSSProp();
  242. }
  243. // Read properties from the settings.
  244. bool QsciLexerCSS::readProperties(QSettings &qs,const QString &prefix)
  245. {
  246. int rc = true;
  247. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  248. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  249. hss_language = qs.value(prefix + "hsslanguage", false).toBool();
  250. less_language = qs.value(prefix + "lesslanguage", false).toBool();
  251. scss_language = qs.value(prefix + "scsslanguage", false).toBool();
  252. return rc;
  253. }
  254. // Write properties to the settings.
  255. bool QsciLexerCSS::writeProperties(QSettings &qs,const QString &prefix) const
  256. {
  257. int rc = true;
  258. qs.setValue(prefix + "foldcomments", fold_comments);
  259. qs.setValue(prefix + "foldcompact", fold_compact);
  260. qs.setValue(prefix + "hsslanguage", hss_language);
  261. qs.setValue(prefix + "lesslanguage", less_language);
  262. qs.setValue(prefix + "scsslanguage", scss_language);
  263. return rc;
  264. }
  265. // Return true if comments can be folded.
  266. bool QsciLexerCSS::foldComments() const
  267. {
  268. return fold_comments;
  269. }
  270. // Set if comments can be folded.
  271. void QsciLexerCSS::setFoldComments(bool fold)
  272. {
  273. fold_comments = fold;
  274. setCommentProp();
  275. }
  276. // Set the "fold.comment" property.
  277. void QsciLexerCSS::setCommentProp()
  278. {
  279. emit propertyChanged("fold.comment",(fold_comments ? "1" : "0"));
  280. }
  281. // Return true if folds are compact.
  282. bool QsciLexerCSS::foldCompact() const
  283. {
  284. return fold_compact;
  285. }
  286. // Set if folds are compact
  287. void QsciLexerCSS::setFoldCompact(bool fold)
  288. {
  289. fold_compact = fold;
  290. setCompactProp();
  291. }
  292. // Set the "fold.compact" property.
  293. void QsciLexerCSS::setCompactProp()
  294. {
  295. emit propertyChanged("fold.compact",(fold_compact ? "1" : "0"));
  296. }
  297. // Set if HSS is supported.
  298. void QsciLexerCSS::setHSSLanguage(bool enabled)
  299. {
  300. hss_language = enabled;
  301. setHSSProp();
  302. }
  303. // Set the "lexer.css.hss.language" property.
  304. void QsciLexerCSS::setHSSProp()
  305. {
  306. emit propertyChanged("lexer.css.hss.language",(hss_language ? "1" : "0"));
  307. }
  308. // Set if Less CSS is supported.
  309. void QsciLexerCSS::setLessLanguage(bool enabled)
  310. {
  311. less_language = enabled;
  312. setLessProp();
  313. }
  314. // Set the "lexer.css.less.language" property.
  315. void QsciLexerCSS::setLessProp()
  316. {
  317. emit propertyChanged("lexer.css.less.language",(less_language ? "1" : "0"));
  318. }
  319. // Set if Sassy CSS is supported.
  320. void QsciLexerCSS::setSCSSLanguage(bool enabled)
  321. {
  322. scss_language = enabled;
  323. setSCSSProp();
  324. }
  325. // Set the "lexer.css.scss.language" property.
  326. void QsciLexerCSS::setSCSSProp()
  327. {
  328. emit propertyChanged("lexer.css.scss.language",(scss_language ? "1" : "0"));
  329. }