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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
// Copyright (c) 2021, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import _ from 'underscore';
import { Container } from 'golden-layout';
import * as monaco from 'monaco-editor';
import { MonacoPaneState, PaneState } from './pane.interfaces';
import { FontScale } from '../widgets/fontscale';
import { SiteSettings } from '../settings';
import * as utils from '../utils';
import { PaneRenaming } from '../widgets/pane-renaming';
import { EventHub } from '../event-hub';
import { Hub } from '../hub';
class PaneCompilerState {
compilerId: number;
compilerName: string;
editorId?: number;
treeId?: number;
}
/**
* Basic container for a tool pane in Compiler Explorer.
*
* Type parameter S refers to a state interface for the pane
*/
export abstract class Pane<S> {
compilerInfo: PaneCompilerState;
container: Container;
domRoot: JQuery;
topBar: JQuery;
hideable: JQuery;
eventHub: EventHub;
isAwaitingInitialResults = false;
settings: SiteSettings | Record<string, never> = {};
paneName: string;
paneRenaming: PaneRenaming;
/**
* Base constructor for any pane. Performs common initialization tasks such
* as registering standard event listeners and lifecycle handlers.
*
* Overridable for implementors
*/
protected constructor(hub: Hub, container: Container, state: S & PaneState) {
this.container = container;
this.eventHub = hub.createEventHub();
this.domRoot = container.getElement();
this.hideable = this.domRoot.find('.hideable');
this.domRoot.html(this.getInitialHTML());
this.compilerInfo = {
compilerId: state.id,
compilerName: state.compilerName,
editorId: state.editorid,
treeId: state.treeid,
};
this.topBar = this.domRoot.find('.top-bar');
this.paneRenaming = new PaneRenaming(this, state);
this.registerButtons(state);
this.registerStandardCallbacks();
this.registerCallbacks();
this.registerOpeningAnalyticsEvent();
}
/**
* Get the initial HTML layout for the pane's default content. A typical
* implementation looks like this:
*
* ```ts
* override getInitialHTML(): string {
* return $('#rustmir').html();
* }
* ```
*/
abstract getInitialHTML(): string;
/**
* Emit analytics event for opening the pane tab. Typical implementation
* looks like this:
*
* ```ts
* ga.proxy('send', {
* hitType: 'event',
* eventCategory: 'OpenViewPane',
* eventAction: 'RustMir',
* });
* ```
*/
abstract registerOpeningAnalyticsEvent(): void
/** Optionally overridable code for initializing pane buttons */
registerButtons(state: S): void {
}
/** Optionally overridable code for initializing event callbacks */
registerCallbacks(): void {
}
/**
* Handle user selected compiler change.
*
* This event is triggered when the user selects a different compiler in the
* compiler dropdown.
*
* Note that this event is also triggered when the changed compiler is not
* the one this view is attached to. Therefore it is smart to check that
* the updated compiler is the one the view is attached to. This can be done
* with a simple check.
*
* ```ts
* if (this.compilerInfo.compilerId === compilerId) { ... }
* ```
*
* @param compilerId - Id of the compiler that had its version changed
* @param compiler - The updated compiler object
* @param options
* @param editorId - The editor id the updated compiler is attached to
*/
abstract onCompiler(compilerId: number, compiler: unknown, options: unknown, editorId: number,
treeId: number): void;
/**
* Handle compilation result.
*
* This event is triggered when a code compilation was triggered.
*
* Note that this event is triggered for *any* compilation, even when the
* compilation was done for a source/compiler this view is not attached to.
* Therefore it is smart to check that the updated compiler is the one the
* view is attached to. This can be done with a simple check.
*
* ```ts
* if (this.compilerInfo.compilerId === compilerId) { ... }
* ```
*
* @param compilerId - Id of the compiler that had a compilation
* @param compiler - The compiler object
* @param result - The entire HTTP request response
*/
abstract onCompileResult(compilerId: number, compiler: unknown, result: unknown): void;
/**
* Perform any clean-up events when the pane is closed.
*
* This is typically used to emit an analytics event for closing the pane,
* unsubscribing from the event hub and disposing the monaco editor.
*/
abstract close(): void;
/** Initialize standard lifecycle hooks */
protected registerStandardCallbacks(): void {
this.paneRenaming.on('renamePane', this.updateState.bind(this));
this.container.on('destroy', this.close.bind(this));
this.container.on('resize', this.resize.bind(this));
this.eventHub.on('compileResult', this.onCompileResult.bind(this));
this.eventHub.on('compiler', this.onCompiler.bind(this));
this.eventHub.on('compilerClose', this.onCompilerClose.bind(this));
this.eventHub.on('settingsChange', this.onSettingsChange.bind(this));
this.eventHub.on('shown', this.resize.bind(this));
this.eventHub.on('resize', this.resize.bind(this));
}
/**
* Produce a default name for the pane. Typical implementation
* looks like this:
*
* ```ts
* return 'Rust MIR Viewer';
* ```
*/
abstract getDefaultPaneName(): string;
/** Generate "(Editor #1, Compiler #1)" tag */
protected getPaneTag() {
const { compilerName, editorId, treeId, compilerId } = this.compilerInfo;
if(editorId) {
return `${compilerName} (Editor #${editorId}, Compiler #${compilerId})`;
} else {
return `${compilerName} (Tree #${treeId}, Compiler #${compilerId})`;
}
}
/** Get name for the pane */
protected getPaneName() {
return this.paneName ?? this.getDefaultPaneName() + ' ' + this.getPaneTag();
}
/** Update the pane's title, called when the pane name or compiler info changes */
protected updateTitle() {
this.container.setTitle(_.escape(this.getPaneName()));
}
/** Close the pane if the compiler this pane was attached to closes */
protected onCompilerClose(compilerId: number) {
if (this.compilerInfo.compilerId === compilerId) {
_.defer(() => this.container.close());
}
}
protected onSettingsChange(settings: SiteSettings) {
this.settings = settings;
}
getCurrentState(): PaneState {
const state = {
id: this.compilerInfo.compilerId,
compilerName: this.compilerInfo.compilerName,
editorid: this.compilerInfo.editorId,
treeid: this.compilerInfo.treeId,
};
this.paneRenaming.addState(state);
return state;
}
updateState() {
this.container.setState(this.getCurrentState());
}
abstract resize(): void;
}
/**
* Basic container for a tool pane with a monaco editor in Compiler Explorer.
*
* Type parameter E indicates which monaco editor kind this pane hosts. Common
* values are monaco.editor.IDiffEditor and monaco.ICodeEditor
*
* Type parameter S refers to a state interface for the pane
*/
export abstract class MonacoPane<E extends monaco.editor.IEditor, S> extends Pane<S> {
editor: E;
selection: monaco.Selection;
fontScale: FontScale;
protected constructor(hub: any /* Hub */, container: Container, state: S & MonacoPaneState) {
super(hub, container, state);
this.selection = state.selection;
this.registerEditorActions();
}
registerButtons(state: S): void {
const editorRoot = this.domRoot.find('.monaco-placeholder')[0];
this.editor = this.createEditor(editorRoot);
this.fontScale = new FontScale(this.domRoot, state, this.editor);
}
getCurrentState(): MonacoPaneState {
const parent = super.getCurrentState();
const state: MonacoPaneState = {
selection: this.selection,
...parent,
};
this.fontScale.addState(state);
return state;
}
resize() {
const topBarHeight = utils.updateAndCalcTopBarHeight(this.domRoot,
this.topBar, this.hideable);
if (!this.editor) return;
this.editor.layout({
width: this.domRoot.width() as number,
height: this.domRoot.height() as number - topBarHeight,
});
}
/**
* Initialize the monaco editor instance. Typical implementation for looks
* like this:
*
* ```ts
* return monaco.editor.create(editorRoot, extendConfig({
* // goodies
* }));
* ```
*/
abstract createEditor(editorRoot: HTMLElement): E;
protected onSettingsChange(settings: SiteSettings) {
super.onSettingsChange(settings);
this.editor.updateOptions({
contextmenu: settings.useCustomContextMenu,
minimap: {
enabled: settings.showMinimap,
},
fontFamily: settings.editorsFFont,
fontLigatures: settings.editorsFLigatures,
});
}
protected onDidChangeCursorSelection(event: monaco.editor.ICursorSelectionChangedEvent) {
if (this.isAwaitingInitialResults) {
this.selection = event.selection;
this.updateState();
}
}
/** Initialize standard lifecycle hooks */
protected registerStandardCallbacks(): void {
super.registerStandardCallbacks();
this.fontScale.on('change', this.updateState.bind(this));
}
/**
* Optionally overridable code for initializing monaco actions on the
* editor instance
*/
registerEditorActions(): void {
}
}
|