Skip to content

Commit

Permalink
[vscode] Use activated venv
Browse files Browse the repository at this point in the history
  • Loading branch information
vthemelis committed Mar 31, 2024
1 parent 25369d9 commit c132e3a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions tools/ide_plugins/vscode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out/**
5 changes: 3 additions & 2 deletions tools/ide_plugins/vscode/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pyre-vscode",
"version": "0.0.1",
"version": "0.0.2",
"publisher": "fb-pyre-check",
"engines": {
"vscode": "^1.15.0"
Expand Down Expand Up @@ -31,10 +31,11 @@
"devDependencies": {
"@types/mocha": "^2.2.44",
"@types/node": "^8.0.53",
"@vscode/python-extension": "^1.0.5",
"cson": "^4.1.0",
"tslint": "^5.8.0",
"tslint-microsoft-contrib": "^5.0.1",
"typescript": "^2.6.1",
"typescript": "^3.7",
"vscode": "^1.1.7"
}
}
64 changes: 61 additions & 3 deletions tools/ide_plugins/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@

import * as vscode from 'vscode';
import { LanguageClient, LanguageClientOptions, DidChangeConfigurationNotification } from 'vscode-languageclient';
import { EnvironmentPath, PythonExtension } from '@vscode/python-extension'
import { basename, dirname, join } from 'path';
import { existsSync, statSync } from 'fs';

let languageClient: LanguageClient;

let outputChannel = vscode.window.createOutputChannel("pyre");

namespace Configuration {

let configurationListener: vscode.Disposable;
Expand All @@ -33,12 +38,42 @@ namespace Configuration {

export async function activate(_: vscode.ExtensionContext) {

let serverOptions = {
command: "pyre",
const pythonExtension = vscode.extensions.getExtension<PythonExtension>('ms-python.python');

if (!pythonExtension) {
outputChannel.appendLine("Python extension not found. Will use the default console environment.");
createLanguageClient('pyre');
return;
}

const activatedEnvPath = pythonExtension.exports.environments.getActiveEnvironmentPath();
const pyrePath = inferPyreCommand(activatedEnvPath);

let languageClient: LanguageClient | undefined;

if (pyrePath) {
languageClient = createLanguageClient(pyrePath);
}

pythonExtension.exports.environments.onDidChangeActiveEnvironmentPath((e) => {
languageClient?.stop();
languageClient = undefined;

const pyrePath = inferPyreCommand(e);
if (pyrePath) {
languageClient = createLanguageClient(pyrePath);
}
});
}

function createLanguageClient(pyrePath: string) : LanguageClient {

const serverOptions = {
command: pyrePath,
args: ["persistent"]
};

let clientOptions: LanguageClientOptions = {
const clientOptions: LanguageClientOptions = {
documentSelector: [{scheme: 'file', language: 'python'}],
synchronize: {
// Notify the server about file changes to '.clientrc files contain in the workspace
Expand All @@ -58,6 +93,29 @@ export async function activate(_: vscode.ExtensionContext) {
Configuration.initialize();
});
languageClient.start();

return languageClient;
}

function inferPyreCommand(envPath: EnvironmentPath) : string | undefined {
const path = envPath.path;
const stat = statSync(path)

const pyrePath = stat.isFile() && basename(path) === 'python'
? join(dirname(envPath.path), 'pyre')
: stat.isDirectory()
? join(path, 'bin', 'pyre')
: undefined;

if (pyrePath && existsSync(pyrePath) && statSync(pyrePath).isFile()) {
outputChannel.appendLine(`Using pyre path: ${pyrePath} from python environment: ${envPath.id} at ${envPath.path}`);
} else if (pyrePath) {
outputChannel.appendLine(`Environment: ${envPath.id} at ${envPath.path} does not contain a pyre executable at ${pyrePath}`);
} else {
outputChannel.appendLine(`Could not infer pyre path from python environment: ${envPath.id} at ${envPath.path}`);
}

return pyrePath;
}

export function deactivate() {
Expand Down

0 comments on commit c132e3a

Please sign in to comment.