/
handler.ts
233 lines (200 loc) · 5.72 KB
/
handler.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { CodeEditor } from '@jupyterlab/codeeditor';
import { IDataConnector, Text } from '@jupyterlab/coreutils';
import { MimeModel, RenderMimeRegistry } from '@jupyterlab/rendermime';
import { ReadonlyJSONObject } from '@phosphor/coreutils';
import { IDisposable } from '@phosphor/disposable';
import { ISignal, Signal } from '@phosphor/signaling';
import { IInspector } from './inspector';
/**
* An object that handles code inspection.
*/
export class InspectionHandler implements IDisposable, IInspector.IInspectable {
/**
* Construct a new inspection handler for a widget.
*/
constructor(options: InspectionHandler.IOptions) {
this._connector = options.connector;
this._rendermime = options.rendermime;
}
/**
* A signal emitted when the inspector should clear all items.
*/
get cleared(): ISignal<InspectionHandler, void> {
return this._cleared;
}
/**
* A signal emitted when the handler is disposed.
*/
get disposed(): ISignal<InspectionHandler, void> {
return this._disposed;
}
/**
* A signal emitted when an inspector value is generated.
*/
get inspected(): ISignal<InspectionHandler, IInspector.IInspectorUpdate> {
return this._inspected;
}
/**
* The editor widget used by the inspection handler.
*/
get editor(): CodeEditor.IEditor | null {
return this._editor;
}
set editor(newValue: CodeEditor.IEditor | null) {
if (newValue === this._editor) {
return;
}
if (this._editor && !this._editor.isDisposed) {
this._editor.model.value.changed.disconnect(this.onTextChanged, this);
}
let editor = (this._editor = newValue);
if (editor) {
// Clear the inspector in preparation for a new editor.
this._cleared.emit(void 0);
editor.model.value.changed.connect(
this.onTextChanged,
this
);
}
}
/**
* Indicates whether the handler makes API inspection requests or stands by.
*
* #### Notes
* The use case for this attribute is to limit the API traffic when no
* inspector is visible.
*/
get standby(): boolean {
return this._standby;
}
set standby(value: boolean) {
this._standby = value;
}
/**
* Get whether the inspection handler is disposed.
*
* #### Notes
* This is a read-only property.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources used by the handler.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
this._disposed.emit(void 0);
Signal.clearData(this);
}
/**
* Handle a text changed signal from an editor.
*
* #### Notes
* Update the hints inspector based on a text change.
*/
protected onTextChanged(): void {
// If the handler is in standby mode, bail.
if (this._standby) {
return;
}
const editor = this.editor;
if (!editor) {
return;
}
const text = editor.model.value.text;
const position = editor.getCursorPosition();
const offset = Text.jsIndexToCharIndex(editor.getOffsetAt(position), text);
const update: IInspector.IInspectorUpdate = { content: null };
const pending = ++this._pending;
this._connector
.fetch({ offset, text })
.then(reply => {
// If handler has been disposed or a newer request is pending, bail.
if (this.isDisposed || pending !== this._pending) {
this._inspected.emit(update);
return;
}
const { data } = reply;
const mimeType = this._rendermime.preferredMimeType(data);
if (mimeType) {
const widget = this._rendermime.createRenderer(mimeType);
const model = new MimeModel({ data });
widget.renderModel(model);
update.content = widget;
}
this._inspected.emit(update);
})
.catch(reason => {
// Since almost all failures are benign, fail silently.
this._inspected.emit(update);
});
}
private _cleared = new Signal<InspectionHandler, void>(this);
private _connector: IDataConnector<
InspectionHandler.IReply,
void,
InspectionHandler.IRequest
>;
private _disposed = new Signal<this, void>(this);
private _editor: CodeEditor.IEditor | null = null;
private _inspected = new Signal<this, IInspector.IInspectorUpdate>(this);
private _isDisposed = false;
private _pending = 0;
private _rendermime: RenderMimeRegistry;
private _standby = true;
}
/**
* A namespace for inspection handler statics.
*/
export namespace InspectionHandler {
/**
* The instantiation options for an inspection handler.
*/
export interface IOptions {
/**
* The connector used to make inspection requests.
*
* #### Notes
* The only method of this connector that will ever be called is `fetch`, so
* it is acceptable for the other methods to be simple functions that return
* rejected promises.
*/
connector: IDataConnector<IReply, void, IRequest>;
/**
* The mime renderer for the inspection handler.
*/
rendermime: RenderMimeRegistry;
}
/**
* A reply to an inspection request.
*/
export interface IReply {
/**
* The MIME bundle data returned from an inspection request.
*/
data: ReadonlyJSONObject;
/**
* Any metadata that accompanies the MIME bundle returning from a request.
*/
metadata: ReadonlyJSONObject;
}
/**
* The details of an inspection request.
*/
export interface IRequest {
/**
* The cursor offset position within the text being inspected.
*/
offset: number;
/**
* The text being inspected.
*/
text: string;
}
}