-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01233bb
commit ebf48bb
Showing
7 changed files
with
374 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# type: ignore | ||
|
||
from yls.snippet_string import SnippetString | ||
|
||
|
||
def test_simple(): | ||
snip = SnippetString("test") | ||
assert str(snip) == "test" | ||
|
||
|
||
def test_placeholder(): | ||
snip = SnippetString() | ||
snip.append_placeholder("one") | ||
snip.append_placeholder("two") | ||
assert str(snip) == "${1:one}${2:two}" | ||
|
||
|
||
def test_complex(): | ||
snip = SnippetString() | ||
snip.append_text("text\n") | ||
snip.append_placeholder("one") | ||
snip.append_tabstop() | ||
snip.append_placeholder("two") | ||
snip.append_variable("CLIPBOARD", "") | ||
snip.append_variable("INVALID_VARIABLE", "default") | ||
snip.append_choice(("c", "ch", "choice")) | ||
assert str(snip) == "text\n${1:one}$2${3:two}${CLIPBOARD}${default}${4|c,ch,choice|}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# type: ignore | ||
|
||
import pytest | ||
|
||
from yls.snippets import SnippetGenerator | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"config, expected", | ||
( | ||
( | ||
None, | ||
"""rule ${1:my_rule} { | ||
\tmeta: | ||
\t\t${2:KEY} = ${3:"VALUE"} | ||
\tstrings: | ||
\t\t${4:\\$name} = ${5|"string",/regex/,{ HEX }|} | ||
\tcondition: | ||
\t\t${6:any of them} | ||
} | ||
""", | ||
), | ||
( | ||
{}, | ||
"""rule ${1:my_rule} { | ||
\tmeta: | ||
\t\t${2:KEY} = ${3:"VALUE"} | ||
\tstrings: | ||
\t\t${4:\\$name} = ${5|"string",/regex/,{ HEX }|} | ||
\tcondition: | ||
\t\t${6:any of them} | ||
} | ||
""", | ||
), | ||
( | ||
{"metaEntries": {"author": "test user", "hash": ""}}, | ||
"""rule ${1:my_rule} { | ||
\tmeta: | ||
\t\tauthor = "test user" | ||
\t\thash = "$2" | ||
\tstrings: | ||
\t\t${3:\\$name} = ${4|"string",/regex/,{ HEX }|} | ||
\tcondition: | ||
\t\t${5:any of them} | ||
} | ||
""", | ||
), | ||
( | ||
{"metaEntries": {"filename": "${TM_FILENAME}"}}, | ||
"""rule ${1:my_rule} { | ||
\tmeta: | ||
\t\tfilename = "${TM_FILENAME}" | ||
\tstrings: | ||
\t\t${2:\\$name} = ${3|"string",/regex/,{ HEX }|} | ||
\tcondition: | ||
\t\t${4:any of them} | ||
} | ||
""", | ||
), | ||
( | ||
{ | ||
"metaEntries": { | ||
"author": "", | ||
"date": "${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}", | ||
} | ||
}, | ||
"""rule ${1:my_rule} { | ||
\tmeta: | ||
\t\tauthor = "$2" | ||
\t\tdate = "${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}" | ||
\tstrings: | ||
\t\t${3:\\$name} = ${4|"string",/regex/,{ HEX }|} | ||
\tcondition: | ||
\t\t${5:any of them} | ||
} | ||
""", | ||
), | ||
( | ||
{ | ||
"metaEntries": { | ||
"date": "${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}", | ||
"author": "", | ||
}, | ||
"sortMeta": True, | ||
}, | ||
"""rule ${1:my_rule} { | ||
\tmeta: | ||
\t\tauthor = "$2" | ||
\t\tdate = "${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}" | ||
\tstrings: | ||
\t\t${3:\\$name} = ${4|"string",/regex/,{ HEX }|} | ||
\tcondition: | ||
\t\t${5:any of them} | ||
} | ||
""", | ||
), | ||
), | ||
) | ||
def test_basic(config, expected): | ||
generator = SnippetGenerator(config) | ||
|
||
assert expected == generator.generate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from __future__ import annotations | ||
from typing import Iterable | ||
|
||
SUPPORTED_VARIABLES = { | ||
"TM_SELECTED_TEXT", | ||
"TM_CURRENT_LINE", | ||
"TM_CURRENT_WORD", | ||
"TM_LINE_INDEX", | ||
"TM_LINE_NUMBER", | ||
"TM_FILENAME", | ||
"TM_FILENAME_BASE", | ||
"TM_DIRECTORY", | ||
"TM_FILEPATH", | ||
"RELATIVE_FILEPATH", | ||
"CLIPBOARD", | ||
"WORKSPACE_NAME", | ||
"WORKSPACE_FOLDER", | ||
"CURSOR_INDEX", | ||
"CURSOR_NUMBER", | ||
"CURRENT_YEAR", | ||
"CURRENT_YEAR_SHORT", | ||
"CURRENT_MONTH", | ||
"CURRENT_MONTH_NAME", | ||
"CURRENT_MONTH_NAME_SHORT", | ||
"CURRENT_DATE", | ||
"CURRENT_DAY_NAME", | ||
"CURRENT_DAY_NAME_SHORT", | ||
"CURRENT_HOUR", | ||
"CURRENT_MINUTE", | ||
"CURRENT_SECOND", | ||
"CURRENT_SECONDS_UNIX", | ||
"RANDOM", | ||
"RANDOM_HEX", | ||
"UUID", | ||
"BLOCK_COMMENT_START", | ||
"BLOCK_COMMENT_END", | ||
"LINE_COMMENT", | ||
} | ||
|
||
|
||
class SnippetString: | ||
cur_idx: int | ||
value: str | ||
|
||
def __init__(self, value: str = ""): | ||
self.value = value | ||
self.cur_idx = 1 | ||
|
||
def append_choice(self, values: Iterable[str]) -> None: | ||
self.value += f"${{{self.get_and_inc()}|{','.join(values)}|}}" | ||
|
||
def append_placeholder(self, value: str) -> None: | ||
self.value += f"${{{self.get_and_inc()}:{value}}}" | ||
|
||
def append_tabstop(self) -> None: | ||
self.value += f"${self.get_and_inc()}" | ||
|
||
def append_text(self, value: str) -> None: | ||
"""WARNING: For now you are expected to escape the string if necessary.""" | ||
self.value += value | ||
|
||
def append_variable(self, name: str, default_value: str) -> None: | ||
if name in SUPPORTED_VARIABLES: | ||
self.value += f"${{{name}}}" | ||
else: | ||
self.value += f"${{{default_value}}}" | ||
|
||
def get_and_inc(self) -> int: | ||
i = self.cur_idx | ||
self.cur_idx += 1 | ||
return i | ||
|
||
def __str__(self) -> str: | ||
return self.value |
Oops, something went wrong.