From b72671133b00473ea3494c01d8dea71965fda5a2 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Tue, 23 Aug 2022 13:57:44 -0700 Subject: [PATCH] Fix any types and extract method (#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 --- extensions/ipynb/src/notebookImagePaste.ts | 36 ++++++++++++---------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/extensions/ipynb/src/notebookImagePaste.ts b/extensions/ipynb/src/notebookImagePaste.ts index 50970f6a8be..bc59c8915bf 100644 --- a/extensions/ipynb/src/notebookImagePaste.ts +++ b/extensions/ipynb/src/notebookImagePaste.ts @@ -8,13 +8,13 @@ import * as vscode from 'vscode'; class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider { async provideDocumentPasteEdits( - _document: vscode.TextDocument, + document: vscode.TextDocument, _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken ): Promise { - const enabled = vscode.workspace.getConfiguration('ipynb', _document).get('experimental.pasteImages.enabled', false); + const enabled = vscode.workspace.getConfiguration('ipynb', document).get('experimental.pasteImages.enabled', false); if (!enabled) { return undefined; } @@ -42,24 +42,13 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider { return undefined; } - // get notebook cell - let notebookUri; - let currentCell; - for (const notebook of vscode.workspace.notebookDocuments) { - if (notebook.uri.path === _document.uri.path) { - for (const cell of notebook.getCells()) { - if (cell.document === _document) { - currentCell = cell; - notebookUri = notebook.uri; - break; - } - } - } - } - if (!currentCell || !notebookUri) { + const currentCell = this.getCellFromCellDocument(document); + if (!currentCell) { return undefined; } + const notebookUri = currentCell.notebook.uri; + // create updated metadata for cell (prep for WorkspaceEdit) const b64string = encodeBase64(fileDataAsUint8); const startingAttachments = currentCell.metadata.custom?.attachments; @@ -78,6 +67,19 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider { return { insertText: pasteSnippet, additionalEdit: workspaceEdit }; } + + private getCellFromCellDocument(cellDocument: vscode.TextDocument): vscode.NotebookCell | undefined { + for (const notebook of vscode.workspace.notebookDocuments) { + if (notebook.uri.path === cellDocument.uri.path) { + for (const cell of notebook.getCells()) { + if (cell.document === cellDocument) { + return cell; + } + } + } + } + return undefined; + } } /**