Skip to content

Commit

Permalink
fix: additional paths shouldn't be duplicated everytime a new session…
Browse files Browse the repository at this point in the history
… starts

Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Jun 13, 2024
1 parent 9de78d5 commit 8a89ce4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
16 changes: 11 additions & 5 deletions st3/lsp_utils/_client_handler/abstract_plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .._util import weak_method
from ..api_wrapper_interface import ApiWrapperInterface
from ..helpers import unique_everseen
from ..server_resource_interface import ServerStatus
from .api_decorator import register_decorated_handlers
from .interface import ClientHandlerInterface
Expand All @@ -15,6 +16,7 @@
from LSP.plugin import WorkspaceFolder
from LSP.plugin.core.rpc import method2attr
from LSP.plugin.core.typing import Any, Callable, Dict, List, Optional, Tuple, TypedDict
from itertools import chain
from os import path
from weakref import ref
import sublime
Expand Down Expand Up @@ -128,12 +130,16 @@ def can_start(cls, window: sublime.Window, initiating_view: sublime.View,
@classmethod
def on_pre_start(cls, window: sublime.Window, initiating_view: sublime.View,
workspace_folders: List[WorkspaceFolder], configuration: ClientConfig) -> Optional[str]:
extra_paths = path.pathsep.join(cls.get_additional_paths())
# To fix https://github.com/TerminalFi/LSP-copilot/issues/163 ,
# We don't want to add the same path multiple times whenever a new server session is created.
extra_paths = cls.get_additional_paths()
if extra_paths:
original_path = configuration.env.get('PATH') or ''
if isinstance(original_path, list):
original_path = path.pathsep.join(original_path)
configuration.env['PATH'] = path.pathsep.join([extra_paths, original_path])
original_path_raw = configuration.env.get('PATH') or ''
if isinstance(original_path_raw, str):
original_paths = original_path_raw.split(path.pathsep)
else:
original_paths = original_path_raw
configuration.env['PATH'] = path.pathsep.join(unique_everseen(chain(extra_paths, original_paths)))
return None

# --- ClientHandlerInterface --------------------------------------------------------------------------------------
Expand Down
47 changes: 46 additions & 1 deletion st3/lsp_utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from LSP.plugin.core.typing import Any, Callable, Dict, List, Optional, Tuple
from LSP.plugin.core.typing import Any, Callable, Dict, Generator, Iterable, List, Optional, Set, Tuple, TypeVar
import os
import shutil
import sublime
import subprocess
import threading

_T = TypeVar('_T')

StringCallback = Callable[[str], None]
SemanticVersion = Tuple[int, int, int]

Expand Down Expand Up @@ -93,3 +95,46 @@ def log_and_show_message(message: str, additional_logs: Optional[str] = None, sh
print(message, '\n', additional_logs) if additional_logs else print(message)
if show_in_status:
sublime.active_window().status_message(message)


def unique_everseen(iterable: Iterable[_T], key: Optional[Callable[[_T], Any]] = None) -> Generator[_T, None, None]:
"""
Yield unique elements, preserving order.
>>> list(unique_everseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D']
>>> list(unique_everseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'D']
Sequences with a mix of hashable and unhashable items can be used.
The function will be slower (i.e., `O(n^2)`) for unhashable items.
Remember that ``list`` objects are unhashable - you can use the *key*
parameter to transform the list to a tuple (which is hashable) to
avoid a slowdown.
>>> iterable = ([1, 2], [2, 3], [1, 2])
>>> list(unique_everseen(iterable)) # Slow
[[1, 2], [2, 3]]
>>> list(unique_everseen(iterable, key=tuple)) # Faster
[[1, 2], [2, 3]]
Similarly, you may want to convert unhashable ``set`` objects with
``key=frozenset``. For ``dict`` objects,
``key=lambda x: frozenset(x.items())`` can be used.
"""
seenset: Set[Any] = set()
seenset_add = seenset.add
seenlist: List[Any] = []
seenlist_add = seenlist.append

for element in iterable:
k = element if key is None else key(element)
try:
if k not in seenset:
seenset_add(k)
yield element
except TypeError:
if k not in seenlist:
seenlist_add(k)
yield element

0 comments on commit 8a89ce4

Please sign in to comment.