sample.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /** @type {monaco.languages.SemanticTokensLegend} */
  2. const legend = {
  3. tokenTypes: [
  4. 'comment',
  5. 'string',
  6. 'keyword',
  7. 'number',
  8. 'regexp',
  9. 'operator',
  10. 'namespace',
  11. 'type',
  12. 'struct',
  13. 'class',
  14. 'interface',
  15. 'enum',
  16. 'typeParameter',
  17. 'function',
  18. 'member',
  19. 'macro',
  20. 'variable',
  21. 'parameter',
  22. 'property',
  23. 'label'
  24. ],
  25. tokenModifiers: [
  26. 'declaration',
  27. 'documentation',
  28. 'readonly',
  29. 'static',
  30. 'abstract',
  31. 'deprecated',
  32. 'modification',
  33. 'async'
  34. ]
  35. };
  36. /** @type {(type: string)=>number} */
  37. function getType(type) {
  38. return legend.tokenTypes.indexOf(type);
  39. }
  40. /** @type {(modifier: string[]|string|null)=>number} */
  41. function getModifier(modifiers) {
  42. if (typeof modifiers === 'string') {
  43. modifiers = [modifiers];
  44. }
  45. if (Array.isArray(modifiers)) {
  46. let nModifiers = 0;
  47. for (let modifier of modifiers) {
  48. const nModifier = legend.tokenModifiers.indexOf(modifier);
  49. if (nModifier > -1) {
  50. nModifiers |= (1 << nModifier) >>> 0;
  51. }
  52. }
  53. return nModifiers;
  54. } else {
  55. return 0;
  56. }
  57. }
  58. const tokenPattern = new RegExp('([a-zA-Z]+)((?:\\.[a-zA-Z]+)*)', 'g');
  59. monaco.languages.registerDocumentSemanticTokensProvider('plaintext', {
  60. getLegend: function () {
  61. return legend;
  62. },
  63. provideDocumentSemanticTokens: function (model, lastResultId, token) {
  64. const lines = model.getLinesContent();
  65. /** @type {number[]} */
  66. const data = [];
  67. let prevLine = 0;
  68. let prevChar = 0;
  69. for (let i = 0; i < lines.length; i++) {
  70. const line = lines[i];
  71. for (let match = null; (match = tokenPattern.exec(line)); ) {
  72. // translate token and modifiers to number representations
  73. let type = getType(match[1]);
  74. if (type === -1) {
  75. continue;
  76. }
  77. let modifier = match[2].length ? getModifier(match[2].split('.').slice(1)) : 0;
  78. data.push(
  79. // translate line to deltaLine
  80. i - prevLine,
  81. // for the same line, translate start to deltaStart
  82. prevLine === i ? match.index - prevChar : match.index,
  83. match[0].length,
  84. type,
  85. modifier
  86. );
  87. prevLine = i;
  88. prevChar = match.index;
  89. }
  90. }
  91. return {
  92. data: new Uint32Array(data),
  93. resultId: null
  94. };
  95. },
  96. releaseDocumentSemanticTokens: function (resultId) {}
  97. });
  98. // add some missing tokens
  99. monaco.editor.defineTheme('myCustomTheme', {
  100. base: 'vs',
  101. inherit: true,
  102. colors: {},
  103. rules: [
  104. { token: 'comment', foreground: 'aaaaaa', fontStyle: 'italic' },
  105. { token: 'keyword', foreground: 'ce63eb' },
  106. { token: 'operator', foreground: '000000' },
  107. { token: 'namespace', foreground: '66afce' },
  108. { token: 'type', foreground: '1db010' },
  109. { token: 'struct', foreground: '0000ff' },
  110. { token: 'class', foreground: '0000ff', fontStyle: 'bold' },
  111. { token: 'interface', foreground: '007700', fontStyle: 'bold' },
  112. { token: 'enum', foreground: '0077ff', fontStyle: 'bold' },
  113. { token: 'typeParameter', foreground: '1db010' },
  114. { token: 'function', foreground: '94763a' },
  115. { token: 'member', foreground: '94763a' },
  116. { token: 'macro', foreground: '615a60' },
  117. { token: 'variable', foreground: '3e5bbf' },
  118. { token: 'parameter', foreground: '3e5bbf' },
  119. { token: 'property', foreground: '3e5bbf' },
  120. { token: 'label', foreground: '615a60' },
  121. { token: 'type.static', fontStyle: 'bold' },
  122. { token: 'class.static', foreground: 'ff0000', fontStyle: 'bold' }
  123. ]
  124. });
  125. const editor = monaco.editor.create(document.getElementById('container'), {
  126. value: [
  127. 'Available token types:',
  128. ' [comment] [string] [keyword] [number] [regexp] [operator] [namespace]',
  129. ' [type] [struct] [class] [interface] [enum] [typeParameter] [function]',
  130. ' [member] [macro] [variable] [parameter] [property] [label]',
  131. '',
  132. 'Available token modifiers:',
  133. ' [type.declaration] [type.documentation] [type.member] [type.static]',
  134. ' [type.abstract] [type.deprecated] [type.modification] [type.async]',
  135. '',
  136. 'Some examples:',
  137. ' [class.static.token] [type.static.abstract]',
  138. ' [class.static.token] [type.static]',
  139. '',
  140. ' [struct]',
  141. '',
  142. ' [function.private]',
  143. '',
  144. 'An error case:',
  145. ' [notInLegend]'
  146. ].join('\n'),
  147. language: 'plaintext',
  148. theme: 'myCustomTheme',
  149. // semantic tokens provider is disabled by default
  150. 'semanticHighlighting.enabled': true
  151. });