66476ff673e274ec3ff9f07a72481ae1edefae7fc320e7af9dec1577afa356556388918cae52d69af6b89a0a76f106727f67494c43feeea11f0884a40cbb51 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 { IModelService } from './model.js';
  18. import { CommandsRegistry, ICommandService } from '../../../platform/commands/common/commands.js';
  19. import { assertType } from '../../../base/common/types.js';
  20. import { encodeSemanticTokensDto } from './semanticTokensDto.js';
  21. import { Range } from '../core/range.js';
  22. import { ILanguageFeaturesService } from './languageFeatures.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(registry, model) {
  37. return registry.has(model);
  38. }
  39. function getDocumentSemanticTokensProviders(registry, model) {
  40. const groups = registry.orderedGroups(model);
  41. return (groups.length > 0 ? groups[0] : []);
  42. }
  43. export function getDocumentSemanticTokens(registry, model, lastProvider, lastResultId, token) {
  44. return __awaiter(this, void 0, void 0, function* () {
  45. const providers = getDocumentSemanticTokensProviders(registry, 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(registry, model) {
  80. const result = registry.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(providers, model) {
  90. return providers.has(model);
  91. }
  92. function getDocumentRangeSemanticTokensProviders(providers, model) {
  93. const groups = providers.orderedGroups(model);
  94. return (groups.length > 0 ? groups[0] : []);
  95. }
  96. export function getDocumentRangeSemanticTokens(registry, model, range, token) {
  97. return __awaiter(this, void 0, void 0, function* () {
  98. const providers = getDocumentRangeSemanticTokensProviders(registry, 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 { documentSemanticTokensProvider } = accessor.get(ILanguageFeaturesService);
  135. const providers = _getDocumentSemanticTokensProviderHighestGroup(documentSemanticTokensProvider, model);
  136. if (!providers) {
  137. // there is no provider => fall back to a document range semantic tokens provider
  138. return accessor.get(ICommandService).executeCommand('_provideDocumentRangeSemanticTokensLegend', uri);
  139. }
  140. return providers[0].getLegend();
  141. }));
  142. CommandsRegistry.registerCommand('_provideDocumentSemanticTokens', (accessor, ...args) => __awaiter(void 0, void 0, void 0, function* () {
  143. const [uri] = args;
  144. assertType(uri instanceof URI);
  145. const model = accessor.get(IModelService).getModel(uri);
  146. if (!model) {
  147. return undefined;
  148. }
  149. const { documentSemanticTokensProvider } = accessor.get(ILanguageFeaturesService);
  150. if (!hasDocumentSemanticTokensProvider(documentSemanticTokensProvider, model)) {
  151. // there is no provider => fall back to a document range semantic tokens provider
  152. return accessor.get(ICommandService).executeCommand('_provideDocumentRangeSemanticTokens', uri, model.getFullModelRange());
  153. }
  154. const r = yield getDocumentSemanticTokens(documentSemanticTokensProvider, model, null, null, CancellationToken.None);
  155. if (!r) {
  156. return undefined;
  157. }
  158. const { provider, tokens } = r;
  159. if (!tokens || !isSemanticTokens(tokens)) {
  160. return undefined;
  161. }
  162. const buff = encodeSemanticTokensDto({
  163. id: 0,
  164. type: 'full',
  165. data: tokens.data
  166. });
  167. if (tokens.resultId) {
  168. provider.releaseDocumentSemanticTokens(tokens.resultId);
  169. }
  170. return buff;
  171. }));
  172. CommandsRegistry.registerCommand('_provideDocumentRangeSemanticTokensLegend', (accessor, ...args) => __awaiter(void 0, void 0, void 0, function* () {
  173. const [uri, range] = args;
  174. assertType(uri instanceof URI);
  175. const model = accessor.get(IModelService).getModel(uri);
  176. if (!model) {
  177. return undefined;
  178. }
  179. const { documentRangeSemanticTokensProvider } = accessor.get(ILanguageFeaturesService);
  180. const providers = getDocumentRangeSemanticTokensProviders(documentRangeSemanticTokensProvider, model);
  181. if (providers.length === 0) {
  182. // no providers
  183. return undefined;
  184. }
  185. if (providers.length === 1) {
  186. // straight forward case, just a single provider
  187. return providers[0].getLegend();
  188. }
  189. if (!range || !Range.isIRange(range)) {
  190. // if no range is provided, we cannot support multiple providers
  191. // as we cannot fall back to the one which would give results
  192. // => return the first legend for backwards compatibility and print a warning
  193. console.warn(`provideDocumentRangeSemanticTokensLegend might be out-of-sync with provideDocumentRangeSemanticTokens unless a range argument is passed in`);
  194. return providers[0].getLegend();
  195. }
  196. const result = yield getDocumentRangeSemanticTokens(documentRangeSemanticTokensProvider, model, Range.lift(range), CancellationToken.None);
  197. if (!result) {
  198. return undefined;
  199. }
  200. return result.provider.getLegend();
  201. }));
  202. CommandsRegistry.registerCommand('_provideDocumentRangeSemanticTokens', (accessor, ...args) => __awaiter(void 0, void 0, void 0, function* () {
  203. const [uri, range] = args;
  204. assertType(uri instanceof URI);
  205. assertType(Range.isIRange(range));
  206. const model = accessor.get(IModelService).getModel(uri);
  207. if (!model) {
  208. return undefined;
  209. }
  210. const { documentRangeSemanticTokensProvider } = accessor.get(ILanguageFeaturesService);
  211. const result = yield getDocumentRangeSemanticTokens(documentRangeSemanticTokensProvider, model, Range.lift(range), CancellationToken.None);
  212. if (!result || !result.tokens) {
  213. // there is no provider or it didn't return tokens
  214. return undefined;
  215. }
  216. return encodeSemanticTokensDto({
  217. id: 0,
  218. type: 'full',
  219. data: result.tokens.data
  220. });
  221. }));