Skip to content

Commit 8256843

Browse files
authored
Store python path in the settings.json file of the workspace folder (microsoft#3779)
1 parent 77d6f27 commit 8256843

File tree

3 files changed

+190
-63
lines changed

3 files changed

+190
-63
lines changed

news/2 Fixes/3419.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When in multi-root workspace, store selected python path in the `settings.json` file of the workspace folder.

src/client/interpreter/configuration/interpreterSelector.ts

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { IApplicationShell, ICommandManager, IDocumentManager, IWorkspaceService
44
import * as settings from '../../common/configSettings';
55
import { Commands } from '../../common/constants';
66
import { IPathUtils } from '../../common/types';
7-
import { IServiceContainer } from '../../ioc/types';
87
import { IInterpreterService, IShebangCodeLensProvider, PythonInterpreter, WorkspacePythonPath } from '../contracts';
98
import { IInterpreterComparer, IInterpreterSelector, IPythonPathUpdaterServiceManager } from './types';
109

@@ -15,51 +14,32 @@ export interface IInterpreterQuickPickItem extends QuickPickItem {
1514
@injectable()
1615
export class InterpreterSelector implements IInterpreterSelector {
1716
private disposables: Disposable[] = [];
18-
private readonly interpreterManager: IInterpreterService;
19-
private readonly workspaceService: IWorkspaceService;
20-
private readonly applicationShell: IApplicationShell;
21-
private readonly documentManager: IDocumentManager;
22-
private readonly pathUtils: IPathUtils;
23-
private readonly interpreterComparer: IInterpreterComparer;
2417

25-
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
26-
this.interpreterManager = serviceContainer.get<IInterpreterService>(IInterpreterService);
27-
this.workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
28-
this.applicationShell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
29-
this.documentManager = this.serviceContainer.get<IDocumentManager>(IDocumentManager);
30-
this.pathUtils = this.serviceContainer.get<IPathUtils>(IPathUtils);
31-
this.interpreterComparer = this.serviceContainer.get<IInterpreterComparer>(IInterpreterComparer);
18+
constructor(@inject(IInterpreterService) private readonly interpreterManager: IInterpreterService,
19+
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
20+
@inject(IApplicationShell) private readonly applicationShell: IApplicationShell,
21+
@inject(IDocumentManager) private readonly documentManager: IDocumentManager,
22+
@inject(IPathUtils) private readonly pathUtils: IPathUtils,
23+
@inject(IInterpreterComparer) private readonly interpreterComparer: IInterpreterComparer,
24+
@inject(IPythonPathUpdaterServiceManager) private readonly pythonPathUpdaterService: IPythonPathUpdaterServiceManager,
25+
@inject(IShebangCodeLensProvider) private readonly shebangCodeLensProvider: IShebangCodeLensProvider,
26+
@inject(ICommandManager) private readonly commandManager: ICommandManager) {
3227
}
3328
public dispose() {
3429
this.disposables.forEach(disposable => disposable.dispose());
3530
}
3631

3732
public initialize() {
38-
const commandManager = this.serviceContainer.get<ICommandManager>(ICommandManager);
39-
this.disposables.push(commandManager.registerCommand(Commands.Set_Interpreter, this.setInterpreter.bind(this)));
40-
this.disposables.push(commandManager.registerCommand(Commands.Set_ShebangInterpreter, this.setShebangInterpreter.bind(this)));
33+
this.disposables.push(this.commandManager.registerCommand(Commands.Set_Interpreter, this.setInterpreter.bind(this)));
34+
this.disposables.push(this.commandManager.registerCommand(Commands.Set_ShebangInterpreter, this.setShebangInterpreter.bind(this)));
4135
}
4236

4337
public async getSuggestions(resourceUri?: Uri) {
4438
const interpreters = await this.interpreterManager.getInterpreters(resourceUri);
4539
interpreters.sort(this.interpreterComparer.compare.bind(this.interpreterComparer));
4640
return Promise.all(interpreters.map(item => this.suggestionToQuickPickItem(item, resourceUri)));
4741
}
48-
private async getWorkspaceToSetPythonPath(): Promise<WorkspacePythonPath | undefined> {
49-
if (!Array.isArray(this.workspaceService.workspaceFolders) || this.workspaceService.workspaceFolders.length === 0) {
50-
return undefined;
51-
}
52-
if (this.workspaceService.workspaceFolders.length === 1) {
53-
return { folderUri: this.workspaceService.workspaceFolders[0].uri, configTarget: ConfigurationTarget.Workspace };
54-
}
55-
56-
// Ok we have multiple interpreters, get the user to pick a folder.
57-
const applicationShell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
58-
const workspaceFolder = await applicationShell.showWorkspaceFolderPick({ placeHolder: 'Select a workspace' });
59-
return workspaceFolder ? { folderUri: workspaceFolder.uri, configTarget: ConfigurationTarget.WorkspaceFolder } : undefined;
60-
}
61-
62-
private async suggestionToQuickPickItem(suggestion: PythonInterpreter, workspaceUri?: Uri): Promise<IInterpreterQuickPickItem> {
42+
protected async suggestionToQuickPickItem(suggestion: PythonInterpreter, workspaceUri?: Uri): Promise<IInterpreterQuickPickItem> {
6343
const detail = this.pathUtils.getDisplayName(suggestion.path, workspaceUri ? workspaceUri.fsPath : undefined);
6444
const cachedPrefix = suggestion.cachedEntry ? '(cached) ' : '';
6545
return {
@@ -70,7 +50,7 @@ export class InterpreterSelector implements IInterpreterSelector {
7050
};
7151
}
7252

73-
private async setInterpreter() {
53+
protected async setInterpreter() {
7454
const setInterpreterGlobally = !Array.isArray(this.workspaceService.workspaceFolders) || this.workspaceService.workspaceFolders.length === 0;
7555
let configTarget = ConfigurationTarget.Global;
7656
let wkspace: Uri | undefined;
@@ -93,14 +73,12 @@ export class InterpreterSelector implements IInterpreterSelector {
9373

9474
const selection = await this.applicationShell.showQuickPick(suggestions, quickPickOptions);
9575
if (selection !== undefined) {
96-
const pythonPathUpdaterService = this.serviceContainer.get<IPythonPathUpdaterServiceManager>(IPythonPathUpdaterServiceManager);
97-
await pythonPathUpdaterService.updatePythonPath(selection.path, configTarget, 'ui', wkspace);
76+
await this.pythonPathUpdaterService.updatePythonPath(selection.path, configTarget, 'ui', wkspace);
9877
}
9978
}
10079

101-
private async setShebangInterpreter(): Promise<void> {
102-
const shebangCodeLensProvider = this.serviceContainer.get<IShebangCodeLensProvider>(IShebangCodeLensProvider);
103-
const shebang = await shebangCodeLensProvider.detectShebang(this.documentManager.activeTextEditor!.document);
80+
protected async setShebangInterpreter(): Promise<void> {
81+
const shebang = await this.shebangCodeLensProvider.detectShebang(this.documentManager.activeTextEditor!.document);
10482
if (!shebang) {
10583
return;
10684
}
@@ -109,17 +87,28 @@ export class InterpreterSelector implements IInterpreterSelector {
10987
const workspaceFolder = this.workspaceService.getWorkspaceFolder(this.documentManager.activeTextEditor!.document.uri);
11088
const isWorkspaceChange = Array.isArray(this.workspaceService.workspaceFolders) && this.workspaceService.workspaceFolders.length === 1;
11189

112-
const pythonPathUpdaterService = this.serviceContainer.get<IPythonPathUpdaterServiceManager>(IPythonPathUpdaterServiceManager);
11390
if (isGlobalChange) {
114-
await pythonPathUpdaterService.updatePythonPath(shebang, ConfigurationTarget.Global, 'shebang');
91+
await this.pythonPathUpdaterService.updatePythonPath(shebang, ConfigurationTarget.Global, 'shebang');
11592
return;
11693
}
11794

11895
if (isWorkspaceChange || !workspaceFolder) {
119-
await pythonPathUpdaterService.updatePythonPath(shebang, ConfigurationTarget.Workspace, 'shebang', this.workspaceService.workspaceFolders![0].uri);
96+
await this.pythonPathUpdaterService.updatePythonPath(shebang, ConfigurationTarget.Workspace, 'shebang', this.workspaceService.workspaceFolders![0].uri);
12097
return;
12198
}
12299

123-
await pythonPathUpdaterService.updatePythonPath(shebang, ConfigurationTarget.WorkspaceFolder, 'shebang', workspaceFolder.uri);
100+
await this.pythonPathUpdaterService.updatePythonPath(shebang, ConfigurationTarget.WorkspaceFolder, 'shebang', workspaceFolder.uri);
101+
}
102+
private async getWorkspaceToSetPythonPath(): Promise<WorkspacePythonPath | undefined> {
103+
if (!Array.isArray(this.workspaceService.workspaceFolders) || this.workspaceService.workspaceFolders.length === 0) {
104+
return undefined;
105+
}
106+
if (this.workspaceService.workspaceFolders.length === 1) {
107+
return { folderUri: this.workspaceService.workspaceFolders[0].uri, configTarget: ConfigurationTarget.WorkspaceFolder };
108+
}
109+
110+
// Ok we have multiple workspaces, get the user to pick a folder.
111+
const workspaceFolder = await this.applicationShell.showWorkspaceFolderPick({ placeHolder: 'Select a workspace' });
112+
return workspaceFolder ? { folderUri: workspaceFolder.uri, configTarget: ConfigurationTarget.WorkspaceFolder } : undefined;
124113
}
125114
}

0 commit comments

Comments
 (0)