Skip to content

Commit 6f04d7b

Browse files
committed
implement update pythonpath and environment at LS initialization
1 parent 44a3932 commit 6f04d7b

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

robotcode/language_server/common/parts/workspace.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ def __init__(
150150
self.root_uri = root_uri
151151

152152
self.root_path = root_path
153-
self.workspace_folders_lock = Lock()
154-
self.workspace_folders: List[WorkspaceFolder] = (
153+
self._workspace_folders_lock = Lock()
154+
self._workspace_folders: List[WorkspaceFolder] = (
155155
[WorkspaceFolder(w.name, Uri(w.uri), w.uri) for w in workspace_folders]
156156
if workspace_folders is not None
157157
else []
@@ -164,6 +164,10 @@ def __init__(
164164
self.parent.on_shutdown.add(self._on_shutdown)
165165
self._config_cache = AsyncSimpleLRUCache(max_items=1000)
166166

167+
@property
168+
def workspace_folders(self) -> List[WorkspaceFolder]:
169+
return self._workspace_folders
170+
167171
@_logger.call
168172
async def _on_shutdown(self, sender: Any) -> None:
169173
for e in self._file_watchers.copy():
@@ -374,19 +378,19 @@ async def _workspace_did_change_workspace_folders(
374378
self, event: WorkspaceFoldersChangeEvent, *args: Any, **kwargs: Any
375379
) -> None:
376380

377-
async with self.workspace_folders_lock:
381+
async with self._workspace_folders_lock:
378382
to_remove: List[WorkspaceFolder] = []
379383
for removed in event.removed:
380-
to_remove += [w for w in self.workspace_folders if w.uri == removed.uri]
384+
to_remove += [w for w in self._workspace_folders if w.uri == removed.uri]
381385

382386
for removed in event.added:
383-
to_remove += [w for w in self.workspace_folders if w.uri == removed.uri]
387+
to_remove += [w for w in self._workspace_folders if w.uri == removed.uri]
384388

385389
for r in to_remove:
386-
self.workspace_folders.remove(r)
390+
self._workspace_folders.remove(r)
387391

388392
for a in event.added:
389-
self.workspace_folders.append(WorkspaceFolder(a.name, Uri(a.uri), a.uri))
393+
self._workspace_folders.append(WorkspaceFolder(a.name, Uri(a.uri), a.uri))
390394

391395
@async_event
392396
async def did_change_watched_files(sender, changes: List[FileEvent]) -> None: # NOSONAR

robotcode/language_server/robotframework/diagnostics/library_doc.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import (
1717
AbstractSet,
1818
Any,
19+
ClassVar,
1920
Dict,
2021
Iterable,
2122
Iterator,
@@ -283,9 +284,6 @@ def resolve(
283284
)
284285
resolver.resolve(arguments, variables)
285286

286-
def __hash__(self) -> int:
287-
return id(self)
288-
289287

290288
@dataclass
291289
class KeywordDoc(SourceEntity):
@@ -436,7 +434,6 @@ def is_run_keywords(self) -> bool:
436434
return self.libname == BUILTIN_LIBRARY_NAME and self.name == RUN_KEYWORDS_NAME
437435

438436
def __hash__(self) -> int:
439-
# return id(self)
440437
return hash(
441438
(
442439
self.name,
@@ -607,12 +604,12 @@ def source_or_origin(self) -> Optional[str]:
607604

608605
return None
609606

610-
_inline_link: re.Pattern = re.compile( # type: ignore
607+
_inline_link: ClassVar[re.Pattern] = re.compile( # type: ignore
611608
r"([\`])((?:\1|.)+?)\1",
612609
re.VERBOSE,
613610
)
614611

615-
_headers: re.Pattern = re.compile(r"^(={1,5})\s+(\S.*?)\s+\1$", re.MULTILINE) # type: ignore
612+
_headers: ClassVar[re.Pattern] = re.compile(r"^(={1,5})\s+(\S.*?)\s+\1$", re.MULTILINE) # type: ignore
616613

617614
def _process_inline_links(self, text: str) -> str:
618615
headers = [v.group(2) for v in self._headers.finditer(text)]
@@ -706,7 +703,7 @@ class VariablesDoc(LibraryDoc):
706703

707704

708705
def is_library_by_path(path: str) -> bool:
709-
return path.lower().endswith((".py", ".java", ".class", "/", os.sep))
706+
return path.lower().endswith((".py", "/", os.sep))
710707

711708

712709
def is_variables_by_path(path: str) -> bool:
@@ -1327,7 +1324,9 @@ def get_test_library(
13271324
is_registered_run_keyword=RUN_KW_REGISTER.is_run_keyword(libdoc.name, kw[0].name),
13281325
args_to_process=RUN_KW_REGISTER.get_args_to_process(libdoc.name, kw[0].name),
13291326
deprecated=kw[0].deprecated,
1330-
arguments=ArgumentSpec.from_robot_argument_spec(kw[1].arguments),
1327+
arguments=ArgumentSpec.from_robot_argument_spec(kw[1].arguments)
1328+
if not kw[1].is_error_handler
1329+
else None,
13311330
)
13321331
for kw in [
13331332
(KeywordDocBuilder().build_keyword(k), k)

robotcode/language_server/robotframework/protocol.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import os
2+
import sys
13
from dataclasses import dataclass
4+
from pathlib import Path
25
from typing import TYPE_CHECKING, Any, Optional
36

47
from ..._version import __version__
@@ -12,6 +15,7 @@
1215
from ..common.lsp_types import InitializeError, Model
1316
from ..common.parts.document_symbols import symbol_information_label
1417
from ..common.protocol import LanguageServerProtocol
18+
from .configuration import RobotConfig
1519
from .parts.codelens import RobotCodeLensProtocolPart
1620
from .parts.completion import RobotCompletionProtocolPart
1721
from .parts.debugging_utils import RobotDebuggingUtilsProtocolPart
@@ -106,6 +110,7 @@ def __init__(self, server: "RobotLanguageServer"):
106110
super().__init__(server)
107111
self.options = Options()
108112
self.on_initialize.add(self._on_initialize)
113+
self.on_initialized.add(self._on_initialized)
109114
self.on_shutdown.add(self._on_shutdown)
110115

111116
@_logger.call
@@ -125,3 +130,21 @@ async def _on_initialize(self, sender: Any, initialization_options: Optional[Any
125130
self.options = from_dict(initialization_options, Options)
126131

127132
self._logger.debug(f"initialized with {repr(self.options)}")
133+
134+
async def _on_initialized(self, sender: Any) -> None:
135+
for folder in self.workspace.workspace_folders:
136+
config: RobotConfig = await self.workspace.get_configuration(RobotConfig, folder.uri)
137+
if config is not None:
138+
if config.env:
139+
for k, v in config.env.items():
140+
os.environ[k] = v
141+
142+
if config.python_path:
143+
for p in config.python_path:
144+
pa = Path(p)
145+
if not pa.is_absolute():
146+
pa = Path(folder.uri.to_path(), pa)
147+
148+
absolute_path = str(pa.absolute())
149+
if absolute_path not in sys.path:
150+
sys.path.insert(0, absolute_path)

0 commit comments

Comments
 (0)