sample.js 4.9 KB

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