diff --git a/plugin/dev_environment/impl/sublime_text.py b/plugin/dev_environment/impl/sublime_text.py index b5ffba4..e3238cf 100644 --- a/plugin/dev_environment/impl/sublime_text.py +++ b/plugin/dev_environment/impl/sublime_text.py @@ -19,7 +19,7 @@ def python_version(self) -> tuple[int, int]: return (3, 3) def handle_(self, *, settings: DottedDict) -> None: - self._inject_extra_paths(settings=settings, paths=self.find_package_dependency_dirs()) + self._inject_extra_paths(settings=settings, paths=self.find_package_dependency_dirs(), operation="replace") def find_package_dependency_dirs(self) -> list[str]: dep_dirs = sys.path.copy() diff --git a/plugin/dev_environment/interfaces.py b/plugin/dev_environment/interfaces.py index a0ddb85..b7344b0 100644 --- a/plugin/dev_environment/interfaces.py +++ b/plugin/dev_environment/interfaces.py @@ -2,7 +2,7 @@ from abc import ABC, abstractmethod from pathlib import Path -from typing import Any, Iterable, Sequence, final +from typing import Any, Iterable, Literal, Sequence, final from LSP.plugin.core.collections import DottedDict @@ -49,7 +49,7 @@ def handle(self, *, settings: DottedDict) -> None: self.handle_(settings=settings) if self.venv_info: - self._inject_extra_paths(settings=settings, paths=(self.venv_info.site_packages_dir,), prepend=True) + self._inject_extra_paths(settings=settings, paths=(self.venv_info.site_packages_dir,)) @abstractmethod def handle_(self, *, settings: DottedDict) -> None: @@ -60,14 +60,18 @@ def _inject_extra_paths( *, settings: DottedDict, paths: Iterable[str | Path], - prepend: bool = False, + operation: Literal["append", "prepend", "replace"] = "prepend", ) -> None: """Injects the given `paths` to `XXX.analysis.extraPaths` setting.""" current_paths: list[str] = settings.get(SERVER_SETTING_ANALYSIS_EXTRAPATHS) or [] extra_paths = list(map(str, paths)) - if prepend: + if operation == "prepend": next_paths = extra_paths + current_paths - else: + elif operation == "append": next_paths = current_paths + extra_paths - log_info(f"Adding extra analysis paths ({prepend = }): {paths}") + elif operation == "replace": + next_paths = extra_paths + else: + raise ValueError(f"Invalid operation: {operation}") + log_info(f"Modified extra analysis paths ({operation = }): {paths}") settings.set(SERVER_SETTING_ANALYSIS_EXTRAPATHS, next_paths)