2024-09-11 02:37:36 +00:00
/ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Copyright ( c ) Microsoft Corporation . All rights reserved .
* Licensed under the MIT License . See License . txt in the project root for license information .
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * /
import * as vscode from 'vscode' ;
import { activate as keepNotebookModelStoreInSync } from './notebookModelStoreSync' ;
import { notebookImagePasteSetup } from './notebookImagePaste' ;
import { AttachmentCleaner } from './notebookAttachmentCleaner' ;
2024-09-24 04:46:08 +00:00
import { serializeNotebookToString } from './serializers' ;
2025-04-21 08:55:32 +00:00
import { defaultNotebookFormat } from './constants' ;
2024-09-11 02:37:36 +00:00
// From {nbformat.INotebookMetadata} in @jupyterlab/coreutils
type NotebookMetadata = {
kernelspec ? : {
name : string ;
display_name : string ;
[ propName : string ] : unknown ;
} ;
language_info ? : {
name : string ;
codemirror_mode? : string | { } ;
file_extension? : string ;
mimetype? : string ;
pygments_lexer? : string ;
[ propName : string ] : unknown ;
} ;
orig_nbformat? : number ;
[ propName : string ] : unknown ;
} ;
2025-03-01 02:01:53 +00:00
type OptionsWithCellContentMetadata = vscode . NotebookDocumentContentOptions & { cellContentMetadata : { attachments : boolean } } ;
2024-09-24 04:46:08 +00:00
export function activate ( context : vscode.ExtensionContext , serializer : vscode.NotebookSerializer ) {
2024-09-11 02:37:36 +00:00
keepNotebookModelStoreInSync ( context ) ;
2025-03-01 02:01:53 +00:00
const notebookSerializerOptions : OptionsWithCellContentMetadata = {
2024-09-11 02:37:36 +00:00
transientOutputs : false ,
2025-03-01 02:01:53 +00:00
transientDocumentMetadata : {
cells : true ,
indentAmount : true
} ,
2024-09-24 04:46:08 +00:00
transientCellMetadata : {
2024-09-11 02:37:36 +00:00
breakpointMargin : true ,
id : false ,
metadata : false ,
attachments : false
} ,
cellContentMetadata : {
attachments : true
}
2025-03-01 02:01:53 +00:00
} ;
context . subscriptions . push ( vscode . workspace . registerNotebookSerializer ( 'jupyter-notebook' , serializer , notebookSerializerOptions ) ) ;
2024-09-11 02:37:36 +00:00
2025-03-01 02:01:53 +00:00
const interactiveSerializeOptions : OptionsWithCellContentMetadata = {
2024-09-11 02:37:36 +00:00
transientOutputs : false ,
2024-09-24 04:46:08 +00:00
transientCellMetadata : {
2024-09-11 02:37:36 +00:00
breakpointMargin : true ,
id : false ,
metadata : false ,
attachments : false
} ,
cellContentMetadata : {
attachments : true
}
2025-03-01 02:01:53 +00:00
} ;
context . subscriptions . push ( vscode . workspace . registerNotebookSerializer ( 'interactive' , serializer , interactiveSerializeOptions ) ) ;
2024-09-11 02:37:36 +00:00
vscode . languages . registerCodeLensProvider ( { pattern : '**/*.ipynb' } , {
provideCodeLenses : ( document ) = > {
if (
document . uri . scheme === 'vscode-notebook-cell' ||
document . uri . scheme === 'vscode-notebook-cell-metadata' ||
document . uri . scheme === 'vscode-notebook-cell-output'
) {
return [ ] ;
}
const codelens = new vscode . CodeLens ( new vscode . Range ( 0 , 0 , 0 , 0 ) , { title : 'Open in Notebook Editor' , command : 'ipynb.openIpynbInNotebookEditor' , arguments : [ document . uri ] } ) ;
return [ codelens ] ;
}
} ) ;
context . subscriptions . push ( vscode . commands . registerCommand ( 'ipynb.newUntitledIpynb' , async ( ) = > {
const language = 'python' ;
const cell = new vscode . NotebookCellData ( vscode . NotebookCellKind . Code , '' , language ) ;
const data = new vscode . NotebookData ( [ cell ] ) ;
2024-09-24 04:46:08 +00:00
data . metadata = {
2024-09-11 02:37:36 +00:00
cells : [ ] ,
metadata : { } ,
2025-04-21 08:55:32 +00:00
nbformat : defaultNotebookFormat.major ,
nbformat_minor : defaultNotebookFormat.minor ,
2024-09-11 02:37:36 +00:00
} ;
const doc = await vscode . workspace . openNotebookDocument ( 'jupyter-notebook' , data ) ;
await vscode . window . showNotebookDocument ( doc ) ;
} ) ) ;
context . subscriptions . push ( vscode . commands . registerCommand ( 'ipynb.openIpynbInNotebookEditor' , async ( uri : vscode.Uri ) = > {
if ( vscode . window . activeTextEditor ? . document . uri . toString ( ) === uri . toString ( ) ) {
await vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
}
const document = await vscode . workspace . openNotebookDocument ( uri ) ;
await vscode . window . showNotebookDocument ( document ) ;
} ) ) ;
context . subscriptions . push ( notebookImagePasteSetup ( ) ) ;
const enabled = vscode . workspace . getConfiguration ( 'ipynb' ) . get ( 'pasteImagesAsAttachments.enabled' , false ) ;
if ( enabled ) {
const cleaner = new AttachmentCleaner ( ) ;
context . subscriptions . push ( cleaner ) ;
}
return {
get dropCustomMetadata() {
2024-09-24 04:46:08 +00:00
return true ;
2024-09-11 02:37:36 +00:00
} ,
2024-09-24 04:46:08 +00:00
exportNotebook : ( notebook : vscode.NotebookData ) : Promise < string > = > {
return Promise . resolve ( serializeNotebookToString ( notebook ) ) ;
2024-09-11 02:37:36 +00:00
} ,
setNotebookMetadata : async ( resource : vscode.Uri , metadata : Partial < NotebookMetadata > ) : Promise < boolean > = > {
const document = vscode . workspace . notebookDocuments . find ( doc = > doc . uri . toString ( ) === resource . toString ( ) ) ;
if ( ! document ) {
return false ;
}
const edit = new vscode . WorkspaceEdit ( ) ;
2024-09-24 04:46:08 +00:00
edit . set ( resource , [ vscode . NotebookEdit . updateNotebookMetadata ( {
. . . document . metadata ,
2025-03-01 02:01:53 +00:00
metadata : {
2024-09-24 04:46:08 +00:00
. . . ( document . metadata . metadata ? ? { } ) ,
. . . metadata
2025-03-01 02:01:53 +00:00
} satisfies NotebookMetadata ,
2024-09-24 04:46:08 +00:00
} ) ] ) ;
2024-09-11 02:37:36 +00:00
return vscode . workspace . applyEdit ( edit ) ;
} ,
} ;
}
export function deactivate() { }