Skip to content

Commit 56fc595

Browse files
committed
Allow to use Jedi language server in system environment or virtual environments
As this extension uses an internal Jedi LSP, it is hard for the users to change the jedi setting to adapt various requirements. And in some cases, the user require a specific version of the Jedi, which is hard to change as it is embedded. Add a new configuration property to allow the users to use Jedi LSP in their system environment or virtual environments, and auto fallback to the internal version if the external version is not found. Signed-off-by: Inochi Amaoto <[email protected]>
1 parent 79035c8 commit 56fc595

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@
536536
"scope": "window",
537537
"type": "string"
538538
},
539+
"python.languageServer.useJediInEnvPath": {
540+
"default": false,
541+
"description": "%python.languageServer.useJediInEnvPath.description%",
542+
"scope": "window",
543+
"type": "boolean"
544+
},
539545
"python.interpreter.infoVisibility": {
540546
"default": "onPythonRelated",
541547
"description": "%python.interpreter.infoVisibility.description%",

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"python.languageServer.jediDescription": "Use Jedi behind the Language Server Protocol (LSP) as a language server.",
5757
"python.languageServer.pylanceDescription": "Use Pylance as a language server.",
5858
"python.languageServer.noneDescription": "Disable language server capabilities.",
59+
"python.languageServer.useJediInEnvPath.description": "Use Jedi in system environment or virtual environments.",
5960
"python.interpreter.infoVisibility.description": "Controls when to display information of selected interpreter in the status bar.",
6061
"python.interpreter.infoVisibility.never.description": "Never display information.",
6162
"python.interpreter.infoVisibility.onPythonRelated.description": "Only display information if Python-related files are opened.",

python_files/run-jedi-language-server.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22
import pathlib
33
import sys
44

5-
# Add the lib path to our sys path so jedi_language_server can find its references
6-
extension_dir = pathlib.Path(__file__).parent.parent
7-
EXTENSION_ROOT = os.fsdecode(extension_dir)
8-
sys.path.insert(0, os.fsdecode(extension_dir / "python_files" / "lib" / "jedilsp"))
9-
del extension_dir
5+
use_external_jedi = sys.argv[-1] == "external"
6+
sys.argv = sys.argv[:-1]
7+
8+
if use_external_jedi:
9+
try:
10+
import jedi_language_server
11+
except Exception:
12+
use_external_jedi = False
13+
14+
if not use_external_jedi:
15+
# Add the lib path to our sys path so jedi_language_server can find its references
16+
extension_dir = pathlib.Path(__file__).parent.parent
17+
EXTENSION_ROOT = os.fsdecode(extension_dir)
18+
sys.path.insert(0, os.fsdecode(extension_dir / "python_files" / "lib" / "jedilsp"))
19+
del extension_dir
1020

1121

1222
from jedi_language_server.cli import cli # noqa: E402

src/client/activation/jedi/languageClientFactory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
import * as path from 'path';
5+
import { WorkspaceConfiguration } from 'vscode';
56
import { LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node';
67

78
import { EXTENSION_ROOT_DIR, PYTHON_LANGUAGE } from '../../common/constants';
@@ -13,7 +14,7 @@ import { ILanguageClientFactory } from '../types';
1314
const languageClientName = 'Python Jedi';
1415

1516
export class JediLanguageClientFactory implements ILanguageClientFactory {
16-
constructor(private interpreterService: IInterpreterService) {}
17+
constructor(private interpreterService: IInterpreterService, private readonly workspaceConfiguration: WorkspaceConfiguration) {}
1718

1819
public async createLanguageClient(
1920
resource: Resource,
@@ -23,9 +24,10 @@ export class JediLanguageClientFactory implements ILanguageClientFactory {
2324
// Just run the language server using a module
2425
const lsScriptPath = path.join(EXTENSION_ROOT_DIR, 'python_files', 'run-jedi-language-server.py');
2526
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
27+
const useJediInEnv = this.workspaceConfiguration.get<boolean>("languageServer.useJediInEnvPath") === true
2628
const serverOptions: ServerOptions = {
2729
command: interpreter ? interpreter.path : 'python',
28-
args: [lsScriptPath],
30+
args: [lsScriptPath, useJediInEnv ? "external": "internal"],
2931
};
3032

3133
return new LanguageClient(PYTHON_LANGUAGE, languageClientName, serverOptions, clientOptions);

src/client/languageServer/jediLSExtensionManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class JediLSExtensionManager implements IDisposable, ILanguageServerExten
4747
configurationService,
4848
workspaceService,
4949
);
50-
this.clientFactory = new JediLanguageClientFactory(interpreterService);
50+
this.clientFactory = new JediLanguageClientFactory(interpreterService, workspaceService.getConfiguration("python"));
5151
this.serverProxy = new JediLanguageServerProxy(this.clientFactory);
5252
this.serverManager = new JediLanguageServerManager(
5353
serviceContainer,

0 commit comments

Comments
 (0)