Skip to content

Commit 49ea6a2

Browse files
seeMwesm
authored andcommitted
Merged PR posit-dev/positron-python#104: refactor kernel to be an attribute + add types
Merge pull request #104 from posit-dev/refactor-kernel-attr refactor kernel to be an attribute + add types -------------------- Commit message for posit-dev/positron-python@ba2682d: black formatting -------------------- Commit message for posit-dev/positron-python@cab2ed3: refactor kernel to be an attribute + add types Authored-by: Wasim Lorgat <[email protected]> Signed-off-by: Wasim Lorgat <[email protected]>
1 parent 1467c05 commit 49ea6a2

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

extensions/positron-python/pythonFiles/positron/lsp.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
#
44

55
import urllib.parse
6-
from typing import Optional, Tuple
6+
from typing import Optional, Tuple, TYPE_CHECKING
77

88
from .positron_jedilsp import POSITRON
99

10+
if TYPE_CHECKING:
11+
from .positron_ipkernel import PositronIPyKernel
12+
1013

1114
class LSPService:
1215
"""
13-
LSPService manages the positron.lsp comm and cooridinates starting the LSP.
16+
LSPService manages the positron.lsp comm and coordinates starting the LSP.
1417
"""
1518

16-
def __init__(self, kernel): # noqa: F821
19+
def __init__(self, kernel: "PositronIPyKernel"):
1720
self.kernel = kernel
1821
self.lsp_comm = None
1922

extensions/positron-python/pythonFiles/positron/positron_jedilsp.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
sys.path.insert(0, os.path.join(EXTENSION_ROOT, "pythonFiles", "lib", "jedilsp"))
1111

1212
from threading import Event
13-
from typing import Any, Callable, List, Optional, Union
13+
from typing import Any, Callable, List, Optional, Union, TYPE_CHECKING
1414

1515

1616
from jedi.api import Interpreter
@@ -83,13 +83,19 @@
8383
from pygls.capabilities import get_capability
8484
from pygls.feature_manager import has_ls_param_or_annotation
8585

86+
if TYPE_CHECKING:
87+
from .positron_ipkernel import PositronIPyKernel
88+
8689

8790
class PositronJediLanguageServer(JediLanguageServer):
88-
"""Positron extenstion to the Jedi language server."""
91+
"""Positron extension to the Jedi language server."""
8992

9093
def __init__(self, *args: Any, **kwargs: Any) -> None:
9194
super().__init__(*args, **kwargs)
9295

96+
# Reference to an IPyKernel set on server start
97+
self.kernel: Optional["PositronIPyKernel"] = None
98+
9399
def feature(self, feature_name: str, options: Optional[Any] = None) -> Callable:
94100
def decorator(f):
95101
# Unfortunately Jedi doesn't handle subclassing of the LSP, so we
@@ -109,21 +115,24 @@ def decorator(f):
109115

110116
return decorator
111117

112-
def start(self, lsp_host: str, lsp_port: int, kernel) -> None:
118+
def start(self, lsp_host: str, lsp_port: int, kernel: "PositronIPyKernel") -> None:
113119
"""
114120
Start the LSP with a reference to Positron's IPyKernel to enhance
115121
completions with awareness of live variables from user's namespace.
116122
"""
117-
global KERNEL
118-
KERNEL = kernel
123+
self.kernel = kernel
119124

120125
try:
121126
asyncio.ensure_future(self._start_jedi(lsp_host, lsp_port))
122127
except KeyboardInterrupt:
123128
pass
124129

125130
async def _start_jedi(self, lsp_host, lsp_port):
126-
"""Starts Jedi LSP as a TCP server using existing asyncio loop."""
131+
"""
132+
Starts Jedi LSP as a TCP server using existing asyncio loop.
133+
134+
Adapted from `pygls.server.Server.start_tcp` to use existing asyncio loop.
135+
"""
127136
self._stop_event = Event()
128137
loop = asyncio.get_event_loop()
129138
self._server = await loop.create_server(self.lsp, lsp_host, lsp_port) # type: ignore
@@ -136,9 +145,6 @@ async def _start_jedi(self, lsp_host, lsp_port):
136145
protocol_cls=JediLanguageServerProtocol,
137146
)
138147

139-
140-
KERNEL = None
141-
142148
# Server Features
143149
# Unfortunately we need to re-register these as Pygls Feature Management does
144150
# not support subclassing of the LSP, and Jedi did not use the expected "ls"
@@ -167,9 +173,8 @@ def positron_completion(
167173

168174
# Get a reference to the kernel's namespace for enhanced completions
169175
namespaces = []
170-
global KERNEL
171-
if KERNEL is not None:
172-
ns = KERNEL.get_user_ns()
176+
if server.kernel is not None:
177+
ns = server.kernel.get_user_ns()
173178
namespaces.append(ns)
174179

175180
# Use Interpreter() to include the kernel namespaces in completions

0 commit comments

Comments
 (0)