Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/hover/browser/hoverActions.ts
4779 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 { DECREASE_HOVER_VERBOSITY_ACTION_ID, DECREASE_HOVER_VERBOSITY_ACTION_LABEL, GO_TO_BOTTOM_HOVER_ACTION_ID, GO_TO_TOP_HOVER_ACTION_ID, HIDE_HOVER_ACTION_ID, INCREASE_HOVER_VERBOSITY_ACTION_ID, INCREASE_HOVER_VERBOSITY_ACTION_LABEL, PAGE_DOWN_HOVER_ACTION_ID, PAGE_UP_HOVER_ACTION_ID, SCROLL_DOWN_HOVER_ACTION_ID, SCROLL_LEFT_HOVER_ACTION_ID, SCROLL_RIGHT_HOVER_ACTION_ID, SCROLL_UP_HOVER_ACTION_ID, SHOW_DEFINITION_PREVIEW_HOVER_ACTION_ID, SHOW_OR_FOCUS_HOVER_ACTION_ID } from './hoverActionIds.js';
7
import { KeyChord, KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';
8
import { ICodeEditor } from '../../../browser/editorBrowser.js';
9
import { EditorAction, ServicesAccessor } from '../../../browser/editorExtensions.js';
10
import { EditorOption } from '../../../common/config/editorOptions.js';
11
import { Range } from '../../../common/core/range.js';
12
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
13
import { GotoDefinitionAtPositionEditorContribution } from '../../gotoSymbol/browser/link/goToDefinitionAtPosition.js';
14
import { HoverStartMode, HoverStartSource } from './hoverOperation.js';
15
import { AccessibilitySupport } from '../../../../platform/accessibility/common/accessibility.js';
16
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
17
import { ContentHoverController } from './contentHoverController.js';
18
import { HoverVerbosityAction } from '../../../common/languages.js';
19
import * as nls from '../../../../nls.js';
20
import './hover.css';
21
22
enum HoverFocusBehavior {
23
NoAutoFocus = 'noAutoFocus',
24
FocusIfVisible = 'focusIfVisible',
25
AutoFocusImmediately = 'autoFocusImmediately'
26
}
27
28
export class ShowOrFocusHoverAction extends EditorAction {
29
30
constructor() {
31
super({
32
id: SHOW_OR_FOCUS_HOVER_ACTION_ID,
33
label: nls.localize2({
34
key: 'showOrFocusHover',
35
comment: [
36
'Label for action that will trigger the showing/focusing of a hover in the editor.',
37
'If the hover is not visible, it will show the hover.',
38
'This allows for users to show the hover without using the mouse.'
39
]
40
}, "Show or Focus Hover"),
41
metadata: {
42
description: nls.localize2('showOrFocusHoverDescription', 'Show or focus the editor hover which shows documentation, references, and other content for a symbol at the current cursor position.'),
43
args: [{
44
name: 'args',
45
schema: {
46
type: 'object',
47
properties: {
48
'focus': {
49
description: 'Controls if and when the hover should take focus upon being triggered by this action.',
50
enum: [HoverFocusBehavior.NoAutoFocus, HoverFocusBehavior.FocusIfVisible, HoverFocusBehavior.AutoFocusImmediately],
51
enumDescriptions: [
52
nls.localize('showOrFocusHover.focus.noAutoFocus', 'The hover will not automatically take focus.'),
53
nls.localize('showOrFocusHover.focus.focusIfVisible', 'The hover will take focus only if it is already visible.'),
54
nls.localize('showOrFocusHover.focus.autoFocusImmediately', 'The hover will automatically take focus when it appears.'),
55
],
56
default: HoverFocusBehavior.FocusIfVisible,
57
}
58
},
59
}
60
}]
61
},
62
precondition: undefined,
63
kbOpts: {
64
kbExpr: EditorContextKeys.editorTextFocus,
65
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.KeyI),
66
weight: KeybindingWeight.EditorContrib
67
}
68
});
69
}
70
71
public run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void {
72
if (!editor.hasModel()) {
73
return;
74
}
75
76
const controller = ContentHoverController.get(editor);
77
if (!controller) {
78
return;
79
}
80
81
const focusArgument = args?.focus;
82
let focusOption = HoverFocusBehavior.FocusIfVisible;
83
if (Object.values(HoverFocusBehavior).includes(focusArgument)) {
84
focusOption = focusArgument;
85
} else if (typeof focusArgument === 'boolean' && focusArgument) {
86
focusOption = HoverFocusBehavior.AutoFocusImmediately;
87
}
88
89
const showContentHover = (focus: boolean) => {
90
const position = editor.getPosition();
91
const range = new Range(position.lineNumber, position.column, position.lineNumber, position.column);
92
controller.showContentHover(range, HoverStartMode.Immediate, HoverStartSource.Keyboard, focus);
93
};
94
95
const accessibilitySupportEnabled = editor.getOption(EditorOption.accessibilitySupport) === AccessibilitySupport.Enabled;
96
97
if (controller.isHoverVisible) {
98
if (focusOption !== HoverFocusBehavior.NoAutoFocus) {
99
controller.focus();
100
} else {
101
showContentHover(accessibilitySupportEnabled);
102
}
103
} else {
104
showContentHover(accessibilitySupportEnabled || focusOption === HoverFocusBehavior.AutoFocusImmediately);
105
}
106
}
107
}
108
109
export class ShowDefinitionPreviewHoverAction extends EditorAction {
110
111
constructor() {
112
super({
113
id: SHOW_DEFINITION_PREVIEW_HOVER_ACTION_ID,
114
label: nls.localize2({
115
key: 'showDefinitionPreviewHover',
116
comment: [
117
'Label for action that will trigger the showing of definition preview hover in the editor.',
118
'This allows for users to show the definition preview hover without using the mouse.'
119
]
120
}, "Show Definition Preview Hover"),
121
precondition: undefined,
122
metadata: {
123
description: nls.localize2('showDefinitionPreviewHoverDescription', 'Show the definition preview hover in the editor.'),
124
},
125
});
126
}
127
128
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
129
const controller = ContentHoverController.get(editor);
130
if (!controller) {
131
return;
132
}
133
const position = editor.getPosition();
134
135
if (!position) {
136
return;
137
}
138
139
const range = new Range(position.lineNumber, position.column, position.lineNumber, position.column);
140
const goto = GotoDefinitionAtPositionEditorContribution.get(editor);
141
if (!goto) {
142
return;
143
}
144
145
const promise = goto.startFindDefinitionFromCursor(position);
146
promise.then(() => {
147
controller.showContentHover(range, HoverStartMode.Immediate, HoverStartSource.Keyboard, true);
148
});
149
}
150
}
151
152
export class HideContentHoverAction extends EditorAction {
153
154
constructor() {
155
super({
156
id: HIDE_HOVER_ACTION_ID,
157
label: nls.localize2({
158
key: 'hideHover',
159
comment: ['Label for action that will hide the hover in the editor.']
160
}, "Hide Hover"),
161
alias: 'Hide Content Hover',
162
precondition: undefined
163
});
164
}
165
166
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
167
ContentHoverController.get(editor)?.hideContentHover();
168
}
169
}
170
171
export class ScrollUpHoverAction extends EditorAction {
172
173
constructor() {
174
super({
175
id: SCROLL_UP_HOVER_ACTION_ID,
176
label: nls.localize2({
177
key: 'scrollUpHover',
178
comment: [
179
'Action that allows to scroll up in the hover widget with the up arrow when the hover widget is focused.'
180
]
181
}, "Scroll Up Hover"),
182
precondition: EditorContextKeys.hoverFocused,
183
kbOpts: {
184
kbExpr: EditorContextKeys.hoverFocused,
185
primary: KeyCode.UpArrow,
186
weight: KeybindingWeight.EditorContrib
187
},
188
metadata: {
189
description: nls.localize2('scrollUpHoverDescription', 'Scroll up the editor hover.')
190
},
191
});
192
}
193
194
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
195
const controller = ContentHoverController.get(editor);
196
if (!controller) {
197
return;
198
}
199
controller.scrollUp();
200
}
201
}
202
203
export class ScrollDownHoverAction extends EditorAction {
204
205
constructor() {
206
super({
207
id: SCROLL_DOWN_HOVER_ACTION_ID,
208
label: nls.localize2({
209
key: 'scrollDownHover',
210
comment: [
211
'Action that allows to scroll down in the hover widget with the up arrow when the hover widget is focused.'
212
]
213
}, "Scroll Down Hover"),
214
precondition: EditorContextKeys.hoverFocused,
215
kbOpts: {
216
kbExpr: EditorContextKeys.hoverFocused,
217
primary: KeyCode.DownArrow,
218
weight: KeybindingWeight.EditorContrib
219
},
220
metadata: {
221
description: nls.localize2('scrollDownHoverDescription', 'Scroll down the editor hover.'),
222
},
223
});
224
}
225
226
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
227
const controller = ContentHoverController.get(editor);
228
if (!controller) {
229
return;
230
}
231
controller.scrollDown();
232
}
233
}
234
235
export class ScrollLeftHoverAction extends EditorAction {
236
237
constructor() {
238
super({
239
id: SCROLL_LEFT_HOVER_ACTION_ID,
240
label: nls.localize2({
241
key: 'scrollLeftHover',
242
comment: [
243
'Action that allows to scroll left in the hover widget with the left arrow when the hover widget is focused.'
244
]
245
}, "Scroll Left Hover"),
246
precondition: EditorContextKeys.hoverFocused,
247
kbOpts: {
248
kbExpr: EditorContextKeys.hoverFocused,
249
primary: KeyCode.LeftArrow,
250
weight: KeybindingWeight.EditorContrib
251
},
252
metadata: {
253
description: nls.localize2('scrollLeftHoverDescription', 'Scroll left the editor hover.'),
254
},
255
});
256
}
257
258
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
259
const controller = ContentHoverController.get(editor);
260
if (!controller) {
261
return;
262
}
263
controller.scrollLeft();
264
}
265
}
266
267
export class ScrollRightHoverAction extends EditorAction {
268
269
constructor() {
270
super({
271
id: SCROLL_RIGHT_HOVER_ACTION_ID,
272
label: nls.localize2({
273
key: 'scrollRightHover',
274
comment: [
275
'Action that allows to scroll right in the hover widget with the right arrow when the hover widget is focused.'
276
]
277
}, "Scroll Right Hover"),
278
precondition: EditorContextKeys.hoverFocused,
279
kbOpts: {
280
kbExpr: EditorContextKeys.hoverFocused,
281
primary: KeyCode.RightArrow,
282
weight: KeybindingWeight.EditorContrib
283
},
284
metadata: {
285
description: nls.localize2('scrollRightHoverDescription', 'Scroll right the editor hover.')
286
},
287
});
288
}
289
290
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
291
const controller = ContentHoverController.get(editor);
292
if (!controller) {
293
return;
294
}
295
controller.scrollRight();
296
}
297
}
298
299
export class PageUpHoverAction extends EditorAction {
300
301
constructor() {
302
super({
303
id: PAGE_UP_HOVER_ACTION_ID,
304
label: nls.localize2({
305
key: 'pageUpHover',
306
comment: [
307
'Action that allows to page up in the hover widget with the page up command when the hover widget is focused.'
308
]
309
}, "Page Up Hover"),
310
precondition: EditorContextKeys.hoverFocused,
311
kbOpts: {
312
kbExpr: EditorContextKeys.hoverFocused,
313
primary: KeyCode.PageUp,
314
secondary: [KeyMod.Alt | KeyCode.UpArrow],
315
weight: KeybindingWeight.EditorContrib
316
},
317
metadata: {
318
description: nls.localize2('pageUpHoverDescription', 'Page up the editor hover.'),
319
},
320
});
321
}
322
323
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
324
const controller = ContentHoverController.get(editor);
325
if (!controller) {
326
return;
327
}
328
controller.pageUp();
329
}
330
}
331
332
export class PageDownHoverAction extends EditorAction {
333
334
constructor() {
335
super({
336
id: PAGE_DOWN_HOVER_ACTION_ID,
337
label: nls.localize2({
338
key: 'pageDownHover',
339
comment: [
340
'Action that allows to page down in the hover widget with the page down command when the hover widget is focused.'
341
]
342
}, "Page Down Hover"),
343
precondition: EditorContextKeys.hoverFocused,
344
kbOpts: {
345
kbExpr: EditorContextKeys.hoverFocused,
346
primary: KeyCode.PageDown,
347
secondary: [KeyMod.Alt | KeyCode.DownArrow],
348
weight: KeybindingWeight.EditorContrib
349
},
350
metadata: {
351
description: nls.localize2('pageDownHoverDescription', 'Page down the editor hover.'),
352
},
353
});
354
}
355
356
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
357
const controller = ContentHoverController.get(editor);
358
if (!controller) {
359
return;
360
}
361
controller.pageDown();
362
}
363
}
364
365
export class GoToTopHoverAction extends EditorAction {
366
367
constructor() {
368
super({
369
id: GO_TO_TOP_HOVER_ACTION_ID,
370
label: nls.localize2({
371
key: 'goToTopHover',
372
comment: [
373
'Action that allows to go to the top of the hover widget with the home command when the hover widget is focused.'
374
]
375
}, "Go To Top Hover"),
376
precondition: EditorContextKeys.hoverFocused,
377
kbOpts: {
378
kbExpr: EditorContextKeys.hoverFocused,
379
primary: KeyCode.Home,
380
secondary: [KeyMod.CtrlCmd | KeyCode.UpArrow],
381
weight: KeybindingWeight.EditorContrib
382
},
383
metadata: {
384
description: nls.localize2('goToTopHoverDescription', 'Go to the top of the editor hover.'),
385
},
386
});
387
}
388
389
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
390
const controller = ContentHoverController.get(editor);
391
if (!controller) {
392
return;
393
}
394
controller.goToTop();
395
}
396
}
397
398
399
export class GoToBottomHoverAction extends EditorAction {
400
401
constructor() {
402
super({
403
id: GO_TO_BOTTOM_HOVER_ACTION_ID,
404
label: nls.localize2({
405
key: 'goToBottomHover',
406
comment: [
407
'Action that allows to go to the bottom in the hover widget with the end command when the hover widget is focused.'
408
]
409
}, "Go To Bottom Hover"),
410
precondition: EditorContextKeys.hoverFocused,
411
kbOpts: {
412
kbExpr: EditorContextKeys.hoverFocused,
413
primary: KeyCode.End,
414
secondary: [KeyMod.CtrlCmd | KeyCode.DownArrow],
415
weight: KeybindingWeight.EditorContrib
416
},
417
metadata: {
418
description: nls.localize2('goToBottomHoverDescription', 'Go to the bottom of the editor hover.')
419
},
420
});
421
}
422
423
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
424
const controller = ContentHoverController.get(editor);
425
if (!controller) {
426
return;
427
}
428
controller.goToBottom();
429
}
430
}
431
432
export class IncreaseHoverVerbosityLevel extends EditorAction {
433
434
constructor() {
435
super({
436
id: INCREASE_HOVER_VERBOSITY_ACTION_ID,
437
label: INCREASE_HOVER_VERBOSITY_ACTION_LABEL,
438
alias: 'Increase Hover Verbosity Level',
439
precondition: EditorContextKeys.hoverVisible
440
});
441
}
442
443
public run(accessor: ServicesAccessor, editor: ICodeEditor, args?: { index: number; focus: boolean }): void {
444
const hoverController = ContentHoverController.get(editor);
445
if (!hoverController) {
446
return;
447
}
448
const index = args?.index !== undefined ? args.index : hoverController.focusedHoverPartIndex();
449
hoverController.updateHoverVerbosityLevel(HoverVerbosityAction.Increase, index, args?.focus);
450
}
451
}
452
453
export class DecreaseHoverVerbosityLevel extends EditorAction {
454
455
constructor() {
456
super({
457
id: DECREASE_HOVER_VERBOSITY_ACTION_ID,
458
label: DECREASE_HOVER_VERBOSITY_ACTION_LABEL,
459
alias: 'Decrease Hover Verbosity Level',
460
precondition: EditorContextKeys.hoverVisible
461
});
462
}
463
464
public run(accessor: ServicesAccessor, editor: ICodeEditor, args?: { index: number; focus: boolean }): void {
465
const hoverController = ContentHoverController.get(editor);
466
if (!hoverController) {
467
return;
468
}
469
const index = args?.index !== undefined ? args.index : hoverController.focusedHoverPartIndex();
470
ContentHoverController.get(editor)?.updateHoverVerbosityLevel(HoverVerbosityAction.Decrease, index, args?.focus);
471
}
472
}
473
474