Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Moosems committed Aug 9, 2024
1 parent 243b4df commit fb323f1
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 36 deletions.
4 changes: 2 additions & 2 deletions albero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
118 changes: 96 additions & 22 deletions albero/ipc.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand All @@ -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",
],
)
6 changes: 5 additions & 1 deletion albero/languages/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
)
1 change: 1 addition & 0 deletions albero/languages/compile_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 4 additions & 11 deletions tests/test_ipc_highlight.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
Expand Down

0 comments on commit fb323f1

Please sign in to comment.