7947944b0b7c4de6281876298b042118959c4968b933d97367551ebe7158a6afd4f5a7abc09bc30c0bafb5e6d5a1c007ad56f03dadb55fc3dfb1dc6973ed0c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  15. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  16. return new (P || (P = Promise))(function (resolve, reject) {
  17. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  18. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  19. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  20. step((generator = generator.apply(thisArg, _arguments || [])).next());
  21. });
  22. };
  23. var __asyncValues = (this && this.__asyncValues) || function (o) {
  24. if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  25. var m = o[Symbol.asyncIterator], i;
  26. return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
  27. function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
  28. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  29. };
  30. import { AsyncIterableObject } from '../../../../base/common/async.js';
  31. import { isEmptyMarkdownString, MarkdownString } from '../../../../base/common/htmlContent.js';
  32. import { Position } from '../../../common/core/position.js';
  33. import { ModelDecorationInjectedTextOptions } from '../../../common/model/textModel.js';
  34. import { HoverForeignElementAnchor } from '../../hover/browser/hoverTypes.js';
  35. import { ILanguageService } from '../../../common/languages/language.js';
  36. import { ITextModelService } from '../../../common/services/resolverService.js';
  37. import { getHover } from '../../hover/browser/getHover.js';
  38. import { MarkdownHover, MarkdownHoverParticipant } from '../../hover/browser/markdownHoverParticipant.js';
  39. import { RenderedInlayHintLabelPart, InlayHintsController } from './inlayHintsController.js';
  40. import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
  41. import { IOpenerService } from '../../../../platform/opener/common/opener.js';
  42. import { ILanguageFeaturesService } from '../../../common/services/languageFeatures.js';
  43. import { localize } from '../../../../nls.js';
  44. import * as platform from '../../../../base/common/platform.js';
  45. import { asCommandLink } from './inlayHints.js';
  46. import { isNonEmptyArray } from '../../../../base/common/arrays.js';
  47. class InlayHintsHoverAnchor extends HoverForeignElementAnchor {
  48. constructor(part, owner) {
  49. super(10, owner, part.item.anchor.range);
  50. this.part = part;
  51. }
  52. }
  53. let InlayHintsHover = class InlayHintsHover extends MarkdownHoverParticipant {
  54. constructor(editor, languageService, openerService, configurationService, _resolverService, languageFeaturesService) {
  55. super(editor, languageService, openerService, configurationService, languageFeaturesService);
  56. this._resolverService = _resolverService;
  57. this.hoverOrdinal = 6;
  58. }
  59. suggestHoverAnchor(mouseEvent) {
  60. var _a;
  61. const controller = InlayHintsController.get(this._editor);
  62. if (!controller) {
  63. return null;
  64. }
  65. if (mouseEvent.target.type !== 6 /* MouseTargetType.CONTENT_TEXT */) {
  66. return null;
  67. }
  68. const options = (_a = mouseEvent.target.detail.injectedText) === null || _a === void 0 ? void 0 : _a.options;
  69. if (!(options instanceof ModelDecorationInjectedTextOptions && options.attachedData instanceof RenderedInlayHintLabelPart)) {
  70. return null;
  71. }
  72. return new InlayHintsHoverAnchor(options.attachedData, this);
  73. }
  74. computeSync() {
  75. return [];
  76. }
  77. computeAsync(anchor, _lineDecorations, token) {
  78. if (!(anchor instanceof InlayHintsHoverAnchor)) {
  79. return AsyncIterableObject.EMPTY;
  80. }
  81. return new AsyncIterableObject((executor) => __awaiter(this, void 0, void 0, function* () {
  82. var e_1, _a;
  83. const { part } = anchor;
  84. yield part.item.resolve(token);
  85. if (token.isCancellationRequested) {
  86. return;
  87. }
  88. // (1) Inlay Tooltip
  89. let itemTooltip;
  90. if (typeof part.item.hint.tooltip === 'string') {
  91. itemTooltip = new MarkdownString().appendText(part.item.hint.tooltip);
  92. }
  93. else if (part.item.hint.tooltip) {
  94. itemTooltip = part.item.hint.tooltip;
  95. }
  96. if (itemTooltip) {
  97. executor.emitOne(new MarkdownHover(this, anchor.range, [itemTooltip], 0));
  98. }
  99. // (1.2) Inlay dbl-click gesture
  100. if (isNonEmptyArray(part.item.hint.textEdits)) {
  101. executor.emitOne(new MarkdownHover(this, anchor.range, [new MarkdownString().appendText(localize('hint.dbl', "Double click to insert"))], 10001));
  102. }
  103. // (2) Inlay Label Part Tooltip
  104. let partTooltip;
  105. if (typeof part.part.tooltip === 'string') {
  106. partTooltip = new MarkdownString().appendText(part.part.tooltip);
  107. }
  108. else if (part.part.tooltip) {
  109. partTooltip = part.part.tooltip;
  110. }
  111. if (partTooltip) {
  112. executor.emitOne(new MarkdownHover(this, anchor.range, [partTooltip], 1));
  113. }
  114. // (2.2) Inlay Label Part Help Hover
  115. if (part.part.location || part.part.command) {
  116. let linkHint;
  117. const useMetaKey = this._editor.getOption(72 /* EditorOption.multiCursorModifier */) === 'altKey';
  118. const kb = useMetaKey
  119. ? platform.isMacintosh
  120. ? localize('links.navigate.kb.meta.mac', "cmd + click")
  121. : localize('links.navigate.kb.meta', "ctrl + click")
  122. : platform.isMacintosh
  123. ? localize('links.navigate.kb.alt.mac', "option + click")
  124. : localize('links.navigate.kb.alt', "alt + click");
  125. if (part.part.location && part.part.command) {
  126. linkHint = new MarkdownString().appendText(localize('hint.defAndCommand', 'Go to Definition ({0}), right click for more', kb));
  127. }
  128. else if (part.part.location) {
  129. linkHint = new MarkdownString().appendText(localize('hint.def', 'Go to Definition ({0})', kb));
  130. }
  131. else if (part.part.command) {
  132. linkHint = new MarkdownString(`[${localize('hint.cmd', "Execute Command")}](${asCommandLink(part.part.command)} "${part.part.command.title}") (${kb})`, { isTrusted: true });
  133. }
  134. if (linkHint) {
  135. executor.emitOne(new MarkdownHover(this, anchor.range, [linkHint], 10000));
  136. }
  137. }
  138. // (3) Inlay Label Part Location tooltip
  139. const iterable = yield this._resolveInlayHintLabelPartHover(part, token);
  140. try {
  141. for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield iterable_1.next(), !iterable_1_1.done;) {
  142. const item = iterable_1_1.value;
  143. executor.emitOne(item);
  144. }
  145. }
  146. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  147. finally {
  148. try {
  149. if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield _a.call(iterable_1);
  150. }
  151. finally { if (e_1) throw e_1.error; }
  152. }
  153. }));
  154. }
  155. _resolveInlayHintLabelPartHover(part, token) {
  156. return __awaiter(this, void 0, void 0, function* () {
  157. if (!part.part.location) {
  158. return AsyncIterableObject.EMPTY;
  159. }
  160. const { uri, range } = part.part.location;
  161. const ref = yield this._resolverService.createModelReference(uri);
  162. try {
  163. const model = ref.object.textEditorModel;
  164. if (!this._languageFeaturesService.hoverProvider.has(model)) {
  165. return AsyncIterableObject.EMPTY;
  166. }
  167. return getHover(this._languageFeaturesService.hoverProvider, model, new Position(range.startLineNumber, range.startColumn), token)
  168. .filter(item => !isEmptyMarkdownString(item.hover.contents))
  169. .map(item => new MarkdownHover(this, part.item.anchor.range, item.hover.contents, 2 + item.ordinal));
  170. }
  171. finally {
  172. ref.dispose();
  173. }
  174. });
  175. }
  176. };
  177. InlayHintsHover = __decorate([
  178. __param(1, ILanguageService),
  179. __param(2, IOpenerService),
  180. __param(3, IConfigurationService),
  181. __param(4, ITextModelService),
  182. __param(5, ILanguageFeaturesService)
  183. ], InlayHintsHover);
  184. export { InlayHintsHover };