@@ -107,6 +107,15 @@ export function activate(context: vscode.ExtensionContext, serializer: vscode.No
107
107
) ;
108
108
}
109
109
110
+ /**
111
+ * Check if Positron notebooks are configured as the default editor for .ipynb files
112
+ */
113
+ function isPositronNotebookConfigured ( ) : boolean {
114
+ const config = vscode . workspace . getConfiguration ( ) ;
115
+ const editorAssociations = config . get < Record < string , string > > ( 'workbench.editorAssociations' ) || { } ;
116
+ return editorAssociations [ '*.ipynb' ] === 'workbench.editor.positronNotebook' ;
117
+ }
118
+
110
119
111
120
/**
112
121
* Generate the next available untitled notebook URI using VS Code's standard naming convention
@@ -139,52 +148,41 @@ export function activate(context: vscode.ExtensionContext, serializer: vscode.No
139
148
// Create an untitled URI for the notebook using standard naming convention
140
149
const untitledUri = getNextUntitledNotebookUri ( ) ;
141
150
142
- // Use vscode.open command to trigger proper editor resolution
143
- // This will respect workbench.editorAssociations settings
144
- try {
145
- const editor = await vscode . commands . executeCommand ( 'vscode.open' , untitledUri , {
146
- preview : false ,
147
- override : 'jupyter-notebook' // Specify the notebook view type
148
- } ) as vscode . NotebookEditor | undefined ;
149
-
150
- if ( ! editor || ! editor . notebook ) {
151
- throw new Error ( 'Failed to open notebook editor via vscode.open command' ) ;
151
+ // Only use vscode.open command when Positron notebooks are configured
152
+ // This ensures proper editor resolution respects workbench.editorAssociations
153
+ if ( isPositronNotebookConfigured ( ) ) {
154
+ try {
155
+ await vscode . commands . executeCommand ( 'vscode.open' , untitledUri , {
156
+ preview : false ,
157
+ override : 'jupyter-notebook' // Specify the notebook view type
158
+ } ) ;
159
+ // Note that this will cause notebooks opened to have no cells unlike the original
160
+ // approach prefilled with a code cell.
161
+ return ;
162
+ } catch ( error ) {
163
+ // Use proper logging and show user notification for critical failures
164
+ const errorMessage = error instanceof Error ? error . message : String ( error ) ;
165
+ console . error ( 'Failed to open notebook with editor associations:' , errorMessage ) ;
166
+
167
+ // Show user notification about the fallback
168
+ vscode . window . showWarningMessage (
169
+ 'Unable to open notebook with preferred editor. Using default editor instead.' ,
170
+ 'OK'
171
+ ) ;
172
+ // Fall through to default approach
152
173
}
153
-
154
- // After opening, initialize the notebook with default content
155
- const cell = new vscode . NotebookCellData ( vscode . NotebookCellKind . Code , '' , language ) ;
156
- const edit = new vscode . WorkspaceEdit ( ) ;
157
- edit . set ( editor . notebook . uri , [
158
- vscode . NotebookEdit . insertCells ( 0 , [ cell ] ) ,
159
- vscode . NotebookEdit . updateNotebookMetadata ( {
160
- cells : [ ] ,
161
- metadata : { } ,
162
- nbformat : defaultNotebookFormat . major ,
163
- nbformat_minor : defaultNotebookFormat . minor ,
164
- } )
165
- ] ) ;
166
- await vscode . workspace . applyEdit ( edit ) ;
167
- } catch ( error ) {
168
- // Use proper logging and show user notification for critical failures
169
- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
170
- console . error ( 'Failed to open notebook with editor associations:' , errorMessage ) ;
171
-
172
- // Show user notification about the fallback
173
- vscode . window . showWarningMessage (
174
- 'Unable to open notebook with preferred editor. Using default editor instead.' ,
175
- 'OK'
176
- ) ;
177
- const cell = new vscode . NotebookCellData ( vscode . NotebookCellKind . Code , '' , language ) ;
178
- const data = new vscode . NotebookData ( [ cell ] ) ;
179
- data . metadata = {
180
- cells : [ ] ,
181
- metadata : { } ,
182
- nbformat : defaultNotebookFormat . major ,
183
- nbformat_minor : defaultNotebookFormat . minor ,
184
- } ;
185
- const doc = await vscode . workspace . openNotebookDocument ( 'jupyter-notebook' , data ) ;
186
- await vscode . window . showNotebookDocument ( doc ) ;
187
174
}
175
+ // Default approach: create notebook with a code cell
176
+ const cell = new vscode . NotebookCellData ( vscode . NotebookCellKind . Code , '' , language ) ;
177
+ const data = new vscode . NotebookData ( [ cell ] ) ;
178
+ data . metadata = {
179
+ cells : [ ] ,
180
+ metadata : { } ,
181
+ nbformat : defaultNotebookFormat . major ,
182
+ nbformat_minor : defaultNotebookFormat . minor ,
183
+ } ;
184
+ const doc = await vscode . workspace . openNotebookDocument ( 'jupyter-notebook' , data ) ;
185
+ await vscode . window . showNotebookDocument ( doc ) ;
188
186
} ) ) ;
189
187
// --- End Positron ---
190
188
0 commit comments