Skip to content

Commit 2c8f365

Browse files
committed
Fix #24 (events in FileEditor), release 0.5.0-rc.2
1 parent e69cd5d commit 2c8f365

File tree

5 files changed

+57
-46
lines changed

5 files changed

+57
-46
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@krassowski/jupyterlab-lsp",
3-
"version": "0.5.0-rc.1",
3+
"version": "0.5.0-rc.2",
44
"description": "Language Server Protocol integration for JupyterLab",
55
"keywords": [
66
"jupyter",

src/adapters/codemirror.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,16 @@ export class CodeMirrorAdapterExtension extends CodeMirrorAdapter {
397397
let root_position = this.editor
398398
.getDoc()
399399
.getCursor('start') as IRootPosition;
400-
let document = this.editor.document_at_root_position(root_position);
400+
let document: VirtualDocument;
401+
try {
402+
document = this.editor.document_at_root_position(root_position);
403+
} catch (e) {
404+
console.warn(
405+
'Could not obtain virtual document from position',
406+
root_position
407+
);
408+
return;
409+
}
401410
if (document !== this.virtual_document) {
402411
return;
403412
}

src/virtual/editor.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
import { until_ready } from '../utils';
1212
import { Signal } from '@phosphor/signaling';
1313

14+
type CodeMirrorHandler = (instance: any, ...args: any[]) => void;
15+
type WrappedHandler = (instance: CodeMirror.Editor, ...args: any[]) => void;
16+
1417
/**
1518
* VirtualEditor extends the CodeMirror.Editor interface; its subclasses may either
1619
* fast-forward any requests to an existing instance of the CodeMirror.Editor
@@ -158,6 +161,44 @@ export abstract class VirtualEditor implements CodeMirror.Editor {
158161
): IEditorPosition {
159162
return this.virtual_document.root.transform_source_to_editor(root_position);
160163
}
164+
165+
abstract forEveryBlockEditor(
166+
callback: (cm_editor: CodeMirror.Editor) => void
167+
): void;
168+
169+
private _event_wrappers = new Map<CodeMirrorHandler, WrappedHandler>();
170+
171+
/**
172+
* Proxy the event handler binding to the CodeMirror editors,
173+
* allowing for multiple actual editors per a virtual editor.
174+
*
175+
* Only handlers accepting CodeMirror.Editor are supported for simplicity.
176+
*/
177+
on(eventName: string, handler: CodeMirrorHandler, ...args: any[]): void {
178+
let wrapped_handler = (instance: CodeMirror.Editor, ...args: any[]) => {
179+
try {
180+
return handler(this, ...args);
181+
} catch (error) {
182+
console.warn(
183+
'Wrapped handler (which should accept a CodeMirror Editor instance) failed',
184+
{ error, instance, args, this: this }
185+
);
186+
}
187+
};
188+
this._event_wrappers.set(handler, wrapped_handler);
189+
190+
this.forEveryBlockEditor(cm_editor => {
191+
cm_editor.on(eventName, wrapped_handler);
192+
});
193+
}
194+
195+
off(eventName: string, handler: CodeMirrorHandler, ...args: any[]): void {
196+
let wrapped_handler = this._event_wrappers.get(handler);
197+
198+
this.forEveryBlockEditor(cm_editor => {
199+
cm_editor.off(eventName, wrapped_handler);
200+
});
201+
}
161202
}
162203

163204
// tslint:disable-next-line:interface-name

src/virtual/editors/file_editor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class VirtualFileEditor extends VirtualEditor {
1717
prop: keyof CodeMirror.Editor,
1818
receiver: any
1919
) {
20-
if (prop in cm_editor) {
20+
if (prop in cm_editor && !(prop in target)) {
2121
return cm_editor[prop];
2222
} else {
2323
return Reflect.get(target, prop, receiver);
@@ -58,4 +58,8 @@ export class VirtualFileEditor extends VirtualEditor {
5858
addEventListener(type: string, listener: EventListenerOrEventListenerObject) {
5959
this.cm_editor.getWrapperElement().addEventListener(type, listener);
6060
}
61+
62+
forEveryBlockEditor(callback: (cm_editor: CodeMirror.Editor) => void): void {
63+
callback(this.cm_editor);
64+
}
6165
}

src/virtual/editors/notebook.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -322,49 +322,6 @@ export class VirtualEditorForNotebook extends VirtualEditor {
322322
return 0;
323323
}
324324

325-
off(
326-
eventName: string,
327-
handler: (doc_or_instance: any, ...args: any[]) => void,
328-
...args: any[]
329-
): void {
330-
let wrapped_handler = this._event_wrappers.get(handler);
331-
332-
this.forEveryBlockEditor(cm_editor => {
333-
// @ts-ignore
334-
cm_editor.off(eventName, wrapped_handler);
335-
});
336-
}
337-
338-
private _event_wrappers = new Map<Function, Function>();
339-
340-
on(
341-
eventName: string,
342-
handler: (doc_or_instance: any, ...args: any[]) => void,
343-
...args: any[]
344-
): void {
345-
let wrapped_handler = (instance_or_doc: any, a: any, b: any, c: any) => {
346-
let editor = instance_or_doc as CodeMirror.Editor;
347-
try {
348-
editor.getDoc();
349-
// @ts-ignore
350-
return handler(this, a, b, c);
351-
} catch (e) {
352-
// TODO verify that the error was due to getDoc not existing on editor
353-
console.log(e);
354-
// also this is not currently in use
355-
console.log('Dispatching wrapped doc handler with', this);
356-
// @ts-ignore
357-
return handler(this.getDoc(), a, b, c);
358-
}
359-
};
360-
this._event_wrappers.set(handler, wrapped_handler);
361-
362-
this.forEveryBlockEditor(cm_editor => {
363-
// @ts-ignore
364-
cm_editor.on(eventName, wrapped_handler);
365-
});
366-
}
367-
368325
addEventListener(type: string, listener: EventListenerOrEventListenerObject) {
369326
this.forEveryBlockEditor(cm_editor => {
370327
cm_editor.getWrapperElement().addEventListener(type, listener);

0 commit comments

Comments
 (0)