-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jack Cherng <[email protected]>
- Loading branch information
Showing
9 changed files
with
92 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
|
||
def reload_plugin() -> None: | ||
import sys | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"*": { | ||
"*": [ | ||
"annotated-types", | ||
"more-itertools", | ||
"pydantic", | ||
"pydantic-core", | ||
"typing-extensions" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,55 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from functools import lru_cache | ||
from itertools import groupby | ||
from typing import Generator, Iterable | ||
|
||
import sublime | ||
|
||
from .constant import DB_DIR | ||
from .types import DatabaseItem, DbSchema, NormalizedDatabaseItem | ||
from .data_types import DbItem, DbModel, NormalizedDbItem | ||
|
||
|
||
@lru_cache | ||
def load_database(version: str) -> DbSchema: | ||
return json.loads(sublime.load_resource(str(DB_DIR / f"{version}.json"))) | ||
def load_db(version: str) -> DbModel: | ||
return DbModel.model_validate_json(sublime.load_resource(str(DB_DIR / f"{version}.json"))) | ||
|
||
|
||
@lru_cache | ||
def get_completion_list(version_str: str) -> sublime.CompletionList: | ||
""" | ||
Gets the completion items. | ||
:param version_str: Versions separated with "," | ||
:type version_str: str | ||
:returns: The completion items. | ||
:rtype: sublime.CompletionList | ||
""" | ||
|
||
versions = sorted(set(filter(None, map(str.strip, version_str.split(","))))) | ||
items = _get_database_items(versions) | ||
def get_completion_list(versions: tuple[str, ...]) -> sublime.CompletionList: | ||
"""Gets the completion items.""" | ||
db_items = [item for version in versions for item in _list_db_items(version)] | ||
|
||
return sublime.CompletionList( | ||
tuple( | ||
map( | ||
lambda item: sublime.CompletionItem( | ||
trigger=item.item_name, | ||
annotation=f"{item.lib_name} {'/'.join(item.lib_versions)}", | ||
completion=item.item_name, | ||
completion_format=sublime.COMPLETION_FORMAT_TEXT, | ||
kind=(sublime.KIND_ID_MARKUP, "c", ""), | ||
details="", | ||
), | ||
_normalize_database_items(items), | ||
sublime.CompletionItem( | ||
trigger=item.item_name, | ||
annotation=f"{item.lib_name} {'/'.join(item.lib_versions)}", | ||
completion=item.item_name, | ||
completion_format=sublime.COMPLETION_FORMAT_TEXT, | ||
kind=(sublime.KIND_ID_MARKUP, "c", ""), | ||
details="", | ||
) | ||
for item in _normalize_db_items_for_completion(db_items) | ||
) | ||
) | ||
|
||
|
||
def _get_database_items(versions: Iterable[str]) -> Generator[DatabaseItem, None, None]: | ||
for version in versions: | ||
db = load_database(version) | ||
for name in db["classes"]: | ||
yield DatabaseItem( | ||
lib_name=db["name"], | ||
lib_version=db["version"], | ||
item_name=name, | ||
) | ||
def _list_db_items(version: str) -> Generator[DbItem, None, None]: | ||
db = load_db(version) | ||
yield from (DbItem(lib_name=db.name, lib_version=db.version, item_name=name) for name in db.classes) | ||
|
||
|
||
def _normalize_database_items(items: Iterable[DatabaseItem]) -> Generator[NormalizedDatabaseItem, None, None]: | ||
def sorter(item: DatabaseItem) -> tuple[str, str]: | ||
def _normalize_db_items_for_completion(db_items: Iterable[DbItem]) -> Generator[NormalizedDbItem, None, None]: | ||
def sorter(item: DbItem) -> tuple[str, str]: | ||
return (item.lib_name, item.item_name) | ||
|
||
# pre-sort for groupby | ||
items = sorted(items, key=sorter) | ||
|
||
db_items = sorted(db_items, key=sorter) | ||
# merges same-name items which have different versions | ||
for _, group in groupby(items, sorter): | ||
group_items = tuple(group) | ||
group_item = group_items[0] | ||
yield NormalizedDatabaseItem( | ||
lib_name=group_item.lib_name, | ||
lib_versions=sorted(item.lib_version for item in group_items), | ||
item_name=group_item.item_name, | ||
for (lib_name, item_name), group in groupby(db_items, sorter): | ||
yield NormalizedDbItem( | ||
lib_name=lib_name, | ||
lib_versions=sorted(item.lib_version for item in group), | ||
item_name=item_name, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import annotations | ||
|
||
from itertools import groupby | ||
from operator import itemgetter | ||
from typing import Any, Callable, Generator, Iterable, TypeVar | ||
|
||
_T = TypeVar("_T") | ||
|
||
|
||
def sort_uniq( | ||
seq: Iterable[_T], | ||
*, | ||
key: Callable[[_T], Any] | None = None, | ||
reverse: bool = False, | ||
) -> Generator[_T, None, None]: | ||
key = key or (lambda x: x) | ||
yield from map( | ||
itemgetter(0), | ||
groupby(sorted(seq, key=key, reverse=reverse), key=key), # type: ignore | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pydantic>=2.7,<3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
# This file was autogenerated by uv via the following command: | ||
# uv pip compile requirements.in -o requirements.txt | ||
annotated-types==0.6.0 | ||
# via pydantic | ||
pydantic==2.7.1 | ||
# via -r requirements.in | ||
pydantic-core==2.18.2 | ||
# via pydantic | ||
typing-extensions==4.11.0 | ||
# via | ||
# pydantic | ||
# pydantic-core |