Skip to content

Commit bce22a2

Browse files
committed
use new api to update configuration
1 parent b42a4f9 commit bce22a2

File tree

1 file changed

+10
-79
lines changed

1 file changed

+10
-79
lines changed

src/client/providers/setInterpreterProvider.ts

Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
import * as child_process from 'child_process';
3-
import * as path from "path";
4-
import * as fs from "fs";
3+
import * as path from "path";
4+
import * as fs from "fs";
55
import * as vscode from "vscode";
66
import * as settings from "./../common/configSettings";
77
import * as utils from "./../common/utils";
@@ -55,26 +55,6 @@ function getSearchPaths(): string[] {
5555
}
5656
}
5757

58-
function workspaceSettingsPath() {
59-
return path.join(vscode.workspace.rootPath, '.vscode', 'settings.json')
60-
}
61-
62-
function openWorkspaceSettings() {
63-
return vscode.commands.executeCommand('workbench.action.openWorkspaceSettings');
64-
}
65-
66-
function replaceContentsOfFile(doc: vscode.TextDocument, newContent: string) {
67-
68-
const lastLine = doc.lineAt(doc.lineCount - 2);
69-
const start = new vscode.Position(0, 0);
70-
const end = new vscode.Position(doc.lineCount - 1, lastLine.text.length);
71-
72-
const textEdit = vscode.TextEdit.replace(new vscode.Range(start, end), newContent);
73-
const workspaceEdit = new vscode.WorkspaceEdit()
74-
workspaceEdit.set(doc.uri, [textEdit]);
75-
return vscode.workspace.applyEdit(workspaceEdit).then(() => doc.save())
76-
}
77-
7858
export function activateSetInterpreterProvider() {
7959
vscode.commands.registerCommand("python.setInterpreter", setInterpreter);
8060
}
@@ -175,7 +155,6 @@ function suggestionsFromConda(): Promise<PythonPathSuggestion[]> {
175155
});
176156
}
177157

178-
179158
function suggestionToQuickPickItem(suggestion: PythonPathSuggestion): PythonPathQuickPickItem {
180159
let detail = suggestion.path;
181160
if (suggestion.path.startsWith(vscode.workspace.rootPath)) {
@@ -209,54 +188,16 @@ function suggestPythonPaths(): Promise<PythonPathQuickPickItem[]> {
209188
}
210189

211190
function setPythonPath(pythonPath: string, created: boolean = false) {
212-
if (pythonPath.startsWith(vscode.workspace.rootPath)){
191+
if (pythonPath.startsWith(vscode.workspace.rootPath)) {
213192
pythonPath = path.join('${workspaceRoot}', path.relative(vscode.workspace.rootPath, pythonPath));
214193
}
215-
const settingsFile = workspaceSettingsPath();
216-
utils.validatePath(settingsFile)
217-
.then(validatedPath => {
218-
if (validatedPath.length === 0 && created === true) {
219-
// Something went wrong
220-
return Promise.reject<any>('Unable to create/open the Workspace Settings file');
221-
}
222-
if (validatedPath.length === 0 && !created) {
223-
return new Promise<any>((resolve, reject) => {
224-
vscode.commands.executeCommand('workbench.action.openWorkspaceSettings').then(() => resolve(null), reject);
225-
});
226-
}
227-
return vscode.workspace.openTextDocument(settingsFile)
228-
})
229-
.then(doc => {
230-
const settingsText = doc ? doc.getText() : '';
231-
if (settingsText.search(REPLACE_PYTHONPATH_REGEXP) === -1) {
232-
// Can't find the setting to replace - will just have to offer a copy button and instruct them to edit themselves.
233-
openWorkspaceSettings().then(() => {
234-
const copyMsg = "Copy to Clipboard"
235-
const newEntry = `"python.pythonPath": "${pythonPath}"`;
236-
vscode.window.showInformationMessage(`Please add an entry: ${newEntry}`, copyMsg)
237-
.then(item => {
238-
if (item === copyMsg) {
239-
ncp.copy(newEntry)
240-
}
241-
})
242-
})
243-
} else {
244-
// Great, the user already has a setting stated that we can relibly replace!
245-
const newSettingsText = settingsText.replace(REPLACE_PYTHONPATH_REGEXP, `$1"${pythonPath}"`);
246-
replaceContentsOfFile(doc, newSettingsText).then(
247-
() => {
248-
vscode.window.setStatusBarMessage(`Workspace Interpreter set to ${pythonPath}`, 1000);
249-
// As the file is saved the following should be the same as each other but they
250-
// aren't - some form of race condition?
251-
// const currentPythonPath = settings.PythonSettings.getInstance().pythonPath;
252-
// console.log(currentPythonPath);
253-
// console.log(pythonPath);
254-
}
255-
)
256-
}
257-
}).catch(reason => {
258-
vscode.window.showErrorMessage('Failed to set the interpreter. ' + reason);
259-
});
194+
const pythonConfig = vscode.workspace.getConfiguration('python');
195+
pythonConfig.update('pythonPath', pythonPath).then(() => {
196+
//Done
197+
}, reason => {
198+
vscode.window.showErrorMessage(`Failed to set 'pythonPath'. Error: ${reason.message}`);
199+
console.error(reason);
200+
})
260201
}
261202

262203
function presentQuickPickOfSuggestedPythonPaths() {
@@ -278,15 +219,5 @@ function presentQuickPickOfSuggestedPythonPaths() {
278219
}
279220

280221
function setInterpreter() {
281-
// For now the user has to manually edit the workspace settings to change the
282-
// pythonPath -> First check they have .vscode/settings.json
283-
let settingsPath: string
284-
try {
285-
settingsPath = workspaceSettingsPath()
286-
} catch (e) {
287-
// We aren't even in a workspace
288-
vscode.window.showErrorMessage("The interpreter can only be set within a workspace (open a folder)")
289-
return
290-
}
291222
presentQuickPickOfSuggestedPythonPaths();
292223
}

0 commit comments

Comments
 (0)