From ebf2a538b0048a279f5f88ab33aee7cf307c7d2f Mon Sep 17 00:00:00 2001 From: akako <69710421+akkako@users.noreply.github.com> Date: Fri, 11 Sep 2026 14:54:51 +0800 Subject: [PATCH] fix(shell): keep else/elif/except/finally inside multi-line REPL block --- src/PikaObj.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/PikaObj.c b/src/PikaObj.c index 0b079bdd8..f74c559b9 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -1807,6 +1807,29 @@ static void handle_history_navigation(char inputChar, return; } +static pika_bool _shell_lineStaysInBlock(char* line) { + if ((line[0] == ' ') || (line[0] == '\t')) { + return pika_true; + } + if (strIsStartWith(line, "else") && + ((line[4] == ' ') || (line[4] == ':'))) { + return pika_true; + } + if (strIsStartWith(line, "elif") && + ((line[4] == ' ') || (line[4] == ':'))) { + return pika_true; + } + if (strIsStartWith(line, "except") && + ((line[6] == ' ') || (line[6] == ':'))) { + return pika_true; + } + if (strIsStartWith(line, "finally") && + ((line[7] == ' ') || (line[7] == ':'))) { + return pika_true; + } + return pika_false; +} + enum shellCTRL _inner_do_obj_runChar(PikaObj* self, char inputChar, ShellConfig* shell) { @@ -1995,7 +2018,7 @@ enum shellCTRL _inner_do_obj_runChar(PikaObj* self, obj_setStr(self, shell->blockBuffName, shell_buff_new); strsDeinit(&buffs); /* go out from block */ - if ((shell->lineBuff[0] != ' ') && (shell->lineBuff[0] != '\t')) { + if (!_shell_lineStaysInBlock(shell->lineBuff)) { shell->inBlock = pika_false; input_line = obj_getStr(self, shell->blockBuffName); ctrl = shell->handler(self, input_line, shell);