diff --git a/README.md b/README.md index 174622a..d6edbe7 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ The `COMMANDS` list contains all valid commands to request server output from. I - "autocomplete" - "replacements" - "highlight" +- "editorconfig" ### `generic_tokens` @@ -39,14 +40,14 @@ The `is_unicode_letter()` function returns a boolean if a given word is a unicod ### `IPC` Class -| Method | Description | Arguments | -| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `.get_response()` | Gets a response of the requested command | `command`: str | -| `.request()` | Makes a request to the server | `command`: str, `file`: str, `expected_keywords`: list[str] ("autocomple" or "replacements"), `current_word`: str ("autocomple" or "replacements"), `language`: str ("highlight"), `text_range`: tuple[int, int] ("highlight") | -| `.cancel_request()` | Cancels request of command type and removes reponse if it was recieved. Must be called before `.get_response()` to work | `command`: str | -| `.update_file()` | Updates files stored on the server that are used to get responses | `filename`: str, `current_state`: str (just the text of the file) | -| `.remove_file()` | Removes a file of the name given if any exists. Note that a file should only be removed when sure that all other requests using the file are completed. If you delete a file right after a request you run the risk of it removing the file before the task could be run and causing a server crash (`Request`'s go after `Notification`'s and `Ping`'s). | `filename`: str | -| `.kill_IPC()` | This kills the IPC process and acts as a precaution against wasted CPU when the main thread no longer needs the IPC | None | +| Method | Description | Arguments | +| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `.get_response()` | Gets a response of the requested command | `command`: str | +| `.request()` | Makes a request to the server | `command`: str, `file`: str, `expected_keywords`: list[str] ("autocomple" or "replacements"), `current_word`: str ("autocomple" or "replacements"), `language`: str ("highlight"), `text_range`: tuple[int, int] ("highlight"), `file_path`: pathlib.Path, str | +| `.cancel_request()` | Cancels request of command type and removes reponse if it was recieved. Must be called before `.get_response()` to work | `command`: str | +| `.update_file()` | Updates files stored on the server that are used to get responses | `filename`: str, `current_state`: str (just the text of the file) | +| `.remove_file()` | Removes a file of the name given if any exists. Note that a file should only be removed when sure that all other requests using the file are completed. If you delete a file right after a request you run the risk of it removing the file before the task could be run and causing a server crash (`Request`'s go after `Notification`'s and `Ping`'s). | `filename`: str | +| `.kill_IPC()` | This kills the IPC process and acts as a precaution against wasted CPU when the main thread no longer needs the IPC | None | ### Basic Usage: diff --git a/requirements.txt b/requirements.txt index a9f49e0..0ff1714 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ pygments +pyeditorconfig diff --git a/salve_ipc/ipc.py b/salve_ipc/ipc.py index f09b76e..62f6ba0 100644 --- a/salve_ipc/ipc.py +++ b/salve_ipc/ipc.py @@ -1,5 +1,6 @@ from multiprocessing import Pipe, Process, Queue, freeze_support from multiprocessing.connection import Connection +from pathlib import Path from random import randint from .misc import COMMANDS, Message, Notification, Request, Response @@ -78,6 +79,7 @@ def create_message(self, type: str, **kwargs) -> None: "current_word": kwargs.get("current_word", ""), "language": kwargs.get("language", ""), "text_range": kwargs.get("text_range", (1, -1)), + "file_path": kwargs.get("file_path", __file__), } self.send_message(request) case "notification": @@ -98,6 +100,7 @@ def request( current_word: str = "", language: str = "Text", text_range: tuple[int, int] = (1, -1), + file_path: Path | str = Path(__file__), ) -> None: """Sends the main_server a request of type command with given kwargs - external API""" if command not in COMMANDS: @@ -118,6 +121,7 @@ def request( current_word=current_word, language=language, text_range=text_range, + file_path=file_path, ) def cancel_request(self, command: str): diff --git a/salve_ipc/misc.py b/salve_ipc/misc.py index 81dbe98..400d561 100644 --- a/salve_ipc/misc.py +++ b/salve_ipc/misc.py @@ -1,6 +1,12 @@ +from pathlib import Path from typing import NotRequired, TypedDict -COMMANDS: list[str] = ["autocomplete", "replacements", "highlight"] +COMMANDS: list[str] = [ + "autocomplete", + "replacements", + "highlight", + "editorconfig", +] class Message(TypedDict): @@ -19,6 +25,7 @@ class Request(Message): current_word: NotRequired[str] # autocomplete, replacements language: NotRequired[str] # highlight text_range: NotRequired[tuple[int, int]] + file_path: NotRequired[Path | str] # editorconfig class Notification(Message): diff --git a/salve_ipc/server.py b/salve_ipc/server.py index c21d978..b107f25 100644 --- a/salve_ipc/server.py +++ b/salve_ipc/server.py @@ -2,6 +2,8 @@ from multiprocessing.connection import Connection from time import sleep +from pyeditorconfig import get_config + from .misc import COMMANDS, Message, Request, Response from .server_functions import ( Token, @@ -83,7 +85,9 @@ def handle_request(self, request: Request) -> None: command: str = request["command"] id: int = self.newest_ids[command] file: str = request["file"] - result: list[str | tuple[tuple[int, int], int, str]] = [] + result: ( + list[str | tuple[tuple[int, int], int, str]] | dict[str, str] + ) = [] cancelled: bool = False match request["command"]: @@ -104,6 +108,8 @@ def handle_request(self, request: Request) -> None: full_text=self.files[file], language=request["language"], text_range=request["text_range"] # type: ignore ) result += [token for token in pre_refined_result] # type: ignore + case "editorconfig": + result = get_config(request["file_path"]) # type: ignore case _: cancelled = True diff --git a/setup.py b/setup.py index 9d9c697..ff95707 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ url="https://github.com/Moosems/salve", long_description=long_description, long_description_content_type="text/markdown", - install_requires=["pygments"], + install_requires=["pygments", "pyeditorconfig"], python_requires=">=3.9", license="MIT license", classifiers=[ diff --git a/tests/test_ipc.py b/tests/test_ipc.py index 2ad84ee..3568d01 100644 --- a/tests/test_ipc.py +++ b/tests/test_ipc.py @@ -21,6 +21,7 @@ def test_IPC(): current_word="thid", ) context.request("highlight", file="test", language="python") + context.request("editorconfig", file="test", file_path=__file__) sleep(1) @@ -97,6 +98,24 @@ def test_IPC(): ], } + editorconfig_response = context.get_response("editorconfig") + if editorconfig_response is None: + raise AssertionError("Editorconfig output is None") + editorconfig_response["id"] = 0 + assert editorconfig_response == { + "id": 0, + "type": "response", + "cancelled": False, + "command": "editorconfig", + "result": { + "end_of_line": "lf", + "insert_final_newline": "true", + "charset": "utf-8", + "indent_style": "space", + "indent_size": "4", + }, + } + context.remove_file("test") context.kill_IPC()