Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: Use IVariableMap instead of VariableMap #8401

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions core/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
// Former goog.module ID: Blockly.Names

import {Msg} from './msg.js';
// import * as Procedures from './procedures.js';
import type {VariableMap} from './variable_map.js';
import type {IVariableMap} from './interfaces/i_variable_map.js';
import type {
IVariableModel,
IVariableState,
} from './interfaces/i_variable_model.js';
import * as Variables from './variables.js';
import type {Workspace} from './workspace.js';

Expand All @@ -39,7 +42,8 @@ export class Names {
/**
* The variable map from the workspace, containing Blockly variable models.
*/
private variableMap: VariableMap | null = null;
private variableMap: IVariableMap<IVariableModel<IVariableState>> | null =
null;

/**
* @param reservedWordsList A comma-separated string of words that are illegal
Expand Down Expand Up @@ -70,7 +74,7 @@ export class Names {
*
* @param map The map to track.
*/
setVariableMap(map: VariableMap) {
setVariableMap(map: IVariableMap<IVariableModel<IVariableState>>) {
this.variableMap = map;
}

Expand Down
125 changes: 39 additions & 86 deletions core/variable_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import './events/events_var_delete.js';
import './events/events_var_rename.js';

import type {Block} from './block.js';
import * as dialog from './dialog.js';
import * as deprecation from './utils/deprecation.js';
import * as eventUtils from './events/utils.js';
import * as registry from './registry.js';
import {Msg} from './msg.js';
import * as Variables from './variables.js';
import {Names} from './names.js';
import * as idGenerator from './utils/idgenerator.js';
import {IVariableModel, IVariableState} from './interfaces/i_variable_model.js';
Expand Down Expand Up @@ -247,7 +247,6 @@ export class VariableMap
this.variableMap.set(type, variables);
}
eventUtils.fire(new (eventUtils.get(eventUtils.VAR_CREATE))(variable));

return variable;
}

Expand All @@ -269,90 +268,51 @@ export class VariableMap

/* Begin functions for variable deletion. */
/**
* Delete a variable.
* Delete a variable and all of its uses without confirmation.
*
* @param variable Variable to delete.
*/
deleteVariable(variable: IVariableModel<IVariableState>) {
const variables = this.variableMap.get(variable.getType());
if (!variables || !variables.has(variable.getId())) return;
variables.delete(variable.getId());
eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
if (variables.size === 0) {
this.variableMap.delete(variable.getType());
const uses = this.getVariableUsesById(variable.getId());
const existingGroup = eventUtils.getGroup();
if (!existingGroup) {
eventUtils.setGroup(true);
}
try {
for (let i = 0; i < uses.length; i++) {
uses[i].dispose(true);
}
const variables = this.variableMap.get(variable.getType());
if (!variables || !variables.has(variable.getId())) return;
variables.delete(variable.getId());
eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
if (variables.size === 0) {
this.variableMap.delete(variable.getType());
}
} finally {
eventUtils.setGroup(existingGroup);
}
}

/**
* Delete a variables by the passed in ID and all of its uses from this
* workspace. May prompt the user for confirmation.
* @deprecated v12 - Delete a variables by the passed in ID and all of its
* uses from this workspace. May prompt the user for confirmation.
*
* @param id ID of variable to delete.
*/
deleteVariableById(id: string) {
deprecation.warn(
'VariableMap.deleteVariableById',
'v12',
'v13',
'Blockly.Variables.deleteVariable',
);
const variable = this.getVariableById(id);
if (variable) {
// Check whether this variable is a function parameter before deleting.
const variableName = variable.getName();
const uses = this.getVariableUsesById(id);
for (let i = 0, block; (block = uses[i]); i++) {
if (
block.type === 'procedures_defnoreturn' ||
block.type === 'procedures_defreturn'
) {
const procedureName = String(block.getFieldValue('NAME'));
const deleteText = Msg['CANNOT_DELETE_VARIABLE_PROCEDURE']
.replace('%1', variableName)
.replace('%2', procedureName);
dialog.alert(deleteText);
return;
}
}

if (uses.length > 1) {
// Confirm before deleting multiple blocks.
const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
.replace('%1', String(uses.length))
.replace('%2', variableName);
dialog.confirm(confirmText, (ok) => {
if (ok && variable) {
this.deleteVariableInternal(variable, uses);
}
});
} else {
// No confirmation necessary for a single block.
this.deleteVariableInternal(variable, uses);
}
} else {
console.warn("Can't delete non-existent variable: " + id);
Variables.deleteVariable(this.workspace, variable);
}
}

/**
* Deletes a variable and all of its uses from this workspace without asking
* the user for confirmation.
*
* @param variable Variable to delete.
* @param uses An array of uses of the variable.
* @internal
*/
deleteVariableInternal(
variable: IVariableModel<IVariableState>,
uses: Block[],
) {
const existingGroup = eventUtils.getGroup();
if (!existingGroup) {
eventUtils.setGroup(true);
}
try {
for (let i = 0; i < uses.length; i++) {
uses[i].dispose(true);
}
this.deleteVariable(variable);
} finally {
eventUtils.setGroup(existingGroup);
}
}
/* End functions for variable deletion. */
/**
* Find the variable by the given name and type and return it. Return null if
Expand Down Expand Up @@ -431,7 +391,7 @@ export class VariableMap
getVariableTypes(ws: Workspace | null): string[] {
const variableTypes = new Set<string>(this.variableMap.keys());
if (ws && ws.getPotentialVariableMap()) {
for (const key of ws.getPotentialVariableMap()!.variableMap.keys()) {
for (const key of ws.getPotentialVariableMap()!.getTypes()) {
variableTypes.add(key);
}
}
Expand Down Expand Up @@ -470,26 +430,19 @@ export class VariableMap
}

/**
* Find all the uses of a named variable.
* @deprecated v12 - Find all the uses of a named variable.
*
* @param id ID of the variable to find.
* @returns Array of block usages.
*/
getVariableUsesById(id: string): Block[] {
const uses = [];
const blocks = this.workspace.getAllBlocks(false);
// Iterate through every block and check the name.
for (let i = 0; i < blocks.length; i++) {
const blockVariables = blocks[i].getVarModels();
if (blockVariables) {
for (let j = 0; j < blockVariables.length; j++) {
if (blockVariables[j].getId() === id) {
uses.push(blocks[i]);
}
}
}
}
return uses;
deprecation.warn(
'VariableMap.getVariableUsesById',
'v12',
'v13',
'Blockly.Variables.getVariableUsesById',
);
return Variables.getVariableUsesById(this.workspace, id);
}
}

Expand Down
69 changes: 69 additions & 0 deletions core/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// Former goog.module ID: Blockly.Variables

import type {Block} from './block.js';
import {Blocks} from './blocks.js';
import * as dialog from './dialog.js';
import {isVariableBackedParameterModel} from './interfaces/i_variable_backed_parameter_model.js';
Expand Down Expand Up @@ -683,6 +684,74 @@ export function compareByName(
.localeCompare(var2.getName(), undefined, {sensitivity: 'base'});
}

/**
* Find all the uses of a named variable.
*
* @param workspace The workspace to search for the variable.
* @param id ID of the variable to find.
* @returns Array of block usages.
*/
export function getVariableUsesById(workspace: Workspace, id: string): Block[] {
const uses = [];
const blocks = workspace.getAllBlocks(false);
// Iterate through every block and check the name.
for (let i = 0; i < blocks.length; i++) {
const blockVariables = blocks[i].getVarModels();
if (blockVariables) {
for (let j = 0; j < blockVariables.length; j++) {
if (blockVariables[j].getId() === id) {
uses.push(blocks[i]);
}
}
}
}
return uses;
}

/**
* Delete a variable and all of its uses from the given workspace. May prompt
* the user for confirmation.
*
* @param workspace The workspace from which to delete the variable.
* @param variable The variable to delete.
*/
export function deleteVariable(
workspace: Workspace,
variable: IVariableModel<IVariableState>,
) {
// Check whether this variable is a function parameter before deleting.
const variableName = variable.getName();
const uses = getVariableUsesById(workspace, variable.getId());
for (let i = 0, block; (block = uses[i]); i++) {
if (
block.type === 'procedures_defnoreturn' ||
block.type === 'procedures_defreturn'
) {
const procedureName = String(block.getFieldValue('NAME'));
const deleteText = Msg['CANNOT_DELETE_VARIABLE_PROCEDURE']
.replace('%1', variableName)
.replace('%2', procedureName);
dialog.alert(deleteText);
return;
}
}

if (uses.length > 1) {
// Confirm before deleting multiple blocks.
const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
.replace('%1', String(uses.length))
.replace('%2', variableName);
dialog.confirm(confirmText, (ok) => {
if (ok && variable) {
workspace.getVariableMap().deleteVariable(variable);
}
});
} else {
// No confirmation necessary for a single block.
workspace.getVariableMap().deleteVariable(variable);
}
}

export const TEST_ONLY = {
generateUniqueNameInternal,
};
Loading