From 3b5967eaf715f6095d14b481927c0c95bee5f727 Mon Sep 17 00:00:00 2001 From: "smalruby3-editor-bot[bot]" <297607354+smalruby3-editor-bot[bot]@users.noreply.github.com> Date: Sat, 28 Nov 2026 23:32:26 +0000 Subject: [PATCH 1/3] fix(dncl): keep full-width characters inside strings and comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The auto-correct no longer changes what is written in a string ("...", '...', and 「...」 in the Japanese tab), in both the Japanese tab and the Ruby tab. Full-width and smart quotes are read as the ends of a string, so an IME-typed "こんにちは!" keeps its !. The "also replace inside strings" setting is removed; an old saved value is ignored. Co-Authored-By: Claude Opus 5.5 --- docs/dncl/README.md | 2 +- docs/ruby-editor/README.md | 2 +- .../auto-correct-modal/auto-correct-modal.jsx | 8 +- packages/scratch-gui/src/lib/auto-correct.js | 56 +++++-------- packages/scratch-gui/src/locales/ja-Hira.js | 1 - packages/scratch-gui/src/locales/ja.js | 1 - .../test/integration/auto-correct.test.js | 26 +++---- .../test/unit/lib/auto-correct.test.js | 78 ++++++++++--------- 8 files changed, 75 insertions(+), 99 deletions(-) diff --git a/docs/dncl/README.md b/docs/dncl/README.md index bae6c091399..e7731f91b57 100644 --- a/docs/dncl/README.md +++ b/docs/dncl/README.md @@ -225,7 +225,7 @@ DNCL 風の形の読み方(切りかえる前の変換に合わせたもの) - Ruby はこれらをどれも名前に使えて、Ruby → 日本語は名前をそのまま書く(`@佐々木` → `佐々木`)。日本語 → Ruby が同じ範囲を名前として読むので、書いた名前は読み戻せる。 - 全角英字・全角数字は、文字列(`"…"` `'…'` `「…」`)とコメントの外では、読む前にすべて半角にする(`x1` は `x1`、`and` は `and`)。見た目だけの揺れで(`policy.md`)、Ruby モードの自動置換も半角にするのと同じ考え。 ただし日本語タブの自動置換は `%` を半角にしない(書き出しが全角の `%` なので、半角にすると書き出すたびに戻って行ったり来たりする)。 - 文字列とコメントの中は書いたとおりに残す(`表示する("ABC")` は `"ABC"` のまま)。全角と半角は 1 字ずつ同じ長さなので、誤りの列は変わらない。 + 文字列とコメントの中は書いたとおりに残す(`表示する("ABC")` は `"ABC"` のまま)。日本語タブ・ルビータブの自動置換も、文字列とコメントの中を変えない(`表示する("こんにちは!")` の `!` は全角のまま。全角の `"` `'` や `“` `”` も文字列の囲みとして読み、囲みだけを半角にする)。全角と半角は 1 字ずつ同じ長さなので、誤りの列は変わらない。 Ruby → 日本語は Ruby の名前をそのまま書く(`@x` → `x`)ので、日本語から Ruby に戻すと `@x` になる。 - `☆` `①` `😀` のような記号は名前に使えず、「「☆」が読めません」の誤りになる(BMP の外の文字も 1 文字として示す)。 diff --git a/docs/ruby-editor/README.md b/docs/ruby-editor/README.md index 0b43e456dd8..72fdc3b04af 100644 --- a/docs/ruby-editor/README.md +++ b/docs/ruby-editor/README.md @@ -57,7 +57,7 @@ ruby-toolbar 上部のセグメントで切替: | 元に戻す/やり直す | Monaco の undo/redo | | 検索 | Monaco の find | | キーボード (`ruby-toolbar-keyboard`) | ソフトウェアキーボードの表示/非表示(タッチデバイスのみ表示)。Monaco 組み込みの iPad キーボードウィジェットは zoom ボタンと重なるため CSS で非表示にしており、その代替 | -| 自動置換 (`ruby-toolbar-auto-correct`) | 自動置換のオン/オフを切りかえるスイッチ(`role="switch"`・`aria-checked`。つまみとラベル「自動置換」。形はデザインシステムの「スイッチ」)。全角の数字・英字・記号・空白を半角にする。コメント(`#` の後ろ。日本語タブでは設定で `//` をコメントにしたときの `//` の後ろも)の中は変えない(`#` は `#` になる) | +| 自動置換 (`ruby-toolbar-auto-correct`) | 自動置換のオン/オフを切りかえるスイッチ(`role="switch"`・`aria-checked`。つまみとラベル「自動置換」。形はデザインシステムの「スイッチ」)。全角の数字・英字・記号・空白を半角にする。文字列(`"…"` `'…'`。日本語タブでは `「…」` も)とコメント(`#` の後ろ。日本語タブでは設定で `//` をコメントにしたときの `//` の後ろも)の中は変えない(`puts("こんにちは!")` の `!` は全角のまま。囲みの `"` `“` と `#` は半角になる)。設定で変えられるのは、半角にする文字の種類だけ | | その他メニュー (`ruby-toolbar-more-menu`) | Ruby スクリプト保存、クラス挿入、プレビュー、自動置換設定 | | スプライト切替 | エディタ対象スプライトの prev/next | diff --git a/packages/scratch-gui/src/components/auto-correct-modal/auto-correct-modal.jsx b/packages/scratch-gui/src/components/auto-correct-modal/auto-correct-modal.jsx index 6b41b7f5629..ed2544a29d5 100644 --- a/packages/scratch-gui/src/components/auto-correct-modal/auto-correct-modal.jsx +++ b/packages/scratch-gui/src/components/auto-correct-modal/auto-correct-modal.jsx @@ -33,14 +33,9 @@ const messages = defineMessages({ description: 'Setting for fullwidth space conversion', id: 'gui.autoCorrectModal.fullwidthSpace', }, - replaceInStrings: { - defaultMessage: 'Also replace inside strings', - description: 'Setting for replacing inside string literals', - id: 'gui.autoCorrectModal.replaceInStrings', - }, }); -const SETTING_KEYS = ['fullwidthNumbers', 'fullwidthAlpha', 'fullwidthSymbols', 'fullwidthSpace', 'replaceInStrings']; +const SETTING_KEYS = ['fullwidthNumbers', 'fullwidthAlpha', 'fullwidthSymbols', 'fullwidthSpace']; const AutoCorrectModal = ({ intl, settings, onSettingChange, onRequestClose }) => { const handleChange = useCallback( @@ -87,7 +82,6 @@ AutoCorrectModal.propTypes = { fullwidthAlpha: PropTypes.bool.isRequired, fullwidthSymbols: PropTypes.bool.isRequired, fullwidthSpace: PropTypes.bool.isRequired, - replaceInStrings: PropTypes.bool.isRequired, }).isRequired, onSettingChange: PropTypes.func.isRequired, onRequestClose: PropTypes.func.isRequired, diff --git a/packages/scratch-gui/src/lib/auto-correct.js b/packages/scratch-gui/src/lib/auto-correct.js index 5ab6a3e9876..0b34b9c4ed4 100644 --- a/packages/scratch-gui/src/lib/auto-correct.js +++ b/packages/scratch-gui/src/lib/auto-correct.js @@ -8,7 +8,6 @@ const defaultSettings = { fullwidthAlpha: true, fullwidthSymbols: true, fullwidthSpace: true, - replaceInStrings: true, }; // Fullwidth ASCII symbols mapped to halfwidth equivalents. @@ -117,6 +116,12 @@ const replaceChar = (ch, settings) => { return ch; }; +// The quotes that open and close a string, half-width, full-width and smart +// ones alike: an IME writes `"` or `“`, and they become `"` only after the +// string is found, so the full-width ones have to be read as quotes too. +const DOUBLE_QUOTES = '"\uFF02\u201C\u201D'; +const SINGLE_QUOTES = "'\uFF07\u2018\u2019"; + /** * Whether `ch` is a `/`, half-width or full-width. * @param {string} ch - A character. @@ -145,27 +150,27 @@ const isSlash = (ch) => ch === '/' || ch === '\uFF0F'; const findRegions = (code, options = {}) => { const strings = []; const comments = []; - const closeOf = (ch) => { - if (ch === '"' || ch === "'") return ch; - if (ch === '\u201C') return '\u201D'; - if (ch === '\u2018') return '\u2019'; + // The quotes that close a string opened by `ch`, or null if `ch` opens none. + const closersOf = (ch) => { + if (DOUBLE_QUOTES.includes(ch)) return DOUBLE_QUOTES; + if (SINGLE_QUOTES.includes(ch)) return SINGLE_QUOTES; if (options.japanese && ch === '\u300C') return '\u300D'; return null; }; let i = 0; while (i < code.length) { const ch = code[i]; - const closeQuote = closeOf(ch); - if (closeQuote) { + const closers = closersOf(ch); + if (closers) { const start = i; i++; // skip opening quote while (i < code.length) { // A `\` in a 「…」 is text (the Japanese tab takes it as it is). - if (code[i] === '\\' && closeQuote !== '\u300D') { + if (code[i] === '\\' && closers !== '\u300D') { i += 2; // skip escaped character continue; } - if (code[i] === closeQuote) { + if (closers.includes(code[i])) { break; } if (options.japanese && code[i] === '\n') { @@ -214,26 +219,11 @@ const isInString = (index, regions) => { return false; }; -/** - * Check if a character at the given index is escaped (preceded by an odd number of backslashes). - * @param {string} code - The source code. - * @param {number} index - The character index. - * @returns {boolean} True if the character is escaped. - */ -const isEscaped = (code, index) => { - let backslashes = 0; - let j = index - 1; - while (j >= 0 && code[j] === '\\') { - backslashes++; - j--; - } - return backslashes % 2 === 1; -}; - /** * Apply fullwidth-to-halfwidth auto-correction to Ruby source code (or the - * Japanese tab's code). What is written in a comment stays as it is, and so - * does the `%` of the Japanese tab. + * Japanese tab's code). What is written in a string or a comment stays as it + * is: it is what is shown or meant, as `"こんにちは!"` (Issue #2621). So does + * the `%` of the Japanese tab. * @param {string} code - The Ruby source code. * @param {object} settings - The auto-correct settings. * @param {object} [options] - How the source is read (see `findRegions`). @@ -248,7 +238,7 @@ const autoCorrect = (code, settings, options = {}) => { const regions = findRegions(code, options); const result = code.replace(pattern, (match, offset) => { - if (isInComment(offset, regions.comments)) { + if (isInComment(offset, regions.comments) || isInString(offset, regions.strings)) { return match; } @@ -258,16 +248,6 @@ const autoCorrect = (code, settings, options = {}) => { return match; } - // If replaceInStrings is off, skip characters inside string literals - if (!settings.replaceInStrings && isInString(offset, regions.strings)) { - return match; - } - - // If replaceInStrings is on but char is escaped inside a string, keep it - if (settings.replaceInStrings && isEscaped(code, offset)) { - return match; - } - return replaceChar(match, settings); }); diff --git a/packages/scratch-gui/src/locales/ja-Hira.js b/packages/scratch-gui/src/locales/ja-Hira.js index ef91fe2b6af..628a935b1ba 100644 --- a/packages/scratch-gui/src/locales/ja-Hira.js +++ b/packages/scratch-gui/src/locales/ja-Hira.js @@ -1966,7 +1966,6 @@ export default { 'gui.autoCorrectModal.fullwidthAlpha': 'ぜんかくアルファベット → はんかくアルファベット', 'gui.autoCorrectModal.fullwidthSymbols': 'ぜんかくきごう → はんかくきごう', 'gui.autoCorrectModal.fullwidthSpace': 'ぜんかくスペース → はんかくスペース', - 'gui.autoCorrectModal.replaceInStrings': 'もじれつもおきかえる', ...lessonsMessages, ...activityDiagramMessages, diff --git a/packages/scratch-gui/src/locales/ja.js b/packages/scratch-gui/src/locales/ja.js index 767b7f2c7e0..a7cb63f294f 100644 --- a/packages/scratch-gui/src/locales/ja.js +++ b/packages/scratch-gui/src/locales/ja.js @@ -1911,7 +1911,6 @@ export default { 'gui.autoCorrectModal.fullwidthAlpha': '全角アルファベット → 半角アルファベット', 'gui.autoCorrectModal.fullwidthSymbols': '全角記号 → 半角記号', 'gui.autoCorrectModal.fullwidthSpace': '全角スペース → 半角スペース', - 'gui.autoCorrectModal.replaceInStrings': '文字列も置き換える', ...lessonsMessages, ...activityDiagramMessages, diff --git a/packages/scratch-gui/test/integration/auto-correct.test.js b/packages/scratch-gui/test/integration/auto-correct.test.js index d6617e448a7..1b0e687baa8 100644 --- a/packages/scratch-gui/test/integration/auto-correct.test.js +++ b/packages/scratch-gui/test/integration/auto-correct.test.js @@ -70,30 +70,28 @@ describe('auto-correct integration', () => { }); }); - describe('string content preservation with replaceInStrings off', () => { - const settingsNoStrings = { ...defaultSettings, replaceInStrings: false }; - + describe('string content preservation', () => { test('should not modify string content but fix surrounding code', () => { const input = 'puts "ABC123"'; - const result = autoCorrect(input, settingsNoStrings); + const result = autoCorrect(input, defaultSettings); expect(result).toBe('puts "ABC123"'); }); test('should handle multiple strings on same line', () => { const input = "puts 'A' + 'B'"; - const result = autoCorrect(input, settingsNoStrings); + const result = autoCorrect(input, defaultSettings); expect(result).toBe("puts 'A' + 'B'"); }); test('should handle code between strings', () => { const input = 'x = "A" + y + "B"'; - const result = autoCorrect(input, settingsNoStrings); + const result = autoCorrect(input, defaultSettings); expect(result).toBe('x = "A" + y + "B"'); }); test('should preserve content inside smart-quoted strings', () => { const input = 'puts \u201CABC\u201D'; - const result = autoCorrect(input, settingsNoStrings); + const result = autoCorrect(input, defaultSettings); // Smart quotes become halfwidth, but content inside is preserved expect(result).toBe('puts "ABC"'); }); @@ -126,18 +124,16 @@ describe('auto-correct integration', () => { }); describe('escape handling in strings', () => { - test('should preserve escaped fullwidth chars', () => { - const input = '"\\1 + 2"'; + test('should keep everything in a string with an escaped full-width char', () => { + const input = '"\\1 + 2" + 1'; const result = autoCorrect(input, defaultSettings); - // \1 stays as-is, but + and 2 are converted - expect(result).toBe('"\\1 + 2"'); + expect(result).toBe('"\\1 + 2" + 1'); }); - test('should handle double escaped backslash', () => { - const input = '"\\\\1"'; + test('should end a string after a double escaped backslash', () => { + const input = '"\\\\1" + 1'; const result = autoCorrect(input, defaultSettings); - // \\\\ is escaped backslash, so 1 should be converted - expect(result).toBe('"\\\\1"'); + expect(result).toBe('"\\\\1" + 1'); }); }); diff --git a/packages/scratch-gui/test/unit/lib/auto-correct.test.js b/packages/scratch-gui/test/unit/lib/auto-correct.test.js index e9a1071c116..36b801a9c67 100644 --- a/packages/scratch-gui/test/unit/lib/auto-correct.test.js +++ b/packages/scratch-gui/test/unit/lib/auto-correct.test.js @@ -7,7 +7,6 @@ describe('autoCorrect', () => { fullwidthAlpha: false, fullwidthSymbols: false, fullwidthSpace: false, - replaceInStrings: true, }; describe('fullwidth numbers', () => { @@ -116,42 +115,56 @@ describe('autoCorrect', () => { }); }); - describe('string handling (replaceInStrings)', () => { - test('should replace inside double-quoted strings when replaceInStrings is on', () => { - expect(autoCorrect('"ABC"', allOn)).toBe('"ABC"'); + describe('strings (kept as they are written)', () => { + test('should keep what is written in a double-quoted string', () => { + expect(autoCorrect('x = "ABC"', allOn)).toBe('x = "ABC"'); }); - test('should replace inside single-quoted strings when replaceInStrings is on', () => { - expect(autoCorrect("'123'", allOn)).toBe("'123'"); + test('should keep what is written in a single-quoted string', () => { + expect(autoCorrect("x = '123'", allOn)).toBe("x = '123'"); }); - test('should NOT replace inside double-quoted strings when replaceInStrings is off', () => { - const settings = { ...allOn, replaceInStrings: false }; - expect(autoCorrect('x = "ABC"', settings)).toBe('x = "ABC"'); + test('should still correct the code outside strings', () => { + expect(autoCorrect('x = "ABC"', allOn)).toBe('x = "ABC"'); + }); + + test('should keep a full-width ! in a string (Issue #2621)', () => { + expect(autoCorrect('puts("こんにちは!ABC")', allOn)).toBe('puts("こんにちは!ABC")'); }); - test('should NOT replace inside single-quoted strings when replaceInStrings is off', () => { - const settings = { ...allOn, replaceInStrings: false }; - expect(autoCorrect("x = '123'", settings)).toBe("x = '123'"); + test('should take full-width quotes as the ends of a string and make them half-width', () => { + expect(autoCorrect('puts("こんにちは!")', allOn)).toBe('puts("こんにちは!")'); + expect(autoCorrect('puts('A!')', allOn)).toBe("puts('A!')"); }); - test('should still replace outside strings when replaceInStrings is off', () => { - const settings = { ...allOn, replaceInStrings: false }; - expect(autoCorrect('x = "ABC"', settings)).toBe('x = "ABC"'); + test('should take smart quotes as the ends of a string and make them half-width', () => { + expect(autoCorrect('puts(\u201CA!\u201D)', allOn)).toBe('puts("A!")'); + expect(autoCorrect('puts(\u201DA!\u201D)', allOn)).toBe('puts("A!")'); + expect(autoCorrect('puts(\u2018A!\u2019)', allOn)).toBe("puts('A!')"); }); - test('should handle escaped fullwidth chars in strings (keep original)', () => { - // \1 should NOT be replaced even when replaceInStrings is on - expect(autoCorrect('"\\1\\2"', allOn)).toBe('"\\1\\2"'); + test('should end a string at a quote of the same kind, half-width or full-width', () => { + expect(autoCorrect('puts("A")+x', allOn)).toBe('puts("A")+x'); + expect(autoCorrect('puts("A")+x', allOn)).toBe('puts("A")+x'); }); - test('should replace non-escaped fullwidth but keep escaped ones in same string', () => { - expect(autoCorrect('"A\\1B"', allOn)).toBe('"A\\1B"'); + test('should not end a double-quoted string at a single quote', () => { + expect(autoCorrect('puts("A'B")', allOn)).toBe('puts("A'B")'); }); - test('should handle strings with escaped backslash before fullwidth', () => { - // \\\1 means escaped backslash followed by fullwidth 1, so 1 should be replaced - expect(autoCorrect('"\\\\1"', allOn)).toBe('"\\\\1"'); + test('should keep the strings of the Japanese tab', () => { + expect(autoCorrect('表示する("こんにちは!ABC")', allOn, { japanese: true })).toBe( + '表示する("こんにちは!ABC")', + ); + expect(autoCorrect('表示する(「こんにちは!ABC」)', allOn, { japanese: true })).toBe( + '表示する(「こんにちは!ABC」)', + ); + expect(autoCorrect("表示する('A!')", allOn, { japanese: true })).toBe("表示する('A!')"); + }); + + test('should ignore an old saved replaceInStrings setting', () => { + const settings = { ...allOn, replaceInStrings: true }; + expect(autoCorrect('x = "ABC"', settings)).toBe('x = "ABC"'); }); }); @@ -163,10 +176,9 @@ describe('autoCorrect', () => { }); test('should handle string spanning context with code around it', () => { - const settings = { ...allOn, replaceInStrings: false }; const input = 'x = "ABC"\ny = 1'; const expected = 'x = "ABC"\ny = 1'; - expect(autoCorrect(input, settings)).toBe(expected); + expect(autoCorrect(input, allOn)).toBe(expected); }); }); @@ -193,16 +205,14 @@ describe('autoCorrect', () => { describe('edge cases', () => { test('should handle unclosed string at end of input', () => { - const settings = { ...allOn, replaceInStrings: false }; // Unclosed string - treat rest as string, don't replace inside const input = 'x = "ABC'; - expect(autoCorrect(input, settings)).toBe('x = "ABC'); + expect(autoCorrect(input, allOn)).toBe('x = "ABC'); }); test('should handle escaped quote inside string', () => { - const settings = { ...allOn, replaceInStrings: false }; const input = 'x = "hello\\"ABC"'; - expect(autoCorrect(input, settings)).toBe('x = "hello\\"ABC"'); + expect(autoCorrect(input, allOn)).toBe('x = "hello\\"ABC"'); }); test('should handle mixed fullwidth and halfwidth', () => { @@ -228,12 +238,11 @@ describe('autoCorrect', () => { }); test('should not take a # in a string as a comment', () => { - expect(autoCorrect('puts("#1") + 1', allOn)).toBe('puts("#1") + 1'); + expect(autoCorrect('puts("#1") + 1', allOn)).toBe('puts("#1") + 1'); }); - test('should keep comments also when replaceInStrings is off', () => { - const settings = { ...allOn, replaceInStrings: false }; - expect(autoCorrect('"AB" + 1 # ,', settings)).toBe('"AB" + 1 # ,'); + test('should keep both a string and a comment on the same line', () => { + expect(autoCorrect('"AB" + 1 # ,', allOn)).toBe('"AB" + 1 # ,'); }); test('should not take // as a comment in Ruby', () => { @@ -254,7 +263,7 @@ describe('autoCorrect', () => { }); test('should not take a # in a 「…」 of the Japanese tab as a comment', () => { - expect(autoCorrect('表示する(「#1」)', allOn, { japanese: true })).toBe('表示する(「#1」)'); + expect(autoCorrect('表示する(「#1」)', allOn, { japanese: true })).toBe('表示する(「#1」)'); }); test('should keep % in the Japanese tab, as the Japanese tab writes it (B17-1)', () => { @@ -280,7 +289,6 @@ describe('autoCorrect', () => { fullwidthAlpha: true, fullwidthSymbols: true, fullwidthSpace: true, - replaceInStrings: true, }); }); }); From 2a348aeb09e8ff7f200846773ddf3e8524e49d4a Mon Sep 17 00:00:00 2001 From: "smalruby3-editor-bot[bot]" <297607354+smalruby3-editor-bot[bot]@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:12:33 +0000 Subject: [PATCH 2/3] fix(dncl): correct the code of #{} and end a string with its line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code in a #{} of a double-quoted Ruby string is code, so it is made half-width again ("#{x + 1}" -> "#{x + 1}"), as it was before strings were kept. A string now ends at the end of its line in the Ruby tab too: while a quote is being typed (an IME ” has no closing pair), the strings on the lines below were read as code and changed. Co-Authored-By: Claude Opus 5.5 --- docs/ruby-editor/README.md | 2 +- packages/scratch-gui/src/lib/auto-correct.js | 56 +++++++++++++++++-- .../test/unit/lib/auto-correct.test.js | 25 +++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/docs/ruby-editor/README.md b/docs/ruby-editor/README.md index 72fdc3b04af..cfdbf47a93f 100644 --- a/docs/ruby-editor/README.md +++ b/docs/ruby-editor/README.md @@ -57,7 +57,7 @@ ruby-toolbar 上部のセグメントで切替: | 元に戻す/やり直す | Monaco の undo/redo | | 検索 | Monaco の find | | キーボード (`ruby-toolbar-keyboard`) | ソフトウェアキーボードの表示/非表示(タッチデバイスのみ表示)。Monaco 組み込みの iPad キーボードウィジェットは zoom ボタンと重なるため CSS で非表示にしており、その代替 | -| 自動置換 (`ruby-toolbar-auto-correct`) | 自動置換のオン/オフを切りかえるスイッチ(`role="switch"`・`aria-checked`。つまみとラベル「自動置換」。形はデザインシステムの「スイッチ」)。全角の数字・英字・記号・空白を半角にする。文字列(`"…"` `'…'`。日本語タブでは `「…」` も)とコメント(`#` の後ろ。日本語タブでは設定で `//` をコメントにしたときの `//` の後ろも)の中は変えない(`puts("こんにちは!")` の `!` は全角のまま。囲みの `"` `“` と `#` は半角になる)。設定で変えられるのは、半角にする文字の種類だけ | +| 自動置換 (`ruby-toolbar-auto-correct`) | 自動置換のオン/オフを切りかえるスイッチ(`role="switch"`・`aria-checked`。つまみとラベル「自動置換」。形はデザインシステムの「スイッチ」)。全角の数字・英字・記号・空白を半角にする。文字列(`"…"` `'…'`。日本語タブでは `「…」` も)とコメント(`#` の後ろ。日本語タブでは設定で `//` をコメントにしたときの `//` の後ろも)の中は変えない(`puts("こんにちは!")` の `!` は全角のまま。囲みの `"` `“` と `#` は半角になる)。ただし `"…"` の中の `#{…}` は式なので半角にする(`"#{x + 1}"` は `"#{x + 1}"`)。文字列はその行の終わりまでと読む(打っている途中の閉じていない引用符で、下の行の文字列が変わらないように)。設定で変えられるのは、半角にする文字の種類だけ | | その他メニュー (`ruby-toolbar-more-menu`) | Ruby スクリプト保存、クラス挿入、プレビュー、自動置換設定 | | スプライト切替 | エディタ対象スプライトの prev/next | diff --git a/packages/scratch-gui/src/lib/auto-correct.js b/packages/scratch-gui/src/lib/auto-correct.js index 0b34b9c4ed4..fd548129054 100644 --- a/packages/scratch-gui/src/lib/auto-correct.js +++ b/packages/scratch-gui/src/lib/auto-correct.js @@ -129,6 +129,36 @@ const SINGLE_QUOTES = "'\uFF07\u2018\u2019"; */ const isSlash = (ch) => ch === '/' || ch === '\uFF0F'; +/** + * Whether a Ruby `#{` (half-width or full-width) starts at `index`. + * @param {string} code - The source code. + * @param {number} index - The character index. + * @returns {boolean} True for `#{`, `#\uFF5B`, `\uFF03{` and `\uFF03\uFF5B`. + */ +const isInterpolationStart = (code, index) => + (code[index] === '#' || code[index] === '\uFF03') && (code[index + 1] === '{' || code[index + 1] === '\uFF5B'); + +/** + * The index of the `}` that closes a `#{`, counting the braces in between + * (`#{h[{a: 1}]}`), or of the end of the line if it is not closed on its line. + * @param {string} code - The source code. + * @param {number} index - The index just after the `{` of `#{`. + * @returns {number} The index of the closing `}` (or `\uFF5D`), or of the `\n` (or the length of the code). + */ +const findInterpolationEnd = (code, index) => { + let depth = 0; + for (let i = index; i < code.length; i++) { + if (code[i] === '\n') return i; + if (code[i] === '{' || code[i] === '\uFF5B') { + depth++; + } else if (code[i] === '}' || code[i] === '\uFF5D') { + if (depth === 0) return i; + depth--; + } + } + return code.length; +}; + /** * Find the string literals and the comments of the source with a simple state * machine. A string region is `[start, end]` (the indexes of its quotes); a @@ -139,8 +169,13 @@ const isSlash = (ch) => ch === '/' || ch === '\uFF0F'; * * A comment starts with `#` (or `#`). With `slashComments`, `//` (or `//`) * starts one too (the Japanese tab, when its settings say so). With - * `japanese`, `「…」` is a string as well and a string ends at the end of its - * line, as the Japanese tab reads it. + * `japanese`, `「…」` is a string as well. Without `japanese`, a `#{…}` in + * a double-quoted string is code: the string is split into two regions there. + * + * A string ends at the end of its line, in both tabs. While a quote is being + * typed, the rest of the program has one quote too many: if a string went on + * to the next lines, the strings below would be read as code and be changed. + * The Japanese tab reads a string so too. * @param {string} code - The source code. * @param {object} [options] - How the source is read. * @param {boolean} [options.japanese] - Whether it is the Japanese tab's code. @@ -162,7 +197,9 @@ const findRegions = (code, options = {}) => { const ch = code[i]; const closers = closersOf(ch); if (closers) { - const start = i; + // A `#{…}` in a Ruby double-quoted string is code, not text. + const interpolates = !options.japanese && closers === DOUBLE_QUOTES; + let start = i; i++; // skip opening quote while (i < code.length) { // A `\` in a 「…」 is text (the Japanese tab takes it as it is). @@ -170,10 +207,21 @@ const findRegions = (code, options = {}) => { i += 2; // skip escaped character continue; } + if (interpolates && isInterpolationStart(code, i)) { + // The string stops before `#` and starts again at the `}`, + // so `#`, `{`, `}` and the code between can be made half-width. + strings.push([start, i]); + start = findInterpolationEnd(code, i + 2); + i = start; + // Not closed on its line: the string ends with the line. + if (code[i] !== '}' && code[i] !== '\uFF5D') break; + i++; + continue; + } if (closers.includes(code[i])) { break; } - if (options.japanese && code[i] === '\n') { + if (code[i] === '\n') { break; } i++; diff --git a/packages/scratch-gui/test/unit/lib/auto-correct.test.js b/packages/scratch-gui/test/unit/lib/auto-correct.test.js index 36b801a9c67..745f1580092 100644 --- a/packages/scratch-gui/test/unit/lib/auto-correct.test.js +++ b/packages/scratch-gui/test/unit/lib/auto-correct.test.js @@ -166,6 +166,22 @@ describe('autoCorrect', () => { const settings = { ...allOn, replaceInStrings: true }; expect(autoCorrect('x = "ABC"', settings)).toBe('x = "ABC"'); }); + + test('should correct the code of a #{} in a double-quoted string', () => { + expect(autoCorrect('puts("xは#{x + 1}!")', allOn)).toBe('puts("xは#{x + 1}!")'); + expect(autoCorrect('puts("#{name}さん!")', allOn)).toBe('puts("#{name}さん!")'); + expect(autoCorrect('puts("#{name}さん!")', allOn)).toBe('puts("#{name}さん!")'); + }); + + test('should end a #{} at its own } and keep the string after it', () => { + expect(autoCorrect('puts("#{h[{a: 1}]}AB")', allOn)).toBe('puts("#{h[{a: 1}]}AB")'); + }); + + test('should not take #{} as code in single quotes, after a \\ or in the Japanese tab', () => { + expect(autoCorrect("puts('#{x}')", allOn)).toBe("puts('#{x}')"); + expect(autoCorrect('puts("\\#{x}")', allOn)).toBe('puts("\\#{x}")'); + expect(autoCorrect('表示する("#{x}")', allOn, { japanese: true })).toBe('表示する("#{x}")'); + }); }); describe('multiline code', () => { @@ -180,6 +196,15 @@ describe('autoCorrect', () => { const expected = 'x = "ABC"\ny = 1'; expect(autoCorrect(input, allOn)).toBe(expected); }); + + test('should not change the strings of the next lines while a quote is being typed', () => { + expect(autoCorrect('y = \u201D\nputs("ABC")', allOn)).toBe('y = "\nputs("ABC")'); + expect(autoCorrect("y = '\nputs('ABC')", allOn)).toBe("y = '\nputs('ABC')"); + }); + + test('should end a #{} that is not closed on its line with the line', () => { + expect(autoCorrect('puts("#{x\nputs("ABC")', allOn)).toBe('puts("#{x\nputs("ABC")'); + }); }); describe('no-op cases', () => { From f3a112feeaf3d881666a048e91dd73a48ff929d3 Mon Sep 17 00:00:00 2001 From: "smalruby3-editor-bot[bot]" <297607354+smalruby3-editor-bot[bot]@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:30:44 +0000 Subject: [PATCH 3/3] fix(dncl): end a string with its line also after a backslash at the line end A \ at the end of a line skipped the newline, so the string went on to the next line and the text of the strings below was read as code and made half-width. Co-Authored-By: Claude Opus 5.5 --- packages/scratch-gui/src/lib/auto-correct.js | 3 ++- packages/scratch-gui/test/unit/lib/auto-correct.test.js | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/scratch-gui/src/lib/auto-correct.js b/packages/scratch-gui/src/lib/auto-correct.js index fd548129054..34d95e672f6 100644 --- a/packages/scratch-gui/src/lib/auto-correct.js +++ b/packages/scratch-gui/src/lib/auto-correct.js @@ -203,7 +203,8 @@ const findRegions = (code, options = {}) => { i++; // skip opening quote while (i < code.length) { // A `\` in a 「…」 is text (the Japanese tab takes it as it is). - if (code[i] === '\\' && closers !== '\u300D') { + // A `\` at the end of a line does not carry the string on to the next line. + if (code[i] === '\\' && closers !== '\u300D' && code[i + 1] !== '\n') { i += 2; // skip escaped character continue; } diff --git a/packages/scratch-gui/test/unit/lib/auto-correct.test.js b/packages/scratch-gui/test/unit/lib/auto-correct.test.js index 745f1580092..b4235168849 100644 --- a/packages/scratch-gui/test/unit/lib/auto-correct.test.js +++ b/packages/scratch-gui/test/unit/lib/auto-correct.test.js @@ -202,6 +202,10 @@ describe('autoCorrect', () => { expect(autoCorrect("y = '\nputs('ABC')", allOn)).toBe("y = '\nputs('ABC')"); }); + test('should end a string with its line also after a \\ at the end of the line', () => { + expect(autoCorrect('puts("a\\\nputs("ABC")', allOn)).toBe('puts("a\\\nputs("ABC")'); + }); + test('should end a #{} that is not closed on its line with the line', () => { expect(autoCorrect('puts("#{x\nputs("ABC")', allOn)).toBe('puts("#{x\nputs("ABC")'); });