ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.58k stars 737 forks source link

[Improvement]: Improve function params names in create function code action #42396

Open LakshanWeerasinghe opened 5 months ago

LakshanWeerasinghe commented 5 months ago

Description

$subject

When we pass a record field to a function invocation that has not been defined yet, the plugin will suggest a create function code action. However, when creating the parameters for this function, we are not utilizing the field name. Because of this user most of the time need to rename the function params to have better readability of the code.

When suggesting the create function code action in such scenarios, we should utilize the field names to define function params.

https://github.com/ballerina-platform/ballerina-lang/assets/46857198/0eee2609-5863-4123-bb56-744b03d9c664

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

nipunayf commented 5 months ago

This concern is valid for other CAs that produce functions, as the parameter name generation can be isolated to the following method.

https://github.com/ballerina-platform/ballerina-lang/blob/b5c30cd073b35e371075bbcc682177541f4587bd/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/utils/NameUtil.java#L136-L149

Once the arg name is empty, it considers the first letter of the type symbol. This is a valid approach for expression values on which we cannot produce an admissible arg name, such as 1, arr[0], true, etc. However, for cases such as yours, we can use the last field to determine the arg name. For instance, for the function call fn(rec1.year, rec1.nestedRec.year, rec2.year), the function function fn(int year, int year2, int year3) {} can be generated.