b3ee75e9252987e4412bda0534ac231ae9c0e4bbb3fd63fc104507608becb1f7cf48e76bfdb70f303c8463a9197fb69a1ab50dec281ffebb0ea32df81b6b4e 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { Color } from '../../../../base/common/color.js';
  6. export class ParsedTokenThemeRule {
  7. constructor(token, index, fontStyle, foreground, background) {
  8. this._parsedThemeRuleBrand = undefined;
  9. this.token = token;
  10. this.index = index;
  11. this.fontStyle = fontStyle;
  12. this.foreground = foreground;
  13. this.background = background;
  14. }
  15. }
  16. /**
  17. * Parse a raw theme into rules.
  18. */
  19. export function parseTokenTheme(source) {
  20. if (!source || !Array.isArray(source)) {
  21. return [];
  22. }
  23. const result = [];
  24. let resultLen = 0;
  25. for (let i = 0, len = source.length; i < len; i++) {
  26. const entry = source[i];
  27. let fontStyle = -1 /* FontStyle.NotSet */;
  28. if (typeof entry.fontStyle === 'string') {
  29. fontStyle = 0 /* FontStyle.None */;
  30. const segments = entry.fontStyle.split(' ');
  31. for (let j = 0, lenJ = segments.length; j < lenJ; j++) {
  32. const segment = segments[j];
  33. switch (segment) {
  34. case 'italic':
  35. fontStyle = fontStyle | 1 /* FontStyle.Italic */;
  36. break;
  37. case 'bold':
  38. fontStyle = fontStyle | 2 /* FontStyle.Bold */;
  39. break;
  40. case 'underline':
  41. fontStyle = fontStyle | 4 /* FontStyle.Underline */;
  42. break;
  43. case 'strikethrough':
  44. fontStyle = fontStyle | 8 /* FontStyle.Strikethrough */;
  45. break;
  46. }
  47. }
  48. }
  49. let foreground = null;
  50. if (typeof entry.foreground === 'string') {
  51. foreground = entry.foreground;
  52. }
  53. let background = null;
  54. if (typeof entry.background === 'string') {
  55. background = entry.background;
  56. }
  57. result[resultLen++] = new ParsedTokenThemeRule(entry.token || '', i, fontStyle, foreground, background);
  58. }
  59. return result;
  60. }
  61. /**
  62. * Resolve rules (i.e. inheritance).
  63. */
  64. function resolveParsedTokenThemeRules(parsedThemeRules, customTokenColors) {
  65. // Sort rules lexicographically, and then by index if necessary
  66. parsedThemeRules.sort((a, b) => {
  67. const r = strcmp(a.token, b.token);
  68. if (r !== 0) {
  69. return r;
  70. }
  71. return a.index - b.index;
  72. });
  73. // Determine defaults
  74. let defaultFontStyle = 0 /* FontStyle.None */;
  75. let defaultForeground = '000000';
  76. let defaultBackground = 'ffffff';
  77. while (parsedThemeRules.length >= 1 && parsedThemeRules[0].token === '') {
  78. const incomingDefaults = parsedThemeRules.shift();
  79. if (incomingDefaults.fontStyle !== -1 /* FontStyle.NotSet */) {
  80. defaultFontStyle = incomingDefaults.fontStyle;
  81. }
  82. if (incomingDefaults.foreground !== null) {
  83. defaultForeground = incomingDefaults.foreground;
  84. }
  85. if (incomingDefaults.background !== null) {
  86. defaultBackground = incomingDefaults.background;
  87. }
  88. }
  89. const colorMap = new ColorMap();
  90. // start with token colors from custom token themes
  91. for (const color of customTokenColors) {
  92. colorMap.getId(color);
  93. }
  94. const foregroundColorId = colorMap.getId(defaultForeground);
  95. const backgroundColorId = colorMap.getId(defaultBackground);
  96. const defaults = new ThemeTrieElementRule(defaultFontStyle, foregroundColorId, backgroundColorId);
  97. const root = new ThemeTrieElement(defaults);
  98. for (let i = 0, len = parsedThemeRules.length; i < len; i++) {
  99. const rule = parsedThemeRules[i];
  100. root.insert(rule.token, rule.fontStyle, colorMap.getId(rule.foreground), colorMap.getId(rule.background));
  101. }
  102. return new TokenTheme(colorMap, root);
  103. }
  104. const colorRegExp = /^#?([0-9A-Fa-f]{6})([0-9A-Fa-f]{2})?$/;
  105. export class ColorMap {
  106. constructor() {
  107. this._lastColorId = 0;
  108. this._id2color = [];
  109. this._color2id = new Map();
  110. }
  111. getId(color) {
  112. if (color === null) {
  113. return 0;
  114. }
  115. const match = color.match(colorRegExp);
  116. if (!match) {
  117. throw new Error('Illegal value for token color: ' + color);
  118. }
  119. color = match[1].toUpperCase();
  120. let value = this._color2id.get(color);
  121. if (value) {
  122. return value;
  123. }
  124. value = ++this._lastColorId;
  125. this._color2id.set(color, value);
  126. this._id2color[value] = Color.fromHex('#' + color);
  127. return value;
  128. }
  129. getColorMap() {
  130. return this._id2color.slice(0);
  131. }
  132. }
  133. export class TokenTheme {
  134. constructor(colorMap, root) {
  135. this._colorMap = colorMap;
  136. this._root = root;
  137. this._cache = new Map();
  138. }
  139. static createFromRawTokenTheme(source, customTokenColors) {
  140. return this.createFromParsedTokenTheme(parseTokenTheme(source), customTokenColors);
  141. }
  142. static createFromParsedTokenTheme(source, customTokenColors) {
  143. return resolveParsedTokenThemeRules(source, customTokenColors);
  144. }
  145. getColorMap() {
  146. return this._colorMap.getColorMap();
  147. }
  148. _match(token) {
  149. return this._root.match(token);
  150. }
  151. match(languageId, token) {
  152. // The cache contains the metadata without the language bits set.
  153. let result = this._cache.get(token);
  154. if (typeof result === 'undefined') {
  155. const rule = this._match(token);
  156. const standardToken = toStandardTokenType(token);
  157. result = (rule.metadata
  158. | (standardToken << 8 /* MetadataConsts.TOKEN_TYPE_OFFSET */)) >>> 0;
  159. this._cache.set(token, result);
  160. }
  161. return (result
  162. | (languageId << 0 /* MetadataConsts.LANGUAGEID_OFFSET */)) >>> 0;
  163. }
  164. }
  165. const STANDARD_TOKEN_TYPE_REGEXP = /\b(comment|string|regex|regexp)\b/;
  166. export function toStandardTokenType(tokenType) {
  167. const m = tokenType.match(STANDARD_TOKEN_TYPE_REGEXP);
  168. if (!m) {
  169. return 0 /* StandardTokenType.Other */;
  170. }
  171. switch (m[1]) {
  172. case 'comment':
  173. return 1 /* StandardTokenType.Comment */;
  174. case 'string':
  175. return 2 /* StandardTokenType.String */;
  176. case 'regex':
  177. return 3 /* StandardTokenType.RegEx */;
  178. case 'regexp':
  179. return 3 /* StandardTokenType.RegEx */;
  180. }
  181. throw new Error('Unexpected match for standard token type!');
  182. }
  183. export function strcmp(a, b) {
  184. if (a < b) {
  185. return -1;
  186. }
  187. if (a > b) {
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. export class ThemeTrieElementRule {
  193. constructor(fontStyle, foreground, background) {
  194. this._themeTrieElementRuleBrand = undefined;
  195. this._fontStyle = fontStyle;
  196. this._foreground = foreground;
  197. this._background = background;
  198. this.metadata = ((this._fontStyle << 11 /* MetadataConsts.FONT_STYLE_OFFSET */)
  199. | (this._foreground << 15 /* MetadataConsts.FOREGROUND_OFFSET */)
  200. | (this._background << 24 /* MetadataConsts.BACKGROUND_OFFSET */)) >>> 0;
  201. }
  202. clone() {
  203. return new ThemeTrieElementRule(this._fontStyle, this._foreground, this._background);
  204. }
  205. acceptOverwrite(fontStyle, foreground, background) {
  206. if (fontStyle !== -1 /* FontStyle.NotSet */) {
  207. this._fontStyle = fontStyle;
  208. }
  209. if (foreground !== 0 /* ColorId.None */) {
  210. this._foreground = foreground;
  211. }
  212. if (background !== 0 /* ColorId.None */) {
  213. this._background = background;
  214. }
  215. this.metadata = ((this._fontStyle << 11 /* MetadataConsts.FONT_STYLE_OFFSET */)
  216. | (this._foreground << 15 /* MetadataConsts.FOREGROUND_OFFSET */)
  217. | (this._background << 24 /* MetadataConsts.BACKGROUND_OFFSET */)) >>> 0;
  218. }
  219. }
  220. export class ThemeTrieElement {
  221. constructor(mainRule) {
  222. this._themeTrieElementBrand = undefined;
  223. this._mainRule = mainRule;
  224. this._children = new Map();
  225. }
  226. match(token) {
  227. if (token === '') {
  228. return this._mainRule;
  229. }
  230. const dotIndex = token.indexOf('.');
  231. let head;
  232. let tail;
  233. if (dotIndex === -1) {
  234. head = token;
  235. tail = '';
  236. }
  237. else {
  238. head = token.substring(0, dotIndex);
  239. tail = token.substring(dotIndex + 1);
  240. }
  241. const child = this._children.get(head);
  242. if (typeof child !== 'undefined') {
  243. return child.match(tail);
  244. }
  245. return this._mainRule;
  246. }
  247. insert(token, fontStyle, foreground, background) {
  248. if (token === '') {
  249. // Merge into the main rule
  250. this._mainRule.acceptOverwrite(fontStyle, foreground, background);
  251. return;
  252. }
  253. const dotIndex = token.indexOf('.');
  254. let head;
  255. let tail;
  256. if (dotIndex === -1) {
  257. head = token;
  258. tail = '';
  259. }
  260. else {
  261. head = token.substring(0, dotIndex);
  262. tail = token.substring(dotIndex + 1);
  263. }
  264. let child = this._children.get(head);
  265. if (typeof child === 'undefined') {
  266. child = new ThemeTrieElement(this._mainRule.clone());
  267. this._children.set(head, child);
  268. }
  269. child.insert(tail, fontStyle, foreground, background);
  270. }
  271. }
  272. export function generateTokensCSSForColorMap(colorMap) {
  273. const rules = [];
  274. for (let i = 1, len = colorMap.length; i < len; i++) {
  275. const color = colorMap[i];
  276. rules[i] = `.mtk${i} { color: ${color}; }`;
  277. }
  278. rules.push('.mtki { font-style: italic; }');
  279. rules.push('.mtkb { font-weight: bold; }');
  280. rules.push('.mtku { text-decoration: underline; text-underline-position: under; }');
  281. rules.push('.mtks { text-decoration: line-through; }');
  282. rules.push('.mtks.mtku { text-decoration: underline line-through; text-underline-position: under; }');
  283. return rules.join('\n');
  284. }