jupyterlab-emrys
Version:
A computational environment for Jupyter. Powered by Emrys
206 lines (205 loc) • 6.79 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";
var phosphor_signaling_1 = require('phosphor-signaling');
/**
* An object that handles code inspection.
*/
var InspectionHandler = (function () {
/**
* Construct a new inspection handler for a widget.
*/
function InspectionHandler(rendermime) {
this._activeCell = null;
this._isDisposed = false;
this._kernel = null;
this._pending = 0;
this._rendermime = null;
this._rendermime = rendermime;
}
Object.defineProperty(InspectionHandler.prototype, "activeCell", {
/**
* The cell widget used by the inspection handler.
*/
get: function () {
return this._activeCell;
},
set: function (newValue) {
if (newValue === this._activeCell) {
return;
}
if (this._activeCell && !this._activeCell.isDisposed) {
this._activeCell.editor.textChanged.disconnect(this.onTextChanged, this);
}
this._activeCell = newValue;
if (this._activeCell) {
// Clear ephemeral inspectors in preparation for a new editor.
this.clearEphemeral.emit(void 0);
this._activeCell.editor.textChanged.connect(this.onTextChanged, this);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectionHandler.prototype, "kernel", {
/**
* The kernel used by the inspection handler.
*/
get: function () {
return this._kernel;
},
set: function (value) {
this._kernel = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectionHandler.prototype, "clearEphemeral", {
/**
* A signal emitted when inspector should clear all items with no history.
*/
get: function () {
return Private.clearEphemeralSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectionHandler.prototype, "disposed", {
/**
* A signal emitted when the handler is disposed.
*/
get: function () {
return Private.disposedSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectionHandler.prototype, "inspected", {
/**
* A signal emitted when an inspector value is generated.
*/
get: function () {
return Private.inspectedSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectionHandler.prototype, "isDisposed", {
/**
* Get whether the inspection handler is disposed.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return this._isDisposed;
},
enumerable: true,
configurable: true
});
/**
* Dispose of the resources used by the handler.
*/
InspectionHandler.prototype.dispose = function () {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
this._activeCell = null;
this.disposed.emit(void 0);
phosphor_signaling_1.clearSignalData(this);
};
/**
* Update the inspector based on page outputs from the kernel.
*
* #### Notes
* Payloads are deprecated and there are no official interfaces for them in
* the kernel type definitions.
* See [Payloads (DEPRECATED)](http://jupyter-client.readthedocs.io/en/latest/messaging.html#payloads-deprecated).
*/
InspectionHandler.prototype.handleExecuteReply = function (content) {
var update = {
content: null,
type: 'details'
};
if (!content || !content.payload || !content.payload.length) {
this.inspected.emit(update);
return;
}
var details = content.payload.filter(function (i) { return i.source === 'page'; })[0];
if (details) {
var bundle = details.data;
var widget = this._rendermime.render(bundle, true);
update.content = widget;
this.inspected.emit(update);
return;
}
this.inspected.emit(update);
};
/**
* Handle a text changed signal from an editor.
*
* #### Notes
* Update the hints inspector based on a text change.
*/
InspectionHandler.prototype.onTextChanged = function (editor, change) {
var _this = this;
var update = {
content: null,
type: 'hints'
};
// Clear hints if the new text value is empty or kernel is unavailable.
if (!change.newValue || !this._kernel) {
this.inspected.emit(update);
return;
}
var contents = {
code: change.newValue,
cursor_pos: change.position,
detail_level: 0
};
var pending = ++this._pending;
this._kernel.inspect(contents).then(function (msg) {
var value = msg.content;
// If handler has been disposed, bail.
if (_this.isDisposed) {
_this.inspected.emit(update);
return;
}
// If a newer text change has created a pending request, bail.
if (pending !== _this._pending) {
_this.inspected.emit(update);
return;
}
// Hint request failures or negative results fail silently.
if (value.status !== 'ok' || !value.found) {
_this.inspected.emit(update);
return;
}
var bundle = value.data;
var widget = _this._rendermime.render(bundle, true);
update.content = widget;
_this.inspected.emit(update);
});
};
return InspectionHandler;
}());
exports.InspectionHandler = InspectionHandler;
/**
* A namespace for inspector handler private data.
*/
var Private;
(function (Private) {
/**
* A signal emitted when inspector should clear all items with no history.
*/
Private.clearEphemeralSignal = new phosphor_signaling_1.Signal();
/**
* A signal emitted when the handler is disposed.
*/
Private.disposedSignal = new phosphor_signaling_1.Signal();
/**
* A signal emitted when an inspector value is generated.
*/
Private.inspectedSignal = new phosphor_signaling_1.Signal();
})(Private || (Private = {}));