Skip to content

Commit c6610d2

Browse files
committed
merge
2 parents f086ebf + 36325fc commit c6610d2

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

pxtblocks/blocksProgram.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface BlocksProgram {
6363
getVariableQualifiedName(varName: string, workspace: Blockly.Workspace): string;
6464
refreshSymbols?(): void;
6565
getVariableSymbol(varId: string): BlocksVariableSymbol;
66+
getFunctionSymbol(functionName: string): BlocksFunctionSymbol;
6667
}
6768

6869

@@ -136,6 +137,11 @@ export class SingleWorkspaceBlocksProgram implements BlocksProgram {
136137
}
137138
return null;
138139
}
140+
141+
getFunctionSymbol(functionName: string): BlocksFunctionSymbol {
142+
const allFunctions = getFunctionSymbols({ fileName: "main.blocks", workspace: this.workspace });
143+
return allFunctions.find(f => f.name === functionName);
144+
}
139145
}
140146

141147

@@ -332,6 +338,17 @@ export class MultiWorkspaceBlocksProgram implements BlocksProgram {
332338
return null;
333339
}
334340

341+
getFunctionSymbol(functionName: string): BlocksFunctionSymbol {
342+
for (const symbols of this.symbolsCache.values()) {
343+
for (const symbol of symbols) {
344+
if (symbol.type === "function" && symbol.name === functionName) {
345+
return symbol;
346+
}
347+
}
348+
}
349+
return null;
350+
}
351+
335352
protected defineSymbolInWorkspace(symbol: BlocksSymbol, workspace: Blockly.Workspace) {
336353
const map = workspace.getVariableMap();
337354

@@ -463,7 +480,7 @@ function getSymbolFromFunctionDefinitionBlock(block: FunctionDefinitionBlock, fi
463480
functionSymbol.arguments.push({
464481
name: child.getAttribute("name"),
465482
type: child.getAttribute("type"),
466-
id: child.getAttribute("argid")
483+
id: child.getAttribute("id")
467484
});
468485
}
469486
}

pxtblocks/plugins/functions/blocks/functionCallBlocks.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
FUNCTION_CALL_OUTPUT_BLOCK_TYPE,
66
FUNCTION_DEFINITION_BLOCK_TYPE,
77
} from "../constants";
8-
import { getArgMap, getDefinition, getShadowBlockInfoFromType_, isVariableBlockType, mutateCallersAndDefinition, mutationsAreEqual } from "../utils";
8+
import { getArgMap, getDefinition, getShadowBlockInfoFromType_, isVariableBlockType, lookupImportedFunctionDef, mutateCallersAndDefinition, mutationsAreEqual } from "../utils";
99
import { MsgKey } from "../msg";
1010
import { BlocksFunctionSymbol, generateFunctionMutationFromSymbol } from "../../../blocksProgram";
1111

@@ -172,10 +172,10 @@ const FUNCTION_CALL_MIXIN: FunctionCallMixin = {
172172
// Propagate the functionId of the definition to the caller
173173
this.functionId_ = def.functionId_;
174174
} else {
175-
const importedDef = this.workspace.getVariableMap().getVariableById(this.functionId_);
175+
const importedFunction = lookupImportedFunctionDef(name, this.workspace, this.functionId_);
176176

177-
if (importedDef) {
178-
const mutation = generateFunctionMutationFromSymbol(JSON.parse(importedDef.getName()) as BlocksFunctionSymbol);
177+
if (importedFunction) {
178+
const mutation = generateFunctionMutationFromSymbol(importedFunction);
179179

180180
if (!mutationsAreEqual(this.mutationToDom(), mutation)) {
181181
mutateCallersAndDefinition(this.getName(), this.workspace, this.mutationToDom(), mutation);
@@ -205,9 +205,9 @@ const FUNCTION_CALL_MIXIN: FunctionCallMixin = {
205205
// this caller.
206206
const name = this.getName();
207207
const def = getDefinition(name, this.workspace);
208-
const importedDef = !def && this.workspace.getVariableMap().getVariableById(this.functionId_);
208+
const importedFunction = !def && lookupImportedFunctionDef(name, this.workspace, this.functionId_);
209209

210-
if (!def && !importedDef) {
210+
if (!def && !importedFunction) {
211211
Blockly.Events.setGroup(event.group);
212212
this.dispose(true);
213213
Blockly.Events.setGroup(false);

pxtblocks/plugins/functions/commonFunctionMixin.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
FUNCTION_DECLARATION_BLOCK_TYPE,
66
FUNCTION_DEFINITION_BLOCK_TYPE,
77
} from "./constants";
8-
import { getDefinition, idsInUse, isCustomType, StringMap } from "./utils";
8+
import { getDefinition, idsInUse, isCustomType, lookupImportedFunctionDef, StringMap } from "./utils";
99
import { MsgKey } from "./msg";
1010
import { BlocksFunctionSymbol } from "../../blocksProgram";
11+
import { external } from "../..";
1112

1213
type CommonFunctionMixinType = typeof COMMON_FUNCTION_MIXIN;
1314

@@ -295,17 +296,18 @@ export const COMMON_FUNCTION_MIXIN = {
295296
}
296297
}
297298
else {
298-
const importedDef = this.workspace.getVariableMap().getVariableById(this.functionId_);
299-
if (importedDef) {
300-
const importedDefArgs = JSON.parse(importedDef.getName()) as BlocksFunctionSymbol;
299+
const importedFunction = lookupImportedFunctionDef(this.name_, this.workspace, this.functionId_);
300+
if (importedFunction) {
301301
for (let i = 0; i < this.arguments_.length; ++i) {
302-
for (let j = 0; j < importedDefArgs.arguments.length; ++j) {
303-
if (importedDefArgs.arguments[j].name == this.arguments_[i].name) {
304-
this.arguments_[i].id = importedDefArgs.arguments[j].id;
302+
for (let j = 0; j < importedFunction.arguments.length; ++j) {
303+
if (importedFunction.arguments[j].name == this.arguments_[i].name) {
304+
this.arguments_[i].id = importedFunction.arguments[j].id;
305305
break;
306306
}
307307
}
308308
}
309+
310+
this.functionId_ = importedFunction.id;
309311
}
310312
}
311313
break;

pxtblocks/plugins/functions/utils.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { ArgumentReporterBlock } from "./blocks/argumentReporterBlocks";
2121
import { DRAGGABLE_PARAM_INPUT_PREFIX } from "../../loader";
2222
import { getGlobalProgram } from "../../external";
2323
import { createFlyoutGroupLabel } from "../../toolbox";
24+
import { BlocksFunctionSymbol, IMPORTED_FUNCTION_TYPE } from "../../blocksProgram";
2425

2526

2627
export type StringMap<T> = { [index: string]: T };
@@ -31,7 +32,22 @@ export function rename(this: Blockly.FieldTextInput, name: string) {
3132
const sourceBlock = this.sourceBlock_ as CommonFunctionBlock;
3233
const oldMutation = sourceBlock.mutationToDom();
3334

34-
const legalName = findLegalName(name, sourceBlock.workspace, sourceBlock);
35+
let legalName = name;
36+
const workspaces = getGlobalProgram()?.getAllWorkspaces()?.map(w => w.workspace) || [sourceBlock.workspace]
37+
38+
let didChange = true;
39+
while (didChange) {
40+
didChange = false;
41+
for (const workspace of workspaces) {
42+
const newName = findLegalName(legalName, workspace, sourceBlock);
43+
if (newName !== legalName) {
44+
legalName = newName;
45+
didChange = true;
46+
break;
47+
}
48+
}
49+
}
50+
3551
const oldName = this.getValue();
3652

3753
if (!name) return oldName;
@@ -231,7 +247,7 @@ export function mutateCallersAndDefinition(name: string, ws: Blockly.Workspace,
231247
} as CallerChange))
232248

233249
const change = new MutateFunctionEvent(
234-
definitionBlock.id,
250+
definitionBlock?.id || newMutation.getAttribute("functionid"),
235251
callerChanges,
236252
Blockly.Xml.domToText(oldMutation),
237253
Blockly.Xml.domToText(newMutation),
@@ -462,8 +478,7 @@ export function flyoutCategory(workspace: Blockly.WorkspaceSvg) {
462478
program.refreshSymbols?.();
463479

464480
const globeIcon = "\uf0ac";
465-
// feels hacky did i miss easy way to do this? easy way for now
466-
const currentFile = (program as any).currentlyLoadedFile as string | undefined;
481+
const currentFile = program.getAllWorkspaces().find(w => w.workspace === workspace)?.fileName || pxt.MAIN_BLOCKS;
467482
const localNames = new Set(getAllFunctionDefinitionBlocks(workspace).map(f => f.getName().toLowerCase()));
468483

469484
for (const file of program.listFiles()) {
@@ -604,6 +619,27 @@ export function isVariableBlockType(type: string) {
604619
return false;
605620
}
606621

622+
export function lookupImportedFunctionDef(name: string, workspace: Blockly.Workspace, functionId?: string) {
623+
const program = getGlobalProgram();
624+
const symbol = program?.getFunctionSymbol(name);
625+
626+
let varModel: Blockly.IVariableModel<Blockly.IVariableState> | undefined;
627+
628+
if (symbol) {
629+
varModel = workspace.getVariableMap().getVariableById(symbol.id);
630+
}
631+
if (!varModel && functionId) {
632+
varModel = workspace.getVariableMap().getVariableById(functionId);
633+
}
634+
635+
if (varModel?.getType() === IMPORTED_FUNCTION_TYPE) {
636+
if (symbol) return symbol;
637+
return JSON.parse(varModel.getName()) as BlocksFunctionSymbol;
638+
}
639+
640+
return undefined;
641+
}
642+
607643
interface DescendantChange {
608644
id: string;
609645
type: string;

0 commit comments

Comments
 (0)