Skip to content

Commit dd5619f

Browse files
authored
Merge pull request DonJayamanne#274 from jabooth/setInterpreterImprovements
Select Workspace Interpreter now can perform change itself
2 parents 8d0f73a + 609cb04 commit dd5619f

1 file changed

Lines changed: 53 additions & 25 deletions

File tree

src/client/providers/setInterpreterProvider.ts

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let ncp = require("copy-paste");
88

99
// where to find the Python binary within a conda env
1010
const CONDA_RELATIVE_PY_PATH = utils.IS_WINDOWS ? ['python'] : ['bin', 'python']
11+
const REPLACE_PYTHONPATH_REGEXP = /("python\.pythonPath"\s*:\s*)"(.*)"/;
1112

1213
interface PythonPathSuggestion {
1314
label: string, // myenvname
@@ -19,6 +20,22 @@ function workspaceSettingsPath() {
1920
return path.join(vscode.workspace.rootPath, '.vscode', 'settings.json')
2021
}
2122

23+
function openWorkspaceSettings() {
24+
return vscode.commands.executeCommand('workbench.action.openWorkspaceSettings');
25+
}
26+
27+
function replaceContentsOfFile(doc: vscode.TextDocument, newContent: string) {
28+
29+
const lastLine = doc.lineAt(doc.lineCount - 2);
30+
const start = new vscode.Position(0, 0);
31+
const end = new vscode.Position(doc.lineCount - 1, lastLine.text.length);
32+
33+
const textEdit = vscode.TextEdit.replace(new vscode.Range(start, end), newContent);
34+
const workspaceEdit = new vscode.WorkspaceEdit()
35+
workspaceEdit.set(doc.uri, [textEdit]);
36+
return vscode.workspace.applyEdit(workspaceEdit).then(() => doc.save())
37+
}
38+
2239
export function activateSetInterpreterProvider() {
2340
vscode.commands.registerCommand("python.setInterpreter", setInterpreter);
2441
}
@@ -75,21 +92,36 @@ function suggestPythonPaths(): Promise<vscode.QuickPickItem[]> {
7592
}
7693

7794
function setPythonPath(pythonPath: string) {
78-
// Waiting on https://github.com/Microsoft/vscode/issues/1396
79-
// For now, just let the user copy this to clipboard
80-
const copy_msg = "Copy to Clipboard"
81-
82-
// If the user already has .vscode/settings.json in the workspace
83-
// open it for them
8495
vscode.workspace.openTextDocument(workspaceSettingsPath())
85-
.then(doc => vscode.window.showTextDocument(doc));
86-
87-
vscode.window.showInformationMessage(pythonPath, copy_msg)
88-
.then(item => {
89-
if (item === copy_msg) {
90-
ncp.copy(pythonPath)
96+
.then(doc => {
97+
const settingsText = doc.getText();
98+
if (settingsText.search(REPLACE_PYTHONPATH_REGEXP) === -1) {
99+
// Can't find the setting to replace - will just have to offer a copy button and instruct them to edit themselves.
100+
openWorkspaceSettings().then(() => {
101+
const copyMsg = "Copy to Clipboard"
102+
const newEntry = `"python.pythonPath": "${pythonPath}"`;
103+
vscode.window.showInformationMessage(`Please add an entry: ${newEntry}`, copyMsg)
104+
.then(item => {
105+
if (item === copyMsg) {
106+
ncp.copy(newEntry)
107+
}
108+
})
109+
})
110+
} else {
111+
// Great, the user already has a setting stated that we can relibly replace!
112+
const newSettingsText = settingsText.replace(REPLACE_PYTHONPATH_REGEXP, `$1"${pythonPath}"`);
113+
replaceContentsOfFile(doc, newSettingsText).then(
114+
() => {
115+
vscode.window.setStatusBarMessage(`Workspace Interpreter set to ${pythonPath}`);
116+
// As the file is saved the following should be the same as each other but they
117+
// aren't - some form of race condition?
118+
// const currentPythonPath = settings.PythonSettings.getInstance().pythonPath;
119+
// console.log(currentPythonPath);
120+
// console.log(pythonPath);
121+
}
122+
)
91123
}
92-
})
124+
});
93125
}
94126

95127
function presentQuickPickOfSuggestedPythonPaths() {
@@ -119,16 +151,12 @@ function setInterpreter() {
119151
vscode.window.showErrorMessage("The interpreter can only be set within a workspace (open a folder)")
120152
return
121153
}
122-
vscode.workspace.openTextDocument(settingsPath)
123-
.then(doc => {
124-
// Great, workspace file exists. Present it for copy/pasting into...
125-
vscode.window.showTextDocument(doc);
126-
// ...and offer the quick-pick suggestions.
127-
presentQuickPickOfSuggestedPythonPaths()
128-
},
129-
() => {
130-
// The user doesn't have any workspace settings!
131-
// Prompt them to create one first
132-
vscode.window.showErrorMessage("No workspace settings file. First, run 'Preferences: Open Workspace Settings' to create one." )
133-
})
154+
vscode.workspace.openTextDocument(settingsPath).then(
155+
presentQuickPickOfSuggestedPythonPaths,
156+
() => {
157+
// No settings present yet! Trigger the opening of the workspace settings for the first time
158+
// then present the picker.
159+
openWorkspaceSettings().then(presentQuickPickOfSuggestedPythonPaths)
160+
}
161+
)
134162
}

0 commit comments

Comments
 (0)