diff --git a/docs/dncl/README.md b/docs/dncl/README.md index bae6c09139..b4c0558bd6 100644 --- a/docs/dncl/README.md +++ b/docs/dncl/README.md @@ -486,6 +486,7 @@ Ruby の `puts` / `gets` は、いままでどおりコンソールのブロッ - ブロックはプログラムを関数の定義のところでしかスクリプトに分けないので、日本語タブではスクリプトの間に空の行を入れない。 ブロックは空の行を持たないので、日本語で書いた空の行は、ブロックを通ると消える(これまでと同じ)。 - コードタブで足したスクリプトは、並びの最後に入る。 +- 日本語タブで実行するときも、ブロックにする Ruby をこの並びで作り直す(`handleExecuteLine`)。関数を先に出す Ruby のままブロックにすると、実行のあとから関数の定義が先頭に移る。 - ルビティー(`genai-ruby-adapter.js`)が読む、エディタに出ていないスプライトの日本語も同じ並びで作る(エディタの日本語と比べるため)。 - 日本語タブに入るとき(ルビータブから・コードタブから)も同じ並びで日本語にする(`ruby-tab.jsx` の `switchLanguage`)。 コードタブから戻るときは、エディタにまだ前の日本語が残っていることがあるので、エディタの中身を読まず、ブロックから作った Ruby を渡して日本語にする diff --git a/packages/scratch-gui/src/containers/ruby-tab.jsx b/packages/scratch-gui/src/containers/ruby-tab.jsx index ff1296f82b..38d1a2e012 100644 --- a/packages/scratch-gui/src/containers/ruby-tab.jsx +++ b/packages/scratch-gui/src/containers/ruby-tab.jsx @@ -1345,8 +1345,6 @@ const RubyTab = (props) => { clearErrors(); - const code = rubyCode.code; - // In DNCL mode, execute all top-level scripts from top to bottom // instead of just the cursor line. const isDncl = dnclModeRef.current; @@ -1355,6 +1353,9 @@ const RubyTab = (props) => { onShowAlert('convertJapaneseError'); return; } + // The Ruby made from the blocks puts the function definitions first: run the program + // in the order it was written, or the blocks take that order (Issue #2620). + const code = isDncl ? rubyInScriptOrder(rubyCode.target, rubyCode.code, rubyVersion) : rubyCode.code; // Running shows the read mode, to follow the program by the line numbers (Issue #2380). if (isDncl && problemFormViewRef.current) problemFormViewRef.current.showReadMode(); const targetLine = isDncl ? 1 : findExecutableLine(code, lineNumber); diff --git a/packages/scratch-gui/test/integration/dncl-code-tab-script-order.test.js b/packages/scratch-gui/test/integration/dncl-code-tab-script-order.test.js index a4c339a287..af1a7d85ee 100644 --- a/packages/scratch-gui/test/integration/dncl-code-tab-script-order.test.js +++ b/packages/scratch-gui/test/integration/dncl-code-tab-script-order.test.js @@ -109,6 +109,22 @@ describe('The Japanese tab after the Code tab (Issue #2577)', () => { expect(await getEditorValue(driver)).toBe(FUNCTIONS_BETWEEN_CALLS); }); + // Running takes the Ruby the blocks give back, which puts the function definitions first: the + // run must keep the written order (Issue #2620). The issue was not seen with short programs + // without a list, so a program with a list is run too. + test.each([ + ['two functions between the calls', FUNCTIONS_BETWEEN_CALLS], + ['a list and a function after the main program', FUNCTION_AFTER_MAIN], + ])('running after the Code tab keeps the order of the functions (%s)', async (_name, program) => { + await openJapaneseTabWith(driver, program); + await throughCodeTab(driver, 1500); + await clickByTestId(driver, 'ruby-toolbar-execute'); + await pause(2000); + expect(await getEditorValue(driver)).toBe(program); + await throughCodeTab(driver, 1500); + expect(await getEditorValue(driver)).toBe(program); + }); + // 30 round trips take about a minute, longer on a slow CI runner than the 95 s of the helper. test('coming back from the Code tab 30 times never falls back to the Ruby tab', async () => { await openJapaneseTabWith(driver, FUNCTION_AFTER_MAIN);