Skip to content

Fix running Untitled files with the play button #20955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/client/terminals/codeExecution/codeExecutionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ export class CodeExecutionManager implements ICodeExecutionManager {
sendTelemetryEvent(EventName.EXECUTION_CODE, undefined, { scope: 'file', trigger });
const codeExecutionHelper = this.serviceContainer.get<ICodeExecutionHelper>(ICodeExecutionHelper);
file = file instanceof Uri ? file : undefined;
const fileToExecute = file ? file : await codeExecutionHelper.getFileToExecute();
let fileToExecute = file ? file : await codeExecutionHelper.getFileToExecute();
if (!fileToExecute) {
return;
}
await codeExecutionHelper.saveFileIfDirty(fileToExecute);
const fileAfterSave = await codeExecutionHelper.saveFileIfDirty(fileToExecute);
if (fileAfterSave) {
fileToExecute = fileAfterSave;
}

try {
const contents = await this.fileSystem.readFile(fileToExecute.fsPath);
Expand Down
15 changes: 11 additions & 4 deletions src/client/terminals/codeExecution/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '../../common/extensions';
import { inject, injectable } from 'inversify';
import { l10n, Position, Range, TextEditor, Uri } from 'vscode';

import { IApplicationShell, IDocumentManager } from '../../common/application/types';
import { IApplicationShell, ICommandManager, IDocumentManager } from '../../common/application/types';
import { PYTHON_LANGUAGE } from '../../common/constants';
import * as internalScripts from '../../common/process/internal/scripts';
import { IProcessServiceFactory } from '../../common/process/types';
Expand All @@ -14,6 +14,7 @@ import { IInterpreterService } from '../../interpreter/contracts';
import { IServiceContainer } from '../../ioc/types';
import { ICodeExecutionHelper } from '../types';
import { traceError } from '../../logging';
import { Resource } from '../../common/types';

@injectable()
export class CodeExecutionHelper implements ICodeExecutionHelper {
Expand All @@ -25,7 +26,7 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {

private readonly interpreterService: IInterpreterService;

constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) {
constructor(@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer) {
this.documentManager = serviceContainer.get<IDocumentManager>(IDocumentManager);
this.applicationShell = serviceContainer.get<IApplicationShell>(IApplicationShell);
this.processServiceFactory = serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory);
Expand Down Expand Up @@ -119,11 +120,17 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {
return code;
}

public async saveFileIfDirty(file: Uri): Promise<void> {
public async saveFileIfDirty(file: Uri): Promise<Resource> {
const docs = this.documentManager.textDocuments.filter((d) => d.uri.path === file.path);
if (docs.length === 1 && docs[0].isDirty) {
await docs[0].save();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upstream bug on VSCode due to which this cannot be used: microsoft/vscode#178713.

const deferred = createDeferred<Uri>();
this.documentManager.onDidSaveTextDocument((e) => deferred.resolve(e.uri));
const commandManager = this.serviceContainer.get<ICommandManager>(ICommandManager);
await commandManager.executeCommand('workbench.action.files.save', file);
const savedFileUri = await deferred.promise;
return savedFileUri;
}
return undefined;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/client/terminals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { Event, Terminal, TextEditor, Uri } from 'vscode';
import { IDisposable } from '../common/types';
import { IDisposable, Resource } from '../common/types';

export const ICodeExecutionService = Symbol('ICodeExecutionService');

Expand All @@ -17,7 +17,7 @@ export const ICodeExecutionHelper = Symbol('ICodeExecutionHelper');
export interface ICodeExecutionHelper {
normalizeLines(code: string): Promise<string>;
getFileToExecute(): Promise<Uri | undefined>;
saveFileIfDirty(file: Uri): Promise<void>;
saveFileIfDirty(file: Uri): Promise<Resource>;
getSelectedTextToExecute(textEditor: TextEditor): Promise<string | undefined>;
}

Expand Down
30 changes: 21 additions & 9 deletions src/test/terminals/codeExecution/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as fs from 'fs-extra';
import * as path from 'path';
import { SemVer } from 'semver';
import * as TypeMoq from 'typemoq';
import { Position, Range, Selection, TextDocument, TextEditor, TextLine, Uri } from 'vscode';
import { IApplicationShell, IDocumentManager } from '../../../client/common/application/types';
import { EventEmitter, Position, Range, Selection, TextDocument, TextEditor, TextLine, Uri } from 'vscode';
import { IApplicationShell, ICommandManager, IDocumentManager } from '../../../client/common/application/types';
import { EXTENSION_ROOT_DIR, PYTHON_LANGUAGE } from '../../../client/common/constants';
import '../../../client/common/extensions';
import { ProcessService } from '../../../client/common/process/proc';
Expand Down Expand Up @@ -37,6 +37,7 @@ suite('Terminal - Code Execution Helper', () => {
let editor: TypeMoq.IMock<TextEditor>;
let processService: TypeMoq.IMock<IProcessService>;
let interpreterService: TypeMoq.IMock<IInterpreterService>;
let commandManager: TypeMoq.IMock<ICommandManager>;
const workingPython: PythonEnvironment = {
path: PYTHON_PATH,
version: new SemVer('3.6.6-final'),
Expand All @@ -49,6 +50,7 @@ suite('Terminal - Code Execution Helper', () => {

setup(() => {
const serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
commandManager = TypeMoq.Mock.ofType<ICommandManager>();
documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
applicationShell = TypeMoq.Mock.ofType<IApplicationShell>();
const envVariablesProvider = TypeMoq.Mock.ofType<IEnvironmentVariablesProvider>();
Expand Down Expand Up @@ -79,6 +81,7 @@ suite('Terminal - Code Execution Helper', () => {
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IApplicationShell), TypeMoq.It.isAny()))
.returns(() => applicationShell.object);
serviceContainer.setup((c) => c.get(TypeMoq.It.isValue(ICommandManager))).returns(() => commandManager.object);
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IEnvironmentVariablesProvider), TypeMoq.It.isAny()))
.returns(() => envVariablesProvider.object);
Expand Down Expand Up @@ -364,15 +367,24 @@ suite('Terminal - Code Execution Helper', () => {
.setup((d) => d.textDocuments)
.returns(() => [document.object])
.verifiable(TypeMoq.Times.once());
document.setup((doc) => doc.isUntitled).returns(() => false);
const saveEmitter = new EventEmitter<TextDocument>();
documentManager.setup((d) => d.onDidSaveTextDocument).returns(() => saveEmitter.event);
document.setup((doc) => doc.isUntitled).returns(() => true);
document.setup((doc) => doc.isDirty).returns(() => true);
document.setup((doc) => doc.languageId).returns(() => PYTHON_LANGUAGE);
const expectedUri = Uri.file('one.py');
document.setup((doc) => doc.uri).returns(() => expectedUri);

await helper.saveFileIfDirty(expectedUri);
documentManager.verifyAll();
document.verify((doc) => doc.save(), TypeMoq.Times.once());
const untitledUri = Uri.file('Untitled-1');
document.setup((doc) => doc.uri).returns(() => untitledUri);
const savedDocument = TypeMoq.Mock.ofType<TextDocument>();
const expectedSavedUri = Uri.file('one.py');
savedDocument.setup((doc) => doc.uri).returns(() => expectedSavedUri);
commandManager
.setup((c) => c.executeCommand('workbench.action.files.save', untitledUri))
.callback(() => saveEmitter.fire(savedDocument.object))
.returns(() => Promise.resolve());

const savedUri = await helper.saveFileIfDirty(untitledUri);

expect(savedUri?.fsPath).to.be.equal(expectedSavedUri.fsPath);
});

test('File will be not saved if file is not dirty', async () => {
Expand Down