|
1 | 1 | import os |
2 | 2 | import sys |
3 | | -from dataclasses import dataclass |
| 3 | +from dataclasses import dataclass, field |
4 | 4 | from pathlib import Path |
5 | 5 | from typing import TYPE_CHECKING, Any, Dict, List, Optional |
6 | 6 |
|
@@ -73,6 +73,8 @@ def check_robotframework() -> None: |
73 | 73 | class Options(Model): |
74 | 74 | storage_uri: Optional[str] = None |
75 | 75 | global_storage_uri: Optional[str] = None |
| 76 | + python_path: List[str] = field(default_factory=list) |
| 77 | + env: Dict[str, str] = field(default_factory=dict) |
76 | 78 |
|
77 | 79 |
|
78 | 80 | @symbol_information_label("robotframework") |
@@ -137,16 +139,31 @@ async def _on_shutdown(self, sender: Any) -> None: |
137 | 139 |
|
138 | 140 | @_logger.call |
139 | 141 | async def _on_initialize(self, sender: Any, initialization_options: Optional[Any] = None) -> None: |
| 142 | + if initialization_options is not None: |
| 143 | + self.options = from_dict(initialization_options, Options) |
| 144 | + |
| 145 | + if self.options.env: |
| 146 | + for k, v in self.options.env.items(): |
| 147 | + os.environ[k] = v |
| 148 | + |
| 149 | + if self.options.python_path: |
| 150 | + for folder in self.workspace.workspace_folders: |
| 151 | + for p in self.options.python_path: |
| 152 | + pa = Path(p) |
| 153 | + if not pa.is_absolute(): |
| 154 | + pa = Path(folder.uri.to_path(), pa) |
| 155 | + |
| 156 | + absolute_path = str(pa.absolute()) |
| 157 | + if absolute_path not in sys.path: |
| 158 | + sys.path.insert(0, absolute_path) |
| 159 | + |
140 | 160 | try: |
141 | 161 | check_robotframework() |
142 | 162 | except RobotCodeException as e: |
143 | 163 | raise JsonRPCErrorException( |
144 | 164 | JsonRPCErrors.INTERNAL_ERROR, f"Can't start language server: {e}", InitializeError(retry=False) |
145 | 165 | ) from e |
146 | 166 |
|
147 | | - if initialization_options is not None: |
148 | | - self.options = from_dict(initialization_options, Options) |
149 | | - |
150 | 167 | self.workspace.did_change_configuration.add(self._on_did_change_configuration) |
151 | 168 |
|
152 | 169 | self._logger.critical(f"initialized with {repr(self.options)}") |
|
0 commit comments