-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathconsole-widget.ts
266 lines (227 loc) · 9.17 KB
/
console-widget.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { ElementExt } from '@phosphor/domutils';
import { injectable, inject, postConstruct, interfaces, Container } from 'inversify';
import { TreeSourceNode } from '@theia/core/lib/browser/source-tree';
import { ContextKey } from '@theia/core/lib/browser/context-key-service';
import { BaseWidget, PanelLayout, Widget, Message, MessageLoop, StatefulWidget, CompositeTreeNode } from '@theia/core/lib/browser';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import URI from '@theia/core/lib/common/uri';
import { MonacoEditorProvider } from '@theia/monaco/lib/browser/monaco-editor-provider';
import { ProtocolToMonacoConverter, MonacoToProtocolConverter } from 'monaco-languageclient/lib';
import { ConsoleHistory } from './console-history';
import { ConsoleContentWidget } from './console-content-widget';
import { ConsoleSession } from './console-session';
export const ConsoleOptions = Symbol('ConsoleWidgetOptions');
export interface ConsoleOptions {
id: string
title?: {
label?: string
iconClass?: string
caption?: string
}
input: {
uri: URI
options?: MonacoEditor.IOptions
}
inputFocusContextKey?: ContextKey<boolean>
}
@injectable()
export class ConsoleWidget extends BaseWidget implements StatefulWidget {
static styles = {
node: 'theia-console-widget',
content: 'theia-console-content',
input: 'theia-console-input',
};
static createContainer(parent: interfaces.Container, options: ConsoleOptions): Container {
const child = ConsoleContentWidget.createContainer(parent);
child.bind(ConsoleHistory).toSelf();
child.bind(ConsoleOptions).toConstantValue(options);
child.bind(ConsoleWidget).toSelf();
return child;
}
@inject(ConsoleOptions)
protected readonly options: ConsoleOptions;
@inject(MonacoToProtocolConverter)
protected readonly m2p: MonacoToProtocolConverter;
@inject(ProtocolToMonacoConverter)
protected readonly p2m: ProtocolToMonacoConverter;
@inject(ConsoleContentWidget)
readonly content: ConsoleContentWidget;
@inject(ConsoleHistory)
protected readonly history: ConsoleHistory;
@inject(MonacoEditorProvider)
protected readonly editorProvider: MonacoEditorProvider;
protected _input: MonacoEditor;
constructor() {
super();
this.node.classList.add(ConsoleWidget.styles.node);
}
@postConstruct()
protected async init(): Promise<void> {
const { id, title, inputFocusContextKey } = this.options;
const { label, iconClass, caption } = Object.assign({}, title);
this.id = id;
this.title.closable = true;
this.title.label = label || id;
if (iconClass) {
this.title.iconClass = iconClass;
}
this.title.caption = caption || label || id;
const layout = this.layout = new PanelLayout();
this.content.node.classList.add(ConsoleWidget.styles.content);
this.toDispose.push(this.content);
layout.addWidget(this.content);
const inputWidget = new Widget();
inputWidget.node.classList.add(ConsoleWidget.styles.input);
layout.addWidget(inputWidget);
const input = this._input = await this.createInput(inputWidget.node);
this.toDispose.push(input);
this.toDispose.push(input.getControl().onDidLayoutChange(() => this.resizeContent()));
// todo update font if fontInfo was changed only
// it's impossible at the moment, but will be fixed for next upgrade of monaco version
// see https://github.com/microsoft/vscode/commit/5084e8ca1935698c98c163e339ca664818786c6d
this.toDispose.push(input.getControl().onDidChangeConfiguration(() => this.updateFont()));
this.updateFont();
if (inputFocusContextKey) {
this.toDispose.push(input.onFocusChanged(() => inputFocusContextKey.set(this.hasInputFocus())));
}
}
protected createInput(node: HTMLElement): Promise<MonacoEditor> {
return this.editorProvider.createInline(this.options.input.uri, node, this.options.input.options);
}
protected updateFont(): void {
const { fontFamily, fontSize, lineHeight } = this._input.getControl().getOption(monaco.editor.EditorOption.fontInfo);
this.content.node.style.fontFamily = fontFamily;
this.content.node.style.fontSize = fontSize + 'px';
this.content.node.style.lineHeight = lineHeight + 'px';
}
protected _session: ConsoleSession | undefined;
set session(session: ConsoleSession | undefined) {
if (this._session === session) {
return;
}
this._session = session;
this.content.source = session;
}
get session(): ConsoleSession | undefined {
return this._session;
}
get input(): MonacoEditor {
return this._input;
}
selectAll(): void {
const selection = document.getSelection();
if (selection) {
selection.selectAllChildren(this.content.node);
}
}
collapseAll(): void {
const { root } = this.content.model;
if (CompositeTreeNode.is(root)) {
this.content.model.collapseAll(root);
}
}
clear(): void {
if (this.session) {
this.session.clear();
}
}
async execute(): Promise<void> {
const value = this._input.getControl().getValue();
this._input.getControl().setValue('');
this.history.push(value);
if (this.session) {
const listener = this.content.model.onNodeRefreshed(() => {
listener.dispose();
this.revealLastOutput();
});
await this.session.execute(value);
}
}
navigateBack(): void {
const value = this.history.previous;
if (value === undefined) {
return;
}
const editor = this.input.getControl();
editor.setValue(value);
editor.setPosition({
lineNumber: 1,
column: 1
});
}
navigateForward(): void {
const value = this.history.next || '';
const editor = this.input.getControl();
editor.setValue(value);
const lineNumber = editor.getModel()!.getLineCount();
const column = editor.getModel()!.getLineMaxColumn(lineNumber);
editor.setPosition({ lineNumber, column });
}
protected revealLastOutput(): void {
const { root } = this.content.model;
if (TreeSourceNode.is(root)) {
this.content.model.selectNode(root.children[root.children.length - 1]);
}
}
protected onActivateRequest(msg: Message): void {
super.onActivateRequest(msg);
this._input.focus();
}
protected totalHeight = -1;
protected totalWidth = -1;
protected onResize(msg: Widget.ResizeMessage): void {
super.onResize(msg);
this.totalWidth = msg.width;
this.totalHeight = msg.height;
this._input.resizeToFit();
this.resizeContent();
}
protected resizeContent(): void {
this.totalHeight = this.totalHeight < 0 ? this.computeHeight() : this.totalHeight;
const inputHeight = this._input.getControl().getLayoutInfo().height;
const contentHeight = this.totalHeight - inputHeight;
this.content.node.style.height = `${contentHeight}px`;
MessageLoop.sendMessage(this.content, new Widget.ResizeMessage(this.totalWidth, contentHeight));
}
protected computeHeight(): number {
const { verticalSum } = ElementExt.boxSizing(this.node);
return this.node.offsetHeight - verticalSum;
}
storeState(): object {
const history = this.history.store();
const input = this.input.storeViewState();
return {
history,
input
};
}
restoreState(oldState: object): void {
if ('history' in oldState) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.history.restore((<any>oldState)['history']);
}
this.input.getControl().setValue(this.history.current || '');
if ('input' in oldState) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.input.restoreViewState((<any>oldState)['input']);
}
}
hasInputFocus(): boolean {
return this._input && this._input.isFocused({ strict: true });
}
}