Skip to content

Commit

Permalink
[BUG] Highlights commands in tip (hedyorg#4235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Felienne authored Apr 24, 2023
1 parent ae7180a commit 87f7c73
Show file tree
Hide file tree
Showing 44 changed files with 66 additions and 70 deletions.
3 changes: 3 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ def translate_error(code, arguments, keyword_lang):
'ask',
'echo',
'is',
'if',
'repeat']
arguments_that_require_highlighting = [
'command',
Expand All @@ -744,6 +745,7 @@ def translate_error(code, arguments, keyword_lang):
'ask',
'echo',
'is',
'if',
'repeat']

# Todo TB -> We have to find a more delicate way to fix this: returns some gettext() errors
Expand All @@ -766,6 +768,7 @@ def translate_error(code, arguments, keyword_lang):
arguments["else"] = "else"
arguments["repeat"] = "repeat"
arguments["is"] = "is"
arguments["if"] = "if"

# some arguments like allowed types or characters need to be translated in the error message
for k, v in arguments.items():
Expand Down
8 changes: 4 additions & 4 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2851,7 +2851,7 @@ def contains_any_of(commands, line):

# no else in next line?
# add a nop (like 'Pass' but we just insert a meaningless assign)
line = line + " else _ is x"
line = line + " else x__x__x__x is 5"

processed_code.append(line)
processed_code.append(lines[-1]) # always add the last line (if it has if and no else that is no problem)
Expand All @@ -2877,7 +2877,7 @@ def check_program_size_is_valid(input_string):
raise exceptions.InputTooBigException(lines_of_code=number_of_lines, max_lines=MAX_LINES)


def process_input_string(input_string, level, lang, escape_backslashes=True):
def process_input_string(input_string, level, lang, escape_backslashes=True, preprocess_ifs_enabled=True):
result = input_string.replace('\r\n', '\n')

location = location_of_first_blank(result)
Expand All @@ -2887,8 +2887,8 @@ def process_input_string(input_string, level, lang, escape_backslashes=True):
if escape_backslashes and level >= 4:
result = result.replace("\\", "\\\\")

# In levels 5 to 8 we do not allow if without else, we add an empty print to make it possible in the parser
if level >= 5 and level <= 8:
# In levels 5 to 7 we do not allow if without else, we add an empty print to make it possible in the parser
if level >= 5 and level < 8 and preprocess_ifs_enabled:
result = preprocess_ifs(result, lang)

# In level 8 we add indent-dedent blocks to the code before parsing
Expand Down
2 changes: 1 addition & 1 deletion hedy_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def translate_keywords(input_string_, from_lang="en", to_lang="nl", level=1):
""" "Return code with keywords translated to language of choice in level of choice"""
try:
processed_input = hedy.process_input_string(
input_string_, level, from_lang, escape_backslashes=False
input_string_, level, from_lang, escape_backslashes=False, preprocess_ifs_enabled=False
)

parser = hedy.get_parser(level, from_lang, True)
Expand Down
2 changes: 1 addition & 1 deletion static/css/generated.css

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions tests/test_level/test_level_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,11 @@ def test_consecutive_if_statements(self):
if name == 'Hedy':
print(f'nice!')
else:
_ = 'x'
x__x__x__x = '5'
if name in names:
print(f'nice!')""")

self.single_level_tester(code=code, expected=expected, translate=False)
self.single_level_tester(code=code, expected=expected)

def test_onno_3372(self):
code = textwrap.dedent("""\
Expand All @@ -609,12 +609,11 @@ def test_onno_3372(self):
if antw == 'schaar':
print(f'gelijk spel!')
else:
_ = 'x'
x__x__x__x = '5'
print(f'test')""")

self.single_level_tester(code=code,
expected=expected,
translate=False)
expected=expected)

def test_restaurant_example(self):
code = textwrap.dedent("""\
Expand All @@ -630,16 +629,15 @@ def test_restaurant_example(self):
if eten == 'friet':
saus = input(f'Welke saus wilt u bij de friet?')
else:
_ = 'x'
x__x__x__x = '5'
if eten == 'pizza':
topping = input(f'Welke topping wilt u op de pizza?')
else:
_ = 'x'
x__x__x__x = '5'
print(f'{eten}')""")

self.single_level_tester(code=code,
expected=expected,
translate=False)
expected=expected, translate=False)

def test_onno_3372_else(self):
code = textwrap.dedent("""\
Expand All @@ -656,8 +654,7 @@ def test_onno_3372_else(self):
print(f'test')""")

self.single_level_tester(code=code,
expected=expected,
translate=False)
expected=expected)

def test_consecutive_if_and_if_else_statements(self):
code = textwrap.dedent("""\
Expand All @@ -671,15 +668,14 @@ def test_consecutive_if_and_if_else_statements(self):
if naam == 'Hedy':
print(f'leuk')
else:
_ = 'x'
x__x__x__x = '5'
if naam == 'Python':
print(f'ook leuk')
else:
print(f'minder leuk!')""")

self.single_level_tester(code=code,
expected=expected,
translate=False)
expected=expected)

def test_consecutive_if_else_statements(self):
code = textwrap.dedent("""\
Expand All @@ -700,7 +696,7 @@ def test_consecutive_if_else_statements(self):
else:
print(f'meh')""")

self.single_level_tester(code=code, expected=expected, translate=False)
self.single_level_tester(code=code, expected=expected)

def test_turn_if_forward(self):
code = textwrap.dedent("""\
Expand Down Expand Up @@ -842,7 +838,7 @@ def test_if_fix_nl(self):
if naam == '5':
print(f'leuk')
else:
_ = 'x'
x__x__x__x = '5'
print(f'minder leuk!')""")

self.multi_level_tester(
Expand Down Expand Up @@ -946,7 +942,7 @@ def test_double_if_pressed(self):
break
# End of PyGame Event Handler""")

self.multi_level_tester(code=code, expected=expected, max_level=7, translate=False)
self.multi_level_tester(code=code, expected=expected, max_level=7)

def test_if_pressed_has_enter_after_pressed(self):
code = textwrap.dedent("""\
Expand Down Expand Up @@ -1242,7 +1238,7 @@ def test_if_pressed_non_latin(self):
break
# End of PyGame Event Handler""")

self.multi_level_tester(code=code, expected=expected, max_level=7, translate=False)
self.multi_level_tester(code=code, expected=expected, max_level=7)

def test_if_pressed_missing_else_gives_error(self):
code = textwrap.dedent("""\
Expand Down
8 changes: 4 additions & 4 deletions tests/test_level/test_level_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,11 +828,11 @@ def test_consecutive_if_statements(self):
if convert_numerals('Latin', name) == convert_numerals('Latin', 'Hedy'):
print(f'nice!')
else:
_ = 'x'
x__x__x__x = '5'
if name in names:
print(f'nice!')""")

self.multi_level_tester(max_level=7, code=code, expected=expected, translate=False)
self.multi_level_tester(max_level=7, code=code, expected=expected)

def test_consecutive_if_and_if_else_statements(self):
code = textwrap.dedent("""\
Expand All @@ -846,13 +846,13 @@ def test_consecutive_if_and_if_else_statements(self):
if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'):
print(f'leuk')
else:
_ = 'x'
x__x__x__x = '5'
if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Python'):
print(f'ook leuk')
else:
print(f'minder leuk!')""")

self.multi_level_tester(max_level=7, code=code, expected=expected, translate=False)
self.multi_level_tester(max_level=7, code=code, expected=expected)

def test_consecutive_if_else_statements(self):
code = textwrap.dedent("""\
Expand Down
5 changes: 2 additions & 3 deletions tests/test_level/test_level_07.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def test_repeat_if_multiple(self):
if convert_numerals('Latin', aan) == convert_numerals('Latin', 'ja'):
print(f'Hedy is leuk!')
else:
_ = 'x'
x__x__x__x = '5'
time.sleep(0.1)
for __i__ in range(int('3')):
if convert_numerals('Latin', aan) == convert_numerals('Latin', 'ja'):
Expand All @@ -437,5 +437,4 @@ def test_repeat_if_multiple(self):
self.single_level_tester(
code=code,
expected=expected,
output=output,
translate=False)
output=output)
2 changes: 1 addition & 1 deletion translations/bg/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

msgid "no_programs"
msgstr "Нямаш програми."
Expand Down
2 changes: 1 addition & 1 deletion translations/bn/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/ca/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/cs/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/cy/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/da/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ msgid "no_certificate"
msgstr "This user hasn't earned the Hedy Certificate of Completion"

msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

msgid "no_programs"
msgstr "You have no programs yet."
Expand Down
2 changes: 1 addition & 1 deletion translations/eo/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

msgid "no_programs"
msgstr "Vi ankoraŭ ne havas programojn."
Expand Down
2 changes: 1 addition & 1 deletion translations/et/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/fa/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/fi/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/fr/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ msgstr "Cet utilisateur n'a pas reçu le certificat de complétion de Hedy"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

msgid "no_programs"
msgstr "Vous n'avez pas encore de programme."
Expand Down
2 changes: 1 addition & 1 deletion translations/fy/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

msgid "no_programs"
msgstr "Noch gjin programma's."
Expand Down
2 changes: 1 addition & 1 deletion translations/he/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/id/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

msgid "no_programs"
msgstr "Kamu belum memiliki program"
Expand Down
2 changes: 1 addition & 1 deletion translations/it/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

msgid "no_programs"
msgstr "Non hai ancora nessun programma."
Expand Down
2 changes: 1 addition & 1 deletion translations/ja/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ msgstr "このユーザはHedy修了証を得ていません"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/kmr/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
2 changes: 1 addition & 1 deletion translations/ko/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ msgstr "This user hasn't earned the Hedy Certificate of Completion"

#, fuzzy
msgid "no_more_flat_if"
msgstr "Starting in level 8, the line after {if} needs to start with 4 spaces."
msgstr msgstr "Starting in level 8, the code after {if} needs to be placed on the next line and start with 4 spaces."

#, fuzzy
msgid "no_programs"
Expand Down
Loading

0 comments on commit 87f7c73

Please sign in to comment.