Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/parameterHints/browser/parameterHints.ts
3296 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 { KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';
7
import { Lazy } from '../../../../base/common/lazy.js';
8
import { Disposable } from '../../../../base/common/lifecycle.js';
9
import { ICodeEditor } from '../../../browser/editorBrowser.js';
10
import { EditorAction, EditorCommand, EditorContributionInstantiation, registerEditorAction, registerEditorCommand, registerEditorContribution, ServicesAccessor } from '../../../browser/editorExtensions.js';
11
import { IEditorContribution } from '../../../common/editorCommon.js';
12
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
13
import * as languages from '../../../common/languages.js';
14
import { ILanguageFeaturesService } from '../../../common/services/languageFeatures.js';
15
import { ParameterHintsModel, TriggerContext } from './parameterHintsModel.js';
16
import { Context } from './provideSignatureHelp.js';
17
import * as nls from '../../../../nls.js';
18
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
19
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
20
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
21
import { ParameterHintsWidget } from './parameterHintsWidget.js';
22
23
export class ParameterHintsController extends Disposable implements IEditorContribution {
24
25
public static readonly ID = 'editor.controller.parameterHints';
26
27
public static get(editor: ICodeEditor): ParameterHintsController | null {
28
return editor.getContribution<ParameterHintsController>(ParameterHintsController.ID);
29
}
30
31
private readonly editor: ICodeEditor;
32
private readonly model: ParameterHintsModel;
33
private readonly widget: Lazy<ParameterHintsWidget>;
34
35
constructor(
36
editor: ICodeEditor,
37
@IInstantiationService instantiationService: IInstantiationService,
38
@ILanguageFeaturesService languageFeaturesService: ILanguageFeaturesService,
39
) {
40
super();
41
42
this.editor = editor;
43
44
this.model = this._register(new ParameterHintsModel(editor, languageFeaturesService.signatureHelpProvider));
45
46
this._register(this.model.onChangedHints(newParameterHints => {
47
if (newParameterHints) {
48
this.widget.value.show();
49
this.widget.value.render(newParameterHints);
50
} else {
51
this.widget.rawValue?.hide();
52
}
53
}));
54
55
this.widget = new Lazy(() => this._register(instantiationService.createInstance(ParameterHintsWidget, this.editor, this.model)));
56
}
57
58
cancel(): void {
59
this.model.cancel();
60
}
61
62
previous(): void {
63
this.widget.rawValue?.previous();
64
}
65
66
next(): void {
67
this.widget.rawValue?.next();
68
}
69
70
trigger(context: TriggerContext): void {
71
this.model.trigger(context, 0);
72
}
73
}
74
75
export class TriggerParameterHintsAction extends EditorAction {
76
77
constructor() {
78
super({
79
id: 'editor.action.triggerParameterHints',
80
label: nls.localize2('parameterHints.trigger.label', "Trigger Parameter Hints"),
81
precondition: EditorContextKeys.hasSignatureHelpProvider,
82
kbOpts: {
83
kbExpr: EditorContextKeys.editorTextFocus,
84
primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.Space,
85
weight: KeybindingWeight.EditorContrib
86
}
87
});
88
}
89
90
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
91
const controller = ParameterHintsController.get(editor);
92
controller?.trigger({
93
triggerKind: languages.SignatureHelpTriggerKind.Invoke
94
});
95
}
96
}
97
98
registerEditorContribution(ParameterHintsController.ID, ParameterHintsController, EditorContributionInstantiation.BeforeFirstInteraction);
99
registerEditorAction(TriggerParameterHintsAction);
100
101
const weight = KeybindingWeight.EditorContrib + 75;
102
103
const ParameterHintsCommand = EditorCommand.bindToContribution<ParameterHintsController>(ParameterHintsController.get);
104
105
registerEditorCommand(new ParameterHintsCommand({
106
id: 'closeParameterHints',
107
precondition: Context.Visible,
108
handler: x => x.cancel(),
109
kbOpts: {
110
weight: weight,
111
kbExpr: EditorContextKeys.focus,
112
primary: KeyCode.Escape,
113
secondary: [KeyMod.Shift | KeyCode.Escape]
114
}
115
}));
116
117
registerEditorCommand(new ParameterHintsCommand({
118
id: 'showPrevParameterHint',
119
precondition: ContextKeyExpr.and(Context.Visible, Context.MultipleSignatures),
120
handler: x => x.previous(),
121
kbOpts: {
122
weight: weight,
123
kbExpr: EditorContextKeys.focus,
124
primary: KeyCode.UpArrow,
125
secondary: [KeyMod.Alt | KeyCode.UpArrow],
126
mac: { primary: KeyCode.UpArrow, secondary: [KeyMod.Alt | KeyCode.UpArrow, KeyMod.WinCtrl | KeyCode.KeyP] }
127
}
128
}));
129
130
registerEditorCommand(new ParameterHintsCommand({
131
id: 'showNextParameterHint',
132
precondition: ContextKeyExpr.and(Context.Visible, Context.MultipleSignatures),
133
handler: x => x.next(),
134
kbOpts: {
135
weight: weight,
136
kbExpr: EditorContextKeys.focus,
137
primary: KeyCode.DownArrow,
138
secondary: [KeyMod.Alt | KeyCode.DownArrow],
139
mac: { primary: KeyCode.DownArrow, secondary: [KeyMod.Alt | KeyCode.DownArrow, KeyMod.WinCtrl | KeyCode.KeyN] }
140
}
141
}));
142
143