Skip to content

Commit 2d9f1c2

Browse files
segevfinerlgeiger
authored andcommitted
Implement virtualenv detection for Windows. Refactoring along the way.
Fixes #69
1 parent 8b73ada commit 2d9f1c2

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

lib/main.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const { Directory } = require("atom");
77
// adapted from http://stackoverflow.com/q/5474008
88
const PYTHON_REGEX = /(([^\d\W]|[\u00A0-\uFFFF])[\w.\u00A0-\uFFFF]*)|\.$/;
99

10+
const VIRTUAL_ENV_BIN_DIRS = ["bin", "Scripts"];
11+
const VIRTUAL_ENV_EXECUTABLES = ["python", "python.exe"];
12+
1013
class PythonLanguageClient extends AutoLanguageClient {
1114
getGrammarScopes() {
1215
return ["source.python"];
@@ -28,11 +31,9 @@ class PythonLanguageClient extends AutoLanguageClient {
2831
return { pyls: { plugins: configuration } }
2932
}
3033

31-
async startServerProcess(projectPath) {
32-
await new Promise(resolve => atom.whenShellEnvironmentLoaded(resolve));
33-
34+
async detectVirtualEnv(path) {
3435
const entries = await new Promise(resolve =>
35-
new Directory(projectPath).getEntries((error, entries) => {
36+
new Directory(path).getEntries((error, entries) => {
3637
if (error === null) {
3738
resolve(entries);
3839
} else {
@@ -41,31 +42,38 @@ class PythonLanguageClient extends AutoLanguageClient {
4142
})
4243
);
4344

44-
let venvPath = null;
45-
4645
if (entries) {
4746
for (let entry of entries) {
4847
if (entry.isDirectory()) {
49-
if (
50-
entry.getBaseName() === "bin" &&
51-
(await entry.getFile("python").exists())
52-
) {
53-
venvPath = projectPath;
54-
break;
48+
if (VIRTUAL_ENV_BIN_DIRS.indexOf(entry.getBaseName()) !== -1) {
49+
for (let executable of VIRTUAL_ENV_EXECUTABLES) {
50+
if (await entry.getFile(executable).exists()) {
51+
return path;
52+
}
53+
}
5554
} else {
56-
if (
57-
await entry
58-
.getSubdirectory("bin")
59-
.getFile("python")
60-
.exists()
61-
) {
62-
venvPath = entry.getPath();
63-
break;
55+
for (let dir_name of VIRTUAL_ENV_BIN_DIRS) {
56+
for (let executable of VIRTUAL_ENV_EXECUTABLES) {
57+
if (
58+
await entry
59+
.getSubdirectory(dir_name)
60+
.getFile(executable)
61+
.exists()
62+
) {
63+
return entry.getPath();
64+
}
65+
}
6466
}
6567
}
6668
}
6769
}
6870
}
71+
}
72+
73+
async startServerProcess(projectPath) {
74+
await new Promise(resolve => atom.whenShellEnvironmentLoaded(resolve));
75+
76+
let venvPath = await this.detectVirtualEnv(projectPath)
6977

7078
const pylsEnvironment = Object.assign({}, process.env);
7179

0 commit comments

Comments
 (0)