diff --git a/albero/__init__.py b/albero/__init__.py index f8f6988..b8d6ca2 100644 --- a/albero/__init__.py +++ b/albero/__init__.py @@ -4,7 +4,7 @@ from token_tools import GENERIC_TOKENS, Token # noqa: F401, E402 -from .languages import get_lang, get_mapping, get_lang_func # noqa: F401, E402 +from .ipc import IPCHighlighter # noqa: F401, E402 +from .languages import get_lang, get_lang_func, get_mapping # noqa: F401, E402 from .misc import AlberoException, lang_from_so # noqa: F401, E402 from .tree_sitter import TreeSitterHighlighter # noqa: F401, E402 -from .ipc import IPCHighlighter # noqa: F401, E402 diff --git a/albero/ipc.py b/albero/ipc.py index 67b3eb9..e4bf08a 100644 --- a/albero/ipc.py +++ b/albero/ipc.py @@ -1,13 +1,22 @@ -from beartype.typing import Callable -from collegamento import Request, Response, SimpleClient, SimpleServer, USER_FUNCTION from logging import Logger -from collegamento.simple_client_server.misc import ResponseQueueType, RequestQueueType -from token_tools import Token -from tree_sitter import Language from pathlib import Path -from .misc import lang_from_so, AlberoException +from beartype.typing import Callable +from collegamento import ( + USER_FUNCTION, + Request, + Response, + SimpleClient, + SimpleServer, +) +from collegamento.simple_client_server.misc import ( + RequestQueueType, + ResponseQueueType, +) +from token_tools import Token +from tree_sitter import Language +from .misc import AlberoException, lang_from_so from .tree_sitter import TreeSitterHighlighter @@ -23,29 +32,41 @@ def add_language(server: "IPCHighlightServer", request: Request) -> None: lang = lang_from_so(str(language), lang_name) server.highlighter.add_language(lang_name, lang, request["mapping"]) # type: ignore + def remove_language(server: "IPCHighlightServer", request: Request) -> None: - server.highlighter.remove_language(request["language_name"]) #type: ignore + server.highlighter.remove_language(request["language_name"]) # type: ignore + def add_file(server: "IPCHighlightServer", request: Request) -> None: server.highlighter.add_file(request["file_name"], request["language_name"]) # type: ignore + def update_file(server: "IPCHighlightServer", request: Request) -> None: server.highlighter.update_file(request["file_name"], request["code"]) # type: ignore + def remove_file(server: "IPCHighlightServer", request: Request) -> None: - server.highlighter.remove_file(request["file_name"]) #type: ignore + server.highlighter.remove_file(request["file_name"]) # type: ignore + def update_mapping(server: "IPCHighlightServer", request: Request) -> None: - server.highlighter.update_mapping(request["language_name"], request["mapping"]) #type: ignore + server.highlighter.update_mapping( + request["language_name"], request["mapping"] + ) # type: ignore + -def get_highlights(server: "IPCHighlightServer", request: Request) -> list[Token]: - return server.highlighter.get_highlights(request["file_name"], request["text_range"]) #type: ignore +def get_highlights( + server: "IPCHighlightServer", request: Request +) -> list[Token]: + return server.highlighter.get_highlights( + request["file_name"], request["text_range"] + ) # type: ignore class IPCHighlighter(SimpleClient): def __init__(self, id_max: int = 15000) -> None: self.faux_langs: list[str] = [] - self.faux_files: dict[str, str] = {} # file, lang + self.faux_files: dict[str, str] = {} # file, lang commands: dict[str, USER_FUNCTION] = { "add-language": add_language, "remove-language": remove_language, @@ -57,11 +78,14 @@ def __init__(self, id_max: int = 15000) -> None: } super().__init__(commands, id_max, server_type=IPCHighlightServer) - def get_response(self) -> Response | None: #type: ignore + def get_response(self) -> Response | None: # type: ignore return super().get_response("get-highlights") def add_language( - self, language_name: str, language: Callable | Path | str, mapping: dict[str, str] + self, + language_name: str, + language: Callable | Path | str, + mapping: dict[str, str], ) -> None: if language_name in self.faux_langs: self.logger.exception( @@ -72,7 +96,14 @@ def add_language( ) self.faux_langs.append(language_name) - super().request({"command": "add-language", "language_name": language_name, "language": language, "mapping": mapping}) + super().request( + { + "command": "add-language", + "language_name": language_name, + "language": language, + "mapping": mapping, + } + ) def remove_language(self, language_name: str) -> None: if language_name not in self.faux_langs: @@ -108,14 +139,22 @@ def add_file(self, file_name: str, language_name: str) -> None: raise AlberoException(f"File {file_name} already an added file") self.faux_files[file_name] = language_name - super().request({"command": "add-file", "file_name": file_name, "language_name": language_name}) + super().request( + { + "command": "add-file", + "file_name": file_name, + "language_name": language_name, + } + ) def update_file(self, file_name: str, code: str) -> None: if file_name not in self.faux_files: self.logger.exception(f"File {file_name} not an added file") raise AlberoException(f"File {file_name} not an added file") - super().request({"command": "update-file", "file_name": file_name, "code": code}) + super().request( + {"command": "update-file", "file_name": file_name, "code": code} + ) def remove_file(self, file_name: str) -> None: if file_name not in self.faux_files: @@ -137,14 +176,28 @@ def update_mapping( f"Language {language_name} not an added language" ) - super().request({"command": "update-mapping", "language_name": language_name, "mapping": mapping}) + super().request( + { + "command": "update-mapping", + "language_name": language_name, + "mapping": mapping, + } + ) - def request_highlights(self, file_name: str, text_range: tuple[int, int] = (1, -1)) -> None: + def request_highlights( + self, file_name: str, text_range: tuple[int, int] = (1, -1) + ) -> None: if file_name not in self.faux_files: self.logger.exception(f"File {file_name} not an added file") raise AlberoException(f"File {file_name} not an added file") - super().request({"command": "get-highlights", "file_name": file_name, "text_range": text_range}) + super().request( + { + "command": "get-highlights", + "file_name": file_name, + "text_range": text_range, + } + ) def get_highlight_response(self) -> list[Token] | None: # type: ignore response = super().get_response("get-highlights") @@ -154,7 +207,28 @@ def get_highlight_response(self) -> list[Token] | None: # type: ignore return None + class IPCHighlightServer(SimpleServer): - def __init__(self, commands: dict[str, USER_FUNCTION], response_queue: ResponseQueueType, requests_queue: RequestQueueType, logger: Logger) -> None: + def __init__( + self, + commands: dict[str, USER_FUNCTION], + response_queue: ResponseQueueType, + requests_queue: RequestQueueType, + logger: Logger, + ) -> None: self.highlighter = TreeSitterHighlighter() - super().__init__(commands, response_queue, requests_queue, logger, ["add-language", "update-mapping", "add-file", "update-file", "get-highlights", "remove-file", "remove-language"]) + super().__init__( + commands, + response_queue, + requests_queue, + logger, + [ + "add-language", + "update-mapping", + "add-file", + "update-file", + "get-highlights", + "remove-file", + "remove-language", + ], + ) diff --git a/albero/languages/__init__.py b/albero/languages/__init__.py index 6f86253..2ca8363 100644 --- a/albero/languages/__init__.py +++ b/albero/languages/__init__.py @@ -1 +1,5 @@ -from .compile_languages import get_lang, get_lang_func, get_mapping # noqa: F401 +from .compile_languages import ( # noqa: F401 + get_lang, + get_lang_func, + get_mapping, +) diff --git a/albero/languages/compile_languages.py b/albero/languages/compile_languages.py index 3f43ce9..d8a6c1e 100644 --- a/albero/languages/compile_languages.py +++ b/albero/languages/compile_languages.py @@ -77,6 +77,7 @@ def get_lang(language_name: str) -> Language: return Language(language_functions[language_name]()) + def get_lang_func(language_name: str) -> Callable: if language_name not in language_functions: raise Exception("Language not in pre-compiled languages") diff --git a/tests/test_ipc_highlight.py b/tests/test_ipc_highlight.py index 52a86ef..1f56d4c 100644 --- a/tests/test_ipc_highlight.py +++ b/tests/test_ipc_highlight.py @@ -1,15 +1,12 @@ from time import sleep -from beartype.typing import Callable -from albero import IPCHighlighter -from sys import platform -from tree_sitter import Language +from beartype.typing import Callable from albero import ( + IPCHighlighter, Token, get_lang_func, get_mapping, - lang_from_so, ) py_lang: Callable = get_lang_func("python") @@ -20,12 +17,8 @@ def test_basic_usage(): highlighter = IPCHighlighter() highlighter.add_language("python", py_lang, mapping) - highlighter.add_file( - "test", "python" - ) - highlighter.update_file( - "test", "def test(): ..." - ) + highlighter.add_file("test", "python") + highlighter.update_file("test", "def test(): ...") highlighter.request_highlights( "test" ) # Trying to use a file not in the system gives an AlberoException