Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/inlayHints/browser/inlayHintsHover.ts
5285 views
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
6
import { AsyncIterableProducer } from '../../../../base/common/async.js';
7
import { CancellationToken } from '../../../../base/common/cancellation.js';
8
import { IMarkdownString, isEmptyMarkdownString, MarkdownString } from '../../../../base/common/htmlContent.js';
9
import { ICodeEditor, IEditorMouseEvent, MouseTargetType } from '../../../browser/editorBrowser.js';
10
import { Position } from '../../../common/core/position.js';
11
import { IModelDecoration } from '../../../common/model.js';
12
import { ModelDecorationInjectedTextOptions } from '../../../common/model/textModel.js';
13
import { HoverAnchor, HoverForeignElementAnchor, IEditorHoverParticipant } from '../../hover/browser/hoverTypes.js';
14
import { ITextModelService } from '../../../common/services/resolverService.js';
15
import { getHoverProviderResultsAsAsyncIterable } from '../../hover/browser/getHover.js';
16
import { MarkdownHover, MarkdownHoverParticipant } from '../../hover/browser/markdownHoverParticipant.js';
17
import { RenderedInlayHintLabelPart, InlayHintsController } from './inlayHintsController.js';
18
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
19
import { ILanguageFeaturesService } from '../../../common/services/languageFeatures.js';
20
import { EditorOption } from '../../../common/config/editorOptions.js';
21
import { localize } from '../../../../nls.js';
22
import * as platform from '../../../../base/common/platform.js';
23
import { asCommandLink } from './inlayHints.js';
24
import { isNonEmptyArray } from '../../../../base/common/arrays.js';
25
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
26
import { IHoverService } from '../../../../platform/hover/browser/hover.js';
27
import { ICommandService } from '../../../../platform/commands/common/commands.js';
28
import { HoverStartSource } from '../../hover/browser/hoverOperation.js';
29
import { IMarkdownRendererService } from '../../../../platform/markdown/browser/markdownRenderer.js';
30
31
class InlayHintsHoverAnchor extends HoverForeignElementAnchor {
32
constructor(
33
readonly part: RenderedInlayHintLabelPart,
34
owner: InlayHintsHover,
35
initialMousePosX: number | undefined,
36
initialMousePosY: number | undefined
37
) {
38
super(10, owner, part.item.anchor.range, initialMousePosX, initialMousePosY, true);
39
}
40
}
41
42
export class InlayHintsHover extends MarkdownHoverParticipant implements IEditorHoverParticipant<MarkdownHover> {
43
44
public override readonly hoverOrdinal: number = 6;
45
46
constructor(
47
editor: ICodeEditor,
48
@IMarkdownRendererService markdownRendererService: IMarkdownRendererService,
49
@IKeybindingService keybindingService: IKeybindingService,
50
@IHoverService hoverService: IHoverService,
51
@IConfigurationService configurationService: IConfigurationService,
52
@ITextModelService private readonly _resolverService: ITextModelService,
53
@ILanguageFeaturesService languageFeaturesService: ILanguageFeaturesService,
54
@ICommandService commandService: ICommandService,
55
) {
56
super(editor, markdownRendererService, configurationService, languageFeaturesService, keybindingService, hoverService, commandService);
57
}
58
59
suggestHoverAnchor(mouseEvent: IEditorMouseEvent): HoverAnchor | null {
60
const controller = InlayHintsController.get(this._editor);
61
if (!controller) {
62
return null;
63
}
64
if (mouseEvent.target.type !== MouseTargetType.CONTENT_TEXT) {
65
return null;
66
}
67
const options = mouseEvent.target.detail.injectedText?.options;
68
if (!(options instanceof ModelDecorationInjectedTextOptions && options.attachedData instanceof RenderedInlayHintLabelPart)) {
69
return null;
70
}
71
return new InlayHintsHoverAnchor(options.attachedData, this, mouseEvent.event.posx, mouseEvent.event.posy);
72
}
73
74
override computeSync(): MarkdownHover[] {
75
return [];
76
}
77
78
override computeAsync(anchor: HoverAnchor, _lineDecorations: IModelDecoration[], source: HoverStartSource, token: CancellationToken): AsyncIterableProducer<MarkdownHover> {
79
if (!(anchor instanceof InlayHintsHoverAnchor)) {
80
return AsyncIterableProducer.EMPTY;
81
}
82
83
return new AsyncIterableProducer<MarkdownHover>(async executor => {
84
85
const { part } = anchor;
86
await part.item.resolve(token);
87
88
if (token.isCancellationRequested) {
89
return;
90
}
91
92
// (1) Inlay Tooltip
93
let itemTooltip: IMarkdownString | undefined;
94
if (typeof part.item.hint.tooltip === 'string') {
95
itemTooltip = new MarkdownString().appendText(part.item.hint.tooltip);
96
} else if (part.item.hint.tooltip) {
97
itemTooltip = part.item.hint.tooltip;
98
}
99
if (itemTooltip) {
100
executor.emitOne(new MarkdownHover(this, anchor.range, [itemTooltip], false, 0));
101
}
102
// (1.2) Inlay dbl-click gesture
103
if (isNonEmptyArray(part.item.hint.textEdits)) {
104
executor.emitOne(new MarkdownHover(this, anchor.range, [new MarkdownString().appendText(localize('hint.dbl', "Double-click to insert"))], false, 10001));
105
}
106
107
// (2) Inlay Label Part Tooltip
108
let partTooltip: IMarkdownString | undefined;
109
if (typeof part.part.tooltip === 'string') {
110
partTooltip = new MarkdownString().appendText(part.part.tooltip);
111
} else if (part.part.tooltip) {
112
partTooltip = part.part.tooltip;
113
}
114
if (partTooltip) {
115
executor.emitOne(new MarkdownHover(this, anchor.range, [partTooltip], false, 1));
116
}
117
118
// (2.2) Inlay Label Part Help Hover
119
if (part.part.location || part.part.command) {
120
let linkHint: MarkdownString | undefined;
121
const useMetaKey = this._editor.getOption(EditorOption.multiCursorModifier) === 'altKey';
122
const kb = useMetaKey
123
? platform.isMacintosh
124
? localize('links.navigate.kb.meta.mac', "cmd + click")
125
: localize('links.navigate.kb.meta', "ctrl + click")
126
: platform.isMacintosh
127
? localize('links.navigate.kb.alt.mac', "option + click")
128
: localize('links.navigate.kb.alt', "alt + click");
129
130
if (part.part.location && part.part.command) {
131
linkHint = new MarkdownString().appendText(localize('hint.defAndCommand', 'Go to Definition ({0}), right click for more', kb));
132
} else if (part.part.location) {
133
linkHint = new MarkdownString().appendText(localize('hint.def', 'Go to Definition ({0})', kb));
134
} else if (part.part.command) {
135
linkHint = new MarkdownString(`[${localize('hint.cmd', "Execute Command")}](${asCommandLink(part.part.command)} "${part.part.command.title}") (${kb})`, { isTrusted: true });
136
}
137
if (linkHint) {
138
executor.emitOne(new MarkdownHover(this, anchor.range, [linkHint], false, 10000));
139
}
140
}
141
142
143
// (3) Inlay Label Part Location tooltip
144
const iterable = this._resolveInlayHintLabelPartHover(part, token);
145
for await (const item of iterable) {
146
executor.emitOne(item);
147
}
148
});
149
}
150
151
private async *_resolveInlayHintLabelPartHover(part: RenderedInlayHintLabelPart, token: CancellationToken): AsyncIterable<MarkdownHover> {
152
if (!part.part.location) {
153
return;
154
}
155
156
const { uri, range } = part.part.location;
157
const ref = await this._resolverService.createModelReference(uri);
158
try {
159
const model = ref.object.textEditorModel;
160
if (!this._languageFeaturesService.hoverProvider.has(model)) {
161
return;
162
}
163
164
for await (const item of getHoverProviderResultsAsAsyncIterable(this._languageFeaturesService.hoverProvider, model, new Position(range.startLineNumber, range.startColumn), token)) {
165
if (!isEmptyMarkdownString(item.hover.contents)) {
166
yield new MarkdownHover(this, part.item.anchor.range, item.hover.contents, false, 2 + item.ordinal);
167
}
168
}
169
} finally {
170
ref.dispose();
171
}
172
}
173
}
174
175