fix: Make all toolbox blocks searchable - #10508
Conversation
| // variants of blocks with different variable values. | ||
| private variableFields = new Map<Blockly.utils.toolbox.BlockInfo, string[]>(); | ||
| // All workspace variables, sorted by name, updated when blocks are indexed. | ||
| private workspaceVariables: Array< |
There was a problem hiding this comment.
Why store this vs using workspace.getVariableMap()?
There was a problem hiding this comment.
No good reason. Dropped it for a general workspace that is set during construction. This should take some of the guessing out about which workspace is the source of truth too.
| @@ -24,21 +48,76 @@ export class BlockSearcher { | |||
| * itself. | |||
| * | |||
| * @param blockInfos A list of blocks to index. | |||
| * @param workspace The workspace source of truth for variables. This is | |||
There was a problem hiding this comment.
Would be helpful if this comment noted if this should be the flyout workspace or the main/target workspace
There was a problem hiding this comment.
I removed this parameter because we can now just access this.workspace directly.
| // If the current variable is one of the workspace variables, record | ||
| // the field name for later use in generating variants. | ||
| if ( | ||
| descendantBlock === block && |
There was a problem hiding this comment.
Why the check for it being the root block? If there were a child block in a flyout stack that had a variable field it seems like we'd want to match/update that too?
There was a problem hiding this comment.
Yeah, you're right. The problem that we only track the field name, but not where it might conceivably live within a stack. Without this guard, it might try to update the a non-existent variable field on a variable block's parent. This check allowed things to work for more common toolbox configurations, but it's definitely not fully functional in the way that it honestly should be. I'm also realizing that there isn't a similar check for non-variable dropdown fields, so I need to address that as well.
There was a problem hiding this comment.
Makes sense, I think that's probably fine in practice but a comment to that effect would be helpful.
There was a problem hiding this comment.
I reworked this so it now handles child blocks too. Whew.
The basics
The details
Resolves
Fixes #10336 and #10337
Proposed Changes
Main behavior changes
getAvailableBlocks()now looks at custom categories and gets blocks by actually calling their callbacks.comment_event types are ignored. A re-index happens only if a snapshot of the available blocks and variables has changed. Note that we can't predict how any application might need to handle manually re-indexing and no new APIs are exposed for this yet. However, manually firing another event type does cause a re-index.math_changeblock using that variable (even if it wasn't the most recently defined variable) and exactly one getter.itemori) that are not-yet-defined on the workspace are only matched by that name. This was a subjective product decision but seems to help prevent making the search results explode with blocks that are likely not of interest.dispose().devDependenciesfor tests, consistent with other plugins.Other bugs fixed
newBlock(type)which effectively disregarded all fields/inputs/extra state. Now blocks are fully deserialized.fieldRowwas walked (andnewBlockskipped children), so values on child/shadow blocks were never indexed.controls_flow_statementsblock matches 'tem', but also 'ite' (due to field value "continue with next iteration"). Because each of those trigrams make up the query "item", this block was incorrectly returned. Now we actually make sure the query is actually a substring of a piece of the block text.keydownwhich fired before the input's value was updated, so the search matched the previous text.Screenshots
Test Coverage
New tests were LLM-written with gentle reminders by me to follow our well-established conventions. Tests cover field and shadow values, workspace variables, flyout-only variables, dropdown variants, procedure
extraState, substring checking and re-indexing.Additional Information
This branch does not include UI updates. Those should come in a follow-up.