From e8800e5bbb52abbf43ba997834ad6735f131268c Mon Sep 17 00:00:00 2001 From: Sangwoo Han Date: Tue, 19 Dec 2017 14:56:44 +0900 Subject: [PATCH 1/3] Improve virtualenv aware by searching subdirectories --- lib/main.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/main.js b/lib/main.js index 301798c1..622cb20e 100644 --- a/lib/main.js +++ b/lib/main.js @@ -31,14 +31,30 @@ class PythonLanguageClient extends AutoLanguageClient { async startServerProcess(projectPath) { await new Promise(resolve => atom.whenShellEnvironmentLoaded(resolve)); - var venvPromise = new Directory(projectPath).getSubdirectory('bin') - .getFile('python') - .exists(); + var venv = false; + var venvPath = ""; + + new Directory(projectPath) + .getEntriesSync() + .forEach(entry => { + if (entry.isDirectory()) { + if (entry.getBaseName() === 'bin' && + entry.getFile('python').existsSync()) { + venv = true; + venvPath = projectPath; + } else { + if (entry.getSubdirectory('bin').getFile('python').existsSync()) { + venv = true; + venvPath = entry.getPath(); + } + } + } + }); var pylsEnvironment = Object.assign({}, process.env); - if(await venvPromise) { - pylsEnvironment['VIRTUAL_ENV'] = projectPath; + if (venv) { + pylsEnvironment['VIRTUAL_ENV'] = venvPath; } const childProcess = cp.spawn(atom.config.get("ide-python.pylsPath"), { From 5aede0a24944bad48219c840fc1b60cc3a980f0c Mon Sep 17 00:00:00 2001 From: Sangwoo Han Date: Wed, 27 Dec 2017 03:23:37 +0900 Subject: [PATCH 2/3] Convert sync into async --- lib/main.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/main.js b/lib/main.js index 622cb20e..c7f5533d 100644 --- a/lib/main.js +++ b/lib/main.js @@ -31,29 +31,37 @@ class PythonLanguageClient extends AutoLanguageClient { async startServerProcess(projectPath) { await new Promise(resolve => atom.whenShellEnvironmentLoaded(resolve)); - var venv = false; - var venvPath = ""; + var entries = await new Promise(resolve => + new Directory(projectPath).getEntries((error, entries) => { + if (error === null) { + resolve(entries); + } else { + resolve(null); + } + })); + + var venvPath = null; - new Directory(projectPath) - .getEntriesSync() - .forEach(entry => { + if (entries) { + for (let entry of entries) { if (entry.isDirectory()) { if (entry.getBaseName() === 'bin' && - entry.getFile('python').existsSync()) { - venv = true; + await entry.getFile('python').exists()) { venvPath = projectPath; + break; } else { - if (entry.getSubdirectory('bin').getFile('python').existsSync()) { - venv = true; + if (await entry.getSubdirectory('bin').getFile('python').exists()) { venvPath = entry.getPath(); + break; } } } - }); + } + } var pylsEnvironment = Object.assign({}, process.env); - if (venv) { + if (venvPath) { pylsEnvironment['VIRTUAL_ENV'] = venvPath; } From d0c90066b68c13935e8edb341a57bf5e1cb9beaa Mon Sep 17 00:00:00 2001 From: Lukas Geiger Date: Wed, 27 Dec 2017 00:01:12 +0100 Subject: [PATCH 3/3] Run prettier and switch from var to let/const --- lib/main.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/main.js b/lib/main.js index c7f5533d..4c5efe15 100644 --- a/lib/main.js +++ b/lib/main.js @@ -31,26 +31,34 @@ class PythonLanguageClient extends AutoLanguageClient { async startServerProcess(projectPath) { await new Promise(resolve => atom.whenShellEnvironmentLoaded(resolve)); - var entries = await new Promise(resolve => + const entries = await new Promise(resolve => new Directory(projectPath).getEntries((error, entries) => { if (error === null) { resolve(entries); } else { resolve(null); } - })); + }) + ); - var venvPath = null; + let venvPath = null; if (entries) { for (let entry of entries) { if (entry.isDirectory()) { - if (entry.getBaseName() === 'bin' && - await entry.getFile('python').exists()) { + if ( + entry.getBaseName() === "bin" && + (await entry.getFile("python").exists()) + ) { venvPath = projectPath; break; } else { - if (await entry.getSubdirectory('bin').getFile('python').exists()) { + if ( + await entry + .getSubdirectory("bin") + .getFile("python") + .exists() + ) { venvPath = entry.getPath(); break; } @@ -59,10 +67,10 @@ class PythonLanguageClient extends AutoLanguageClient { } } - var pylsEnvironment = Object.assign({}, process.env); + const pylsEnvironment = Object.assign({}, process.env); if (venvPath) { - pylsEnvironment['VIRTUAL_ENV'] = venvPath; + pylsEnvironment["VIRTUAL_ENV"] = venvPath; } const childProcess = cp.spawn(atom.config.get("ide-python.pylsPath"), {