Skip to content

Commit

Permalink
refactor: replace "first()" with "first_true()" from "more_itertools"
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed May 29, 2024
1 parent c1749f9 commit d009031
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 23 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
name: ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}

env:
UV_SYSTEM_PYTHON: 1

strategy:
matrix:
os: ['ubuntu-latest']
Expand All @@ -44,7 +47,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install -U uv
make UV_INSTALL_FLAGS="--system" install
make install-dev
- name: Do linting
run: |
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ all:
install:
uv pip install $(UV_INSTALL_FLAGS) -r requirements.txt

.PHONY: install-dev
install-dev:
uv pip install $(UV_INSTALL_FLAGS) -r requirements-dev.txt

.PHONY: pip-compile
pip-compile:
uv pip compile --upgrade requirements.in -o requirements.txt
uv pip compile --upgrade requirements-dev.in -o requirements-dev.txt

.PHONY: ci-check
ci-check:
Expand Down
1 change: 1 addition & 0 deletions dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"*": {
"*": [
"lsp_utils",
"more-itertools",
"sublime_lib"
]
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/ui/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import mdpopups
import sublime
from more_itertools import first_true

from ..types import CopilotPayloadCompletion
from ..utils import (
clamp,
first,
fix_completion_syntax_highlight,
get_copilot_view_setting,
get_view_language_id,
Expand Down Expand Up @@ -95,7 +95,7 @@ def current_completion(self) -> CopilotPayloadCompletion | None:

@property
def completion_style_type(self) -> type[_BaseCompletion]:
completion_cls = first(_BaseCompletion.__subclasses__(), lambda t: t.name == self.completion_style)
completion_cls = first_true(_BaseCompletion.__subclasses__(), pred=lambda t: t.name == self.completion_style)
if completion_cls:
return completion_cls
raise RuntimeError(f"Unknown completion style type: {self.completion_style}")
Expand Down
4 changes: 2 additions & 2 deletions plugin/ui/panel_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import mdpopups
import sublime
from more_itertools import first_true

from ..types import CopilotPayloadPanelSolution, StLayout
from ..utils import (
all_views,
find_view_by_id,
first,
fix_completion_syntax_highlight,
get_copilot_view_setting,
get_view_language_id,
Expand Down Expand Up @@ -124,7 +124,7 @@ def find_view_by_panel_id(panel_id: str) -> sublime.View | None:

@classmethod
def from_sheet_id(cls, sheet_id: int) -> ViewPanelCompletionManager | None:
return first(map(cls, all_views()), lambda self: self.sheet_id == sheet_id)
return first_true(map(cls, all_views()), pred=lambda self: self.sheet_id == sheet_id)

def open(self, *, completion_target_count: int | None = None) -> None:
"""Open the completion panel."""
Expand Down
13 changes: 3 additions & 10 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from LSP.plugin.core.sessions import Session
from LSP.plugin.core.types import basescope2languageid
from LSP.plugin.core.url import filename_to_uri
from more_itertools import first_true

from .constants import COPILOT_VIEW_SETTINGS_PREFIX, PACKAGE_NAME
from .types import CopilotPayloadCompletion, CopilotPayloadPanelSolution, T_Callable
Expand Down Expand Up @@ -82,25 +83,17 @@ def call_function() -> Any:


def find_sheet_by_id(id: int) -> sublime.Sheet | None:
return first(all_sheets(include_transient=True), lambda sheet: sheet.id() == id)
return first_true(all_sheets(include_transient=True), pred=lambda sheet: sheet.id() == id)


def find_view_by_id(id: int) -> sublime.View | None:
return first(all_views(include_transient=True), lambda view: view.id() == id)
return first_true(all_views(include_transient=True), pred=lambda view: view.id() == id)


def is_active_view(obj: Any) -> bool:
return bool(obj and obj == sublime.active_window().active_view())


def first(items: Iterable[T], test: Callable[[T], bool] | None = None, default: T | None = None) -> T | None:
"""
Gets the first item which satisfies the `test`. Otherwise, `default`.
If `test` is not given or `None`, the first truthy item will be returned.
"""
return next(filter(test, items), default)


def fix_completion_syntax_highlight(view: sublime.View, point: int, code: str) -> str:
if view.match_selector(point, "source.php"):
return f"<?php\n{code}"
Expand Down
4 changes: 4 additions & 0 deletions requirements-dev.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-r requirements.in

mypy
ruff>=0.4
12 changes: 12 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements-dev.in -o requirements-dev.txt
more-itertools==10.2.0
# via -r requirements.in
mypy==1.10.0
# via -r requirements-dev.in
mypy-extensions==1.0.0
# via mypy
ruff==0.4.6
# via -r requirements-dev.in
typing-extensions==4.12.0
# via mypy
3 changes: 1 addition & 2 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
mypy
ruff>=0.4
more-itertools>=10,<11
8 changes: 2 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o requirements.txt
mypy==1.9.0
mypy-extensions==1.0.0
# via mypy
ruff==0.4.1
typing-extensions==4.11.0
# via mypy
more-itertools==10.2.0
# via -r requirements.in

0 comments on commit d009031

Please sign in to comment.