Skip to content

Commit b726711

Browse files
authored
Fix any types and extract method (microsoft#158967)
`currentCell` and `notebookUri` are currently any types, which hides type errors. To fix this and clean up the code, I've extracted a new `getCellFromCellDocument` method
1 parent 95e0f54 commit b726711

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

extensions/ipynb/src/notebookImagePaste.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import * as vscode from 'vscode';
88
class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
99

1010
async provideDocumentPasteEdits(
11-
_document: vscode.TextDocument,
11+
document: vscode.TextDocument,
1212
_ranges: readonly vscode.Range[],
1313
dataTransfer: vscode.DataTransfer,
1414
_token: vscode.CancellationToken
1515
): Promise<vscode.DocumentPasteEdit | undefined> {
1616

17-
const enabled = vscode.workspace.getConfiguration('ipynb', _document).get('experimental.pasteImages.enabled', false);
17+
const enabled = vscode.workspace.getConfiguration('ipynb', document).get('experimental.pasteImages.enabled', false);
1818
if (!enabled) {
1919
return undefined;
2020
}
@@ -42,24 +42,13 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
4242
return undefined;
4343
}
4444

45-
// get notebook cell
46-
let notebookUri;
47-
let currentCell;
48-
for (const notebook of vscode.workspace.notebookDocuments) {
49-
if (notebook.uri.path === _document.uri.path) {
50-
for (const cell of notebook.getCells()) {
51-
if (cell.document === _document) {
52-
currentCell = cell;
53-
notebookUri = notebook.uri;
54-
break;
55-
}
56-
}
57-
}
58-
}
59-
if (!currentCell || !notebookUri) {
45+
const currentCell = this.getCellFromCellDocument(document);
46+
if (!currentCell) {
6047
return undefined;
6148
}
6249

50+
const notebookUri = currentCell.notebook.uri;
51+
6352
// create updated metadata for cell (prep for WorkspaceEdit)
6453
const b64string = encodeBase64(fileDataAsUint8);
6554
const startingAttachments = currentCell.metadata.custom?.attachments;
@@ -78,6 +67,19 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
7867

7968
return { insertText: pasteSnippet, additionalEdit: workspaceEdit };
8069
}
70+
71+
private getCellFromCellDocument(cellDocument: vscode.TextDocument): vscode.NotebookCell | undefined {
72+
for (const notebook of vscode.workspace.notebookDocuments) {
73+
if (notebook.uri.path === cellDocument.uri.path) {
74+
for (const cell of notebook.getCells()) {
75+
if (cell.document === cellDocument) {
76+
return cell;
77+
}
78+
}
79+
}
80+
}
81+
return undefined;
82+
}
8183
}
8284

8385
/**

0 commit comments

Comments
 (0)