Skip to content

Commit 9475849

Browse files
committed
Automatically choose a function name in the function extraction dialog
1 parent 82632b9 commit 9475849

5 files changed

Lines changed: 63 additions & 152 deletions

File tree

newIDE/app/src/EventsFunctionsExtensionEditor/EventsFunctionConfigurationEditor/CompactEventsFunctionPropertiesEditor.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ type Props = {|
3939
helpPagePath?: string,
4040
onConfigurationUpdated?: (?ExtensionItemConfigurationAttribute) => void,
4141
freezeEventsFunctionType?: boolean,
42-
getFunctionGroupNames?: () => string[],
4342
|};
4443

4544
const getFullNameHintText = (
@@ -80,7 +79,6 @@ export const CompactEventsFunctionPropertiesEditor = ({
8079
helpPagePath,
8180
eventsBasedBehavior,
8281
eventsBasedObject,
83-
getFunctionGroupNames,
8482
eventsFunctionsContainer,
8583
}: Props): React.Node => {
8684
const forceUpdate = useForceUpdate();

newIDE/app/src/EventsFunctionsExtensionEditor/EventsFunctionConfigurationEditor/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ type Props = {|
6969
parameterName: string
7070
) => void,
7171
unsavedChanges?: ?UnsavedChanges,
72-
getFunctionGroupNames?: () => string[],
7372
onWillInstallExtension: (extensionNames: Array<string>) => void,
7473
onExtensionInstalled: (extensionNames: Array<string>) => void,
7574
|};
@@ -102,7 +101,6 @@ const EventsFunctionConfigurationEditor: React.ComponentType<{
102101
onFunctionParameterWillBeRenamed,
103102
onFunctionParameterTypeChanged,
104103
unsavedChanges,
105-
getFunctionGroupNames,
106104
onWillInstallExtension,
107105
onExtensionInstalled,
108106
},
@@ -241,7 +239,6 @@ const EventsFunctionConfigurationEditor: React.ComponentType<{
241239
forceUpdate();
242240
}}
243241
freezeEventsFunctionType={freezeEventsFunctionType}
244-
getFunctionGroupNames={getFunctionGroupNames}
245242
/>
246243
</CompactEventsFunctionParametersEditor>
247244
) : null}

newIDE/app/src/EventsFunctionsExtensionEditor/index.js

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,63 +1302,6 @@ export default class EventsFunctionsExtensionEditor extends React.Component<
13021302
}
13031303
};
13041304

1305-
_getFunctionGroupNames = (): Array<string> => {
1306-
const groupNames = new Set<string>();
1307-
// Look only in the edited function container because
1308-
// functions from the extension or different behaviors
1309-
// won't use the same groups names.
1310-
// An independent autocompletion is done for each of them.
1311-
const {
1312-
selectedEventsBasedBehavior,
1313-
selectedEventsBasedObject,
1314-
} = this.state;
1315-
if (selectedEventsBasedBehavior) {
1316-
const eventFunctionContainer = selectedEventsBasedBehavior.getEventsFunctions();
1317-
for (
1318-
let index = 0;
1319-
index < eventFunctionContainer.getEventsFunctionsCount();
1320-
index++
1321-
) {
1322-
const groupName = eventFunctionContainer
1323-
.getEventsFunctionAt(index)
1324-
.getGroup();
1325-
if (groupName) {
1326-
groupNames.add(groupName);
1327-
}
1328-
}
1329-
} else if (selectedEventsBasedObject) {
1330-
const eventFunctionContainer = selectedEventsBasedObject.getEventsFunctions();
1331-
for (
1332-
let index = 0;
1333-
index < eventFunctionContainer.getEventsFunctionsCount();
1334-
index++
1335-
) {
1336-
const groupName = eventFunctionContainer
1337-
.getEventsFunctionAt(index)
1338-
.getGroup();
1339-
if (groupName) {
1340-
groupNames.add(groupName);
1341-
}
1342-
}
1343-
} else {
1344-
const { eventsFunctionsExtension } = this.props;
1345-
const freeEventsFunctions = eventsFunctionsExtension.getEventsFunctions();
1346-
for (
1347-
let index = 0;
1348-
index < freeEventsFunctions.getEventsFunctionsCount();
1349-
index++
1350-
) {
1351-
const groupName = freeEventsFunctions
1352-
.getEventsFunctionAt(index)
1353-
.getGroup();
1354-
if (groupName) {
1355-
groupNames.add(groupName);
1356-
}
1357-
}
1358-
}
1359-
return [...groupNames].sort((a, b) => a.localeCompare(b));
1360-
};
1361-
13621305
_onConfigurationUpdated = (
13631306
attribute: ?ExtensionItemConfigurationAttribute
13641307
) => {
@@ -1492,7 +1435,6 @@ export default class EventsFunctionsExtensionEditor extends React.Component<
14921435
onWillInstallExtension={this.props.onWillInstallExtension}
14931436
onExtensionInstalled={this.props.onExtensionInstalled}
14941437
unsavedChanges={this.props.unsavedChanges}
1495-
getFunctionGroupNames={this._getFunctionGroupNames}
14961438
/>
14971439
) : (selectedEventsBasedObject ||
14981440
selectedEventsBasedBehavior) &&

newIDE/app/src/EventsSheet/EventsFunctionExtractor/EventsFunctionExtractorDialog.js

Lines changed: 38 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ import {
1414
setupFunctionFromEvents,
1515
canCreateEventsFunction,
1616
functionHasLotsOfParameters,
17-
validateExtensionNameUniqueness,
18-
validateExtensionName,
19-
validateEventsFunctionNameUniqueness,
20-
validateEventsFunctionName,
17+
getSafeExtensionName,
18+
getSafeEventsFunctionName,
2119
} from '.';
2220
import AlertMessage from '../../UI/AlertMessage';
2321
import DismissableAlertMessage from '../../UI/DismissableAlertMessage';
@@ -27,6 +25,7 @@ import HelpButton from '../../UI/HelpButton';
2725
import { ColumnStackLayout, ResponsiveLineStackLayout } from '../../UI/Layout';
2826
import { type EventsScope } from '../../InstructionOrExpression/EventsScope';
2927
import { ProjectScopedContainersAccessor } from '../../InstructionOrExpression/EventsScope';
28+
3029
const gd: libGDevelop = global.gd;
3130

3231
type Props = {|
@@ -90,47 +89,25 @@ export default class EventsFunctionExtractorDialog extends React.Component<
9089
});
9190

9291
// Prepopulate the form
93-
const eventsFunctionsExtensions = enumerateEventsFunctionsExtensions(
94-
project
92+
const extensionName = getSafeExtensionName(project, 'MyExtension');
93+
this.setState({
94+
createNewExtension: true,
95+
extensionName,
96+
});
97+
eventsFunction.setName(
98+
getSafeEventsFunctionName(
99+
project,
100+
extensionName,
101+
eventsFunction.getName()
102+
)
95103
);
96-
if (eventsFunctionsExtensions.length === 0) {
97-
this.setState({
98-
createNewExtension: true,
99-
});
100-
}
101104
}
102105

103106
componentWillUnmount() {
104107
const { eventsFunction } = this.state;
105108
if (eventsFunction) eventsFunction.delete();
106109
}
107110

108-
_getFunctionGroupNames = (): Array<string> => {
109-
const { createNewExtension, extensionName } = this.state;
110-
if (createNewExtension || !extensionName) {
111-
return [];
112-
}
113-
const groupNames = new Set<string>();
114-
const { project } = this.props;
115-
const eventsFunctionsExtension = project.getEventsFunctionsExtension(
116-
extensionName
117-
);
118-
const freeEventsFunctions = eventsFunctionsExtension.getEventsFunctions();
119-
for (
120-
let index = 0;
121-
index < freeEventsFunctions.getEventsFunctionsCount();
122-
index++
123-
) {
124-
const groupName = freeEventsFunctions
125-
.getEventsFunctionAt(index)
126-
.getGroup();
127-
if (groupName) {
128-
groupNames.add(groupName);
129-
}
130-
}
131-
return [...groupNames].sort((a, b) => a.localeCompare(b));
132-
};
133-
134111
render(): any {
135112
const { project, onClose, onCreate } = this.props;
136113
const { eventsFunction, extensionName, createNewExtension } = this.state;
@@ -205,14 +182,24 @@ export default class EventsFunctionExtractorDialog extends React.Component<
205182
if (extensionName === CREATE_NEW_EXTENSION_PLACEHOLDER) {
206183
this.setState({
207184
createNewExtension: true,
208-
extensionName: '',
185+
extensionName: getSafeExtensionName(
186+
project,
187+
'MyExtension'
188+
),
209189
});
210190
} else {
211191
this.setState({
212192
createNewExtension: false,
213193
extensionName,
214194
});
215195
}
196+
eventsFunction.setName(
197+
getSafeEventsFunctionName(
198+
project,
199+
extensionName,
200+
eventsFunction.getName()
201+
)
202+
);
216203
}}
217204
fullWidth
218205
>
@@ -237,23 +224,14 @@ export default class EventsFunctionExtractorDialog extends React.Component<
237224
value={extensionName}
238225
floatingLabelText={<Trans>New extension name</Trans>}
239226
onChange={(extensionName: string) =>
240-
this.setState({ extensionName })
227+
this.setState({
228+
extensionName: getSafeExtensionName(
229+
project,
230+
extensionName
231+
),
232+
})
241233
}
242234
fullWidth
243-
errorText={
244-
!validateExtensionNameUniqueness(project, extensionName) ? (
245-
<Trans>
246-
This name is already taken by another extension.
247-
</Trans>
248-
) : !validateExtensionName(extensionName) ? (
249-
<Trans>
250-
This name is not valid. Only use alphanumeric characters
251-
(0-9, a-z) and underscores.
252-
</Trans>
253-
) : (
254-
undefined
255-
)
256-
}
257235
/>
258236
) : null}
259237
</ResponsiveLineStackLayout>
@@ -263,29 +241,16 @@ export default class EventsFunctionExtractorDialog extends React.Component<
263241
value={eventsFunction.getName()}
264242
floatingLabelText={<Trans>Function name</Trans>}
265243
onChange={(functionName: string) => {
266-
eventsFunction.setName(functionName);
244+
eventsFunction.setName(
245+
getSafeEventsFunctionName(
246+
project,
247+
extensionName,
248+
functionName
249+
)
250+
);
267251
this.forceUpdate();
268252
}}
269253
fullWidth
270-
errorText={
271-
!validateEventsFunctionNameUniqueness(
272-
project,
273-
extensionName,
274-
eventsFunction
275-
) ? (
276-
<Trans>
277-
This name is already taken by another function. Choose
278-
another name.
279-
</Trans>
280-
) : !validateEventsFunctionName(eventsFunction.getName()) ? (
281-
<Trans>
282-
This name is not valid. Only use alphanumeric characters
283-
(0-9, a-z) and underscores.
284-
</Trans>
285-
) : (
286-
undefined
287-
)
288-
}
289254
/>
290255
</Line>
291256
{hasLotsOfParameters ? (
@@ -312,7 +277,6 @@ export default class EventsFunctionExtractorDialog extends React.Component<
312277
this.forceUpdate();
313278
}}
314279
freezeEventsFunctionType
315-
getFunctionGroupNames={this._getFunctionGroupNames}
316280
/>
317281
{this._projectScopedContainersAccessor && (
318282
<CompactEventsFunctionParametersEditor

newIDE/app/src/EventsSheet/EventsFunctionExtractor/index.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
ProjectScopedContainersAccessor,
77
type EventsScope,
88
} from '../../InstructionOrExpression/EventsScope';
9+
import newNameGenerator from '../../Utils/NewNameGenerator';
10+
911
const gd: libGDevelop = global.gd;
1012

1113
/**
@@ -175,15 +177,13 @@ export const validateExtensionName = (extensionName: string): boolean => {
175177
return gd.Project.isNameSafe(extensionName);
176178
};
177179

178-
/**
179-
* Validate that an events functions extension name is unique in a project.
180-
*/
181-
export const validateExtensionNameUniqueness = (
180+
export const getSafeExtensionName = (
182181
project: gdProject,
183-
extensionName: string
184-
): boolean => {
185-
return !project.hasEventsFunctionsExtensionNamed(extensionName);
186-
};
182+
chosenExtensionName: string
183+
): string =>
184+
newNameGenerator(gd.Project.getSafeName(chosenExtensionName), name =>
185+
project.hasEventsFunctionsExtensionNamed(name)
186+
);
187187

188188
/**
189189
* Validate that an events function name is unique in a project extension.
@@ -206,6 +206,22 @@ export const validateEventsFunctionNameUniqueness = (
206206
return true;
207207
};
208208

209+
export const getSafeEventsFunctionName = (
210+
project: gdProject,
211+
extensionName: string,
212+
chosenFunctionName: string
213+
): string => {
214+
if (!project.hasEventsFunctionsExtensionNamed(extensionName)) {
215+
return gd.Project.getSafeName(chosenFunctionName);
216+
}
217+
const eventsFunctionsExtension = project.getEventsFunctionsExtension(
218+
extensionName
219+
);
220+
return newNameGenerator(gd.Project.getSafeName(chosenFunctionName), name =>
221+
eventsFunctionsExtension.getEventsFunctions().hasEventsFunctionNamed(name)
222+
);
223+
};
224+
209225
/**
210226
* Return true if the events function can be added to the given extension
211227
* without any conflict/invalid name.
@@ -220,13 +236,7 @@ export const canCreateEventsFunction = (
220236
validateExtensionName(extensionName) &&
221237
eventsFunction.getName() !== '' &&
222238
validateEventsFunctionName(eventsFunction.getName()) &&
223-
validateEventsFunctionNameUniqueness(
224-
project,
225-
extensionName,
226-
eventsFunction
227-
) &&
228-
eventsFunction.getFullName() !== '' &&
229-
eventsFunction.getSentence() !== ''
239+
validateEventsFunctionNameUniqueness(project, extensionName, eventsFunction)
230240
);
231241
};
232242

0 commit comments

Comments
 (0)