getSemanticTokens.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. import { CancellationToken } from '../../../base/common/cancellation.js';
  15. import { onUnexpectedExternalError } from '../../../base/common/errors.js';
  16. import { URI } from '../../../base/common/uri.js';
  17. import { DocumentSemanticTokensProviderRegistry, DocumentRangeSemanticTokensProviderRegistry } from '../modes.js';
  18. import { IModelService } from './modelService.js';
  19. import { CommandsRegistry, ICommandService } from '../../../platform/commands/common/commands.js';
  20. import { assertType } from '../../../base/common/types.js';
  21. import { encodeSemanticTokensDto } from './semanticTokensDto.js';
  22. import { Range } from '../core/range.js';
  23. export function isSemanticTokens(v) {
  24. return v && !!(v.data);
  25. }
  26. export function isSemanticTokensEdits(v) {
  27. return v && Array.isArray(v.edits);
  28. }
  29. export class DocumentSemanticTokensResult {
  30. constructor(provider, tokens, error) {
  31. this.provider = provider;
  32. this.tokens = tokens;
  33. this.error = error;
  34. }
  35. }
  36. export function hasDocumentSemanticTokensProvider(model) {
  37. return DocumentSemanticTokensProviderRegistry.has(model);
  38. }
  39. function getDocumentSemanticTokensProviders(model) {
  40. const groups = DocumentSemanticTokensProviderRegistry.orderedGroups(model);
  41. return (groups.length > 0 ? groups[0] : []);
  42. }
  43. export function getDocumentSemanticTokens(model, lastProvider, lastResultId, token) {
  44. return __awaiter(this, void 0, void 0, function* () {
  45. const providers = getDocumentSemanticTokensProviders(model);
  46. // Get tokens from all providers at the same time.
  47. const results = yield Promise.all(providers.map((provider) => __awaiter(this, void 0, void 0, function* () {
  48. let result;
  49. let error = null;
  50. try {
  51. result = yield provider.provideDocumentSemanticTokens(model, (provider === lastProvider ? lastResultId : null), token);
  52. }
  53. catch (err) {
  54. error = err;
  55. result = null;
  56. }
  57. if (!result || (!isSemanticTokens(result) && !isSemanticTokensEdits(result))) {
  58. result = null;
  59. }
  60. return new DocumentSemanticTokensResult(provider, result, error);
  61. })));
  62. // Try to return the first result with actual tokens or
  63. // the first result which threw an error (!!)
  64. for (const result of results) {
  65. if (result.error) {
  66. throw result.error;
  67. }
  68. if (result.tokens) {
  69. return result;
  70. }
  71. }
  72. // Return the first result, even if it doesn't have tokens
  73. if (results.length > 0) {
  74. return results[0];
  75. }
  76. return null;
  77. });
  78. }
  79. function _getDocumentSemanticTokensProviderHighestGroup(model) {
  80. const result = DocumentSemanticTokensProviderRegistry.orderedGroups(model);
  81. return (result.length > 0 ? result[0] : null);
  82. }
  83. class DocumentRangeSemanticTokensResult {
  84. constructor(provider, tokens) {
  85. this.provider = provider;
  86. this.tokens = tokens;
  87. }
  88. }
  89. export function hasDocumentRangeSemanticTokensProvider(model) {
  90. return DocumentRangeSemanticTokensProviderRegistry.has(model);
  91. }
  92. function getDocumentRangeSemanticTokensProviders(model) {
  93. const groups = DocumentRangeSemanticTokensProviderRegistry.orderedGroups(model);
  94. return (groups.length > 0 ? groups[0] : []);
  95. }
  96. export function getDocumentRangeSemanticTokens(model, range, token) {
  97. return __awaiter(this, void 0, void 0, function* () {
  98. const providers = getDocumentRangeSemanticTokensProviders(model);
  99. // Get tokens from all providers at the same time.
  100. const results = yield Promise.all(providers.map((provider) => __awaiter(this, void 0, void 0, function* () {
  101. let result;
  102. try {
  103. result = yield provider.provideDocumentRangeSemanticTokens(model, range, token);
  104. }
  105. catch (err) {
  106. onUnexpectedExternalError(err);
  107. result = null;
  108. }
  109. if (!result || !isSemanticTokens(result)) {
  110. result = null;
  111. }
  112. return new DocumentRangeSemanticTokensResult(provider, result);
  113. })));
  114. // Try to return the first result with actual tokens
  115. for (const result of results) {
  116. if (result.tokens) {
  117. return result;
  118. }
  119. }
  120. // Return the first result, even if it doesn't have tokens
  121. if (results.length > 0) {
  122. return results[0];
  123. }
  124. return null;
  125. });
  126. }
  127. CommandsRegistry.registerCommand('_provideDocumentSemanticTokensLegend', (accessor, ...args) => __awaiter(void 0, void 0, void 0, function* () {
  128. const [uri] = args;
  129. assertType(uri instanceof URI);
  130. const model = accessor.get(IModelService).getModel(uri);
  131. if (!model) {
  132. return undefined;
  133. }
  134. const providers = _getDocumentSemanticTokensProviderHighestGroup(model);
  135. if (!providers) {
  136. // there is no provider => fall back to a document range semantic tokens provider
  137. return accessor.get(ICommandService).executeCommand('_provideDocumentRangeSemanticTokensLegend', uri);
  138. }
  139. return providers[0].getLegend();
  140. }));
  141. CommandsRegistry.registerCommand('_provideDocumentSemanticTokens', (accessor, ...args) => __awaiter(void 0, void 0, void 0, function* () {
  142. const [uri] = args;
  143. assertType(uri instanceof URI);
  144. const model = accessor.get(IModelService).getModel(uri);
  145. if (!model) {
  146. return undefined;
  147. }
  148. if (!hasDocumentSemanticTokensProvider(model)) {
  149. // there is no provider => fall back to a document range semantic tokens provider
  150. return accessor.get(ICommandService).executeCommand('_provideDocumentRangeSemanticTokens', uri, model.getFullModelRange());
  151. }
  152. const r = yield getDocumentSemanticTokens(model, null, null, CancellationToken.None);
  153. if (!r) {
  154. return undefined;
  155. }
  156. const { provider, tokens } = r;
  157. if (!tokens || !isSemanticTokens(tokens)) {
  158. return undefined;
  159. }
  160. const buff = encodeSemanticTokensDto({
  161. id: 0,
  162. type: 'full',
  163. data: tokens.data
  164. });
  165. if (tokens.resultId) {
  166. provider.releaseDocumentSemanticTokens(tokens.resultId);
  167. }
  168. return buff;
  169. }));
  170. CommandsRegistry.registerCommand('_provideDocumentRangeSemanticTokensLegend', (accessor, ...args) => __awaiter(void 0, void 0, void 0, function* () {
  171. const [uri, range] = args;
  172. assertType(uri instanceof URI);
  173. const model = accessor.get(IModelService).getModel(uri);
  174. if (!model) {
  175. return undefined;
  176. }
  177. const providers = getDocumentRangeSemanticTokensProviders(model);
  178. if (providers.length === 0) {
  179. // no providers
  180. return undefined;
  181. }
  182. if (providers.length === 1) {
  183. // straight forward case, just a single provider
  184. return providers[0].getLegend();
  185. }
  186. if (!range || !Range.isIRange(range)) {
  187. // if no range is provided, we cannot support multiple providers
  188. // as we cannot fall back to the one which would give results
  189. // => return the first legend for backwards compatibility and print a warning
  190. console.warn(`provideDocumentRangeSemanticTokensLegend might be out-of-sync with provideDocumentRangeSemanticTokens unless a range argument is passed in`);
  191. return providers[0].getLegend();
  192. }
  193. const result = yield getDocumentRangeSemanticTokens(model, Range.lift(range), CancellationToken.None);
  194. if (!result) {
  195. return undefined;
  196. }
  197. return result.provider.getLegend();
  198. }));
  199. CommandsRegistry.registerCommand('_provideDocumentRangeSemanticTokens', (accessor, ...args) => __awaiter(void 0, void 0, void 0, function* () {
  200. const [uri, range] = args;
  201. assertType(uri instanceof URI);
  202. assertType(Range.isIRange(range));
  203. const model = accessor.get(IModelService).getModel(uri);
  204. if (!model) {
  205. return undefined;
  206. }
  207. const result = yield getDocumentRangeSemanticTokens(model, Range.lift(range), CancellationToken.None);
  208. if (!result || !result.tokens) {
  209. // there is no provider or it didn't return tokens
  210. return undefined;
  211. }
  212. return encodeSemanticTokensDto({
  213. id: 0,
  214. type: 'full',
  215. data: result.tokens.data
  216. });
  217. }));