-
Notifications
You must be signed in to change notification settings - Fork 28.9k
/
standaloneQuickInputService.ts
205 lines (164 loc) · 7.68 KB
/
standaloneQuickInputService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./standaloneQuickInput';
import { Event } from 'vs/base/common/event';
import { ICodeEditor, IOverlayWidget, IOverlayWidgetPosition, OverlayWidgetPositionPreference } from 'vs/editor/browser/editorBrowser';
import { EditorContributionInstantiation, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { IQuickInputService, IQuickPickItem, IQuickPick, IInputBox, IQuickNavigateConfiguration, IPickOptions, QuickPickInput, IInputOptions, IQuickWidget } from 'vs/platform/quickinput/common/quickInput';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { EditorScopedLayoutService } from 'vs/editor/standalone/browser/standaloneLayoutService';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { QuickInputController, IQuickInputControllerHost } from 'vs/platform/quickinput/browser/quickInputController';
import { QuickInputService } from 'vs/platform/quickinput/browser/quickInputService';
import { createSingleCallFunction } from 'vs/base/common/functional';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
class EditorScopedQuickInputService extends QuickInputService {
private host: IQuickInputControllerHost | undefined = undefined;
constructor(
editor: ICodeEditor,
@IInstantiationService instantiationService: IInstantiationService,
@IContextKeyService contextKeyService: IContextKeyService,
@IThemeService themeService: IThemeService,
@ICodeEditorService codeEditorService: ICodeEditorService,
@IConfigurationService configurationService: IConfigurationService,
) {
super(
instantiationService,
contextKeyService,
themeService,
new EditorScopedLayoutService(editor.getContainerDomNode(), codeEditorService),
configurationService,
);
// Use the passed in code editor as host for the quick input widget
const contribution = QuickInputEditorContribution.get(editor);
if (contribution) {
const widget = contribution.widget;
this.host = {
_serviceBrand: undefined,
get mainContainer() { return widget.getDomNode(); },
getContainer() { return widget.getDomNode(); },
whenContainerStylesLoaded() { return undefined; },
get containers() { return [widget.getDomNode()]; },
get activeContainer() { return widget.getDomNode(); },
get mainContainerDimension() { return editor.getLayoutInfo(); },
get activeContainerDimension() { return editor.getLayoutInfo(); },
get onDidLayoutMainContainer() { return editor.onDidLayoutChange; },
get onDidLayoutActiveContainer() { return editor.onDidLayoutChange; },
get onDidLayoutContainer() { return Event.map(editor.onDidLayoutChange, dimension => ({ container: widget.getDomNode(), dimension })); },
get onDidChangeActiveContainer() { return Event.None; },
get onDidAddContainer() { return Event.None; },
get mainContainerOffset() { return { top: 0, quickPickTop: 0 }; },
get activeContainerOffset() { return { top: 0, quickPickTop: 0 }; },
focus: () => editor.focus()
};
} else {
this.host = undefined;
}
}
protected override createController(): QuickInputController {
return super.createController(this.host);
}
}
export class StandaloneQuickInputService implements IQuickInputService {
declare readonly _serviceBrand: undefined;
private mapEditorToService = new Map<ICodeEditor, EditorScopedQuickInputService>();
private get activeService(): IQuickInputService {
const editor = this.codeEditorService.getFocusedCodeEditor();
if (!editor) {
throw new Error('Quick input service needs a focused editor to work.');
}
// Find the quick input implementation for the focused
// editor or create it lazily if not yet created
let quickInputService = this.mapEditorToService.get(editor);
if (!quickInputService) {
const newQuickInputService = quickInputService = this.instantiationService.createInstance(EditorScopedQuickInputService, editor);
this.mapEditorToService.set(editor, quickInputService);
createSingleCallFunction(editor.onDidDispose)(() => {
newQuickInputService.dispose();
this.mapEditorToService.delete(editor);
});
}
return quickInputService;
}
get currentQuickInput() { return this.activeService.currentQuickInput; }
get quickAccess() { return this.activeService.quickAccess; }
get backButton() { return this.activeService.backButton; }
get onShow() { return this.activeService.onShow; }
get onHide() { return this.activeService.onHide; }
constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
@ICodeEditorService private readonly codeEditorService: ICodeEditorService
) {
}
pick<T extends IQuickPickItem, O extends IPickOptions<T>>(picks: Promise<QuickPickInput<T>[]> | QuickPickInput<T>[], options: O = <O>{}, token: CancellationToken = CancellationToken.None): Promise<(O extends { canPickMany: true } ? T[] : T) | undefined> {
return (this.activeService as unknown as QuickInputController /* TS fail */).pick(picks, options, token);
}
input(options?: IInputOptions | undefined, token?: CancellationToken | undefined): Promise<string | undefined> {
return this.activeService.input(options, token);
}
createQuickPick<T extends IQuickPickItem>(): IQuickPick<T> {
return this.activeService.createQuickPick();
}
createInputBox(): IInputBox {
return this.activeService.createInputBox();
}
createQuickWidget(): IQuickWidget {
return this.activeService.createQuickWidget();
}
focus(): void {
return this.activeService.focus();
}
toggle(): void {
return this.activeService.toggle();
}
navigate(next: boolean, quickNavigate?: IQuickNavigateConfiguration | undefined): void {
return this.activeService.navigate(next, quickNavigate);
}
accept(): Promise<void> {
return this.activeService.accept();
}
back(): Promise<void> {
return this.activeService.back();
}
cancel(): Promise<void> {
return this.activeService.cancel();
}
}
export class QuickInputEditorContribution implements IEditorContribution {
static readonly ID = 'editor.controller.quickInput';
static get(editor: ICodeEditor): QuickInputEditorContribution | null {
return editor.getContribution<QuickInputEditorContribution>(QuickInputEditorContribution.ID);
}
readonly widget = new QuickInputEditorWidget(this.editor);
constructor(private editor: ICodeEditor) { }
dispose(): void {
this.widget.dispose();
}
}
export class QuickInputEditorWidget implements IOverlayWidget {
private static readonly ID = 'editor.contrib.quickInputWidget';
private domNode: HTMLElement;
constructor(private codeEditor: ICodeEditor) {
this.domNode = document.createElement('div');
this.codeEditor.addOverlayWidget(this);
}
getId(): string {
return QuickInputEditorWidget.ID;
}
getDomNode(): HTMLElement {
return this.domNode;
}
getPosition(): IOverlayWidgetPosition | null {
return { preference: OverlayWidgetPositionPreference.TOP_CENTER };
}
dispose(): void {
this.codeEditor.removeOverlayWidget(this);
}
}
registerEditorContribution(QuickInputEditorContribution.ID, QuickInputEditorContribution, EditorContributionInstantiation.Lazy);