diff --git a/docs/blocks/grid-picker.md b/docs/blocks/grid-picker.md index 9b77b6772403..96d409c744cd 100644 --- a/docs/blocks/grid-picker.md +++ b/docs/blocks/grid-picker.md @@ -245,6 +245,9 @@ Detailed behavior and edge cases, mainly for contributors and automated agents. keys may use either the TypeScript parameter name or its name in the block definition, and both sets are combined. If both set the same key, the block definition name wins. +- When an input uses an identity grid picker as its shadow, an enum member and + its `enumIdentity` constant alias both use that picker if the choice matches + its enum sources and tag filter. Other values keep their normal blocks. - Field options are read as strings, so `fixedInstances` is only on when set to `true` (or `1`). `fixedInstances=false` leaves it off. - A receiver (`this`) shown as a grid picker is always a dropdown. Calls on an diff --git a/pxtcompiler/emitter/decompiler.ts b/pxtcompiler/emitter/decompiler.ts index 9412392e5210..4ac4409da6ff 100644 --- a/pxtcompiler/emitter/decompiler.ts +++ b/pxtcompiler/emitter/decompiler.ts @@ -2210,6 +2210,12 @@ ${output}`; return; } + const pickerShadow = getEnumGridPickerShadow(e, shadowBlockInfo); + if (pickerShadow) { + addInput(mkValue(U.htmlEscape(param.definitionName), pickerShadow, param.shadowBlockId, shadowMutation)); + return; + } + switch (e.kind) { case SK.FunctionExpression: case SK.ArrowFunction: @@ -2381,6 +2387,37 @@ ${output}`; return r; } + function getEnumGridPickerShadow(value: Expression, shadow: SymbolInfo | undefined): ExpressionNode | undefined { + if (shadow?.attributes.shim !== "TD_ID") return undefined; + const field = fieldBlockInfo(shadow.attributes.blockId); + if (field?.fieldEditor !== "gridpicker") return undefined; + + let memberName: string | undefined; + if (value.kind === SK.Identifier) { + memberName = pxtInfo(value).commentAttrs?.enumIdentity; + } + else if (value.kind === SK.PropertyAccessExpression) { + const info = pxtInfo(value).callInfo; + if (info) { + memberName = info.decl?.kind === SK.EnumMember ? info.qName : attrs(info).enumIdentity; + } + } + if (!memberName) return undefined; + + const apis = blocksInfo.apis; + const member = apis.byQName[memberName]; + if (member?.kind !== SymbolKind.EnumMember || member.attributes.blockHidden) return undefined; + const enums = pxt.blocks.getConfiguredEnumNames(field, apis); + if (enums.length ? enums.indexOf(member.namespace) < 0 : member.namespace !== field.type) return undefined; + + // An out-of-filter value keeps its ordinary block, rather than being forced into this picker. + const filter = field.fieldOptions?.filter; + if (typeof filter === "string" && !pxt.sprite.compileTagFilter(filter)(pxt.sprite.getSymbolTags(apis, member))) { + return undefined; + } + return getFieldBlock(U.htmlEscape(shadow.attributes.blockId), U.htmlEscape(field.definitionName), member.qName); + } + function fieldBlockInfo(blockId: string) { if (blocksInfo.blocksById[blockId]) { const comp = pxt.blocks.compileInfo(blocksInfo.blocksById[blockId]); diff --git a/tests/decompile-test/baselines/gridPickerShadow.blocks b/tests/decompile-test/baselines/gridPickerShadow.blocks new file mode 100644 index 000000000000..e50399a6efbb --- /dev/null +++ b/tests/decompile-test/baselines/gridPickerShadow.blocks @@ -0,0 +1,120 @@ + + + + + + +TestMonster.Zombie + + + + +TestAnimal.Chicken + + + + + + +TestMonster.Zombie + + + + +TestAnimal.Chicken + + + + + + + +TestMonster.Creeper + + + + + +TestAnimal.Cow + + + + + + + +TestMonster.Creeper + + + + + +TestAnimal.Cow + + + + + + +TestMonster.Zombie + + + + + +TestTerrain.Grass + + + + + + +TestMonster.Zombie + + + + + +TestTerrain.Grass + + + + +mount + +0 + +TestAnimal.Chicken + + + + + + +TestMonster.Zombie + + + + + +mount + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/decompile-test/cases/gridPickerShadow.ts b/tests/decompile-test/cases/gridPickerShadow.ts new file mode 100644 index 000000000000..c984e1ca537e --- /dev/null +++ b/tests/decompile-test/cases/gridPickerShadow.ts @@ -0,0 +1,8 @@ +gridPickerTest.spawnJockey(TEST_ZOMBIE, TEST_CHICKEN) +gridPickerTest.spawnJockey(TestMonster.Zombie, TestAnimal.Chicken) +gridPickerTest.spawnJockey(TEST_CREEPER, TEST_COW) +gridPickerTest.spawnJockey(TestMonster.Creeper, TestAnimal.Cow) +gridPickerTest.spawnJockey(TEST_ZOMBIE, TEST_GRASS) +gridPickerTest.spawnJockey(TEST_ZOMBIE, TestTerrain.Grass) +let mount = TEST_CHICKEN +gridPickerTest.spawnJockey(TEST_ZOMBIE, mount) diff --git a/tests/decompile-test/cases/testBlocks/gridPickerShadow.ts b/tests/decompile-test/cases/testBlocks/gridPickerShadow.ts new file mode 100644 index 000000000000..9746e75aad80 --- /dev/null +++ b/tests/decompile-test/cases/testBlocks/gridPickerShadow.ts @@ -0,0 +1,63 @@ +enum TestAnimal { + //% blockIdentity=gridPickerTest.animal tags=mount + Chicken = 10, + //% blockIdentity=gridPickerTest.animal tags="mount excluded" + Cow = 11 +} + +enum TestMonster { + //% blockIdentity=gridPickerTest.monster tags="mount rider" + Zombie = 32, + //% blockIdentity=gridPickerTest.monster + Creeper = 33 +} + +enum TestTerrain { + //% blockIdentity=gridPickerTest.terrain tags=mount + Grass = 10 +} + +//% blockIdentity=gridPickerTest.animal enumIdentity=TestAnimal.Chicken +const TEST_CHICKEN = TestAnimal.Chicken; +//% blockIdentity=gridPickerTest.animal enumIdentity=TestAnimal.Cow +const TEST_COW = TestAnimal.Cow; +//% blockIdentity=gridPickerTest.monster enumIdentity=TestMonster.Zombie +const TEST_ZOMBIE = TestMonster.Zombie; +//% blockIdentity=gridPickerTest.monster enumIdentity=TestMonster.Creeper +const TEST_CREEPER = TestMonster.Creeper; +//% blockIdentity=gridPickerTest.terrain enumIdentity=TestTerrain.Grass +const TEST_GRASS = TestTerrain.Grass; + +namespace gridPickerTest { + //% blockId=test_grid_picker_animal block="animal $value" shim=TD_ID + export function animal(value: TestAnimal): number { + return value; + } + + //% blockId=test_grid_picker_monster block="monster $value" shim=TD_ID + export function monster(value: TestMonster): number { + return value; + } + + //% blockId=test_grid_picker_terrain block="terrain $value" shim=TD_ID + export function terrain(value: TestTerrain): number { + return value; + } + + //% blockId=test_grid_picker_rider block="$choice" shim=TD_ID blockHidden=1 + //% choice.fieldEditor=gridpicker choice.fieldOptions.enumNames="TestAnimal,TestMonster" choice.fieldOptions.filter=rider + export function rider(choice: number): number { + return choice; + } + + //% blockId=test_grid_picker_mount block="$choice" shim=TD_ID blockHidden=1 + //% choice.fieldEditor=gridpicker choice.fieldOptions.enumNames="TestAnimal,TestMonster" choice.fieldOptions.filter="mount !excluded" + export function mount(choice: number): number { + return choice; + } + + //% blockId=test_grid_picker_jockey block="spawn $rider riding $mount" + //% rider.shadow=test_grid_picker_rider mount.shadow=test_grid_picker_mount + export function spawnJockey(rider: number, mount: number): void { + } +} diff --git a/tests/decompile-test/cases/testBlocks/pxt.json b/tests/decompile-test/cases/testBlocks/pxt.json index d1719e885305..9742c4215cb3 100644 --- a/tests/decompile-test/cases/testBlocks/pxt.json +++ b/tests/decompile-test/cases/testBlocks/pxt.json @@ -16,6 +16,7 @@ "globals.ts", "spritekind.ts", "decompilerShadowAlias.ts", + "gridPickerShadow.ts", "blockAliasFor.ts", "forceStatement.ts", "robot.ts"