diff --git a/news/2 Fixes/16980.md b/news/2 Fixes/16980.md new file mode 100644 index 000000000000..2338273d3c2b --- /dev/null +++ b/news/2 Fixes/16980.md @@ -0,0 +1 @@ +Ensure we use fragment when formatting notebook cells. diff --git a/src/client/common/editor.ts b/src/client/common/editor.ts index 2d8becaebba5..eeaded58fce4 100644 --- a/src/client/common/editor.ts +++ b/src/client/common/editor.ts @@ -248,11 +248,13 @@ export async function getTempFileWithDocumentContents(document: TextDocument, fs // because the language server is watching the file system for Python // file add/delete/change and we don't want this temp file to trigger it. - let fileName = `${document.uri.fsPath}.${md5(document.uri.fsPath)}.tmp`; + let fileName = `${document.uri.fsPath}.${md5(document.uri.fsPath + document.uri.fragment)}.tmp`; try { // When dealing with untitled notebooks, there's no original physical file, hence create a temp file. if (isNotebookCell(document.uri) && !(await fs.fileExists(document.uri.fsPath))) { - fileName = (await fs.createTemporaryFile(`${path.basename(document.uri.fsPath)}.tmp`)).filePath; + fileName = ( + await fs.createTemporaryFile(`${path.basename(document.uri.fsPath)}-${document.uri.fragment}.tmp`) + ).filePath; } await fs.writeFile(fileName, document.getText()); } catch (ex) { diff --git a/src/client/providers/formatProvider.ts b/src/client/providers/formatProvider.ts index 29da69dd1f43..0c9155ce2b3a 100644 --- a/src/client/providers/formatProvider.ts +++ b/src/client/providers/formatProvider.ts @@ -3,6 +3,7 @@ import * as vscode from 'vscode'; import { ICommandManager, IDocumentManager, IWorkspaceService } from '../common/application/types'; +import { PYTHON_LANGUAGE } from '../common/constants'; import { IConfigurationService } from '../common/types'; import { IInterpreterService } from '../interpreter/contracts'; import { IServiceContainer } from '../ioc/types'; @@ -76,7 +77,7 @@ export class PythonFormattingEditProvider // Workaround is to resolve promise to nothing here, then execute format document and force new save. // However, we need to know if this is 'format document' or formatting on save. - if (this.saving) { + if (this.saving || document.languageId !== PYTHON_LANGUAGE) { // We are saving after formatting (see onSaveDocument below) // so we do not want to format again. return [];