extending-language-services-semantic-tokens-provider-example.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <!-- THIS IS A GENERATED FILE VIA `npm run simpleserver` -->
  3. <html>
  4. <head>
  5. <base href="..">
  6. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  7. </head>
  8. <body>
  9. <style>
  10. /*----------------------------------------SAMPLE CSS START*/
  11. /*----------------------------------------SAMPLE CSS END*/
  12. </style>
  13. <a class="loading-opts" href="playground.generated/index.html">[&lt;&lt; BACK]</a> <br/>
  14. THIS IS A GENERATED FILE VIA `npm run simpleserver`
  15. <div id="bar" style="margin-bottom: 6px;"></div>
  16. <div style="clear:both"></div>
  17. <div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">
  18. <!-- ----------------------------------------SAMPLE HTML START-->
  19. <div id="container" style="height: 100%"></div>
  20. <!-- ----------------------------------------SAMPLE HTML END-->
  21. </div>
  22. <div style="clear:both"></div>
  23. <script src="../../metadata.js"></script>
  24. <script src="dev-setup.js"></script>
  25. <script>
  26. loadEditor(function() {
  27. /*----------------------------------------SAMPLE JS START*/
  28. /** @type {monaco.languages.SemanticTokensLegend} */
  29. const legend = {
  30. tokenTypes: [
  31. 'comment',
  32. 'string',
  33. 'keyword',
  34. 'number',
  35. 'regexp',
  36. 'operator',
  37. 'namespace',
  38. 'type',
  39. 'struct',
  40. 'class',
  41. 'interface',
  42. 'enum',
  43. 'typeParameter',
  44. 'function',
  45. 'member',
  46. 'macro',
  47. 'variable',
  48. 'parameter',
  49. 'property',
  50. 'label'
  51. ],
  52. tokenModifiers: [
  53. 'declaration',
  54. 'documentation',
  55. 'readonly',
  56. 'static',
  57. 'abstract',
  58. 'deprecated',
  59. 'modification',
  60. 'async'
  61. ]
  62. };
  63. /** @type {(type: string)=>number} */
  64. function getType(type) {
  65. return legend.tokenTypes.indexOf(type);
  66. }
  67. /** @type {(modifier: string[]|string|null)=>number} */
  68. function getModifier(modifiers) {
  69. if (typeof modifiers === 'string') {
  70. modifiers = [modifiers];
  71. }
  72. if (Array.isArray(modifiers)) {
  73. let nModifiers = 0;
  74. for (let modifier of modifiers) {
  75. const nModifier = legend.tokenModifiers.indexOf(modifier);
  76. if (nModifier > -1) {
  77. nModifiers |= (1 << nModifier) >>> 0;
  78. }
  79. }
  80. return nModifiers;
  81. } else {
  82. return 0;
  83. }
  84. }
  85. const tokenPattern = new RegExp('([a-zA-Z]+)((?:\\.[a-zA-Z]+)*)', 'g');
  86. monaco.languages.registerDocumentSemanticTokensProvider('plaintext', {
  87. getLegend: function () {
  88. return legend;
  89. },
  90. provideDocumentSemanticTokens: function (model, lastResultId, token) {
  91. const lines = model.getLinesContent();
  92. /** @type {number[]} */
  93. const data = [];
  94. let prevLine = 0;
  95. let prevChar = 0;
  96. for (let i = 0; i < lines.length; i++) {
  97. const line = lines[i];
  98. for (let match = null; (match = tokenPattern.exec(line)); ) {
  99. // translate token and modifiers to number representations
  100. let type = getType(match[1]);
  101. if (type === -1) {
  102. continue;
  103. }
  104. let modifier = match[2].length ? getModifier(match[2].split('.').slice(1)) : 0;
  105. data.push(
  106. // translate line to deltaLine
  107. i - prevLine,
  108. // for the same line, translate start to deltaStart
  109. prevLine === i ? match.index - prevChar : match.index,
  110. match[0].length,
  111. type,
  112. modifier
  113. );
  114. prevLine = i;
  115. prevChar = match.index;
  116. }
  117. }
  118. return {
  119. data: new Uint32Array(data),
  120. resultId: null
  121. };
  122. },
  123. releaseDocumentSemanticTokens: function (resultId) {}
  124. });
  125. // add some missing tokens
  126. monaco.editor.defineTheme('myCustomTheme', {
  127. base: 'vs',
  128. inherit: true,
  129. colors: {},
  130. rules: [
  131. { token: 'comment', foreground: 'aaaaaa', fontStyle: 'italic' },
  132. { token: 'keyword', foreground: 'ce63eb' },
  133. { token: 'operator', foreground: '000000' },
  134. { token: 'namespace', foreground: '66afce' },
  135. { token: 'type', foreground: '1db010' },
  136. { token: 'struct', foreground: '0000ff' },
  137. { token: 'class', foreground: '0000ff', fontStyle: 'bold' },
  138. { token: 'interface', foreground: '007700', fontStyle: 'bold' },
  139. { token: 'enum', foreground: '0077ff', fontStyle: 'bold' },
  140. { token: 'typeParameter', foreground: '1db010' },
  141. { token: 'function', foreground: '94763a' },
  142. { token: 'member', foreground: '94763a' },
  143. { token: 'macro', foreground: '615a60' },
  144. { token: 'variable', foreground: '3e5bbf' },
  145. { token: 'parameter', foreground: '3e5bbf' },
  146. { token: 'property', foreground: '3e5bbf' },
  147. { token: 'label', foreground: '615a60' },
  148. { token: 'type.static', fontStyle: 'bold' },
  149. { token: 'class.static', foreground: 'ff0000', fontStyle: 'bold' }
  150. ]
  151. });
  152. const editor = monaco.editor.create(document.getElementById('container'), {
  153. value: [
  154. 'Available token types:',
  155. ' [comment] [string] [keyword] [number] [regexp] [operator] [namespace]',
  156. ' [type] [struct] [class] [interface] [enum] [typeParameter] [function]',
  157. ' [member] [macro] [variable] [parameter] [property] [label]',
  158. '',
  159. 'Available token modifiers:',
  160. ' [type.declaration] [type.documentation] [type.member] [type.static]',
  161. ' [type.abstract] [type.deprecated] [type.modification] [type.async]',
  162. '',
  163. 'Some examples:',
  164. ' [class.static.token] [type.static.abstract]',
  165. ' [class.static.token] [type.static]',
  166. '',
  167. ' [struct]',
  168. '',
  169. ' [function.private]',
  170. '',
  171. 'An error case:',
  172. ' [notInLegend]'
  173. ].join('\n'),
  174. language: 'plaintext',
  175. theme: 'myCustomTheme',
  176. // semantic tokens provider is disabled by default
  177. 'semanticHighlighting.enabled': true
  178. });
  179. /*----------------------------------------SAMPLE JS END*/
  180. });
  181. </script>
  182. </body>
  183. </html>