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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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') modifiers = [modifiers];
  47. if (Array.isArray(modifiers)) {
  48. let nModifiers = 0;
  49. for (let modifier of modifiers) {
  50. nModifier = legend.tokenModifiers.indexOf(modifier);
  51. if (nModifier > -1) {
  52. nModifiers |= (1 << nModifier) >>> 0;
  53. }
  54. }
  55. return nModifiers;
  56. } else {
  57. return 0;
  58. }
  59. }
  60. const tokenPattern = new RegExp('(?<=\\[)([a-zA-Z]+)((?:\\.[a-zA-Z]+)*)(?=\\])', 'g');
  61. monaco.languages.registerDocumentSemanticTokensProvider('plaintext', {
  62. getLegend: function () {
  63. return legend;
  64. },
  65. provideDocumentSemanticTokens: function (model, lastResultId, token) {
  66. const lines = model.getLinesContent();
  67. /** @type {number[]} */
  68. const data = [];
  69. let prevLine = 0;
  70. let prevChar = 0;
  71. for (let i = 0; i < lines.length; i++) {
  72. const line = lines[i];
  73. for (let match = null; match = tokenPattern.exec(line);) {
  74. // translate token and modifiers to number representations
  75. let type = getType(match[1]);
  76. if (type === -1) continue;
  77. let modifier = match[2].length
  78. ? getModifier(match[2].split('.').slice(1))
  79. : 0;
  80. data.push(
  81. // translate line to deltaLine
  82. i - prevLine,
  83. // for the same line, translate start to deltaStart
  84. prevLine === i ? match.index - prevChar : match.index,
  85. match[0].length,
  86. type,
  87. modifier
  88. );
  89. prevLine = i;
  90. prevChar = match.index;
  91. }
  92. }
  93. return {
  94. data: new Uint32Array(data),
  95. resultId: null
  96. };
  97. },
  98. releaseDocumentSemanticTokens: function (resultId) { }
  99. });
  100. // add some missing tokens
  101. monaco.editor.defineTheme('myCustomTheme', {
  102. base: 'vs',
  103. inherit: true,
  104. rules: [
  105. { token: 'comment', foreground: 'aaaaaa', fontStyle: 'italic' },
  106. { token: 'keyword', foreground: 'ce63eb' },
  107. { token: 'operator', foreground: '000000' },
  108. { token: 'namespace', foreground: '66afce' },
  109. { token: 'type', foreground: '1db010' },
  110. { token: 'struct', foreground: '0000ff' },
  111. { token: 'class', foreground: '0000ff', fontStyle: 'bold' },
  112. { token: 'interface', foreground: '007700', fontStyle: 'bold' },
  113. { token: 'enum', foreground: '0077ff', fontStyle: 'bold' },
  114. { token: 'typeParameter', foreground: '1db010' },
  115. { token: 'function', foreground: '94763a' },
  116. { token: 'member', foreground: '94763a' },
  117. { token: 'macro', foreground: '615a60' },
  118. { token: 'variable', foreground: '3e5bbf' },
  119. { token: 'parameter', foreground: '3e5bbf' },
  120. { token: 'property', foreground: '3e5bbf' },
  121. { token: 'label', foreground: '615a60' },
  122. { token: 'type.static', fontStyle: 'bold' },
  123. { token: 'class.static', foreground: 'ff0000', fontStyle: 'bold' }
  124. ]
  125. });
  126. const editor = monaco.editor.create(document.getElementById("container"), {
  127. value: `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. Available token modifiers:
  132. [type.declaration] [type.documentation] [type.member] [type.static]
  133. [type.abstract] [type.deprecated] [type.modification] [type.async]
  134. Some examples:
  135. [class.static.token] [type.static.abstract]
  136. [class.static.token] [type.static]
  137. [struct]
  138. [function.private]
  139. An error case:
  140. [notInLegend]`,
  141. language: "plaintext",
  142. theme: 'myCustomTheme',
  143. // semantic tokens provider is disabled by default
  144. 'semanticHighlighting.enabled': true
  145. });
  146. // currently there isn't builtin token handling
  147. editor._themeService._knownThemes.forEach(function (theme) {
  148. theme.getTokenStyleMetadata = (type, modifiers) => {
  149. // use theme rules match
  150. const style = theme._tokenTheme._root.match([type, ...modifiers].join('.'));
  151. return {
  152. foreground: style._foreground,
  153. italic: style._fontStyle & 1,
  154. bold: style._fontStyle & 2,
  155. underline: style._fontStyle & 4
  156. };
  157. };
  158. });
  159. // press F4 to change theme
  160. editor.addCommand(monaco.KeyCode.F4, function () {
  161. switch (editor._themeService.getTheme().themeName) {
  162. case 'vs': monaco.editor.setTheme('vs-dark'); break;
  163. case 'vs-dark': monaco.editor.setTheme('hc-black'); break;
  164. case 'hc-black': monaco.editor.setTheme('myCustomTheme'); break;
  165. case 'myCustomTheme': monaco.editor.setTheme('vs'); break;
  166. }
  167. });
  168. /*----------------------------------------SAMPLE JS END*/
  169. });
  170. </script>
  171. </body>
  172. </html>