Skip to content

Commit

Permalink
refactor: improve python extra paths injection messages
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Oct 7, 2024
1 parent b1f48f4 commit 369fd24
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion plugin/dev_environment/impl/sublime_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 10 additions & 6 deletions plugin/dev_environment/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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)

0 comments on commit 369fd24

Please sign in to comment.