Skip to content

Commit

Permalink
Make links and hidden chars standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
Moosems committed Jul 26, 2024
1 parent ab1f511 commit 9ba4dec
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions salve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
DEFINITION,
EDITORCONFIG,
HIGHLIGHT,
LINKS_AND_CHARS,
REPLACEMENTS,
Response,
)
Expand Down
5 changes: 3 additions & 2 deletions salve/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"highlight",
"editorconfig",
"definition",
"links_and_chars"
"links_and_chars",
]

COMMAND = str
Expand All @@ -21,6 +21,7 @@
DEFINITION: COMMAND = COMMANDS[4]
LINKS_AND_CHARS: COMMAND = COMMANDS[5]


class Message(TypedDict):
"""Base class for messages in and out of the server"""

Expand All @@ -36,7 +37,7 @@ class Request(Message):
expected_keywords: NotRequired[list[str]] # autocomplete, replacements
current_word: NotRequired[str] # autocomplete, replacements, definition
language: NotRequired[str] # highlight
text_range: NotRequired[tuple[int, int]] # highlight
text_range: NotRequired[tuple[int, int]] # highlight, links_and_chars
file_path: NotRequired[Path | str] # editorconfig
definition_starters: NotRequired[
list[tuple[str, str]]
Expand Down
5 changes: 5 additions & 0 deletions salve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
get_definition,
get_highlights,
get_replacements,
get_special_tokens,
)


Expand Down Expand Up @@ -156,6 +157,10 @@ def handle_request(self, request: Request) -> None:
)
case "links_and_chars":
self.logger.info("Searching for Links and chars")
result = get_special_tokens(
self.files[file],
request["text_range"], # type: ignore
)
case _:
self.logger.warning(f"Command {command} not recognized")
cancelled = True
Expand Down
1 change: 1 addition & 0 deletions salve/server_functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .autocompletions import find_autocompletions # noqa: F401
from .definitions import get_definition # noqa: F401
from .highlight import get_highlights # noqa: F401
from .links_and_hidden_chars import get_special_tokens # noqa: F401
from .misc import is_unicode_letter # noqa: F401
from .replacements import get_replacements # noqa: F401
22 changes: 13 additions & 9 deletions salve/server_functions/links_and_hidden_chars.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
url_regex: Pattern = compile(r"(ftp|http|https)://[a-zA-Z0-9_-]")


def get_urls(lines: list[str], start_line: int = 1) -> list[Token]:
start_pos: tuple[int, int] = (start_line, 0)
def get_urls(whole_text: str, text_range: tuple[int, int]) -> list[Token]:
lines: list[str] = whole_text.splitlines()
start_pos: tuple[int, int] = (text_range[0], 0)
url_toks: list[Token] = []
while True:
if start_pos[0] >= len(lines) + start_line:
if start_pos[0] >= text_range[1]:
break
line: str = lines[start_pos[0] - start_line][start_pos[1] :]
line: str = lines[start_pos[0] - text_range[0]][start_pos[1] :]
match_start: Match[str] | None = url_regex.search(line)
if match_start is None:
start_pos = (start_pos[0] + 1, 0)
Expand Down Expand Up @@ -95,9 +96,12 @@ def get_urls(lines: list[str], start_line: int = 1) -> list[Token]:
}


def find_hidden_chars(lines: list[str], start_line: int = 1) -> list[Token]:
def find_hidden_chars(
whole_text: str, text_range: tuple[int, int]
) -> list[Token]:
lines: list[str] = whole_text.splitlines()
hidden_char_indexes: list[tuple[tuple[int, int], str]] = [
((line_index + start_line, char_index), char)
((line_index + text_range[0], char_index), char)
for line_index, line in enumerate(lines)
for char_index, char in enumerate(line)
if char in list(hidden_chars.keys())
Expand All @@ -109,11 +113,11 @@ def find_hidden_chars(lines: list[str], start_line: int = 1) -> list[Token]:


def get_special_tokens(
whole_text: str, split_text: list[str], start_offset: int
whole_text: str, text_range: tuple[int, int]
) -> list[Token]:
return_tokens: list[Token] = []
return_tokens.extend(get_urls(split_text, start_offset))
return_tokens.extend(get_urls(whole_text, text_range))
if [char for char in hidden_chars if char in whole_text]:
# If there are no hidden chars we don't want to needlessly compute this
return_tokens.extend(find_hidden_chars(split_text, start_offset))
return_tokens.extend(find_hidden_chars(whole_text, text_range))
return return_tokens
17 changes: 16 additions & 1 deletion tests/test_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
EDITORCONFIG,
HIGHLIGHT,
IPC,
LINKS_AND_CHARS,
REPLACEMENTS,
Response,
)
Expand Down Expand Up @@ -48,7 +49,7 @@ def test_IPC():
(r":?.*=.*", "before"),
],
)

context.request(LINKS_AND_CHARS, file="test", text_range=(1, 18))
sleep(1)

# Check output
Expand Down Expand Up @@ -165,6 +166,20 @@ def test_IPC():

assert highlight_output == expected_output

links_and_hidden_chars_result: Response | None = context.get_response(
LINKS_AND_CHARS
)
if links_and_hidden_chars_result is None:
raise AssertionError("links_and_hidden_chars_result output is None")
links_and_hidden_chars_result["id"] = 0
assert links_and_hidden_chars_result == {
"id": 0,
"type": "response",
"cancelled": False,
"command": LINKS_AND_CHARS,
"result": [((5, 7), 1, "Hidden_Char")],
}

context.update_file(
"foo", open(Path("tests/testing_file2.py"), "r+").read()
)
Expand Down

0 comments on commit 9ba4dec

Please sign in to comment.