Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/p5-kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"dependencies": {
"@jupyterlab/nbformat": "^4.5.0",
"@jupyterlite/javascript-kernel": "^0.4.0-alpha.1",
"@jupyterlite/javascript-kernel": "^0.4.0-alpha.3",
"@jupyterlite/services": "^0.7.0"
},
"devDependencies": {
Expand Down
60 changes: 3 additions & 57 deletions packages/p5-kernel/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

import type { IMimeBundle } from '@jupyterlab/nbformat';

import type { KernelMessage } from '@jupyterlab/services';

import {
JavaScriptExecutor,
type IInspectResult
} from '@jupyterlite/javascript-kernel';
import { JavaScriptExecutor } from '@jupyterlite/javascript-kernel';

import { P5_DOCS } from './p5-docs';

Expand Down Expand Up @@ -42,60 +37,11 @@ export class P5Executor extends JavaScriptExecutor {
return super.getMimeBundle(value);
}

/**
* Override inspect to replace bound function stubs with proper p5.js docs.
*
* The base inspect method evaluates the expression at runtime, which
* succeeds for p5 globals but yields unhelpful "function bound ()" output.
* This override replaces the result entirely when we have p5 docs.
*/
override inspect(
code: string,
cursorPos: number,
detailLevel: KernelMessage.IInspectRequestMsg['content']['detail_level'] = 0
): IInspectResult {
const result = super.inspect(code, cursorPos, detailLevel);

if (result.found) {
// Extract expression the same way the base does
const expression = this._extractExpressionForInspect(code, cursorPos);
if (expression) {
const doc = P5_DOCS[expression];
if (doc) {
result.data = {
'text/plain': `${expression}: ${doc}`,
'text/markdown': `**${expression}**\n\n${doc}`
};
}
}
}

return result;
}

/**
* Extract the expression at the cursor position for inspect lookups.
* Mirrors the logic in the base class's private _extractExpressionAtCursor.
*/
private _extractExpressionForInspect(
code: string,
cursorPos: number
): string | null {
const beforeCursor = code.substring(0, cursorPos);
const afterCursor = code.substring(cursorPos);
const beforeMatch = beforeCursor.match(/[\w.$]+$/);
const afterMatch = afterCursor.match(/^[\w]*/);
if (!beforeMatch) {
return null;
}
return beforeMatch[0] + (afterMatch?.[0] || '');
}

/**
* Provide p5.js-specific documentation for inspect requests.
*
* p5.js uses bound methods, so runtime inspection only shows
* `function bound ()`. This override provides proper docs.
* The base class calls this method during inspect to append predefined
* documentation to the runtime inspection data.
*/
protected override getBuiltinDocumentation(
expression: string
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2957,9 +2957,9 @@ __metadata:
languageName: node
linkType: hard

"@jupyterlite/javascript-kernel@npm:^0.4.0-alpha.1":
version: 0.4.0-alpha.1
resolution: "@jupyterlite/javascript-kernel@npm:0.4.0-alpha.1"
"@jupyterlite/javascript-kernel@npm:^0.4.0-alpha.3":
version: 0.4.0-alpha.3
resolution: "@jupyterlite/javascript-kernel@npm:0.4.0-alpha.3"
dependencies:
"@jupyterlab/coreutils": ^6.5.5
"@jupyterlab/nbformat": ^4.5.0
Expand All @@ -2968,7 +2968,7 @@ __metadata:
astring: ^1.9.0
comlink: ^4.3.1
meriyah: ^4.3.9
checksum: fa1260cf2a400d3ca470d845040ebc2ff75215eed87ecac14712035e3eb3c7cf37ee7c3177fcfdf5e77471384b9ac28f0c6f843191fe0e171b1d054e92ba89fd
checksum: baadc4a7435b9b9132a1a206dc2ffc938379d1a9fb99818dc553a555ecef685bae85dc9cd14d8f2ea9530934ce2a80bb8dc65cead797a076158f6933f60b6500
languageName: node
linkType: hard

Expand Down Expand Up @@ -3031,7 +3031,7 @@ __metadata:
"@babel/preset-env": ^7.12.1
"@jupyterlab/nbformat": ^4.5.0
"@jupyterlab/testutils": ~4.5.0
"@jupyterlite/javascript-kernel": ^0.4.0-alpha.1
"@jupyterlite/javascript-kernel": ^0.4.0-alpha.3
"@jupyterlite/services": ^0.7.0
"@types/jest": ^26.0.10
"@types/p5": ^1.7.7
Expand Down