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

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