From 145f7e42976cd6fa75b0674a9aa3dae0089b2ba8 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Tue, 26 Sep 2023 20:36:23 +0200 Subject: [PATCH] [FIX] Localizes errors and fixes if newline error (#4555) Localizes errors and fixes if newline error (#4482 and #4495) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- app.py | 2 ++ hedy.py | 17 ++++++++++++++--- tests/test_level/test_level_05.py | 16 ++++++++++++++++ translations/en/LC_MESSAGES/messages.po | 2 +- translations/nl/LC_MESSAGES/messages.po | 2 +- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index c84a1a6f5a9..c766fefdec0 100644 --- a/app.py +++ b/app.py @@ -746,6 +746,7 @@ def translate_error(code, arguments, keyword_lang): 'allowed_types', 'invalid_type', 'invalid_type_2', + 'offending_keyword', 'character_found', 'concept', 'tip', @@ -762,6 +763,7 @@ def translate_error(code, arguments, keyword_lang): 'guessed_command', 'invalid_argument', 'invalid_argument_2', + 'offending_keyword', 'variable', 'invalid_value', 'print', diff --git a/hedy.py b/hedy.py index 1078256641d..6648b2d61a0 100644 --- a/hedy.py +++ b/hedy.py @@ -3007,12 +3007,23 @@ def contains_any_of(commands, line): return True return False + def next_non_empty_line(lines, start_line_number): + if start_line_number > len(lines): + return '' # end of code, return empty so that starts_with doesnt find anything + else: + for i in range(start_line_number + 1, len(lines)): + if lines[i] == '': + continue + else: + return lines[i] + + return '' # nothing found? return empty so that starts_with doesnt find anything + for i in range(len(lines) - 1): line = lines[i] - next_line = lines[i + 1] - # if this line starts with if but does not contain an else, and the next line too is not an else. - if (starts_with('if', line) or starts_with_after_repeat('if', line)) and (not starts_with('else', next_line)) and (not contains('else', line)): + # if this line starts with if but does not contain an else, and the next non-empty line too is not an else. + if (starts_with('if', line) or starts_with_after_repeat('if', line)) and (not starts_with('else', next_non_empty_line(lines, i))) and (not contains('else', line)): # is this line just a condition and no other keyword (because that is no problem) commands = ["print", "ask", "forward", "turn"] excluded_commands = ["pressed"] diff --git a/tests/test_level/test_level_05.py b/tests/test_level/test_level_05.py index 77c09d0d5c5..cb1ae00fc9c 100644 --- a/tests/test_level/test_level_05.py +++ b/tests/test_level/test_level_05.py @@ -435,6 +435,22 @@ def test_if_equality_print_linebreak_else_print(self): self.single_level_tester(code=code, expected=expected) + def test_if_equality_print_linebreaks_else_print(self): + # line break before else is allowed + code = textwrap.dedent("""\ + naam is Hedy + if naam is Hedy print 'leuk'\n\n + else print 'minder leuk'""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if naam == 'Hedy': + print(f'leuk') + else: + print(f'minder leuk')""") + + self.single_level_tester(code=code, expected=expected) + def test_if_equality_linebreak_print_else_print(self): # line break after if-condition is allowed code = textwrap.dedent("""\ diff --git a/translations/en/LC_MESSAGES/messages.po b/translations/en/LC_MESSAGES/messages.po index 29e52717293..0986a041c7c 100644 --- a/translations/en/LC_MESSAGES/messages.po +++ b/translations/en/LC_MESSAGES/messages.po @@ -300,7 +300,7 @@ msgstr "Are you sure? You cannot revert this action." msgid "ask_needs_var" msgstr "" "Starting in level 2, {ask} needs to be used with a variable. Example: " -"name {is} {ask} What are you called?" +"name {is} {ask} What is your name?" msgid "back_to_class" msgstr "Go back to class" diff --git a/translations/nl/LC_MESSAGES/messages.po b/translations/nl/LC_MESSAGES/messages.po index 125555a34fa..7549fdae32b 100644 --- a/translations/nl/LC_MESSAGES/messages.po +++ b/translations/nl/LC_MESSAGES/messages.po @@ -302,7 +302,7 @@ msgstr "Weet je het zeker? Je kunt dit niet meer ongedaan maken." msgid "ask_needs_var" msgstr "" -"Vanaf level 2 hoort {ask} met een variabele ervoor. Bijv: naam is {ask} " +"Vanaf level 2 hoort {ask} met een variabele ervoor. Bijv: naam {is} {ask} " "Hoe heet jij?" msgid "back_to_class"