Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add pyeditorconfig #12

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The `COMMANDS` list contains all valid commands to request server output from. I
- "autocomplete"
- "replacements"
- "highlight"
- "editorconfig"

### `generic_tokens`

Expand All @@ -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:

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pygments
pyeditorconfig
4 changes: 4 additions & 0 deletions salve_ipc/ipc.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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:
Expand All @@ -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):
Expand Down
9 changes: 8 additions & 1 deletion salve_ipc/misc.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion salve_ipc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"]:
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
19 changes: 19 additions & 0 deletions tests/test_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
Loading