MCPcopy
hub / github.com/RaspberryPiFoundation/blockly / deleteVariable

Function deleteVariable

packages/blockly/core/variables.ts:847–895  ·  view source on GitHub ↗
(
  workspace: Workspace,
  variable: IVariableModel<IVariableState>,
  triggeringBlock?: Block,
)

Source from the content-addressed store, hash-verified

845 * referencing the variable being deleted.
846 */
847export function deleteVariable(
848 workspace: Workspace,
849 variable: IVariableModel<IVariableState>,
850 triggeringBlock?: Block,
851) {
852 // Check whether this variable is a function parameter before deleting.
853 const variableName = variable.getName();
854 const uses = getVariableUsesById(workspace, variable.getId());
855 for (let i = uses.length - 1; i >= 0; i--) {
856 const block = uses[i];
857 if (
858 block.type === 'procedures_defnoreturn' ||
859 block.type === 'procedures_defreturn'
860 ) {
861 const procedureName = String(block.getFieldValue('NAME'));
862 const deleteText = Msg['CANNOT_DELETE_VARIABLE_PROCEDURE']
863 .replace('%1', variableName)
864 .replace('%2', procedureName);
865 dialog.alert(deleteText);
866 return;
867 }
868 if (block === triggeringBlock) {
869 uses.splice(i, 1);
870 }
871 }
872
873 if ((triggeringBlock && uses.length) || uses.length > 1) {
874 // Confirm before deleting multiple blocks.
875 const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
876 .replace(
877 '%1',
878 String(
879 uses.length +
880 (triggeringBlock && !triggeringBlock.workspace.isFlyout ? 1 : 0),
881 ),
882 )
883 .replace('%2', variableName);
884 dialog.confirm(confirmText, (ok) => {
885 if (ok && variable) {
886 workspace.getVariableMap().deleteVariable(variable);
887 }
888 });
889 } else {
890 // No confirmation necessary when the block that triggered the deletion is
891 // the only block referencing this variable or if only one block referencing
892 // this variable exists and the deletion was triggered programmatically.
893 workspace.getVariableMap().deleteVariable(variable);
894 }
895}
896
897export const TEST_ONLY = {
898 generateUniqueNameInternal,

Callers

nothing calls this directly

Calls 6

getVariableUsesByIdFunction · 0.85
getFieldValueMethod · 0.80
getVariableMapMethod · 0.80
getNameMethod · 0.65
getIdMethod · 0.65
deleteVariableMethod · 0.65

Tested by

no test coverage detected