Repository navigation
Add support for disabling/enabling blocks with roundtrip comment preservation (#10042) - #11699
Open
Dmitriy Antipov (THEb0nny) wants to merge 3 commits into
Open
Dmitriy Antipov (THEb0nny) wants to merge 3 commits into
Dmitriy Antipov (THEb0nny) wants to merge 3 commits into
Conversation
…crosoft#10042) - Code Generation (Blocks -> TS / Python): - In pxtblocks/compiler/compiler.ts: emit disabled statement blocks as commented-out code using '//' for TypeScript and '#' for Python. - In commentOutJsNodes: check if line is already commented to avoid duplicating prefixes ('// //' in TS and '# //' in Python). - Update leftoverVars to only emit variable declarations for variables actively referenced in enabled blocks (v.firstReference && v.firstReference.isEnabled()), preventing variable duplication and preserving declaration order when variable initializers are disabled. - Guard binding.alreadyDeclared, isAssigned, and firstReference in compileSet and compileVariableGet when blocks are disabled. - Decompilation (TS / Python -> Blocks): - In pxtcompiler/emitter/decompiler.ts: add disabled flag to BlockNode and StatementNode; emit disabled="true" disabled-reasons="MANUALLY_DISABLED" in openBlockTag. - Add isBlockCode and preprocessDisabledBlocks to distinguish valid MakeCode statements from human comments (TODOs, notes). Valid code is marked with /*__disabled__*/ and decompiled into disabled blocks, while human comments remain comment sticky notes. - Hook preprocessDisabledBlocks into decompile operations in service.ts, driver.ts, and webapp compiler.ts. - Fix upstream MakeCode bug in getNodeComments where comment.splice threw when ownerStatement.comment was undefined. - Python Support: - In pxtpy/parser.ts: preserve trailing comments at the end of the file by emitting a Pass statement with attached comments when currComments remains at EOF. - Typings: - Add preprocessDisabledBlocks?: boolean to CompileOptions in localtypings/pxtarget.d.ts.
Dmitriy Antipov (THEb0nny)
marked this pull request as draft
October 28, 2026 00:23
Contributor
Author
Microsoft.MakeCode.for.micro_bit.-.Google.Chrome.2026-10-28.03-11-33-compressed.mp4 |
…oft#10042) Enable bidirectional translation of commented-out code statements when switching between TypeScript and Python in the editor: - TS -> Python (pydecompiler.ts, service.ts): run preprocessDisabledBlocks during pydecompile to convert commented-out statements into AST nodes tagged with /*__disabled__*/, and prefix the transpiled Python statement with '# '. This ensures identifiers and casing (e.g. motors.largeB.run -> motors.large_b.run, booleans true -> True) are properly converted to Python syntax. - Python -> TS (converter.ts, parser.ts, ast.ts): detect and parse comment statements (calls, assignments) into AST statements tagged with _disabled, transpile them to TypeScript, and emit them prefixed with '// '. Suppress diagnostics on disabled statements during typechecking passes so commented code does not block conversion. - Preserve non-code human comments unmodified in both directions.
Contributor
Author
Update: Bidirectional translation for commented-out code statements between TypeScript and PythonWhen disabling blocks or commenting out code in the text editor, switching between TypeScript and Python previously retained the commented text verbatim (e.g. This commit adds bidirectional statement transpilation for commented-out code:
|
Contributor
Author
BrickCode.for.EV3.-.Google.Chrome.2026-10-28.22-16-38-compressed.mp4 |
Dmitriy Antipov (THEb0nny)
marked this pull request as ready for review
October 28, 2026 20:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improvement #10042
Summary
This PR implements block disabling/enabling in MakeCode with seamless roundtrip preservation across Blocks, TypeScript, and Python:
Context Menu & Workspace Support:
disable: trueworkspace option.blockDisablecontext menu entry ("Disable Block" / "Enable Block") with telemetry.MANUALLY_DISABLED) remain disabled.Code Generation (
Blocks -> TS / Python):// ...in TypeScript and# ...in Python without custom symbols or prefixes.if,while, loops) and top-level event blocks properly comment out their entire subtrees.// //in TS and# //in Python).Decompilation (
TS / Python -> Blocks):isBlockCodeand converts valid MakeCode statement code to disabled blocks (disabled="true" disabled-reasons="MANUALLY_DISABLED").// TODO: ...,// Note: ..., sentences) remain preserved as comment notes / sticky notes.py2ts) instead of being dropped.Variable Handling:
leftoverVarsonly emits declarations for variables actively referenced in enabled blocks (v.firstReference && v.firstReference.isEnabled()).// let y = 0) stay in their original position without duplicating or swapping order with other variables.Upstream Bug Fix:
TypeError: Cannot read properties of undefined (reading 'splice')inpxtcompiler/emitter/decompiler.tswhenownerStatement.commentis undefined.Verification & Testing
npm run test:decompiler/gulp testdecompiler).gulp testpydecomp).pxt-microbiteditor:led.plot,basic.showNumber).