Skip to content

Commit

Permalink
Merge branch 'fix/remove-generators' into fix/global-listener
Browse files Browse the repository at this point in the history
* fix/remove-generators:
  Use orjson to de-/serialize json-rpc messages (#2513)
  remove unnecessary use of generators for session retrieval
  • Loading branch information
rchl committed Oct 1, 2024
2 parents 0b82ef9 + 4addbea commit 17e7d0c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
python-version: '3.8'
- run: sudo apt update
- run: sudo apt install --no-install-recommends -y x11-xserver-utils
- run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 --user
- run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 orjson==3.10.7 --user
- run: echo "$HOME/.local/bin" >> $GITHUB_PATH
- run: mypy stubs
- run: flake8 plugin tests
Expand Down
1 change: 1 addition & 0 deletions dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
">=4096": [
"bracex",
"mdpopups",
"orjson",
"typing_extensions",
"wcmatch"
]
Expand Down
6 changes: 3 additions & 3 deletions plugin/core/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .windows import WindowManager
from .windows import WindowRegistry
from functools import partial
from typing import Any, Generator, Iterable
from typing import Any, Generator
import operator
import sublime
import sublime_plugin
Expand All @@ -21,14 +21,14 @@
windows = WindowRegistry()


def best_session(view: sublime.View, sessions: Iterable[Session], point: int | None = None) -> Session | None:
def best_session(view: sublime.View, sessions: list[Session], point: int | None = None) -> Session | None:
if point is None:
try:
point = view.sel()[0].b
except IndexError:
return None
try:
return max(sessions, key=lambda s: view.score_selector(point, s.config.priority_selector)) # type: ignore
return max(sessions, key=lambda s: view.score_selector(point, s.config.priority_selector))
except ValueError:
return None

Expand Down
6 changes: 3 additions & 3 deletions plugin/core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,15 +701,15 @@ def session_async(self, capability: str, point: int | None = None) -> Session |
raise NotImplementedError()

@abstractmethod
def sessions_async(self, capability: str | None = None) -> Generator[Session, None, None]:
def sessions_async(self, capability: str | None = None) -> list[Session]:
raise NotImplementedError()

@abstractmethod
def session_buffers_async(self, capability: str | None = None) -> Generator[SessionBufferProtocol, None, None]:
def session_buffers_async(self, capability: str | None = None) -> list[SessionBufferProtocol]:
raise NotImplementedError()

@abstractmethod
def session_views_async(self) -> Generator[SessionViewProtocol, None, None]:
def session_views_async(self) -> list[SessionViewProtocol]:
raise NotImplementedError()

@abstractmethod
Expand Down
8 changes: 8 additions & 0 deletions plugin/core/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import time
import weakref

try:
import orjson
except ImportError:
orjson = None

T = TypeVar('T')
T_contra = TypeVar('T_contra', contravariant=True)
Expand Down Expand Up @@ -80,6 +84,8 @@ def read_data(self, reader: IO[bytes]) -> dict[str, Any] | None:

@staticmethod
def _encode(data: dict[str, Any]) -> bytes:
if orjson:
return orjson.dumps(data)
return json.dumps(
data,
ensure_ascii=False,
Expand All @@ -90,6 +96,8 @@ def _encode(data: dict[str, Any]) -> bytes:

@staticmethod
def _decode(message: bytes) -> dict[str, Any]:
if orjson:
return orjson.loads(message)
return json.loads(message.decode('utf-8'))


Expand Down
22 changes: 12 additions & 10 deletions plugin/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,14 @@ def _update_diagnostic_in_status_bar_async(self) -> None:
return
self.view.erase_status(self.ACTIVE_DIAGNOSTIC)

def session_buffers_async(self, capability: str | None = None) -> Generator[SessionBuffer, None, None]:
for sv in self.session_views_async():
if capability is None or sv.has_capability_async(capability):
yield sv.session_buffer
def session_buffers_async(self, capability: str | None = None) -> list[SessionBuffer]:
return [
sv.session_buffer for sv in self.session_views_async()
if capability is None or sv.has_capability_async(capability)
]

def session_views_async(self) -> Generator[SessionView, None, None]:
yield from self._session_views.values()
def session_views_async(self) -> list[SessionView]:
return list(self._session_views.values())

def on_text_changed_async(self, change_count: int, changes: Iterable[sublime.TextChange]) -> None:
if self.view.is_primary():
Expand Down Expand Up @@ -857,10 +858,11 @@ def _on_initial_folding_ranges(self, kinds: list[str], response: list[FoldingRan
def session_async(self, capability: str, point: int | None = None) -> Session | None:
return best_session(self.view, self.sessions_async(capability), point)

def sessions_async(self, capability: str | None = None) -> Generator[Session, None, None]:
for sb in self.session_buffers_async():
if capability is None or sb.has_capability(capability):
yield sb.session
def sessions_async(self, capability: str | None = None) -> list[Session]:
return [
sb.session for sb in self.session_buffers_async()
if capability is None or sb.has_capability(capability)
]

def session_by_name(self, name: str | None = None) -> Session | None:
for sb in self.session_buffers_async():
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ deps =
mypy==1.7.1
flake8==5.0.4
pyright==1.1.381
orjson==3.10.7
commands =
# mypy disabled for main code as it doesn't currently support cyclic definitions - https://github.com/python/mypy/issues/731
mypy stubs
Expand Down

0 comments on commit 17e7d0c

Please sign in to comment.