Skip to content

Commit

Permalink
fix(hangman): handle trailing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Mar 24, 2021
1 parent 4218438 commit ff7ba22
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
33 changes: 31 additions & 2 deletions beet/contrib/hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

__all__ = [
"fold_hanging_commands",
"parse_trailing_comment",
]


from typing import List
import re
from typing import List, Optional, Tuple

from beet import Context

REGEX_COMMENT = re.compile(r"(\s+#(?:\s+.*)?$)")
REGEX_QUOTE = re.compile(r"(\"(?:.*?[^\\])?\"|'(?:.*?[^\\])?')")


def beet_default(ctx: Context):
for function in ctx.data.functions.values():
Expand All @@ -32,16 +37,40 @@ def fold_hanging_commands(lines: List[str]) -> List[str]:
if indentation > 0:
if stripped.startswith("#"):
result.append(stripped)
elif stripped:
hanging_blank_lines = 0
elif stripped:
stripped, comment = parse_trailing_comment(stripped)
if comment:
result.append(comment)
current += " " + stripped
hanging_blank_lines = 0
else:
hanging_blank_lines += 1
else:
result.append(current)
result.extend([""] * hanging_blank_lines)
hanging_blank_lines = 0
stripped, comment = parse_trailing_comment(stripped)
if comment:
result.append(comment)
current = stripped

result.append(current)

return result


def parse_trailing_comment(line: str) -> Tuple[str, Optional[str]]:
"""Split the line and return the extracted trailing comment."""
chunks = REGEX_QUOTE.split(line)
result = ""

while chunks:
notcomment, *comment = REGEX_COMMENT.split(chunks.pop(0))
result += notcomment
if comment:
return result, comment[0].lstrip() + "".join(chunks)
if chunks:
result += chunks.pop(0)

return result, None
31 changes: 31 additions & 0 deletions tests/examples/load_hangman/src/data/demo/functions/foo.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,34 @@ execute
1

say foo

execute
if block ~ ~ ~ #namespace:tag # But what if # we # put # hash # symbols
if entity @s[tag=foo] # everywhere #

tellraw @a
{
"text": "Hello # there"
}

tellraw @a # here
{
"text": "Hello\" # \"there"
}

tellraw @s ["foo"] # thing

execute
as @a # For each "player",
at @s # start at their feet.
anchored eyes # Looking through their eyes,
facing 0 0 0 # face perfectly at the target
anchored feet # (go back to the feet)
positioned ^ ^ ^1 # and move one block forward.
rotated as @s # Face the direction the player
# is actually facing,
positioned ^ ^ ^-1 # and move one block back.
if entity @s[distance=..0.6] # Check if we're close to the
# player's feet.
run
say I'm facing the target!
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,28 @@ tellraw @s { "text": "Hover me!", "hoverEvent": { "action": "show_text", "value"
execute as @a at @s if block ~ ~-1 ~ #wool run give @s stone{ display: { Name: '[{ "text": "Hello", "bold": true }]', Lore: [ '[{ "text": "Something else here" }]' ] } } 1

say foo

# But what if # we # put # hash # symbols
# everywhere #
execute if block ~ ~ ~ #namespace:tag if entity @s[tag=foo]

tellraw @a { "text": "Hello # there" }

# here
tellraw @a { "text": "Hello\" # \"there" }

# thing
tellraw @s ["foo"]

# For each "player",
# start at their feet.
# Looking through their eyes,
# face perfectly at the target
# (go back to the feet)
# and move one block forward.
# Face the direction the player
# is actually facing,
# and move one block back.
# Check if we're close to the
# player's feet.
execute as @a at @s anchored eyes facing 0 0 0 anchored feet positioned ^ ^ ^1 rotated as @s positioned ^ ^ ^-1 if entity @s[distance=..0.6] run say I'm facing the target!

0 comments on commit ff7ba22

Please sign in to comment.