Skip to content

Commit cd1698e

Browse files
committed
docstrings and type errors
1 parent 1052c25 commit cd1698e

File tree

8 files changed

+142
-14
lines changed

8 files changed

+142
-14
lines changed

src/notebooks/deepnote/MimeTypeProcessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class ImageMimeProcessor implements MimeProcessor {
6262
return null;
6363
}
6464

65-
return NotebookCellOutputItem.binary(uint8Array, mimeType);
65+
return new NotebookCellOutputItem(uint8Array, mimeType);
6666
} catch {
6767
return NotebookCellOutputItem.text(String(content), mimeType);
6868
}

src/notebooks/deepnote/deepnoteActivationService.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ import type { DeepnoteProject, DeepnoteNotebook } from './deepnoteTypes';
77
import { DeepnoteNotebookSelector } from './deepnoteNotebookSelector';
88
import { Commands } from '../../platform/common/constants';
99

10+
/**
11+
* Service responsible for activating and configuring Deepnote notebook support in VS Code.
12+
* Registers serializers, command handlers, and manages the notebook selection workflow.
13+
*/
1014
@injectable()
1115
export class DeepnoteActivationService implements IExtensionSyncActivationService {
1216
private serializer: DeepnoteNotebookSerializer;
1317
private selector: DeepnoteNotebookSelector;
1418

1519
constructor(@inject(IExtensionContext) private extensionContext: IExtensionContext) {}
1620

21+
/**
22+
* Activates Deepnote support by registering serializers and commands.
23+
* Called during extension activation to set up Deepnote integration.
24+
*/
1725
public activate() {
1826
this.serializer = new DeepnoteNotebookSerializer();
1927
this.selector = new DeepnoteNotebookSelector();

src/notebooks/deepnote/deepnoteDataConverter.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,44 @@ import { ErrorOutputHandler } from './outputHandlers/ErrorOutputHandler';
77
import { RichOutputHandler } from './outputHandlers/RichOutputHandler';
88
import { mergeMetadata, hasMetadataContent, generateBlockId, generateSortingKey } from './dataConversionUtils';
99

10+
/**
11+
* Utility class for converting between Deepnote block structures and VS Code notebook cells.
12+
* Handles bidirectional conversion while preserving metadata and execution state.
13+
*/
1014
export class DeepnoteDataConverter {
1115
private readonly outputDetector = new OutputTypeDetector();
1216
private readonly streamHandler = new StreamOutputHandler();
1317
private readonly errorHandler = new ErrorOutputHandler();
1418
private readonly richHandler = new RichOutputHandler();
19+
20+
/**
21+
* Converts Deepnote blocks to VS Code notebook cells.
22+
* Sorts blocks by sortingKey before conversion to maintain proper order.
23+
* @param blocks Array of Deepnote blocks to convert
24+
* @returns Array of VS Code notebook cell data
25+
*/
1526
convertBlocksToCells(blocks: DeepnoteBlock[]): NotebookCellData[] {
1627
return blocks
1728
.sort((a, b) => a.sortingKey.localeCompare(b.sortingKey))
1829
.map((block) => this.convertBlockToCell(block));
1930
}
2031

32+
/**
33+
* Converts VS Code notebook cells back to Deepnote blocks.
34+
* Generates missing IDs and sorting keys as needed.
35+
* @param cells Array of VS Code notebook cells to convert
36+
* @returns Array of Deepnote blocks
37+
*/
2138
convertCellsToBlocks(cells: NotebookCellData[]): DeepnoteBlock[] {
2239
return cells.map((cell, index) => this.convertCellToBlock(cell, index));
2340
}
2441

2542
private convertBlockToCell(block: DeepnoteBlock): NotebookCellData {
2643
const cellKind = block.type === 'code' ? NotebookCellKind.Code : NotebookCellKind.Markup;
2744
const languageId = block.type === 'code' ? 'python' : 'markdown';
28-
45+
2946
const cell = new NotebookCellData(cellKind, block.content, languageId);
30-
47+
3148
cell.metadata = {
3249
deepnoteBlockId: block.id,
3350
deepnoteBlockType: block.type,
@@ -36,9 +53,9 @@ export class DeepnoteDataConverter {
3653
...(typeof block.executionCount === 'number' && { executionCount: block.executionCount }),
3754
...(block.outputReference && { deepnoteOutputReference: block.outputReference })
3855
};
39-
56+
4057
cell.outputs = this.convertDeepnoteOutputsToVSCodeOutputs(block.outputs || []);
41-
58+
4259
return cell;
4360
}
4461

@@ -79,7 +96,7 @@ export class DeepnoteDataConverter {
7996

8097
private convertSingleOutput(output: DeepnoteOutput): NotebookCellOutput {
8198
const outputItems = this.createOutputItems(output);
82-
99+
83100
const metadata = mergeMetadata(
84101
output.metadata,
85102
output.execution_count !== undefined ? { executionCount: output.execution_count } : undefined
@@ -101,7 +118,7 @@ export class DeepnoteDataConverter {
101118

102119
switch (detection.type) {
103120
case 'error':
104-
deepnoteOutput = this.errorHandler.convertToDeepnote(output, detection.errorItem!);
121+
deepnoteOutput = this.errorHandler.convertToDeepnote(detection.errorItem!);
105122
break;
106123
case 'stream':
107124
deepnoteOutput = this.streamHandler.convertToDeepnote(output);
@@ -115,7 +132,7 @@ export class DeepnoteDataConverter {
115132
// Preserve metadata from VS Code output
116133
if (output.metadata) {
117134
deepnoteOutput.metadata = mergeMetadata(deepnoteOutput.metadata, output.metadata);
118-
135+
119136
// Extract execution count from metadata
120137
if (output.metadata.executionCount !== undefined) {
121138
deepnoteOutput.execution_count = output.metadata.executionCount as number;
@@ -125,7 +142,6 @@ export class DeepnoteDataConverter {
125142
return deepnoteOutput;
126143
}
127144

128-
129145
private createOutputItems(output: DeepnoteOutput): NotebookCellOutputItem[] {
130146
switch (output.output_type) {
131147
case 'stream':
@@ -143,5 +159,4 @@ export class DeepnoteDataConverter {
143159
return [];
144160
}
145161
}
146-
147162
}

src/notebooks/deepnote/deepnoteNotebookManager.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
import type { DeepnoteProject } from './deepnoteTypes';
22

3+
/**
4+
* Centralized manager for tracking Deepnote notebook selections and project state.
5+
* Manages per-project and per-URI state including current selections and user preferences.
6+
*/
37
export class DeepnoteNotebookManager {
48
private currentNotebookId = new Map<string, string>();
59
private originalProjects = new Map<string, DeepnoteProject>();
610
private selectedNotebookByUri = new Map<string, string>();
711
private skipPromptForUri = new Set<string>();
812

13+
/**
14+
* Gets the currently selected notebook ID for a project.
15+
* @param projectId Project identifier
16+
* @returns Current notebook ID or undefined if not set
17+
*/
918
getCurrentNotebookId(projectId: string): string | undefined {
1019
return this.currentNotebookId.get(projectId);
1120
}
1221

22+
/**
23+
* Retrieves the original project data for a given project ID.
24+
* @param projectId Project identifier
25+
* @returns Original project data or undefined if not found
26+
*/
1327
getOriginalProject(projectId: string): DeepnoteProject | undefined {
1428
return this.originalProjects.get(projectId);
1529
}
1630

31+
/**
32+
* Gets the selected notebook ID for a specific file URI.
33+
* @param uri File URI string
34+
* @returns Selected notebook ID or undefined if not set
35+
*/
1736
getSelectedNotebookForUri(uri: string): string | undefined {
1837
return this.selectedNotebookByUri.get(uri);
1938
}
@@ -32,6 +51,12 @@ export class DeepnoteNotebookManager {
3251
this.skipPromptForUri.add(uri);
3352
}
3453

54+
/**
55+
* Checks if prompts should be skipped for a given URI and consumes the skip flag.
56+
* This is used to avoid showing selection prompts immediately after a user makes a choice.
57+
* @param uri File URI string
58+
* @returns True if prompts should be skipped (and resets the flag)
59+
*/
3560
shouldSkipPrompt(uri: string): boolean {
3661
if (this.skipPromptForUri.has(uri)) {
3762
this.skipPromptForUri.delete(uri);
@@ -42,11 +67,24 @@ export class DeepnoteNotebookManager {
4267
return false;
4368
}
4469

70+
/**
71+
* Stores the original project data and sets the initial current notebook.
72+
* This is used during deserialization to cache project data and track the active notebook.
73+
* @param projectId Project identifier
74+
* @param project Original project data to store
75+
* @param notebookId Initial notebook ID to set as current
76+
*/
4577
storeOriginalProject(projectId: string, project: DeepnoteProject, notebookId: string): void {
4678
this.originalProjects.set(projectId, project);
4779
this.currentNotebookId.set(projectId, notebookId);
4880
}
4981

82+
/**
83+
* Updates the current notebook ID for a project.
84+
* Used when switching notebooks within the same project.
85+
* @param projectId Project identifier
86+
* @param notebookId New current notebook ID
87+
*/
5088
updateCurrentNotebookId(projectId: string, notebookId: string): void {
5189
this.currentNotebookId.set(projectId, notebookId);
5290
}

src/notebooks/deepnote/deepnoteNotebookSelector.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ interface NotebookQuickPickItem extends QuickPickItem {
77
notebook: DeepnoteNotebook;
88
}
99

10+
/**
11+
* Provides user interface for selecting notebooks within a Deepnote project.
12+
* Creates and manages VS Code QuickPick interface for notebook selection.
13+
*/
1014
export class DeepnoteNotebookSelector {
15+
/**
16+
* Presents a notebook selection interface to the user.
17+
* @param notebooks Available notebooks to choose from
18+
* @param currentNotebookId Currently selected notebook ID for pre-selection
19+
* @param options Optional configuration for the selection UI
20+
* @returns Promise resolving to selected notebook or undefined if cancelled
21+
*/
1122
async selectNotebook(
1223
notebooks: DeepnoteNotebook[],
1324
currentNotebookId?: string,

src/notebooks/deepnote/deepnoteSerializer.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,64 @@
1-
import { type NotebookData, type NotebookSerializer, type CancellationToken } from 'vscode';
1+
import { type CancellationToken, type NotebookData, type NotebookSerializer } from 'vscode';
22
import * as yaml from 'js-yaml';
3-
import { DeepnoteProject, DeepnoteNotebook } from './deepnoteTypes';
3+
import type { DeepnoteProject, DeepnoteNotebook } from './deepnoteTypes';
44
import { DeepnoteNotebookManager } from './deepnoteNotebookManager';
55
import { DeepnoteNotebookSelector } from './deepnoteNotebookSelector';
66
import { DeepnoteDataConverter } from './deepnoteDataConverter';
77

88
export { DeepnoteProject, DeepnoteNotebook, DeepnoteBlock, DeepnoteOutput } from './deepnoteTypes';
99

10+
/**
11+
* Callback function type for handling notebook selection during deserialization.
12+
* @param projectId Project identifier containing the notebooks
13+
* @param notebooks Available notebooks to choose from
14+
* @returns Promise resolving to selected notebook or undefined
15+
*/
1016
export type NotebookSelectionCallback = (
1117
projectId: string,
1218
notebooks: DeepnoteNotebook[]
1319
) => Promise<DeepnoteNotebook | undefined>;
1420

21+
/**
22+
* Serializer for converting between Deepnote YAML files and VS Code notebook format.
23+
* Handles reading/writing .deepnote files and manages project state persistence.
24+
*/
1525
export class DeepnoteNotebookSerializer implements NotebookSerializer {
1626
private manager = new DeepnoteNotebookManager();
1727
private selector = new DeepnoteNotebookSelector();
1828
private converter = new DeepnoteDataConverter();
1929
private notebookSelectionCallback?: NotebookSelectionCallback;
2030

31+
/**
32+
* Gets the notebook manager instance for accessing project state.
33+
* @returns DeepnoteNotebookManager instance
34+
*/
2135
getManager(): DeepnoteNotebookManager {
2236
return this.manager;
2337
}
2438

39+
/**
40+
* Gets the data converter instance for cell/block conversion.
41+
* @returns DeepnoteDataConverter instance
42+
*/
2543
getConverter(): DeepnoteDataConverter {
2644
return this.converter;
2745
}
2846

47+
/**
48+
* Sets a custom callback for handling notebook selection during deserialization.
49+
* @param callback Function to call when notebook selection is needed
50+
*/
2951
setNotebookSelectionCallback(callback: NotebookSelectionCallback) {
3052
this.notebookSelectionCallback = callback;
3153
}
3254

55+
/**
56+
* Deserializes a Deepnote YAML file into VS Code notebook format.
57+
* Parses YAML, selects appropriate notebook, and converts blocks to cells.
58+
* @param content Raw file content as bytes
59+
* @param _token Cancellation token (unused)
60+
* @returns Promise resolving to notebook data
61+
*/
3362
async deserializeNotebook(content: Uint8Array, _token: CancellationToken): Promise<NotebookData> {
3463
try {
3564
const contentString = Buffer.from(content).toString('utf8');
@@ -71,6 +100,13 @@ export class DeepnoteNotebookSerializer implements NotebookSerializer {
71100
}
72101
}
73102

103+
/**
104+
* Serializes VS Code notebook data back to Deepnote YAML format.
105+
* Converts cells to blocks, updates project data, and generates YAML.
106+
* @param data Notebook data to serialize
107+
* @param _token Cancellation token (unused)
108+
* @returns Promise resolving to YAML content as bytes
109+
*/
74110
async serializeNotebook(data: NotebookData, _token: CancellationToken): Promise<Uint8Array> {
75111
try {
76112
const projectId = data.metadata?.deepnoteProjectId;

src/notebooks/deepnote/deepnoteTypes.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
/**
2+
* Represents a complete Deepnote project structure with metadata, notebooks, and settings.
3+
*/
14
export interface DeepnoteProject {
25
metadata: {
6+
/** ISO timestamp when the project was created */
37
createdAt: string;
8+
/** ISO timestamp when the project was last modified */
49
modifiedAt: string;
510
};
611
project: {
@@ -12,6 +17,9 @@ export interface DeepnoteProject {
1217
version: string;
1318
}
1419

20+
/**
21+
* Represents a single notebook within a Deepnote project.
22+
*/
1523
export interface DeepnoteNotebook {
1624
blocks: DeepnoteBlock[];
1725
executionMode: string;
@@ -21,6 +29,10 @@ export interface DeepnoteNotebook {
2129
workingDirectory?: string;
2230
}
2331

32+
/**
33+
* Represents a single block (cell) within a Deepnote notebook.
34+
* Can be either a code block or a markdown block.
35+
*/
2436
export interface DeepnoteBlock {
2537
content: string;
2638
executionCount?: number;
@@ -32,11 +44,19 @@ export interface DeepnoteBlock {
3244
type: 'code' | 'markdown';
3345
}
3446

47+
/**
48+
* Represents output data generated by executing a code block.
49+
*/
3550
export interface DeepnoteOutput {
3651
data?: Record<string, unknown>;
52+
ename?: string;
53+
error?: unknown;
54+
evalue?: string;
3755
execution_count?: number;
3856
metadata?: Record<string, unknown>;
3957
name?: string;
4058
output_type: string;
59+
stack?: string;
4160
text?: string;
61+
traceback?: string[];
4262
}

src/notebooks/deepnote/outputHandlers/ErrorOutputHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NotebookCellOutput, NotebookCellOutputItem, l10n } from 'vscode';
1+
import { NotebookCellOutputItem, l10n } from 'vscode';
22
import { decodeContent, parseJsonSafely } from '../dataConversionUtils';
33
import type { DeepnoteOutput } from '../deepnoteTypes';
44

@@ -9,7 +9,7 @@ export class ErrorOutputHandler {
99
/**
1010
* Convert VS Code error output to Deepnote format
1111
*/
12-
convertToDeepnote(output: NotebookCellOutput, errorItem: { mime: string; data: Uint8Array }): DeepnoteOutput {
12+
convertToDeepnote(errorItem: { mime: string; data: Uint8Array }): DeepnoteOutput {
1313
const deepnoteOutput: DeepnoteOutput = {
1414
output_type: 'error'
1515
};

0 commit comments

Comments
 (0)